W_UF = "PB"
z_tela:=Janela(6,30,20,75," Impressao de NF Talao ") 
DbCloseAll() 

DbSelectArea(2)
dbUseArea( .F.,, (r_siga-"se_movnf"), "se_movnf", NIL, .F. )
if file(r_siga-"ntx\seim_f"-ordbagext())
   ordListClear(); ordListAdd( (r_siga-"ntx\seim_f") )
else
   dbCreateIndex( (r_siga-"ntx\seim_f"), "mfnum", _CreateBlock("mfnum"), NIL )
endif

Abra ("3",r_siga,"SE_PROD","SE_PROD","SEIP_C","PCOD") 
Abra ("4",r_siga,"SCCRCLI","SCCRCLI") 
Abra ("5",r_siga,"SF_TRN","SF_TRN") 
Abra ("6",r_siga,"SF_CODOP","SF_CODOP","SF_CODOP","CODOP") 
IF ! ABRE_PRT("12",.T.) 
   DbCloseAll() 
   RETURN 
ENDI 
while .T. .AND. lastkey()<>27 
   Set( 20, "SCREEN" ) 
   zCodFat=0 
   zbIcmSubst=0
   zIcmSubst=0
   zIcmRepas=0
   zCodDsc=0
   zbIcm=0
   zvIcms=0
   *-
      M_subtot = 0
      M_Xunids  = 0 
      M_xprods  = 0      
   *-
   DbSelectArea(1)
   dbUseArea( .F.,, (r_siga-"sf_fatnf"), "sf_fatnf", NIL, .F. )
   m_fgdev=sf_fatnf->fgdev
   if file(r_siga-"ntx\sf_fatnf"-ordbagext())
      ordListClear(); ordListAdd( (r_siga-"ntx\sf_fatnf") )
   else
      dbCreateIndex( (r_siga-"ntx\sf_fatnf"), "fnum", _CreateBlock("fnum"), NIL )
   endif      

   *- if type("m_fnum")<>"U" 
   *-   zCodFat = m_fnum
   *-   dbSeek( zCodFat )
   *- else 
      while .T. 
         DevPos( 10, 32 ) ; DevOut( "Codigo da Fatura : " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( zCodFat, "zCodFat", "999999",, ):display() ) 
         ReadModal(GetList) ; GetList := {} 
         if zCodFat=0 
            DbCloseAll() 
            return 
         endif          
         if ! dbSeek( zCodFat ) 
            Mensagem("Nota Fiscal nao Encontrada...Reinforme Numero...",1)
         else 
            M_FOBS    = SF_FATnf->FOBS
            m_fgdev   = sf_fatnf->fgdev         
            M_FOBS    = if( .not.m_fgdev$"DT", "SUBST TRIB CONF CONV 76/94 E DEC 17417/95"+chr(13)+chr(10), "")+m_fobs
            X_cli     = sf_fatnf->fcli
            if m_fgdev$"DT"
               Abra ("14",r_siga,"SCCPFOR","SCCPFOR")
               Sccpfor->(DbGoto(SF_FATnf->fcli))            
               m_ftaxaicms = sccpfor->cp_taxaicms
               Sccpfor->(DbCloseArea()) 
               dbSelectArea( "Sf_FatNf" )
            else
               Sccrcli->( dbGoto(SF_FATnf->FCLI) )
               m_ftaxaicms = if (sccrcli->cr_uf="PB",17,12) 
            endif         
            Z_DESCONTO = IF(FVAL>0, FDESCONTO/(FVAL+FDESCONTO), 0)
            dbSelectArea( "Se_MovNf" ) 
            dbSeek( zCodFat ) 
            While ! Eof() .AND. zCodFat=Mfnum
               If (MFGES="S".OR.MFGES="M") .AND. x_cli=mcodforc 
                  se_prod->( dbSeek( SE_MOVnf->PCOD ) )
                  M_SUBTOT  = M_SUBTOT + se_movnf->mvalor 
                  M_XUNIDS   = M_XUNIDS + MQUANT
                  m_xprods   = m_xprods + 1
                  IF .not.("I"$se_prod->pcategoria.or."F"$se_prod->pcategoria)
                     zbIcm = zbIcm + se_movnf->mvalor*(1-Z_DESCONTO)
                     zbIcmSubst  = zbIcmSubst  + se_movnf->mvalor*(1-Z_DESCONTO)
                  Endif
               ENDIF
               dbSkip(1)
            ENDDO
            dbSelectArea( "SF_FATnf" ) 
            zvIcms = zbIcm*m_ftaxaicms/100
            zIcmSubst = zbIcmSubst*m_ftaxaicms/100-zvIcms
            if m_fgdev="D"
               DevPos( 11, 32 ) ; DevOut( "Desconto Recebido: " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( zCodDsc, "zCodDsc", "999999.99",, ):display() )
               DevPos( 12, 32 ) ; DevOut( "Repasse de ICMs  : " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( zIcmRepas, "zIcmRepas", "999999.99",, ):display() )
            endif         
            DevPos( 13, 32 ) ; DevOut( "Base do ICMS.....: " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( zbIcm, "zbIcm", "999999.99",, ):display() )
            DevPos( 14, 32 ) ; DevOut( "Valor do ICMS....: " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( zvIcms, "zvIcms", "999999.99",, ):display() )
            DevPos( 15, 32 ) ; DevOut( "Base ICMS Substi.: " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( zbIcmSubst, "zbIcmSubst", "999999.99",, ):display() )
            DevPos( 16, 32 ) ; DevOut( "Valor ICMS Subst.: " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( zIcmSubst, "zIcmSubst", "999999.99",, ):display() )
            ReadModal(GetList) ; GetList := {}            
            editor(@M_FOBS,"Observacao do Cupom",16,05,21,75)
            exit 
         endif 
      enddo 
   *- Endif 
 
   dbSelectArea( "SCCRCLI" ) 
   dbGoto(SF_FATnf->FCLI) 
   dbSelectArea( "SF_CODOP" ) 
   dbSeek( SF_FATnf->FCODOP) 
   dbSelectArea( "SF_TRN" ) 
   if SF_FATnf->FTRANSP>0 
      dbGoto(SF_FATnf->FTRANSP) 
   else 
      dbGoto(1) 
   Endif 
   dbSelectArea( "SF_FATnf" ) 
 
   Set( 20, "PRINTER" )
 
   __SetCentury( "on" ) 
   DO NOTAN 
   Set( 20, "SCREEN" ) 
   __SetCentury( "off" ) 
   Mensagem("Nota Fiscal Impressa... Indique Outra ou Enter para Encerrar",1) 
