;** RO_5009R *******************************************************************
;** Imprime a Relacao de Romaneio                          ** MODAMIL**        *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Ultima Alteracao: 14/08/2007 por Antonio Marcelo Alves
;Motivo Alteracao: Ajustes Deleta_Arquivo
;Ultima Alteracao: 18/09/2013 por Andrey Fillipe
;Motivo Alteracao: Inclusao Valor Total no Final do Relatorio  
;Ultima Alteracao: 26/06/2018 por Anivel Augusto Rodrigues                           
;Motivo Alteracao: Ajustes itens mercadoria/servico
;Ultima Alteracao: 07/11/2018 por Anivel Augusto Rodrigues                           
;Motivo Alteracao: Ajustes formata‡Æo peso EIC com 3 casas decimais

;******************************************************************************

Proc Cabecalho()
  If NForm=5
     Then Print File ARQIMP ASP1+"\f"+ASP2+"\n"
          NForm=1
  EndIf

  NPag=NPag+1

  Print File ARQIMP ASP1+CI_12CPP+CI_6LPP
  Print File ARQIMP Format("W42,AL",[FaturaC->Razao Social])
  Print File ARQIMP Format("W23,AL",[FaturaC->Telefone])
  Print File ARQIMP "Pedido: "+Format("W12,AL",[FaturaC->Numero da OF])+"Pag: "+StrVal(NPag)+"/"+StrVal(TPag)+ASP2+"\n"

  Print File ARQIMP ASP1+Format("W65,AL",[FaturaC->Endereco])
  Print File ARQIMP "Volume: "+Format("W03,AL",[FaturaC->Numero de Volumes])
  Print File ARQIMP "  "+Format("W15,AL",[FaturaC->Especie de Volumes])+ASP2+"\n"

  Print File ARQIMP ASP1+Format("W25,AL",[FaturaC->Bairro])
  Print File ARQIMP Format("W13,AL",[FaturaC->CEP])
  Print File ARQIMP Format("W29,AL",[FaturaC->Cidade]+"/"+[FaturaC->UF])
  Print File ARQIMP "Peso:"+Format("W9.3,EIC",[FaturaC->Peso Bruto])+" Kg"+ASP2+"\n"

  Print File ARQIMP ASP1+"------------------------------------------------------------------------------------------------"+ASP2+"\n"
  NForm=NForm+1
EndProc
                                          
Proc Detalhe()
Moveto "FaturaC"

NPag=0
TPag=Int(wc/06)
If ((wc/06)-TPag)>0 Then TPag=TPag+1 EndIf

Cabecalho()
Imprimir_Rodape=True
j=0
For c From 1 to wc
  Print File ARQIMP ASP1+Format("W9.2,EIC",Qtde[c])
  Print File ARQIMP Format("W4,AC",Unid[c])
  Print File ARQIMP Format("W11,Al",Item[c])
  Print File ARQIMP Format("W49,AL",Desc[c])
  Print File ARQIMP Format("W13.2,EIC",Ptot[c])+ASP2+"\n"
;  If Mod(c,2)=0 Or Qtde[c+1]=BlankNum()
;     Then Print File ARQIMP "\n"
          j=j+1
;  EndIf
  If Mod(c,06)=0 And wc>c
     Then Imprimir_Rodape=False
          Rodape()
          Cabecalho()
          Imprimir_Rodape=True
  EndIf
EndFor
RodaPe()  
 
EndProc

Proc Rodape()
  For r From j To 5
    Print File ARQIMP ASP1+ASP2+"\n"
  EndFor

  Print File ARQIMP ASP1+"------------------------------------------------------------------------------------------------"+ASP2+"\n"
  If Imprimir_Rodape
     Then
          Print File ARQIMP ASP1+"VrMercadoria=> $"+RTrim(Format("W10.2,EIC",[FaturaC->Valor Mercadoria]))
          Print File ARQIMP "  Descto"+Format("W06.2,EIC",[Taxa de Desconto])+"%.."
          Print File ARQIMP Format("W8.2,EIC",[Valor Desconto])
          Print File ARQIMP "Total=> $"+RTrim(Format("W10.2,EIC",[FaturaC->Valor Nota Fiscal]))
          Print File ARQIMP "          Romaneio: "+[FaturaC->Numero Nota Gerada]+ASP2+"\n"
          If IsBlank([FaturaC->Composicao de Dias])
             Then Print File ARQIMP ASP1+"Cond.Pagto: "+[FaturaC->Forma de Pagamento]+"  "+Format("W19,AL",[FaturaC->Descricao CondPagto])+ASP2+"\n"
             Else Print File ARQIMP ASP1+"Cond.Pagto: "+[FaturaC->Forma de Pagamento]+"  "+Format("W19,AL",[FaturaC->Composicao de Dias ])+ASP2+"\n"
          Endif
          Print File ARQIMP ASP1+"Dia: "+LINHA1+ASP2+"\n"
          Print File ARQIMP ASP1+"     "+LINHA2+ASP2+"\n"
          Print File ARQIMP ASP1+"Obs: "+SubStr([FaturaC->Observacao na Nota Fiscal],01,90)+ASP2+"\n"
        ;;Print File ARQIMP ASP1+""+SubStr([FaturaC->Observacao na Nota Fiscal],91,90)+ASP2+"\n"
     Else Print File ARQIMP ASP1+ASP2+"\n"
          Print File ARQIMP ASP1+Spaces(80)+"* * * Continua=>"+ASP2+"\n"
  EndIf
  Print File ARQIMP ASP1+"================================================================================================"                   
  Print File ARQIMP CI_10CPP+CI_6LPP+ASP2+"\n"

  j=0
