ALIAS HELP

VARIAVEL Z_UNIDS = 0
VARIAVEL DTA1 = S_DATUAL
VARIAVEL DTA2 = S_DATUAL

VARIAVEL hReg := {=>}

VARIAVEL #TEXTO_FONTE
   Private zbFor, zbComprasVnd:="", aResultCompilacao, zComprasDoVnd:=0, zTotVendas:=0, z_totcomis:=0, zCodVnd:=0, zTxAdicServ:=0, zTxAdicProd:=0, lFgCortesia,nfPercentDesconto, zCodVndComoCli

   Abra('0NOIDX', r_siga, 'SF_VEND', 'SF_VEND')
   if .not. QuickDialog("lst_comoper", "Comissao por Vendedores",{ ;
       {"2;2;Profissional:", "@zCodVnd", @zCodVnd,"999","","psq_brow;sf_vend;psfvend;N;N;;"},;
       {"3;2;Data Inicial:", "@dta1",    @dta1, "99.99.9999", "", ""} ,;
       {"4;2;Data Final..:", "@dta2",    @dta2, "99.99.9999", "", ""}  } )
       DbCloseArea()
      Return
   endif
   sf_vend->(DbGoTo(zCodVnd), zCodVndComoCli:=Cr_Cod_Cli)
   Abra('0NOIDX', r_siga, 'SE_MovNF', 'SE_MovNF')
   zbFor := {|| mdata>=dta1.and.mdata<=dta2 .and. MFGES="S".and.mtipoven$"123456Z".and.fnumped<>0.and.pOperador=zCodVnd.and.mvalor>0 }
   xDbCopyTo('mem:_movnf', '_movnf', zbFor, "pOperador,pCod,mQuant,mValor,fNumPed")
   DbCloseArea()

   Abra('0NOIDX', r_siga, 'SCCRDUP', 'SCCRDUP')
   xDbCopyTo('mem:_dupvnd', '_dupvnd', {||zCodVndComoCli>0.and.zCodVndComoCli=cr_cod_cli.and.day(cr_dt_lqd)=0}, "cr_tip_dup,cr_dt_emis,cr_valor,fNumPed")
   DbCloseArea()
   
   oLst:lOutputPdf      := .f.
   oLst:lOutputFr3      := .t.
   oLst:cAliasMaster    := '_movtot' 
   oLst:cAlias_DataCols := '_movtot'
   oLst:cReport         := 'ComOper1'

   // Abra('0NOIDX',r_siga,'osag','osag','osag','Dtos(DtPrevista)+Str(fVendedor,6)+Str(fCli,6)+HrPrevista','osagcli','Str(fCli,6)+Dtos(DtPrevista)+HrPrevista+Str(fVendedor,6)','osagped','fNumPed')
   Abra("0NOIDX", r_siga, "sccrcli", "sccrcli")
   Abra('0NOIDX', r_siga, wsf_fat,   "Sf_FatNF", wsf_fat_p, 'fnumped')
   Abra('0NOIDX', r_siga, 'SE_PROD',  'SE_PROD',  'SEIP_C',   'PCOD')
   use ("mem:_movnf") new exclusive
   if reccount()=0
      DbCloseAll()
      dbDrop("mem:_movnf")	  
	   Alert("nao encontrei atendimentos para "+ltrim(Str(zCodVnd,6))+" entre "+Dtoc(dta1)+" e "+dtoc(dta2) )
	   Return
   endif

   do while .not. eof()
      se_prod->( DbSeek(_movnf->pCod) )
      sf_vend->( DbGoTo(_movnf->pOperador), zTxAdicServ:=Comis_Serv, zTxAdicProd:=Comis_Prod )
      Sf_FatNF->( DbSeek(_movnf->fNumPed) )

      lFgCortesia := Abs(Sf_FatNF->fVal)<=0.10 .and. Sf_FatNF->(fDesconto > fVal)
      nfPercentDesconto := Sf_FatNF->(if(lFgCortesia.or.fVal+fDesconto<0.10, 0, 100*fDesconto/(fVal+fDesconto) ))
      nfPercentDesconto := if( nfPercentDesconto>0, nfPercentDesconto, 0)

      hReg['PCOD']      := _movnf->pCod
      hReg['POPERADOR'] := _movnf->pOperador
      hReg['MQUANT']    := _movnf->mQuant
      hReg['MVALOR']    := _movnf->mvalor * if(lFgCortesia,1,(1-nfPercentDesconto/100))
      hReg['PRUNIT']    := _movnf->(Round(mvalor/mQuant,2))
      hReg['MCOMISSAO'] := _movnf->mvalor * se_prod->( if(pComisVis>0, pComisVis+if('S'$pServico,zTxAdicServ,zTxAdicProd), 0)/100 )* if(lFgCortesia,1,(1-nfPercentDesconto/100))
      hReg['FCORTESIA'] := if(lFgCortesia,'Cortesia',if(nfPercentDesconto>0,'Convenio',space(8)))
      hReg['PERCDSC']   := Pad(if(nfPercentDesconto>0, "-"+lTrim(if(nfPercentDesconto-int(nfPercentDesconto)>0, Str(nfPercentDesconto,6,2), Str(nfPercentDesconto,3)))+"%",""),6)
      hReg['MDATA']     := Sf_FatNF->fDataEmi
      hReg['MHORA']     := left(Sf_FatNF->fHora,5)
      hReg['PDESC']     := SE_PROD->PDESC
      hReg['NOMEVND']   := SF_VEND->NOME
      hReg['NOMECLI']   := SCCRCLI->(DbGoTo(Sf_FatNF->fCli),cr_nome)
      	  
      oLst:PutRsFr3('_movtot', hReg )
	  
      zTotVendas += mValor
      z_totcomis += hReg['MCOMISSAO']
      skip
   enddo
   
   _movnf->( DbCloseArea() )
   DbDrop("mem:_movnf")

   hReg := {=>}
   // sFgDebugAlert:=.t.
   BLOCO_TXT zbComprasVnd
      Se_prod->(DbSeek(SE_MovNF->pCod))
      Sf_FatNF->(DbSeek(SE_MovNF->fNumPed))
      hReg['PCOD']    :=SE_MovNF->pCod
      hReg['MQUANT']  :=SE_MovNF->mQuant
      hReg['MVALOR']  :=SE_MovNF->mvalor
      hReg['PRUNIT']  :=SE_MovNF->(Round(mvalor/mQuant,2))
      hReg['MDATA']   :=Sf_FatNF->fDataEmi
      hReg['MHORA']   :=left(Sf_FatNF->fHora,5)
      hReg['PDESC']   :=SE_PROD->PDESC
      hReg['FNUMPED'] :=_dupvnd->fnumped
      oLst:PutRsFr3('_movcmp', hReg )
   FIM_BLOCO_TXT
   zbComprasVnd := if( len(aResultCompilacao:=RdScript(,@zbComprasVnd))>0, {||DoScript(aResultCompilacao)}, NIL)