ENDDO 
Fechajan(z_tela)
DbCloseAll() 
RETU 
 
Proc NOTAN 
 
      priva x_prenota, x_cli, x_ini_ll, x_fim_ll, x_fileprom, x_pz,  x_str_descontos, x_mensagem, x_linha_mens, x_pdesc
      x_texto_mens:=""
      X_ini_ll=21
      X_fim_ll=62 - IF(SCCRCLI->CR_PRECOFAB<>"S",2,0)
      DO Cab_frf
      dbSelectArea( "Se_MovNf" ) 
      dbSeek( zCodFat, if( .F., .T., NIL ) ) 
      Ll        = x_ini_ll 
      Pag       = 1 
      X_cli     = sf_fatnf->fcli
      *- alert("teste antes do while" )
      While ! Eof() .AND. zCodFat=Mfnum
         *- alert("teste dentro do while" )
         If (MFGES="S".OR.MFGES="M") .AND. x_cli=mcodforc 
            IF LL > x_fim_ll 
               DO RODAPE_NF With "*" 
               dbSelectArea( "se_movNf" ) 
               LL = x_ini_ll 
            ENDIF         
            Se_Prod->( DbSeek(SE_MOVnf->PCOD) )
            DevPos( ll,   0) ; DevOut( Str(Pcod,5) ) 
            DevPos( Ll,  21) ; DevOut( se_prod->pdesc+" "+if(se_prod->pnegpos$"+-",se_prod->pnegpos,"N") ) 
            DevPos( LL, 100) ; DevOut( se_prod->PCATEGORIA ) 
            DevPos( LL, 105) ; DevOutPict( se_prod->PUNIDADE, "XXX" )
            DevPos( LL, 107) ; DevOutPict( se_movnf->mquant, "@E 9,999.999" ) 
            DevPos( LL, 115) ; DevOutPict( se_movnf->mvalor/se_movnf->mquant, "@E 9999.99" )
            DevPos( LL, 123) ; DevOutPict( se_movnf->mvalor, "@E 999,999.99" ) 
            If .not. ("I"$se_prod->pcategoria.or."F"$se_prod->pcategoria) .or. m_fgdev$"DT"
               DevPos( LL, 135 ) ; DevOutPict( m_FtaxaIcms, "@E 99" ) 
            Endif 
            Ll = Ll + 1
         ENDIF
         dbSkip(1)
      ENDDO
      If prow()<=62
         If SccrCli->Cr_PrecoFab<>"S"
         EndIf
         If (x_linha_mens := mlcount( x_mensagem, 80) ) >0
            x_linha_mens = random() % x_linha_mens + 1
            x_mensagem = memoline( x_mensagem, 80, x_linha_mens)
            x_texto_mens += " -/- "+x_mensagem
         Endif
      endif
      Do Rodape_Nf With " "
      Return

