ALIAS HELP
variavel zaLinhasDaTela := {}
variavel zFgConfirmouFiltros := .f.
VARIAVEL LST_TP = 60
VARIAVEL znil = SetDebugInfo( .t. )
VARIAVEL z_de = ctod( "01."+SubStr(Dtoc(S_Datual),4) )
VARIAVEL z_ate = s_datual
VARIAVEL zSaldoInicial = 0
VARIAVEL zSaldo0Cta = 0
VARIAVEL zComplemento = ""
VARIAVEL zAux = ""
VARIAVEL Z_VDIVIS = {}
VARIAVEL Z_VSUBD = {}
VARIAVEL zFgContaVales = .f. 
VARIAVEL zFgParaCtaCxa = .f.
variavel oQbe=""
VARIAVEL zFgSoResumoDespesas = .f.
VARIAVEL zFgPrtResumosDiarios = .t.
VARIAVEL z0StrSldAnt      := ''
VARIAVEL z0StrSuprimentos := ''
VARIAVEL z0StrRetiradas   := ''
VARIAVEL z0StrSaldo       := ''
VARIAVEL zoProgressBar    := ''
VARIAVEL zCmdsDurante     := ''
VARIAVEL zSaldoAcum := 0
VARIAVEL zSaldo := 0
VARIAVEL z0SldDia := 0
VARIAVEL zSinalAnt:=''
VARIAVEL ztSupDia:=0
VARIAVEL ztRetDia:=0
VARIAVEL zCred:=0
VARIAVEL zDeb:=0
VARIAVEL zCab1:=''
VARIAVEL zCab2:=''
VARIAVEL zCab3:=''

