ALIAS Sf_FatP
VARIAVEL znil := DbGoto(z_recno)
VARIAVEL wOpcao := if( type('wOpcao')='U', 'a4', wOpcao)
VARIAVEL zFgLogo:= if( File(r_siga+"usr\logo_cliente.jpg"), "S", "N")

VARIAVEL #TEXTO_FONTE
   Private hReg,zDadosCli:="",zStruct,m_fDataemi,zfnumped,zNumOs,ztProds:=0,ztLocacao:=0,ztServ:=0,ztDev:=0,zfVal3:=0,wCrNumPed,zPercAbat,zFgServ:=.f.,zPcod,zQuant,zPrUnit,zPrTotal,zPrCusto,zpVluVenda,zPrLocUnit,zPrLocTotal,zpDesc,zpUnid,zFatpRecno,zFgPnPcOpen,zNi:=0,zNdp:=0,zfCli,zbAjust,zFgImprimeFormasPagDoPedidoEnaoDasFaturas:=.t.,zFgFieldCompl,zFgFieldObs,zFgFieldObsMov,zbSubTot

   DebugAlert('000-na cupom_pn_prt_fr3.lst  wOpcao='+wOpcao)

   zfCli      := Sf_FatP->fCli
   zPercAbat  := Sf_FatP->( if( type('fTaxaAbat')="U", 0, fTaxaAbat) )
   zFatpRecno := Sf_FatP->( Recno() )
   zfnumped   := Sf_FatP->fNumPed
   zfnumfich  := Sf_FatP->( if(fieldpos('fNumficha')=0,0,fNumficha) )
   zfnumMesa  := Sf_FatP->( if(fieldpos('fmesa')=0,0,fmesa) )
   m_fDataemi := Sf_FatP->fDataEmi
   zfNum      := Sf_FatP->fNum

   se .not. select('sccrcli')>0 entao Sf_FatP->( Abra_Arq("sccrcli") )
   se .not. select('sf_vend')>0 entao Sf_FatP->( Abra_Arq("sf_vend") )
   se .not. select('se_prod')>0 entao Sf_FatP->( Abra('0NOIDX', r_siga, 'SE_PROD', 'SE_PROD', 'SEIP_C', 'PCOD') )
   se_prod->( zFgFieldCompl:=FieldPos('pComplemen')>0, zFgFieldObs:=FieldPos('pObs')>0 )

   hReg := Sf_FatP->( Scatter() )
   hReg['IDPED'] := Sf_FatP->( lTrim(Str(fnumped,12)) + if(zPercAbat>0, ' / '+lTrim(Str(fnum,12)), '') )
   Sf_FatP->( oLst:PutRsFr3('_ped',hReg) )

   hReg := sccrcli->( if( zfCli=0, Scatter('blank'), (DbGoTo(zfCli),Scatter()) ) )
   hReg['CR_NOME'] := if(zfCli=0,"Consumidor Final",hReg["CR_NOME"])
   hReg['FCLI'] := zfCli
   if zfCli>0
      zbAjust := {|x,cSep, cStart| zDadosCli += if(empty(zDadosCli).or.empty(x), "", cSep) + if(empty(x),"",cStart+AllTrim(x)) }
      ( eVal(zbAjust, hReg["CR_RUA"], "", "<b>End:</b>") , eVal(zbAjust, hReg["CR_NUMRUA"], ",", "") , eVal(zbAjust, hReg["CR_BAIRRO"], ",", "") )
      ( eVal(zbAjust, hReg["CR_CEP"], chr(13)+Chr(10), "<b>CEP </b>") , eVal(zbAjust, hReg["CR_CIDADE"], ",", ""), eVal(zbAjust, hReg["CR_UF"],      ", ", "") )
      se cgc_ok(hReg["CR_CGC"]) entao ( eVal(zbAjust, hReg["CR_CGC"], chr(13)+Chr(10)+"<b>CNPJ:</b>", ""), eVal(zbAjust, hReg["CR_INSC_ES"], "<b>IE:</b>", "") ) senao ( eVal(zbAjust, hReg["CR_CGC"], "<b>CPF:</b>", ""), eVal(zbAjust, hReg["CR_RG"], "<b>RG:</b>", "") )
      eVal(zbAjust, hReg["CR_EMAIL"], chr(13)+Chr(10),"<b>Email:</b>")
      eVal(zbAjust, hReg["CR_PONTORE"], chr(13)+Chr(10),"<b>Ponto Ref:</b>")
   endif
   oLst:PutRsFr3('_cli', hReg )

   sf_vend->( DbGoto(Sf_FatP->fVendedor), oLst:PutRsFr3('_vend',Scatter()) )

   if sys_segmento='oticas'
      zbAjust := {|x| x:= if(x=0, "0", if(x>0, "+", "")+lTrim(Str(x,10,2)) ) }
      Sf_FatP->( Abra('0NOIDX', r_siga, 'OSOT', 'OSOT', 'OSOT_REC', 'FATPRECNO', "OSOT", "CR_COD_CLI") , Abra('0NOIDX', r_siga, 'vb_crm', 'vb_crm', 'vb_crm', 'crm') )
      se zFatpRecno>0.and.OsOt->(DbSeek(zFatpRecno)) entao hReg := OsOt->( zNumOs:=fNumOs, Scatter() ) senao hReg := OsOt->( OrdSetFocus('osot'), if(DbSeek(zfCli), (zNumOs:=fNumOs, Scatter()),(zNumOs:=0, Scatter("blank"))) )
      hReg['MEDICO']   := if( OsOt->FatpRecno=zFatpRecno.and.vb_crm->(DbSeek(osot->crm)), vb_crm->(AllTrim(MEDICO)), '') + ' '
      hReg['CLI_NOME'] := AllTrim(sccrcli->CR_NOME)
      hReg['VND_NOME'] := AllTrim(sf_vend->NOME)+' '

      eVal(zbAjust, @hReg["L_OD_ESF"] )
      eVal(zbAjust, @hReg["L_OD_CIL"] )
      eVal(zbAjust, @hReg["L_OE_ESF"] )
      eVal(zbAjust, @hReg["L_OE_CIL"] )

      eVal(zbAjust, @hReg["P_OD_ADIC"] )   

      eVal(zbAjust, @hReg["P_OD_ESF"] )
      eVal(zbAjust, @hReg["P_OD_CIL"] )
      eVal(zbAjust, @hReg["P_OE_ESF"] )
      eVal(zbAjust, @hReg["P_OE_CIL"] )
      oLst:PutRsFr3('_osot', hReg )
      osot->(DbCloseArea())
      vb_crm->(DbCloseArea())
   endif

   zStruct := {}
   aAdd(zStruct, {'IDPED','C',13,0} )
   aAdd(zStruct, {'DTVENC','C',10,0}  )
   aAdd(zStruct, {'VPAG','N',12,2} )
   aAdd(zStruct, {'TPAG','C',20,0} )
   DbCreate('mem:_dup', zStruct)
   use ('mem:_dup') new exclusive
   oLst:AddDbFr3('mem:_dup', '_dup')

   hReg := {=>}
   if zPercAbat > 0 .and. .not. zFgImprimeFormasPagDoPedidoEnaoDasFaturas
      Abra ("0NOIDX",r_siga,"Sf_FatNF", "Sf_FatNF", "Sf_FatNF", "FNUM", "sffatnf2","pfnum")
      Sf_FatNF->(DbSeek(Sf_FatP->fnum))
      wCrNumPed := strzero(Sf_FatNF->fnumped,6)
      ABRA ("0NOIDX", r_siga, "SCCRDUP", "SCCRDUP", "SCRID_ND", "CR_NUM_PED")
      if DbSeek(wCrNumPed)
         do while .not. eof() .and. left(cr_num_ped,6) = wCrNumPed
            se (cr_cod_Cli=zfCli .or. cr_tip_dup="FP" ) entao (++zNdp, hReg['IDPED']:=cr_num_ped, hReg['DTVENC']:=Dtoc(if(cr_tip_dup$"CT/CH",cr_dt_doc,cr_dt_venc)) , hReg['VPAG']:=cr_valor, hReg['TPAG']:=eVal(zbAjust,cr_tip_dup), oLst:PutRsFr3('_dup', hReg) )
            skip
         enddo
      endif
      Sf_FatNF->(OrdSetFocus( "sffatnf2" ) )
      Sf_FatNF->(DbSeek(Sf_FatP->fnum))
      wCrNumPed := strzero(Sf_FatNF->fnumped,6)
      if DbSeek(wCrNumPed)
         do while .not. eof() .and. left(cr_num_ped,6) = wCrNumPed
            se (cr_cod_Cli=zfCli .or. cr_tip_dup="FP" ) entao (++zNdp, hReg['IDPED']:=cr_num_ped, hReg['DTVENC']:=Dtoc(if(cr_tip_dup$"CT/CH",cr_dt_doc,cr_dt_venc)) , hReg['VPAG']:=cr_valor, hReg['TPAG']:=eVal(zbAjust,cr_tip_dup), oLst:PutRsFr3('_dup', hReg) )
            skip
         enddo
      endif
      SCCRDUP->( DbCloseArea() )
      Sf_FatNF->( DbCloseArea() )
   endif

   if zPercAbat=0 .or. zFgImprimeFormasPagDoPedidoEnaoDasFaturas
      zbAjust := {|x|x:=upper(x),if( x='ES','Especie', if(x='CH','Cheque', if(x='CT','Cartao', if(x='CV','Convenio', if(x='CA','Carne', x)))) )}
      se (zFgPnPcOpen:=Select('sf_pnpc')=0) entao Abra ('0NOIDX', r_siga, 'sf_pnpc', 'sf_pnpc', 'sf_pnpc', 'fnumped') senao DbSelectArea('sf_pnpc')
      lFgOk:=sf_pnpc->( DbSeek(zfnumped) )
      Do While .Not. Eof() .And. zfnumped=fNumPed 
         se Cr_valor>0 entao (++zNdp, hReg['IDPED']:=strzero(zfnumped,6), hReg['DTVENC']:=Dtoc(m_fDataemi+fprazo) , hReg['VPAG']:=cr_valor, hReg['TPAG']:=eVal(zbAjust,cr_tip_dup), oLst:PutRsFr3('_dup', hReg))
         skip
      EndDo
      se zFgPnPcOpen entao sf_pnpc->( DbCloseArea() )
   endif

   // se zNdp=0 entao (hReg['IDPED']:=' ', hReg['DTVENC']:=' ', hReg['VPAG']:=' ', hReg['TPAG']:=' ', oLst:PutRsFr3('_dup', hReg))

   hReg := {=>}
   zStruct := {}
   aAdd(zStruct, {'PCOD','N',13,0} )
   aAdd(zStruct, {'QUANT','N',10,2} )
   aAdd(zStruct, {'PRUNIT','N',10,2}  )
   aAdd(zStruct, {'PRLOCUNIT','N',10,2} )
   aAdd(zStruct, {'PVLUVENDA','N',12,2} )
   aAdd(zStruct, {'PRTOTAL','N',12,2} )
   aAdd(zStruct, {'PRCUSTO','N',12,2} )
   aAdd(zStruct, {'PDESC','M',10,0} )
   aAdd(zStruct, {'PUNID','C',5,0} )
   DbCreate('mem:itens',   zStruct)
   DbCreate('mem:serv',    zStruct)
   DbCreate('mem:itemdev', zStruct)
   oLst:AddDbFr3('mem:itens',  'itens')
   oLst:AddDbFr3('mem:serv',   'serv')
   oLst:AddDbFr3('mem:itemdev','itemdev')
   use ('mem:itens') new alias itens exclusive

   DbSelectArea("Sf_PnMv")
   zFgFieldObsMov := FieldPos('pObs')>0
   DbSeek(Sf_FatP->fNumPed)
   Do While Sf_FatP->fNumPed = fNumPed .and. .not. eof()
      if Quant>0 .and. se_prod->(DbSeek(Sf_PnMv->Cod))
         if se_prod->pServico="S"
            zFgServ := .t.               
         else
            zPcod       := Sf_PnMv->Cod
            zQuant      := Sf_PnMv->Quant
            zPrTotal    := Sf_PnMv->PrTotal
            zPrCusto    := zQuant * se_prod->pvluven3

            zPrLocUnit  := Sf_PnMv->pValLoca
            zPrLocTotal := zQuant * Sf_PnMv->pValLoca
            zpVluVenda  := Sf_PnMv->( if(FieldPos('PVLUVENDA')=0, se_prod->pVluVenda, pVluVenda) )

            zfVal3      := zfVal3 + zPrCusto
            ztProds     := ztProds + zPrTotal

            ztLocacao   := ztLocacao + zPrLocTotal

            zpDesc := se_prod->( AllTrim(pDesc) + if(zFgFieldCompl.and..not.empty(pComplemen)," "+AllTrim(pComplemen),"") ) + if(zFgFieldObsMov.and..not.empty(pObs), AllTrim(pObs), if(zFgFieldObs.and..not.empty(se_prod->pObs),AllTrim(se_prod->pObs),"") )
            zpUnid := se_prod->pUnidade
            zPrUnit := zPrTotal/zQuant

            hReg['PCOD']      := zPcod
            hReg['QUANT']     := zQuant
            hReg['PRUNIT']    := zPrUnit
            hReg['PRTOTAL']   := zPrTotal
            hReg['PRCUSTO']   := zPrCusto
            hReg['PRLOCUNIT'] := zPrLocUnit
            hReg['PVLUVENDA'] := zpVluVenda
            hReg['PDESC']     := zpDesc
            hReg['PUNID']     := zpUnid
            oLst:PutRsFr3('itens', hReg )
            
            ++zNi
            
         endif
      endif
      DbSkip()
   enddo

