ALIAS SF_NFS
// a Variavel z_Recno guarda a posicao do registro do arquivo mestre do relatorio
VARIAVEL znil := SF_NFS->(DbGoto(z_Recno))
VARIAVEL zFaturaXLS := 'fatura_modelo.xls'
VARIAVEL cAaMm := ''
VARIAVEL zfNumS := 0
VARIAVEL zCrNumDupS := ''
VARIAVEL zCrDtVencS := ''
VARIAVEL zCrFileName := ''
VARIAVEL zfTxDesconto := 0
VARIAVEL zDadosDaObra := ""
VARIAVEL zkk := 0
VARIAVEL zbl := {|cColLine| left(cColLine,1) + lTrim(Str( Val(SubStr(cColLine,2))+zkk,4)) }
VARIAVEL oFileComSheets_Modelo := ''
VARIAVEL oSheet_Modelo := ''

VARIAVEL #TEXTO_FONTE
   Private zoProgressBar, zCmdsDurante, zCmdsDepois, zl:=0, zMaxLines:=33, zDifLines:=0, zPages:=1, zPagesImpressas:=0, zAditivo, zStatus, vOpcoes, wOpt:=1, oClasse, zaLines:={}, zCodCli

   vOpcoes := { {'&1. So a Selecionada ',1},{'&2. Todas as Faturas ',2},{'&3. Seleciona Periodo',3} }
   aAdd( zaLines, {"1;2;Faturas a Listar:","@wOpt", @wOpt,  "9", "", "RADIOGROUP vOpcoes" } )

   if .not. QuickDialog("cr_recfx_exc", "41;5;Impressao de Faturas", zaLines)
      Return .f.
   endif
   
   if wOpt<>1
      DbGoTop()
   endif

   //sfgdebugalert:=.t.

   debugalert('vai ativar excel...')
   oClasse := tCvXls():New()
   debugalert('vai exibir excel...')
   oClasse:oXls:Visible := .t.
   debugalert('exibiu excel...')

   do while .not. eof()

      cAaMm := SubStr( Dtos(sf_nfs->fDataEmi), 3, 4)
      zfNumS := sf_nfs->fNumS
      zCrNumDupS := StrZero(zfNumS,10)
      zCrDtVencS := hb_dtoc(sf_nfs->cr_dt_venc,"dd/mm/yyyy")
   
      zCodCli := sf_nfs->fCli 
      SccrCli->( DbGoto(zCodCli) )
      // zCrFileName := Lower(RemoverAcentos( Left( 'nfs'+cAaMm+'_'+lTrim(Str(zfNumS,6)) +'_'+ SccrCli->( StrTran( AllTrim( if(empty(cr_fantasi),cr_nome,cr_fantasi) ) , " ","_") ) , 30) ) )
      zCrFileName := Lower(RemoverAcentos( Left( lTrim(Str(zfNumS,6)) +'_'+ 'nfs'+cAaMm+'_'+SccrCli->( StrTran( AllTrim( if(empty(cr_fantasi),cr_nome,cr_fantasi) ) , " ","_") ) , 30) ) )
      zCrFileName := StrTran(StrTran(zCrFileName,'*'),'@') 
      // zCrFileName := s_pathdb+'rel\boletos\'+StrTran(StrTran(zCrFileName,'*'),'@') 
      debugalert('CLI: '+STR(zCodCli))

      DbSelectArea("sf_nfsmv")
      DbSeek(zfNumS)
      do while .not. eof() .and. fnums=zfNumS
         ++zl
         DbSkip()
      enddo
      zDifLines:=zl-zMaxLines
      zMaxLines:=zl
      
      zPages := int(zl/zMaxLines) + if( zl%zMaxLines>0, 1, 0)
      zFaturaXLS := if( zPages>1, 'fatura_modelo_'+lTrim(Str(zPages,2))+'pg.xls', 'fatura_modelo.xls')
      
      debugalert('vai usar '+str(zPages)+' paginas')

      if .not. oClasse:AddSheet( zFaturaXLS, zCrFileName )
         Alert("Nao consegui abrir modelo de fatura "+zFaturaXLS,,"R/W")
         Return .f.
      endif

      zMaxLines:=33
      debugalert('-> copiara '+hb_valtoexp(zl-zMaxLines)+' linhas')
      do while zl>zMaxLines
         oClasse:oSheet:Rows("58:59"):Copy()
         oClasse:oSheet:Rows("60:60"):insert()
         zMaxLines:=zMaxLines+2
         debugalert('-> faltam '+hb_valtoexp(zl-zMaxLines)+' linhas')
      enddo
      zMaxLines:=zl+1
      zl := 1

      DbSeek(zfNumS)
      
      zDadosDaObra := vb_pref->( DbGoto(sf_nfs->fObra) , AllTrim(cr_fantasi)+" "+AllTrim(cr_rua)+" "+AllTrim(cr_bairro)+" "+AllTrim(cr_cidade) )

      do while zPagesImpressas<zPages

         oClasse:Cell( eVal(zbl,"n5") , StrZero(zfNumS,6) )
         oClasse:Cell( eVal(zbl,"l9"), hb_dtoc(s_datual,"dd/mm/yyyy") )
      
         oClasse:Cell( eVal(zbl,"c15"), RemoverAcentos(AllTrim(sccrcli->Cr_Nome), 'ANSI') )
         oClasse:Cell( eVal(zbl,"c16"), RemoverAcentos(sccrcli->(AllTrim(Cr_Rua)+" "+cr_NumRua), 'ANSI') )
         oClasse:Cell( eVal(zbl,"m16"), RemoverAcentos(AllTrim(sccrcli->Cr_Bairro), 'ANSI') )
      
         oClasse:Cell( eVal(zbl,"c17"), AllTrim(sccrcli->Cr_email) )
         oClasse:Cell( eVal(zbl,"n17"), AllTrim(sccrcli->Cr_Cep) )
         oClasse:Cell( eVal(zbl,"c18"), AllTrim(sccrcli->Cr_Cidade) )
         oClasse:Cell( eVal(zbl,"h18"), AllTrim(sccrcli->Cr_uf) )
         oClasse:Cell( eVal(zbl,"m18"), AllTrim(sccrcli->Cr_fone) )
      
         oClasse:Cell( eVal(zbl,"c19"), AllTrim(sccrcli->Cr_cgc) )
         oClasse:Cell( eVal(zbl,"k19"), AllTrim(sccrcli->Cr_insc_es) )
      
         // oClasse:Cell( eVal(zbl,"i29"), StrZero(zCodCli,6) )
         // oClasse:Cell( eVal(zbl,"k29"), zCrNumDupS )
         oClasse:Cell( eVal(zbl,"l24"), zCrDtVencS )
   
         DbSelectArea("sf_nfsmv")
         Do While .not. eof() .and. fnums=zfNumS
            oClasse:Cell( eVal(zbl,"b"+lTrim(Str(28+zl,4))),    AllTrim(fAditivo)+lTrim(Str(mfNum,10)) )
            oClasse:Cell( eVal(zbl,"c"+lTrim(Str(28+zl,4))),    lTrim(Str(mQuant,10)) )
            if mfges$"FAD"
               oClasse:Cell( eVal(zbl,"d"+lTrim(Str(28+zl,4))), if(mfges="F",if("Taxa de Rec"$pObs,"Taxa de Recolhimento","Taxa de Entrega"),AllTrim(MemoLine(pDesc,1024,1,,,.t.))) ) 
               oClasse:Cell( eVal(zbl,"i"+lTrim(Str(28+zl,4))),    "" )
               oClasse:Cell( eVal(zbl,"j"+lTrim(Str(28+zl,4))),    "" )
               oClasse:Cell( eVal(zbl,"k"+lTrim(Str(28+zl,4))),    "" )
               oClasse:Cell( eVal(zbl,"m"+lTrim(Str(28+zl,4))),    oClasse:Val(sf_nfsmv->pValLoca) )
            else
               oClasse:Cell( eVal(zbl,"d"+lTrim(Str(28+zl,4))),    AllTrim(Se_prod->( DbSeek(sf_nfsmv->pCod), pDesc)) )
               oClasse:Cell( eVal(zbl,"i"+lTrim(Str(28+zl,4))),    if(mfges="E","DEVOLVIDO",if(mfges="M","RELOCADO",if(mfges="N","Credito","LOCADO")) ) )
               oClasse:Cell( eVal(zbl,"j"+lTrim(Str(28+zl,4))),    AllTrim(pObs) )
               oClasse:Cell( eVal(zbl,"k"+lTrim(Str(28+zl,4))),    lTrim(Str(pDiasLoca,10)) )
            endif
            oClasse:Cell( eVal(zbl,"l"+lTrim(Str(28+zl,4))),    oClasse:Val(sf_nfsmv->pValLoca) )
            skip
            if ++zl > zMaxLines
               exit
            endif
         enddo
         DbSelectArea('sf_nfs')

         // oClasse:Cell("m29", oClasse:Val(zfTxDesconto) )
      
if zDifLines>0
   oClasse:Cell( eVal(zbl,"c"+lTrim(Str(63+zDifLines,4))), zDadosDaObra )
else         
   oClasse:Cell( eVal(zbl,"c63"), zDadosDaObra )
endif

         ++zPagesImpressas
         zkk += 74
         zl:=1
      enddo

      DebugAlert('vai oClasse:oXls:ActiveWorkbook:Save()')
      oClasse:oXls:ActiveWorkbook:Save()

      if wOpt=1
         exit
      endif
      DbSkip()
   
   enddo

   DebugAlert('vai oClasse:oXls:PrintPreview()')
   oClasse:oXls:ActiveWorkbook:PrintPreview()
   
   // DebugAlert('vai oClasse:oXls:Workbooks:Close()')
   // oClasse:oXls:Workbooks:Close()  
   // DebugAlert('Vai Encerrar Excel com oClasse:oXls:Quit()')
   // oClasse:oXls:Quit() 
   // DebugAlert('Encerrou  Excel com oClasse:oXls:Quit()')

   DbSelectArea('sf_nfs')
   debugalert('4')
 
#FIM_TEXTO_FONTE
