;** RO_0397 ********************************************************************
;** Imprime a Digitacao do Romaneio                             ** PADRAO **   *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 05/09/2017 por Bruno Carvalho           
;Ultima Altera‡Æo: 11/09/2017 por Bruno Carvalho 
;Motivo Altera‡Æo: Ajustes
;Ultima Altera‡Æo: 28/11/2017 por Bruno Carvalho 
;Motivo Altera‡Æo: Adicionado Data de EmissÆo
;Ultima Altera‡Æo: 28/11/2017 por Bruno Carvalho 
;Motivo Altera‡Æo: Ajustes na Observa‡Æo
;Ultima Altera‡Æo: 10/08/2018 por Nikolas
;Motivo Altera‡Æo: Valor unitario do item com desconto, saindo o liquido  

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc IMPFAT()

;************************** IMPRESSAO DO CABECALHO *****************************
If Not IsAssigned(NOMEMPRESA) Then NOMEMPRESA="" EndIf

AREA_DESCRICAO = ""
GetRecord "\\Sistemas\\Cadastro\\Destinat" [Codigo] To REG_DESTINAT
If RetVal
   Then AREA_DESCRICAO = REG_DESTINAT["Descricao da Area"]
EnDIf   

Moveto "Faturac"
Print File ARQIMP chr(27)+chr(67)+chr(33) 
Print File ARQIMP CI_10CPP
Print File ARQIMP CI_6LPP

Print File ARQIMP Spaces(2)+Format("W47,AL","CONTROLE INTERNO - Balcao 3")+"Impressao: "+Format("D10",Today())+"-"+Time()+ "\n\n"

Print File ARQIMP "\014"
Print File ARQIMP "\027E"
Print File ARQIMP Spaces(18)+"Orcamento N§ "+Format("W06.3,AL",NOTAFISC)+"\n"
Print File ARQIMP "\020"

Print File ARQIMP Spaces(2)+"Cliente : "+Format("W05,AL",[Codigo])+Format("W34,AL",[Razao Social])+
                  Spaces(1)+Format("D13",[Data de Emissao])+"\n"
Print File ARQIMP "\027F"

Print File ARQIMP CI_8LPP
Print File ARQIMP "\027P"     

Print File ARQIMP Spaces(2)+Spaces(10)+Format("W39,AL",[Endereco])+Spaces(1)
Print File ARQIMP Format("W18,AL",[CGC])+"\n"

Print File ARQIMP Spaces(2)+Spaces(10)+Format("W20,AL",[Bairro])
Print File ARQIMP Format("W20,AL",[CEP])
Print File ARQIMP Format("W25,AL",[IE])+"\n"

Print File ARQIMP Spaces(2)+Spaces(10)+Format("W20,AL",[Cidade])
Print File ARQIMP Format("W20,AL",[UF])
Print File ARQIMP Format("W25,AL",[Telefone])+"\n"

Print File ARQIMP Spaces(2)+"Operacao: "              
Print File ARQIMP Chr(27)+Chr(71)
Print File ARQIMP Format("W38",[Codigo da Destinacao]+" "+[Descricao da Destinacao])
Print File ARQIMP Chr(27)+Chr(72)
Print File ARQIMP Spaces(2)+"SEMANA: "+Format("W9",AREA_DESCRICAO)+"\n"

Print File ARQIMP Spaces(2)+"Vendedor: "+Format("W05,AL",[Codigo do Representante])
Print File ARQIMP Format("W25,AL",[Nome Fantasia Represent])+"\n"
Print File ARQIMP "\015"         ;Imprime 17 Caracteres por polegada

;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP Spaces(2)+"---------------------------------------------------------------------------------------------------------------------"+"\n"
Print File ARQIMP Spaces(4)+"Item                                                                          Un  Quantidade  Unitario        Total"+"\n"
Print File ARQIMP Spaces(2)+"---------------------------------------------------------------------------------------------------------------------"+"\n"
Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
QUANT    = 0
TOT      = 0
CONTADOR = 0

While [Numero da OF]=[FaturaC->Numero da OF]
  DESC_ITEM = ""
  GetRecord "\\Sistemas\\Estoque\\Itens" [Codigo do Item] To REG_ITEM
  If Retval
     Then DESC_ITEM = REG_ITEM["Descricao Complementar"]
  EndIf
  If Rtrim(DESC_ITEM) = "" Then DESC_ITEM = [Descricao do Item] EndIf     
  
  Print File ARQIMP Spaces(2)+Format("W4,AR",[Item da OF])
  Print File ARQIMP Spaces(01)+Format("W11,AL",[Codigo do Item])
  Print File ARQIMP Spaces(01)+Format("W62,AL",DESC_ITEM)
  Print File ARQIMP Spaces(01)+Format("W03,Al",[Unidade Fiscal])

  If [Quantidade]-Int([Quantidade])>0
     Then Print File ARQIMP Format("W011.3,EIC",[Quantidade])
     Else Print File ARQIMP Format("W011,EIC",[Quantidade])
  EndIf

  Print File ARQIMP Format("W10.2,EIC",[Preco Unitario]-[Valor Desconto Unitario])
  Print File ARQIMP Format("W13.2,EIC",[Preco Total])+"\n"
  TOT   = TOT + [Preco Total]
  Skip
  If EOT() Then QuitLoop EndIf
  CONTADOR = CONTADOR + 1
EndWhile

For ITEM From CONTADOR To 12
    Print File ARQIMP "\n"
EndFor
Print File ARQIMP Spaces(2)+"---------------------------------------------------------------------------------------------------------------------"+"\n"

;********************* IMPRESSAO DOS TOTAIS **************************
Moveto "FaturaC"