debugalert('2 imprimira servicos vendidos na cupom_pn_prt.lst')
   hReg := {=>}
   if zFgServ
      DbSeek(Sf_FatP->fNumPed)
      do while Sf_FatP->fNumPed = fNumPed .and. .not. eof()
         if Sf_PnMv->Quant>0 .and. se_prod->(DbSeek(Sf_PnMv->Cod)) .and. se_prod->pServico="S"
            zPcod    := Sf_PnMv->Cod
            zQuant   := Sf_PnMv->Quant 
            zPrTotal := Sf_PnMv->PrTotal
            zPrUnit  := zPrTotal/zQuant
            zPdesc   := se_prod->pDesc
            zpUnid   := se_prod->pUnidade
            zpVluVenda := Sf_PnMv->( if(FieldPos('PVLUVENDA')=0, se_prod->pVluVenda, pVluVenda) )

            ztServ := ztServ + zPrTotal

            hReg['PCOD']       := zPcod
            hReg['QUANT']      := zQuant
            hReg['PRUNIT']     := zPrUnit
            hReg['PRTOTAL']    := zPrTotal
            hReg['PDESC']      := zpDesc
            hReg['PUNID']      := zpUnid
            hReg['PVLUVENDA']  := zpVluVenda
            oLst:PutRsFr3('serv', hReg )

            ++zNi
            
         endif
         DbSkip()
      enddo
   endif

