local x_aamm_old:= SUBSTR(DTOS(S_DATUAL),3,4), se_mov
x_cat := {}
FG_ALFA = SIMNAO(8,32,"Ordem de Impressao: "," Alfabetica "," Numerica ",1,0)
IF ! ABRE_PRT("12",.T.)
   RETURN
ENDI
while .T.
   Set( 20, "SCREEN" )
   ABRA ("3",r_siga,"SE_PROD","SE_PROD", "SEIP_C",  "PCOD")
   ABRA ("4",r_siga,"SCCPFOR","SCCPFOR")
   COD_PED := 0
   cod_for := 0
   DevPos( 13, 32 ) ; DevOut( "Ano e Mes..:" ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( x_aamm_old, "x_aamm_old",, {|| (sf_fatf := "fatf"+x_aamm_old,.T.)}, ):display() )
   ReadModal(GetList) ; GetList := {}
   ABRA ("1",r_siga,sf_fatf, "SF_FATF", sf_fatf, "PFNUM")

   DevPos( 14, 32 ) ; DevOut( "Fatura.....:" ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( COD_PED, "COD_PED", "9999999999", {||Psq_brow("sf_fatf","psffatf","COD_PED","N","S",14,25,"","")}, ):display() )

   DevPos( 15, 32 ) ; DevOut( "Fornecedor: " ) ; SetPos( Row(), Col()+1 ) ; AAdd( GetList, _GET_( cod_for, "cod_for", "999", {||cod_for=0 .OR. lastkey()=27 .OR.  Psq_brow("sccpfor","psccpfor","cod_for","N","N",16,32,"cp_nome","@s30")}, ):display() )
   ReadModal(GetList) ; GetList := {}
   IF COD_PED = 0
      EXIT
   ENDIF
   se_mov := "move"+substr(dtos(sf_fatf->pfdataemi),3,4)
   Abra ("2",r_siga,se_mov,"SE_MOV",SE_MOV-".F","mfnum")
   Set( 20, "PRINTER" )
   sccpfor->(dbgoto(sF_fatf->PFFOR))

   PAG = 1
   LL = 66
   CAB_RAP2()
   M_SUBTOT := M_VAF := M_VFALTAS := 0
   dbSelectArea( "se_mov" )
   if FG_ALFA
      dbSelectArea( "0" )
      dbUseArea( .F.,, "SI_PFRB",, if(.F. .OR. .T., !.T., NIL), .F. )
      __dbZap()
      dbSelectArea( "sE_mov" )
      dbSeek( COD_PED, if( .F., .T., NIL ) )
      while ! EOF() .AND. COD_PED=mfnum
         if mcodforc = cod_for .AND. MFGES = "E"
            se_prod->(dbseek(se_mov->PCOD))
            dbSelectArea( "SI_PFRB" )
            APPENDBLANK()
            _FIELD->PDESC := SE_PROD->PDESC; _FIELD->PCOD := se_mov->PCOD; _FIELD->pitens := SE_PROD->pitens
            _FIELD->MQUANT := sE_mov->MQUANT; _FIELD->MQATEND := sE_mov->MQUANT
            _FIELD->MVALOR := sE_mov->MVALOR
         endif
         dbSelectArea( "se_mov" )
         dbSkip(1)
      ENDDO
      dbSelectArea( "SI_PFRB" )
      dbCloseArea()
      dbSelectArea( "se_mov" )
      dbUseArea( .F.,, "SI_PFRB", "sE_mov", if(.F. .OR. .F., !.F., NIL), .F. )
      dbCreateIndex( "SI_PFRB", "PDESC", {|| PDESC}, if( .F., .T., NIL ) )
      dbGoTop()
      while ! EOF() 
         prt_detalhe()
         dbSkip(1)
      ENDDO
   else
      dbSeek( COD_PED, if( .F., .T., NIL ) )
      while ! EOF() .AND. COD_PED=mfnum
         if mcodforc = cod_for .AND. MFGES = "E"
            prt_detalhe()
         endif
         dbSkip(1)
      ENDDO
   endif

   IF LL>50
      LL = 66
      CAB_RAP2()
   ENDIF
   dbSelectArea( "sF_fatf" )
   M_PFVALTOT =   PFVAL + PFVALREP + PFVALDES
   x_PFVALTOT = 0

   DevPos( Prow()+2, 0 ) ; DevOut( Repl("-",86) )
   DevPos( Prow()+1, 5 ) ; DevOut( "VALOR CONTABIL....:" +TRANSFORM(PFVAL     , "@e 9999,999,999.99") )
   DevPos( Prow()+1, 5 ) ; DevOut( "ALIQUOTA DO ICMS..:" +TRANSFORM(PFTAXAICMS, "@e 999.99%"        ) )
   DevPos( Prow()+1, 5 ) ; DevOut( "BASE DO ICMS......:" +TRANSFORM(PFBASEICMS, "@e 9999,999,999.99") )
   DevPos( Prow()+1, 5 ) ; DevOut( "VALOR DO ICMS.....:" +TRANSFORM(PFTOTICMS , "@e 9999,999,999.99") )
   DevPos( Prow()+1, 5 ) ; DevOut( "ALIQUOTA DO IPI...:" +TRANSFORM(PFTAXAIPI , "@e 999.99%"        ) )
   DevPos( Prow()+1, 5 ) ; DevOut( "VALOR DO IPI......:" +TRANSFORM(PFTOTIPI  , "@e 9999,999,999.99") )
   DevPos( Prow()+1, 5 ) ; DevOut( "REPASSE DE ICMS...:" +TRANSFORM(PFVALREP  , "@e 9999,999,999.99") )
   DevPos( Prow()+1, 5 ) ; DevOut( "DESCONTO COMERCIAL:" +TRANSFORM(PFVALDES  , "@e 9999,999,999.99") )
   DevPos( Prow()+1, 5 ) ; DevOut( "ICMS RETIDO.......:" +TRANSFORM(PFVALRET  , "@e 9999,999,999.99") )
   DevPos( Prow()+1, 5 ) ; DevOut( "OUTROS ENCARGOS ..:" +TRANSFORM(PFOUTROS  , "@e 9999,999,999.99") )














   DevPos( PROW()+1, 0 ) ; DevOut( REPL("-",86) )
   DevPos( PROW()+1, 40 ) ; DevOut( "Numero de Duplicatas ..........:" + TRANSFORM( PFnumdup  , "@E 999,999") )


   DevPos( prow()+2, 00 ) ; DevOut( padc("Resumo Categorias",30,"-") )
   for i = 1 to len(X_cat)
       DevPos( prow()+1, 00 ) ; DevOut( x_cat[i,1]+" - "+transform(X_cat[i,2],"@E 9,999")+" - "+transform(X_cat[i,3],"@E 999,999.99") )
   next
   dbCloseAll()