Print File ARQIMP Spaces(2)+Spaces(87)+"Total.............."+Format("W11.2,EIC",TOT)+"\n"

Print File ARQIMP Spaces(2)+"          *** FAVOR MANTER O ENDERECO E O TELEFONE ATUALIZADOS ***"
Print File ARQIMP Spaces(21)+"Desconto"+Format("W06.2,EIC",[Taxa de Desconto]) + "%..."
Print File ARQIMP Format("W12.2,EIC",[Valor Desconto])+"\n"

Print File ARQIMP Spaces(2)+Spaces(87)+"DespAces..........."+Format("W11.2,EIC",[Valor Despesas Acessorias])+"\n"
Print File ARQIMP Spaces(2)+Spaces(87)+"Total Liquido......"+Format("W11.2,EIC",([Valor Mercadoria Liquido]+[Valor Despesas Acessorias]))+"\n\n"

Print File ARQIMP Spaces(4)+"Cond. Pagto: "+[Descricao CondPagto]
Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
Print File ARQIMP "   CD: "+[Composicao de Dias]
Print File ARQIMP "  VOL: "+StrVal([Numero de Volumes])+"\n"
     
OBS_1=""                            
OBS_2=""
OBS_3=""
OBS_4=""
OBS_5=""

CONT_OBS = 0
OBS = [Observacao na Nota Fiscal]
PosI = Search("\n",OBS)
If PosI > 0
   Then While PosI > 0
          CONT_OBS = CONT_OBS + 1
          Switch
            Case CONT_OBS=1 : OBS_1 = Substr(OBS,1,PosI-1)
            Case CONT_OBS=2 : OBS_2 = Substr(OBS,1,PosI-1)
            Case CONT_OBS=3 : OBS_3 = Substr(OBS,1,PosI-1)
            Case CONT_OBS=4 : OBS_4 = Substr(OBS,1,PosI-1)
            OtherWise       : QuitLoop
          EndSwitch
          OBS   = Substr(OBS,PosI+1,Len(OBS))
          PosI  = Search("\n",OBS)
          If PosI=0
             Then OBS_5 = OBS
                  QuitLoop
          EndIf    
        EndWhile
   Else OBS_1 = OBS
EndIf     

IMPRESSO_LABEL_OBS = False 
NUM_CHAR = 109
For ii From 1 To 5
  Switch
    Case ii = 1 : OBS = OBS_1
    Case ii = 2 : OBS = OBS_2
    Case ii = 3 : OBS = OBS_3
    Case ii = 4 : OBS = OBS_4
    Case ii = 5 : OBS = OBS_5
  EndSwitch
  OBS = Ltrim(Rtrim(OBS))
  If OBS="" Then Loop EndIf
  While OBS<>""
    If Rtrim(SubStr(OBS,1,NUM_CHAR))<>""
       Then Print File ARQIMP Spaces(04)
            If Not IMPRESSO_LABEL_OBS
               Then IMPRESSO_LABEL_OBS = True
                    Print File ARQIMP "\027E"+"OBS.: "+"\027F"
               Else Print File ARQIMP Spaces(6)      
           EndIf
           Print File ARQIMP "\027E"+Ltrim(Rtrim(SubStr(OBS,1,NUM_CHAR))) + "\027F" + "\n"
    EndIf
    OBS = SubStr(OBS,NUM_CHAR+1,Len(OBS))
    If OBS="" Then QuitLoop EndIf
  EndWhile 
Endfor   
Print File ARQIMP "\n"

;********************* IMPRESSAO DOS VENCIMENTOS **************************
Print File ARQIMP "\020"
Print File ARQIMP "\027E"
Print File ARQIMP Spaces(4)+"Vencimentos: "

Array DATA [9]
Array VALOR[9]
Array FPGT [9]
K=0
KV=0
Moveto "FaturaV"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
While [Numero da OF]=[FaturaC->Numero da OF]
  K=K+1
  DATA[K]  = Format("D13",[Data])
  VALOR[K] = Format("W10.2,AR,EIC",[Valor])
  FPGT[K]  = Format("W10","FP: "+[Forma de Pagamento])
  If K=9
     Then VENC()
          K=0
  EndIf
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile
If K>0
   Then VENC()
EndIf
Print File ARQIMP "\027F"

;************************* IMPRESSAO DO RODAPE *********************************
Print File ARQIMP "\n\n"
Print File ARQIMP "\018"
Print File ARQIMP Format("W68,AC","   /   /     -                                   ")+ "\n"
Print File ARQIMP Format("W68,AC","-------------------------------------------------")+ "\n"
Print File ARQIMP Format("W68,AC","Data e Assinatura")+ "\n"
Print File ARQIMP CI_6LPP
Print File ARQIMP CI_10CPP
Print File ARQIMP "\f"
EndProc

;*******************************************************************************
;* Rotina de Impressao dos Vencimentos                                         *
;*******************************************************************************
Proc VENC()
If KV>0
   Then Print File ARQIMP Spaces(17)
EndIf
Print File ARQIMP Spaces(4)
For I From 1 to K
  Print File ARQIMP FPGT[I]+Spaces(2)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(17)
For I From 1 to K
  Print File ARQIMP DATA[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(17)
For I From 1 to K
  Print File ARQIMP VALOR[I]+Spaces(02)
EndFor
Print File ARQIMP "\n\n"

KV=KV+1
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************

ArqImp=PrivDir()+"RO_0397.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)
IMPFAT()

;---- Imprime Arquivo Impressao ------------------------------------------------
ImpriArq(ARQIMP)

Release Vars VALOR,DATA
Release Procs IMPFAT,VENC
LIMPAGUARDE()