debugalert('1 imprimira DEVOLUCOES na cupom_pn_prt.lst ')
   hReg := {=>}
   if Sf_FatP->fValDev > 0
      DbSeek(Sf_FatP->fNumPed)
      do while Sf_FatP->fNumPed = fNumPed .and. .not. eof()
         if quantdev>0 .and. se_prod->(DbSeek(Sf_PnMv->Cod)) 
            zPcod    := Sf_PnMv->Cod
            zQuant   := Sf_PnMv->quantdev
            zPrTotal := Sf_PnMv->PrTotal
            zPrUnit  := zPrTotal/zQuant
            zPdesc   := se_prod->pDesc
            zpUnid   := se_prod->pUnidade

            hReg['PCOD']       := zPcod
            hReg['QUANT']      := zQuant
            hReg['PRUNIT']     := zPrUnit
            hReg['PRTOTAL']    := zPrTotal
            hReg['PDESC']      := zpDesc
            hReg['PUNID']      := zpUnid
            oLst:PutRsFr3('itemdev', hReg )

            ztDev := ztDev + zPrTotal
         endif
         DbSkip()
      enddo
   endif

   hReg := {=>}
   hReg['FVAL3']      := zfVal3
   hReg['TOTPROD']    := ztProds
   hReg['TOTLOCA']    := ztLocacao
   hReg['TOTDEVOL']   := ztDev
   hReg['TOTSERV']    := ztServ
   hReg['DESCONTOS']  := Sf_FatP->fDesconto
   hReg['ACRESCIMO']  := Sf_FatP->( if(fieldpos('fAcrescimo')=0,0,fAcrescimo) )
   hReg['TOTLIQUIDO'] := Sf_FatP->fVal
   hReg['FVALCOVER']  := Sf_FatP->( if(hReg['ACRESCIMO']=0.or.fieldpos('fvalcover')=0,0,fvalcover) )
   hReg['TXSER']      := Sf_FatP->( if(hReg['ACRESCIMO']=0.or.fieldpos('fPercTxSer')=0,0,fPercTxSer) )
   hReg['FVALTXSER']  := ztProds * hReg['TXSER']/100 
   hReg['FVALTOTAL']  := ztProds + hReg['FVALTXSER'] + hReg['FVALCOVER']
   hReg['QTDITENS']   := zNi
   oLst:PutRsFr3('_tot', hReg )

   DbCreate('mem:_subtot', {{'DESC','C',30,0},{'OBS','C',20,0}})
   zbSubTot:={|cDesc,nValor,cConteudoComPict| if(nValor>0, _subtot->(AppendBlank(), _field->Desc:=cDesc, _Field->obs:=cConteudoComPict), ) }
   use ('mem:_subtot') new exclusive
   eVal( zbSubTot, "Valor dos Produtos",   ztProds,           Transform(ztProds,           "@E 999,999,999.99"))
   eVal( zbSubTot, "Valor dos Servicos",   ztServ,            Transform(ztServ,            "@E 999,999,999.99"))
   eVal( zbSubTot, "Valor das Devolucoes", ztDev,             Transform(ztDev,             "@E 999,999,999.99"))
   eVal( zbSubTot, "Valor das Locacoes",   ztLocacao,         Transform(ztLocacao,         "@E 999,999,999.99"))
   // eVal( zbSubTot, "Taxa de Servico",   hReg['TXSER'],     Transform(hReg['TXSER'],     "@E 999.99%") )
   eVal( zbSubTot, "Taxa de Servico",      hReg['FVALTXSER'], "("+lTrim(Transform(hReg['TXSER'],"@E 999.99%"))+") "+Transform(hReg['FVALTXSER'], "@E 999,999,999.99") )
   eVal( zbSubTot, "Valor dos Descontos",  hReg['DESCONTOS'], Transform(hReg['DESCONTOS'], "@E 999,999,999.99"))
   eVal( zbSubTot, "Valor dos Acrescimos", hReg['ACRESCIMO'], Transform(hReg['ACRESCIMO'], "@E 999,999,999.99"))
   eVal( zbSubTot, "Cover Artistico",      hReg['FVALCOVER'], Transform(hReg['FVALCOVER'], "@E 999,999,999.99"))
   oLst:AddDbFr3('mem:_subtot', '_subtot')
   // sfgdebugalert:=.t.
   oLst:lOutputPdf      := .f.
   oLst:lOutputFr3      := .t.
   oLst:cAliasMaster    := 'itens' 
   oLst:cAlias_DataCols := 'itens'
   oLst:cReport         := 'cupom_'+wOpcao

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

   oLst:Run()
   oLst:End()
   dbCloseAll()
   
   dbDrop('mem:_ped')
   dbDrop('mem:_cli')
   dbDrop('mem:_dup')
   dbDrop('mem:_vend')
   dbDrop('mem:_osot')
   dbDrop('mem:itens')
   dbDrop('mem:serv')
   dbDrop('mem:itemdev')
   dbDrop('mem:_tot')
   dbDrop('mem:_subtot')
#FIM_TEXTO_FONTE

// eixo nao tem decimais e nao precisa de sinal + ou -  ... deve-se usar o simbolo de grau ... o eixo possui ate 3 digitos, de 5graus a 180graus

// dnp  nao tem sinais + e - ...  tem ate 2 digitos e 2 decimais ... distancia naso pupilar (do nariz a pupila)
// co tem ate dois digitos nao tem decimais nem sinal de + e -
// dp   nao tem sinais + e - ...  tem ate 2 digitos e 2 decimais ... distancia entre as pupilas

// obs: o cilindrico ou é zero ou é negativo ... 
// obs: esta faltando destacar a adicao na nova receita ... eh um numero com 2 decimais ...
// 
//   DebugAlert('2 ')