VARIAVEL #TEXTO_FONTE
   Private zSxConta,hReg,zCodDiv,zCodSubd,zDivDesc,zSubdDesc,zctde,zctpara,zxCtDe,zxCtPara,zStrSldFinal,zDesc,zAux,nvCred,nvDeb,ztCred:=0,ztDeb:=0,zbMemo,zbNomeDiv,zbNomeSubd,zcCust
   DbCloseAll()
   Abra('0NOIDX',r_siga,'cp_subd', 'cp_subd', 'cp_subd', 'cp_subdiv')
   Abra('0NOIDX',r_siga,'sx_conta','sx_conta')
   Abra('0NOIDX',r_siga,'cp_divis','cp_divis','cp_divis','cp_divisao')
   Abra('0NOIDX',r_siga,'sx_caixa','sx_caixa','sx_caixa', 'str(Sx_Conta,3)+dtos(data)+str(cod_adiant)')

   oQbe:=oQbe():New('tesou01')

   oQbe:AddFiltro(" 9;3;Divisao de Despesas :", {||cp_divisao} ,, 'cp_divis;qbe_pcpdivis', {'Todas','Algumas'})

   oQbe:AddFiltro("10;3;Grupo de Lancamentos:", {||grupo} )
   oQbe:AddFiltro("11;3;Fornecedor / Cliente:", {||cp_cod_for} )
   oQbe:AddFiltro("12;3;Tipo Lancto    (S/R):", {||tipo} )

   aAdd( zaLinhasDaTela, {"3;3;Data inicial:", "@z_De",  @z_De,  "99.99.9999", "", "" } )
   aAdd( zaLinhasDaTela, {"4;3;Data final..:", "@z_Ate", @z_Ate, "99.99.9999", "", "z_Ate>=z_De"} ) 
   aAdd( zaLinhasDaTela, {"6;3;CHECKBOX 'Apenas o Resumo das &Despesas ?'", "@zFgSoResumoDespesas",  @zFgSoResumoDespesas,  "!", "", ""} )
   aAdd( zaLinhasDaTela, {"7;3;CHECKBOX 'Imprime os &Resumos Diarios   ?'", "@zFgPrtResumosDiarios", @zFgPrtResumosDiarios, "!", "", ""} )
  
   if .not. oQbe:QuickDialog("Qbe_tesou01", "70;13;Emissao do Livro Caixa", zaLinhasDaTela)
      DbCloseAll()
      Return
   endif
   
   oLst:nFontSize := 10
   oLst:lPortrait := .f.
   oLst:nLinesPorPagina := 40
   // oLst:lPortrait := .t.
   // oLst:nLinesPorPagina := 99

   DbSelectArea('sx_caixa')

   oLst:lOutputFr3 := .t.
   oLst:cReport := 'tesou01all'
   oLst:cAliasMaster := 'tesou'
   oLst:cAlias_DataCols := 'tesou'
   oLst:SetDbLst('mem:tesou')

   oLst:Cab1( zCab1:="LIVRO CAIXA GERENCIAL" )
   oLst:Cab2( zCab2:="Periodo: "+Dtoc(z_de)+" a "+Dtoc(z_ate)+", emitido em "+Dtoc(s_datual)+" as "+Time() )
   oLst:Cab4( {||!empty(oQbe:ExpressaoDeFiltragemParaImprimir)}, {||zCab3:=oQbe:ExpressaoDeFiltragemParaImprimir})

   oLst:AddCab( '_LINHA_FINA_' )
   oLst:AddCab( '_CABCOLS_' )
   oLst:AddCab( '_LINHA_GROSSA_' )

   oLst:AddCol('Data;data;D;99.99.9999;10;;')
   oLst:AddCol('Descricao;zDesc;P;@S35;35;;;Direita;NaoSome;Imprima;zdesc;')
   oLst:AddCol('Cxa;zCtDe;P;@E 9999;4;;;Direita;NaoSome;Imprima;CtaDe;')
   oLst:AddCol('c/f;zCtPara;P;@E 999999;6;;;Direita;NaoSome;Imprima;CtaPara;')
   oLst:AddCol('g;Grupo;N;99;2;;;Direita;NaoSome;Imprima;grupo;')
   oLst:AddCol('Pt St CCust Obs;zComplemento;P;@S45;45;;;Esquerda;NaoSome;Imprima;obs;')
   oLst:AddCol('Deb/Cred;valor;N;{|x|transform(x,"@E 99999,999.99")+if(tipo="S","+",if(tipo="R","-","B"))};13,2;;;Direita;NaoSome;Imprima;valor;')
   oLst:AddCol('Saldo;zSaldoAcum;P;9999999999.99;13,2;;;Direita;NaoSome;NaoImprima;saldo;')

   if oLst:lOutputFr3
      oLst:AddCol('tipo;tipo;C;X;1;;;Esquerda;NaoSome;NaoImprima;tipo;')
      oLst:AddCol('CP_SUBDIV;CP_SUBDIV;C;99999;5;;;Direita;NaoSome;NaoImprima;CP_SUBDIV;')
      oLst:AddCol('sld_adiant;sld_adiant;N;999999999.99;12,2;;;Direita;NaoSome;NaoImprima;sld_adiant;')
      oLst:AddCol('cod_adiant;cod_adiant;N;999999999.99;12,2;;;Direita;NaoSome;NaoImprima;cod_adiant;')
      oLst:AddCol('zCodDiv;zCodDiv;P;999;3;;;Direita;NaoSome;NaoImprima;zCodDiv;')
      oLst:AddCol('zCodSubd;zCodSubd;P;XX;2;;;Direita;NaoSome;NaoImprima;zCodSubd;')
      oLst:AddCol('zcCust;zcCust;P;@S6;6;;;Direita;NaoSome;NaoImprima;zcCust;')
      oLst:AddCol('zxCtDe;zxCtDe;P;@S12;12;;;Direita;NaoSome;NaoImprima;zxCtDe;')
      oLst:AddCol('zxCtPara;zxCtPara;P;@S12;12;;;Direita;NaoSome;NaoImprima;zxCtPara;')
   endif

   oLst:AddRodape( '_LINHA_FINA_', "", {|| "Pagina "+Str(++pag,4)+" / ####"} )

   oLst:AddGroup( { "KEY"=>{||Data} } )
   if !zFgPrtResumosDiarios
      oLst:AddGroup( { "FOOTER"=>{ {|| " "} } } )
   else
      oLst:AddGroup( { "FOOTER"=>{ '_PULE_1LINHA_', {|| zAux:="........  SubTotal do dia "+Dtoc(oLst:GroupKeyOld)+" :",zAux + Transform(abs(z0SldDia),"@E 999,999,999.99")+" "+if(z0SldDia<0,"-","+")+"  "+z0strsldant} } } )
      oLst:AddGroup( { "FOOTER"=>{ {|| Pad(" ",len(zAux))          + Transform(ztSupDia,"@E 999,999,999.99")+" +  "+z0strsuprimentos} } } )
      oLst:AddGroup( { "FOOTER"=>{ {|| Pad(" ",len(zAux))          + Transform(ztRetDia,"@E 999,999,999.99")+" -  "+z0strretiradas} } } )
      oLst:AddGroup( { "FOOTER"=>{ {|| zAux:=Pad(" ",len(zAux)) + Transform(zSaldoAcum,"@E 999,999,999.99") +" T  "+z0strsaldo, ztSupDia:=0,ztRetDia:=0,z0SldDia:=zSaldoAcum, zAux}, '_PULE_1LINHA_' } } )
   endif

   oLst:AddGroup( { "SOMAS"=> {||nvCred:=if(tipo="S",valor,0), nvDeb:=if(tipo="R",valor,0), ztSupDia+=nvCred, ztRetDia+=nvDeb, ztCred+=nvCred, ztDeb+=nvDeb, zSaldoAcum+=nvCred-nvDeb } } )
   oLst:SetDbLst('mem:_totcta', '_totcta', {{||Sx_Conta->Sx_Conta}, {||Sx_Conta->Ct_titulo}, {||zSaldo0Cta}, {||zCred}, {||zDeb}, {||zSaldo}}, {{"sx_conta","N",6,0 },{"ct_titulo","C",20,0},{"valor","N",12,2},{"cred","N",12,2},{"deb","N",12,2},{"saldo","N",12,2}})
   zSaldoInicial := 0
   DbSelectArea("Sx_Conta")
   DbGoTop()
   do while .not. eof()
      zSxConta := Sx_Conta->Sx_Conta  
      zSaldo0Cta := if(zSxConta>0,sx_caixa("SALDO_INICIAL",zSxConta,z_de),0)
      oLst:PrtD(zSaldo0Cta<>0, Pad(" ***** Saldo Anterior Cta"+StrZero(zSxConta,3)+" "+AllTrim(Sx_Conta->Ct_titulo),55) +Transform(zSaldo0Cta,"@E 99999,999.99")+ if(zSaldo0Cta>0,"+",if(zSaldo0Cta<0,"-"," ")))
      oLst:DbAppend('_totcta')
      zSaldoInicial += zSaldo0Cta
      DbSkip()
   enddo
   oLst:EvalLineBlocks('DETALHE', '_PULE_1LINHA_')
   if select('_totcta')>0 .and. _totcta->(RecCount())>0
      DbSelectArea("_totcta")
      dbCreateIndex( 'mem:_totcta', "Sx_Conta", {||Sx_Conta}) 
   endif
   DbSelectArea("Sx_Conta")
   
   z0SldDia := zSaldoInicial
   zSaldoAcum := zSaldoInicial

   z0StrSldAnt      := if(zFgContaVales, "(Pendencia Anterior)",       "(Sld.Ant)")
   z0StrSuprimentos := if(zFgContaVales, "(Adiantado no Dia)",         "(Suprimentos)")   
   z0StrRetiradas   := if(zFgContaVales, "(Contas prestadas no dia)",  "(Retiradas)")
   z0StrSaldo       := if(zFgContaVales, "(Pendente de Prest.Contas)", "(Saldo)")

   sx_caixa->( DbGoTop(), xDbCopyTo('mem:_cxa', '_cxa', {||Data>=z_de .and. DATA<=z_ate .and. oQbe:Filtro_ok()}) )
   ABRA('0NOIDX','','mem:_cxa',  '_cxa', 'mem:_cxa', 'Dtos(DATA)+if(tipo="S","1","2")')

   zbNomeDiv := {|nDiv| if(nDiv=0,"", cp_divis->( if( DbSeek(nDiv), AllTrim(Left(Desc,1)+Lower(SubStr(Desc,2))),"Div"+StrZero(nDiv,3)) ))}
   zbNomeSubd := {|nDiv,cSubd| cp_subd->( if(nDiv>0.and.DbSeek(StrZero(nDiv,3)+cSubd), AllTrim(Left(Desc,1)+Lower(SubStr(Desc,2))) , "") )}
   zbMemo := {|nDiv,cSubd,cDesc| cDivDesc:=eVal(zbNomeDiv,nDiv), cSubdDesc:=eVal(zbNomeSubd,nDiv,cSubd), if(nDiv=0,"", cDivDesc + if(empty(cSubdDesc).or.cSubdDesc$cDivDesc,"",": "+cSubdDesc) + " (cc "+StrZero(nDiv,3) + if(val(cSubd)=0,"","."+cSubd)+") - ") + AllTrim(zDesc) }
   // zbMemo := {|nDiv,cSubd,cDesc| cDivDesc:=eVal(zbNomeDiv,nDiv), cSubdDesc:=eVal(zbNomeSubd,nDiv,cSubd), if(nDiv=0,"", cDivDesc + if(empty(cSubdDesc),"",": "+cSubdDesc) + " (cc "+StrZero(nDiv,3) + if(val(cSubd)=0,"","."+cSubd)+") - ") + AllTrim(zDesc) }

   //Cx_001 -> Cta999
   //Cta999 -> For9999
   //Cli2211 -> Cta999
   //Cta999 -> Cta222

   BLOCO_TXT zCmdsDurante
      zoProgressBar:Step()
      zComplemento := ""
      zCodDiv := if(grupo<>3.and.tipo="R", VAL(LEFT(CP_SUBDIV,3)), 0)
      zCodSubd := if(zCodDiv=0.or.Val(SubStr(CP_SUBDIV,4))=0, "", SubStr(CP_SUBDIV,4) )
      zcCust := if(zCodDiv=0, "", StrZero(zCodDiv,3)) + if(val(zCodSubd)=0,"","."+zCodSubd) 
      zDesc := Left(desc,1)+lower(Substr(desc,2))
      zctde := if(tipo="S",cp_cod_for,Sx_Conta)
      zctpara := if(tipo="S",Sx_Conta,cp_cod_for)
      zFgParaCtaCxa:=(cp_cod_for=999.or.cp_cod_for=500)
      zAux := Sx_caixa("GET_DE_PARA",zctde,zctpara,grupo,Tipo,Desc)
      zxCtDe := zAux[1]
      zxCtPara := zAux[2]
      if select('_totcta')>0 .and. _totcta->(RecCount())>0
         se tipo="S" entao _totcta->(DbSeek(_cxa->Sx_Conta), _field->cred+=_cxa->valor) senao se tipo="R" entao _totcta->(DbSeek(_cxa->Sx_Conta), _field->deb+=_cxa->valor)
      endif
      if zFgContaVales 
         if tipo="S"
            zComplemento := if(abs(valor-sld_adiant)<=.5, "Prestou Contas",  if( sld_adiant>valor, "Prestou contas. Reembolsar "+lTrim(str(sld_adiant-valor,12,2)), if( sld_adiant>0, "Prest.Cta Pendente: "+lTrim(str(valor-sld_adiant,12,2)), "Nao prestou contas" ) ) )
         else
            zComplemento := if( zFgParaCtaCxa, "Devolucao de Adiantamento", "(ad:"+Str(cod_adiant,6)+") "+zDesc )
         endif
      else 
         zDivDesc := eVal(zbNomeDiv,zCodDiv)
         zSubdDesc := eVal(zbNomeSubd,zCodDiv,zCodSubd)
         se zSubdDesc=zDivDesc entao zSubdDesc:=""
         zComplemento := if(!empty(zDivDesc),Str(Cp_Local,2)+" "+Str(Cp_Situaca,2)+" "+Cp_SubDiv+" "+zDivDesc,"")
      endif
   FIM_BLOCO_TXT

   zoProgressBar := myhProgressBar():Create('Processando Lancamentos...')
   _cxa->( DbGoTop(), oLst:DbEval(zCmdsDurante) )
   zoProgressBar:Close()
   if select('tesou')=0
      oLst:lOutputPdf:=.f.
      oLst:lOutputFr3:=.f.
      oLst:lOutputMatricial:=.f.
      Alert("A Conta "+str(zSxConta,3)+" nao tem lancamentos entre "+Dtoc(z_de)+" a "+Dtoc(z_ate) )
   endif   

   DbSelectArea("tesou")
   copy to _zzz
   DbSelectArea("_totcta")
   dbCreateIndex( 'mem:_totcta1', "valor+cred-deb", {||valor+cred-deb}) 
   // copy to _ztot
   
   oLst:Run()
   
   DbCloseAll()
   dbDrop('mem:tesou')
   dbDrop('mem:_totcta')
   
   Abra('0NOIDX',r_siga,'help','help')
   DbGoBottom()
#FIM_TEXTO_FONTE