Proc Cab_Frf
      dbSelectArea( "Sf_fatnf" )

      Setprc(0,Pcol())
      Set( 25, 0 )
      DevPos( 0, 0 ) ; DevOut( chr(15)+chr(27)+"0" )
      DevPos( 1,  88 ) ; DevOut( "X" )
      DevPos( 1, 126 ) ; DevOut( strzero(fnum,6) )

      DevPos( 6, 0 ) ; DevOut( alltrim(sf_codop->descop) )
      DevPos( 6,39 ) ; DevOut( fcodop )

      if m_fgdev$"DT"

         Abra ("14",r_siga,"SCCPFOR","SCCPFOR")
         Sccpfor->(DbGoto(SF_FATnf->fcli))
         DevPos( 9, 0 ) ; DevOut( SCCpfor->Cp_NOME )
         DevPos( 9, 93 ) ; DevOut( SCCpfor->cp_CGC )
         DevPos( 9,126 ) ; DevOut( sf_fatnf->fdataemi )
         DevPos( 11, 0 ) ; DevOut( SCCpfor->cp_RUA )
         DevPos( 11, 69 ) ; DevOut( SCCpfor->cp_bairro ) 
         DevPos( 11, 106 ) ; DevOut( SCCpfor->cp_CEP )
         DevPos( 13, 0 ) ; DevOut( SCCpfor->cp_CIDADE ) 
         DevPos( 13, 59 ) ; DevOut( SCCpfor->cp_fone ) 
         DevPos( 13, 83 ) ; DevOut( SCCpfor->cp_uf )
         DevPos( 13, 91 ) ; DevOut( ALLTRIM(SCCpfor->cp_INSC_EST) ) 
         
      else
            DevPos( 9, 0 ) ; DevOut( SCCRCLI->CR_NOME ) 
            DevPos( 9, 93 ) ; DevOut( SCCRCLI->CR_CGC ) 
            DevPos( 9,126 ) ; DevOut( fdataemi ) 
       
            DevPos( 11, 0 ) ; DevOut( SCCRCLI->CR_RUA ) 
            DevPos( 11, 69 ) ; DevOut( SCCRCLI->CR_bairro ) 
            DevPos( 11, 106 ) ; DevOut( SCCRCLI->CR_CEP ) 
            *- DevPos( 11,123 )
            *- DevOut( fdataemi )

            DevPos( 13, 0 ) ; DevOut( SCCRCLI->CR_CIDADE ) 
            DevPos( 13, 56 ) ; DevOut( SCCRCLI->CR_fone ) 
            DevPos( 13, 83 ) ; DevOut( SCCRCLI->CR_uf )
            DevPos( 13, 91 ) ; DevOut( ALLTRIM(SCCRCLI->CR_INSC_EST) ) 
      endif
      Return 
 
