ALIAS SI_CAIXA

VARIAVEL #TEXTO_FONTE

   Private hReg, oProgressBar, DTA1:=S_DATUAL, DTA2:=S_DATUAL, zFgOk:=.t., z_pgcx01:=0,z_pgcx02:=0,z_pgcx03:=0,z_pgcx04:=0, z_vfundo:=0,z_vidinh:=0,z_vicart:=0,z_numcx:=RecCount()
   
   if .not. QuickDialog("lst_cxadirfr3", "CAIXA DIARIO",{ ;
       {"2;2;Fechamento Caixa:", "@z_numcx", @z_numcx, "99999", "", ""} } )
      Return
   endif

   Abra('0NOIDX', r_siga, 'SF_FATNF', 'SF_FATNF')
   zFgCover:=FieldPos('fValCover')>0
   oProgressBar:= myHProgressBar():Create( "Processando Caixa "+lTrim(Str(z_numcx,12)) )
   oLst:GroupBy('mem:_movfat0', {||fcaixa}, 'fcaixa as char,'+;
                                    'if(ftipodig="S",fvales,0) as ves, if(ftipodig="M",fvales,0) as vesf,'+;
                                    'if(ftipodig="S",fvalch,0) as vch, if(ftipodig="M",fvalch,0) as vchf,'+;
                                    'if(ftipodig="S",fvalct,0) as vct, if(ftipodig="M",fvalct,0) as vctf,'+;
                                    'if(ftipodig="S",fvalcv,0) as vcv, if(ftipodig="M",fvalcv,0) as vcvf,'+;
                                    'if(ftipodig="S",fvalca,0) as vca, if(ftipodig="M",fvalca,0) as vcaf,'+;
                                    'if(zFgCover.and.fvalcover>0,fvalcover,0) as vcover,'+;
                                    'if(zFgCover.and.fvaltxser>0,fvaltxser,0) as vtaxa,'+;
                                    '0 as vqt,'+;
                                    '0 as vqpg,'+;
                                    '0 as vfundo,'+;
                                    '0 as vidinh,'+;
                                    '0 as vicart,'+;
                                    'if(ftipodig="S",fsubsidio,0) as vfp, if(ftipodig="M",fsubsidio,0) as vfpf' ,;
                                {||oProgressBar:step(), fg_fat<>"C".and.val(cod_sit)<>2.and.si_numfech=z_numcx}, .t.)

   oProgressBar:close()

   SF_FATNF->(DbCloseArea())
   Abra ('0NOIDX', r_siga, wsf_fat, 'SF_FATNF',   wsf_fat_p, 'fnumped')

   Abra ('0NOIDX', r_siga, wSE_MOV, 'SE_MOVNF')
   oProgressBar:= myHProgressBar():Create( "Processando Itens do Caixa "+lTrim(Str(z_numcx,12)) )
   oLst:GroupBy('mem:_movprd0', {||pcod}, 'pcod as char, mquant, mvalor3, mvalor', ;
                                {||oProgressBar:step(), mfges$"SM".and.sf_fatnf->( DbSeek(SE_MOVNF->fNumPed) .and. fg_fat<>"C".and.val(cod_sit)<>2.and.si_numfech=z_numcx ) }, .t.)
   oProgressBar:close()

   Abra('0NOIDX', r_siga, 'SCCRDUP', 'SCCRDUP')
   oProgressBar:= myHProgressBar():Create( "Processando Quitacoes do Caixa "+dtoc(dta1))
   copy to ("mem:_movlqd") fields si_caixa, cr_num_ped, cr_hora, cr_val_lqd, cr_dt_lqd,cr_obs, cr_cod_cli,cr_usuario,si_numfech for (oProgressBar:step().or.zFgOk).and.si_numfech=z_numcx.and.left(cr_num_ped,4)="CXES".or.right(cr_num_ped,4)="CXES"
   oProgressBar:close()
   DbCloseAll()

	use ("mem:_movfat0") new exclusive
	use ("mem:_movlqd") new exclusive
	do while .not. eof()
	   hReg := {=>}	
	   hReg['FCAIXA'] := _movlqd->si_caixa
	   hReg['VQT']    := _movlqd->cr_val_lqd      
      _movfat0->( AppendBlank(), Gather(hReg) )
      Skip
   enddo
   DbCloseAll()
   Abra('0NOIDX', r_siga, 'SCCPPED', 'SCCPPED')
   oProgressBar:= myHProgressBar():Create( "Processando Despesas do Caixa "+dtoc(dta1))
   copy to ("mem:_movpgto") fields cp_num_ped, cp_val_lqd, si_caixa, cp_dt_lqd,cp_hora,cp_cod_for,cp_desc,si_numfech,cp_divisao for (oProgressBar:step().or.zFgOk).and.si_numfech=z_numcx.and.left(cp_num_ped,2)="CX"
   oProgressBar:close()
   DbCloseAll()

   use ("mem:_movfat0") new exclusive
   use ("mem:_movpgto") new exclusive
	do while .not. eof()
	   hReg := {=>}	
	   hReg['FCAIXA'] := _movpgto->si_caixa
	   hReg['VQPG']    := _movpgto->cp_val_lqd      
      _movfat0->( AppendBlank(), Gather(hReg) )
      Skip
   enddo
   DbCloseAll()   

   Abra('0NOIDX', r_siga, 'SI_CAIXA', 'SI_CAIXA')
   oProgressBar:= myHProgressBar():Create( "Processando Despesas do Caixa "+dtoc(dta1))
   copy to ("mem:_movcxa") fields fdinh, vidinh,vicart,si_caixa,si_numfech for (oProgressBar:step().or.zFgOk).and.recno()=z_numcx
   oProgressBar:close()
   DbCloseAll()

   use ("mem:_movfat0") new exclusive
   use ("mem:_movcxa") new exclusive
   do while .not. eof()
      hReg := {=>}	
      hReg['FCAIXA']    := _movcxa->si_numfech
      hReg['VFUNDO']    := _movcxa->fdinh   
      hReg['VIDINH']    := _movcxa->vidinh
      hReg['VICART']    := _movcxa->vicart
      _movfat0->( AppendBlank(), Gather(hReg) )
	  
      z_vfundo  +=  fdinh
      z_vidinh  +=  vidinh
      z_vicart  +=  vicart
      Skip
   enddo
   
   DbCloseAll() 
   
	use ("mem:_movfat0") new exclusive
   oLst:GroupBy('mem:_movfat', {||fcaixa}, 'fcaixa as char, ves, vesf, vch, vchf, vct, vctf, vcv, vcvf, vca, vcaf, vqt, vqpg,vidinh,vicart,vfundo,vcover,vtaxa, vfp, vfpf',, .t.)
   DbCloseAll()
   DbDrop("mem:_movfat0")

   Abra('0NOIDX', r_siga, 'se_prod', 'se_prod', 'seip_c', 'pcod')
	use ("mem:_movprd0")  new exclusive
	index on se_prod->(DbSeek(_movprd0->pCod), pDesc) to ("mem:_movprd0")
	
   Abra('0NOIDX', r_siga, 'CP_DIVIS', 'CP_DIVIS')
   Abra('0NOIDX', r_siga, 'SCCPFOR',  'SCCPFOR')
   Abra('0NOIDX', r_siga, 'SCCRCLI',  'SCCRCLI')
	use ("mem:_movfat")  new exclusive
	use ("mem:_movlqd")  new exclusive
	use ("mem:_movpgto") new exclusive
	  
   debugalert('7 vai :run e :end')
   oLst:lOutputPdf      := .f.
   oLst:lOutputFr3      := .t.
   oLst:cAliasMaster    := if( _movfat->(RecCount())>0, '_movfat', IF(_movlqd->(RecCount())>0, '_movlqd', '_movpgto') )
   oLst:cReport         := 'cxa_r01b_reprocessamento_rolo'
   
   oLst:AddDbFr3("mem:_movprd0", "_movprd0")
   oLst:AddDbFr3("mem:_movfat", "_movfat")
   oLst:AddDbFr3("mem:_movlqd", "_movlqd")
   oLst:AddDbFr3("mem:_movpgto","_movpgto")

   oLst:Run()
   oLst:End()
   dbCloseAll()

   DbDrop('mem:_movfat')   // limpa da memoria tabela que foi criada na memoria nos passos anteriores
   DbDrop("mem:_movlqd")   
   DbDrop("mem:_movpgto")   
   DbDrop("mem:_movprd0")

#FIM_TEXTO_FONTE