EndProc

Proc Limpa_Array()
  For i From 1 to 99
    Item[i]=""
    Qtde[i]=BlankNum()
    Unid[i]=""
    Desc[i]=""
    Ptot[i]=BlankNum()
  EndFor
  wc=0
EndProc

Proc imprimir()

Array dtvencto[12]
Array valorven[12]

Array Item[99]
Array Qtde[99]
Array Unid[99]
Array Desc[99]
Array Ptot[99]

View "FaturaV"
View "FaturaI"
View "FaturaC"
View "RCargaF"

TotPeso=0
TotVolume=0
TotGeral=0

Scan

  Moveto "FaturaC"
  Moveto [Numero da OF]
  Locate [RCargaF->Numero da OF]
  If Not RetVal
     Then Moveto "RCargaF"
          Loop
  EndIf           

  TotPeso=TotPeso+[Peso Bruto]
  TotVolume=TotVolume+[Numero de Volumes]
  TotGeral=TotGeral+[Valor Nota Fiscal]
  
  
  
  For I From 1 To 12
      dtvencto[i]=BlankDate()
      valorven[i]=BlankNum()
  EndFor 

  LINHA1=""
  LINHA2=""
  Moveto "FaturaV"
  Moveto [Numero da OF]
  Locate [FaturaC->Numero da OF]
  If RetVal
     Then i=0
          While [Numero da OF] = [FaturaC->Numero da OF]
            i=i+1
            dtvencto[i] = [Data ]
            valorven[i] = [Valor]
            LINHA1=LINHA1+Format("W11,AR",Format("D13",dtvencto[i]))
            LINHA2=LINHA2+Format("W11.2,EIC",valorven[i])
            Skip
            If EOT()
               Then QuitLoop
            Endif
          EndWhile
  Endif 

  Limpa_Array()
  Moveto "FaturaI"
  Moveto [Numero da OF]
  Locate [FaturaC->Numero da OF]
  While [Numero da OF]=[FaturaC->Numero da OF]
    wc=wc+1
    Item[wc]=[Codigo do Item   ]
    Qtde[wc]=[Quantidade       ]
    Unid[wc]=[Unidade Fiscal   ]
    Desc[wc]=[Descricao do Item]
    Ptot[wc]=[Preco Total      ]
    
    Skip
    If EOT() Then QuitLoop EndIf
  EndWhile
  Detalhe()
 
  Moveto "RCargaF"
EndScan
ClearAll

Print File ARQIMP ASP1+"------------------------------------------------------------------------------------------------"+ASP2+"\n"
Print File ARQIMP ASP1+"                                              Total Peso=>"+Format("W14,EIC",TotPeso)+" Kg"+ASP2+"\n"
Print File ARQIMP ASP1+"                                            Total Volume=>"+Format("W14,EIC",TotVolume)+ASP2+"\n"
Print File ARQIMP ASP1+"                                             Total Geral=>"+RTrim(Format("W14,EIC",TotGeral))+ASP2+"\n"
Print File ARQIMP ASP1+"------------------------------------------------------------------------------------------------"+ASP2+"\n"

EndProc

;**********************************************************************************************************************
;** Rotina Principal                                                                                                  *
;**********************************************************************************************************************
ASP1=""
ASP2=""
ARQIMP = PrivDir()+"RELROMAN.TXT"
Switch
  Case OK="A": ARQIMP = DIREPORT+ARQUIVO+".TXT"
  Case OK="V": ASP1="\""
               ASP2="\""
EndSwitch
If IsFile(ARQIMP) 
   Then Deleta_Arquivo(ARQIMP)
EndIf
If IsTable("Answer") Then Delete "Answer" EndIf

AGUARDE("Imprimindo rela‡Ço de Romaneios")
NForm=1
Imprimir()

If OK="V"
   Then {Tools} {Copy} {Table} {\\Base\\Sistemas\\Estoque\\Rm_tx160} {Answer} ClearAll
        {Tools} {ExportImport} {Import} {ASCII} {AppendDelimited} TypeIn ARQIMP Enter
        {Answer} ClearAll
        OPCAOPAG="Todas"
        IMPRIREL("Answer","2")
        Deleta_Arquivo(ARQIMP)
   Else Mensagem("Impress„o Concluida !")
        If OK="I"
           Then ;---- Imprime Arquivo Impressao ------------------------------------------------
                ImpriArq(ARQIMP)
        EndIf
EndIf