PROC RODAPE_NF 
      Para TIPO 
      x_linha78:="" 
 
      dbSelectArea( "sf_fatnf" ) 
      IF TIPO<>"*" 
         dbSelectArea( "SF_FATnf" ) 

         if m_fgdev="D"
           DevPos( 64,  5 ) ; DevOutPict( zbIcm, "@E 99,999.99" )
           DevPos( 64, 30 ) ; DevOutPict( zvIcms, "@E 999,999.99" )
           DevPos( 64, 55 ) ; DevOutPict( zbIcmSubst, "@E 99,999.99" )
           DevPos( 64, 80 ) ; DevOutPict( zIcmSubst, "@E 99,999.99" )
           z_fvaltot = fval-zCodDsc-zIcmRepas+zIcmSubst
           DevPos( 64, 115 ) ; DevOutPict( M_SUBTOT-zCodDsc-zIcmRepas, "@E 9999,999,999.99" )
           DevPos( 66, 115 ) ; DevOutPict( z_fvaltot, "@E 9999,999,999.99" )

         else
           DevPos( 61, 20 ) ; DevOut( "*********** Desconto:" )
           DevPos( 61, 50 ) ; DevOutPict( if(m_fgdev="T",0,FDESCONTO), "@E 999,999.99" )
           DevPos( 61, 65 ) ; DevOut( "**********" )
           DevPos( 64,  5 ) ; DevOutPict( zbIcm, "@E 99,999.99" ) 
           DevPos( 64, 30 ) ; DevOutPict( zbIcm*m_ftaxaicms/100, "@E 999,999.99" )
           z_fvaltot = fval
           DevPos( 64, 115 ) ; DevOutPict( M_SUBTOT, "@E 9999,999,999.99" ) 
           DevPos( 66, 115 ) ; DevOutPict( z_fvaltot, "@E 9999,999,999.99" )                     

         endif
               
      ELSE
         DevPos( 64, 5 ) ; DevOut( "***,***.**" )
         DevPos( 64, 30 ) ; DevOut( "***,***.**" ) 
         DevPos( 64, 115) ; DevOut( "****,***,***.**" ) 

         DevPos( 66, 115) ; DevOut( "****,***,***.**" ) 
      ENDIF 
 
      IF TIPO="*" 
         x_linha78 = "Folha: "+str(pag,2)+".  Continua na Folha "+str(pag+1,2)+"."
         pag = pag+1 
      else 
         if pag>1 
            x_linha78 += "  (Folha: "+str(pag,2)+"/"+str(pag,2)+")" 
         endif 
      ENDIF 

      M_FOBS = ALLTRIM(M_FOBS)
      if zIcmSubst>0.and..not.m_fgdev$"DT"
         DevPos( 76, 0 ) ; DevOut( "Base Prods. c/ Subst. Tributaria: "+Transform(zbIcmSubst,"@E 999,999.99") )
         DevPos( 77, 0 ) ; DevOut( "Ressarcimento de ICMs pago na fonte: "+Transform(zIcmSubst,"@E 999,999.99") )
      endif

      if zIcmRepas>0.and..not.m_fgdev$"DT"
         DevPos( 77+2, 0 ) ; DevOut( "Repasse de ICMs:"+Transform(zIcmRepas,"@E 999,999.99") )
      ENDIF
      DevPos( 77+3, 0 ) ; DevOut( memoline(m_fobs,60,1))
      DevPos( 77+4, 0 ) ; DevOut( memoline(m_fobs,60,2))
      DevPos( 77+5, 0 ) ; DevOut( memoline(m_fobs,60,3))
      if m_fgdev="D"
         DevPos( prow()+1, 0 ) ; DevOut( "Desconto Recebido: "+Transform(zCodDsc,"@E 999,999.99")+spac(10)+"  Repasse de ICMs:"+Transform(zIcmRepas,"@E 999,999.99") )
      endif
      if m_fgdev$"DT"
         DevPos( prow()+1, 0 ) ; DevOut( "* NOTA DE DEVOLUCAO DE MERCADORIAS *")
         DevPos( 90, 30 ) ; DevOut( Sccpfor->cp_nome )
      else
         DevPos( 90, 30 ) ; DevOut( SccrCli->cr_nome )
      endif
      DevPos( 90, 115 ) ; DevOut( Strzero(Fnum,6) )
      DevPos( 96, 15 ) ; DevOut( "" )

      if tipo="*"
         Do cab_frf
      endif
      RETURN