debugalert("0=> valtype(zbComprasVnd) = "+valtype(zbComprasVnd)+" -> "+hb_valtoexp(zbComprasVnd) )

   Abra('0NOIDX', r_siga, 'SE_MovNF', 'SE_MovNF', wSeim_p, 'FNUMPED')
   use ("mem:_dupvnd") new exclusive
   copy to _dp
   dbGoTop()
debugalert("1=> valtype(zbComprasVnd) = "+valtype(zbComprasVnd)+" -> "+hb_valtoexp(zbComprasVnd) )
   
   do while .not. eof()
      zComprasDoVnd += _dupvnd->cr_valor
      if _dupvnd->fnumped>0 .and. se_movnf->(DbSeek(_dupvnd->fnumped)) 
         se_movnf->( DbEval(zbComprasVnd,{||MFGES="S".and.mvalor>0},{||_dupvnd->fnumped=fNumPed}) )
      else
         hReg['PCOD']    := 0
         hReg['MQUANT']  := 1
         hReg['MVALOR']  := _dupvnd->Cr_Valor
         hReg['PRUNIT']  := _dupvnd->Cr_Valor
         hReg['MDATA']   := _dupvnd->cr_dt_emis
         hReg['MHORA']   := Space(5)
         hReg['PDESC']   := Pad("Compra Interna",35)
         hReg['FNUMPED'] := _dupvnd->fnumped
         oLst:PutRsFr3('_movcmp', hReg )
      endif
      skip
   enddo
   SE_MovNF->(DbCloseArea())

debugalert("2=> valtype(zbComprasVnd) = "+valtype(zbComprasVnd)+" -> "+hb_valtoexp(zbComprasVnd) )
  
   if zComprasDoVnd=0
      hReg['PCOD']    :=0
      hReg['MQUANT']  :=0
      hReg['MVALOR']  :=0
      hReg['PRUNIT']  :=0
      hReg['MDATA']   := Ctod("")
      hReg['MHORA']   := space(5)
      hReg['PDESC']   := Pad("Sem Compras Internas",35)
      hReg['FNUMPED'] := 0
      oLst:PutRsFr3('_movcmp', hReg )
      _movcmp->( __dbZap() )
      debugalert("3=> _movcmp ficou vazio com um __dbZap")
   endif

debugalert("4=> valtype(zbComprasVnd) = "+valtype(zbComprasVnd)+" -> "+hb_valtoexp(zbComprasVnd) )
   
   // Select _movcmp
   // index on fnumped to ("mem:_movcmp")

   Select _movcmp
   index on mData to ("mem:_movcmp")

debugalert("5=> valtype(zbComprasVnd) = "+valtype(zbComprasVnd)+" -> "+hb_valtoexp(zbComprasVnd) )

   // oLst:AddDbFr3('_dupvnd','_dupvnd')
   // oLst:AddMasterDetailFr3('_dupvnd','_movcmp','fnumped')

debugalert("6=> valtype(zbComprasVnd) = "+valtype(zbComprasVnd)+" -> "+hb_valtoexp(zbComprasVnd) )
   
   Select _movtot
   index on dtos(mData)+pdesc+str(pcod,6) to ("mem:_movtot")
   DbGoTop()   // a instrucao oLst:PutRsFr3('_movtot', hReg ) criou essa tabela _movtot.dbf na memoria ... o dbgotop eh para reposicionar no topo dela

   debugalert('7 vai :run e :end')

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

   DbDrop("mem:_movtot")
   DbDrop("mem:_movcmp")
   DbDrop("mem:_dupvnd")
   
#FIM_TEXTO_FONTE