ENDDO
RETU

PROC CAB_RAP2
     IF LL>60
        DevPos( 1, 0 ) ; DevOut( &M_DEZCPI + &M_ENFAT + LTRIM(EMPRESA) )
        DevPos( 2, 0 ) ; DevOut( ALLTRIM(SISTEMA) + &M_NAOENFAT + &M_DOZECPI )
        DevPos( 3, 0 ) ; DevOut( "Impressao do Nota Fiscal "+STRZERO(COD_PED,10)+", Emitido em "+dtoc(sf_fatf->PFdataped)+", listado em "+DTOC(D_ATUAL) )
        DevPos( 3, 111 ) ; DevOut( "Pag "+STR(pag,5) )
        DevPos( 5, 0 ) ; DevOut( REPL("-",86) )
        DevPos( 6, 0 ) ; DevOut( "Fornecedor: " + LTRIM(STR(sf_fatf->PFFOR)) + " - " + SCCPFOR->CP_NOME )
        DevPos( 7, 0 ) ; DevOut( "Endereco..: " + SCCPFOR->CP_RUA )
        DevPos( 8, 0 ) ; DevOut( "Municipio.: " + RTRIM(SCCPFOR->CP_CIDADE) + " - " + SCCPFOR->CP_UF + " - " + SCCPFOR->CP_CEP )
        DevPos( 9, 0 ) ; DevOut( "CGC ......: " + SCCPFOR->CP_CGC )
        DevPos( 9, 51 ) ; DevOut( right(spac(25)+"Insc. Est.  :" + ALLTRIM(SCCPFOR->CP_INSC_EST),29) )
        DevPos( 10, 0 ) ; DevOut( REPL("-",86) )

        DevPos( 11, 0 ) ; DevOut( "PRODUTO E APRESENTACAO            Cat   Emb  UN CodLab  Quant   PrUnit     Valor   Cod" )



        DevPos( 12, 0 ) ; DevOut( REPL("-",86) )
        PAG = PAG+1
        LL = 13
     ENDIF
RETURN

proc prt_detalhe
      CAB_RAP2()
      dbSelectArea( "SE_PROD" )
      dbSeek( sE_mov->PCOD, if( .F., .T., NIL ) )
      DevPos( LL, 0 ) ; DevOutPict( PDESC, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" )
      DevPos( LL, 34 ) ; DevOutPict( pcategoria, "XXXX" )
      DevPos( LL, 39 ) ; DevOutPict( pitens, "9999" )
      DevPos( LL, 44 ) ; DevOutPict( PUNIDADE, "XXX" )
      DevPos( ll, 48 ) ; DevOutPict( pcprodforn, "999999" )
      dbSelectArea( "sE_mov" )
      DevPos( LL, 55 ) ; DevOutPict( MQUANT, "999999" )
      IF MQUANT>0
         DevPos( LL, 62 ) ; DevOutPict( MVALOR/MQUANT, "@E 9,999.99" )
      ENDIF
      x_pos := ascan(x_cat,{ |x| x[1] = se_prod->pcategoria } )
      if x_pos > 0
         x_cat[x_pos,2]++
         x_cat[x_pos,3]+=mvalor
      else
         aadd(X_cat,{ sE_prod->pcategoria,1,mvalor } )
      endif
      DevPos( LL, 71 ) ; DevOutPict( MVALOR, "@E 99,999.99" )
      DevPos( LL, 81 ) ; DevOutPict( se_prod->PCOD, "99999" )

      dbSelectArea( "se_mov" )
      LL = LL + 1
      M_SUBTOT  = M_SUBTOT  + MVALOR

      M_VAF     = M_VAF     + IF( MQUANT>0 , MVALOR/MQUANT , 0)*MQUANT
return
