;** RO_0380 ********************************************************************
;** Imprime a Digitacao do Romaneio                                            *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 29/09/2018 por Bruno Carvalho             
;Ultima Alteracao: 20/11/2018 por Bruno Carvalho                   
;Motivo Alteracao: Ajustes na Observa‡Æo da Nota Fiscal

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

;************************** IMPRESSAO DO CABECALHO *****************************
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf
Moveto "Faturac"
Print File ARQIMP chr(27)+chr(67)+chr(33) ;Imprime 33 Linhas por pagina
Print File ARQIMP CI_10CPP
Print File ARQIMP CI_6LPP

;Print File ARQIMP "\n"
Print File ARQIMP Format("W50,AL","")+"Impressao: "+Format("D10",Today())+"-"+Time()+ "\n\n"
;Print File ARQIMP "\027E"
;Print File ARQIMP Spaces(01)+ "ENTREGA : "+Format("W35,AL",[Endereco de entrega])+"Bairro :" +Format("W15,AL",[Bairro de Entrega])+"\n"

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

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

Print File ARQIMP CI_8LPP
Print File ARQIMP "\027P"         ;Imprime 10 Caracteres por polegada

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

Print File ARQIMP 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(10)+Format("W20,AL",[Cidade])
Print File ARQIMP Format("W20,AL",[UF])
Print File ARQIMP Format("W25,AL",[Telefone])+"\n"

Print File ARQIMP "Operacao: "
Print File ARQIMP Chr(27)+Chr(71)
Print File ARQIMP ""+[Codigo da Destinacao]+" "+[Descricao da Destinacao]+"\n"
Print File ARQIMP Chr(27)+Chr(72)

Print File ARQIMP "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            "-----------------------------------------------------------------------------------------------------------------------"+"\n"
Print File ARQIMP Spaces(02)+"Item                                                                            Un  Quantidade  Unitario        Total"+"\n"
Print File ARQIMP            "-----------------------------------------------------------------------------------------------------------------------"+"\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]


  GetRecord "\\Sistemas\\Estoque\\Itens" [Codigo do Item] To REG_ITEM
  If RetVal
     Then REFEREMCIA = Rtrim(REG_ITEM["Referencia"])
     Else REFEREMCIA = [Codigo do Item]
  EndIf
  If REFEREMCIA="" Then REFEREMCIA=[Codigo do Item] EndIf
  
  Print File ARQIMP Spaces(02)+Format("W04,AL",[Item da OF])
  Print File ARQIMP Spaces(01)+Format("W11,AL",REFEREMCIA)
  Print File ARQIMP Spaces(01)+Format("W62,AL",[Descricao do 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])
  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 "-----------------------------------------------------------------------------------------------------------------------"+"\n"

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

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

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

;Print File ARQIMP Spaces(89)+"DespAces..........."+Format("W11.2,EIC",[Valor Despesas Acessorias])+"\n"
Print File ARQIMP Spaces(89)+"Total Liquido......"+Format("W11.2,EIC",[Valor Nota Fiscal])+"\n\n"
                                   
Print File ARQIMP "Cond. Pagto: "+[Descricao CondPagto]
Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
Print File ARQIMP "   CD: "+[Composicao de Dias]+"\n"
                              
If [Numero de Volumes]<>""
   Then Print File ARQIMP Spaces(2)+"    Volumes: "+Format("W13,AL",[Numero de Volumes]) 
EndIf   

If [Peso Bruto]<>""
   Then Print File ARQIMP " Peso: "+Format("W13.3,EIC,AL",[Peso Bruto])
EndIf
Print File ARQIMP "\n"                              

OBS = [Observacao na Nota Fiscal]

LINHA1 = "\n" 
LINHA2 = "\n"
LINHA3 = "\n" 

LARGURA  = 119        
TM_OBS   = "113"        
        
LABEL_OBS    = "OBS.: "        
TM_LABEL_OBS = Len(LABEL_OBS)       
        
OBS_NOTA1 = LABEL_OBS+Ltrim(Rtrim(SubStr(OBS,001,NumVal(TM_OBS))))
OBS_NOTA2 = Ltrim(Rtrim(SubStr(OBS,NumVal(TM_OBS)+1,NumVal(TM_OBS))))
OBS_NOTA3 = Ltrim(Rtrim(SubStr(OBS,NumVal(TM_OBS)*2+1,NumVal(TM_OBS))))

CONT_OBS = 0
DynArray OBS[] 
For i From 1 To 3
  Switch
    Case i = 1 : AUX_OBS = OBS_NOTA1
    Case i = 2 : AUX_OBS = OBS_NOTA2 
    Case i = 3 : AUX_OBS = OBS_NOTA3 
  EndSwitch
  If AUX_OBS="" Then Loop EndIf   
  While True
    posI = Search("\n",AUX_OBS)
    If posI>0
       Then CONT_OBS = CONT_OBS + 1
            OBS[CONT_OBS] = AlinhM(StrVal(CONT_OBS),3)+Format("W"+StrVal(TM_OBS),SubStr(AUX_OBS,1,posI-1))
            AUX_OBS = SubStr(AUX_OBS,posI+1,Len(AUX_OBS))
       Else CONT_OBS = CONT_OBS + 1 
            OBS[CONT_OBS] = AlinhM(StrVal(CONT_OBS),3)+Format("W"+StrVal(TM_OBS),AUX_OBS)
            QuitLoop
    EndIf
  EndWhile
EndFor   

If CONT_OBS > 0
   Then For i From 1 To 3
          Switch                                     
            Case i = 1 : AUX_LINHA = LINHA1
            Case i = 2 : AUX_LINHA = LINHA2
            Case i = 3 : AUX_LINHA = LINHA3
          EndSwitch
          If IsAssigned(OBS[i])
             Then AUX_OBS = Format("W"+StrVal(TM_OBS),SubStr(OBS[i],4,Len(OBS[i])))
             Else AUX_OBS = Spaces(NumVal(TM_OBS))
          EndIf          
          If AUX_OBS<>""                                  
             Then If i = 2 or i = 3 Then AUX_OBS = Spaces(TM_LABEL_OBS)+AUX_OBS EndIf
                  Print File ARQIMP AUX_OBS + Spaces(LARGURA-NumVal(TM_OBS)-2) + AUX_LINHA  
             Else Print File ARQIMP Spaces(LARGURA) + AUX_LINHA 
          EndIf
        EndFor
   Else Print File ARQIMP Spaces(LARGURA)+LINHA1
        Print File ARQIMP Spaces(LARGURA)+LINHA2
        Print File ARQIMP Spaces(LARGURA)+LINHA3
EndIf   
Print File ARQIMP "\n"

IMPRIME_VENCTO = False
;********************* IMPRESSAO DOS VENCIMENTOS **************************
If IMPRIME_VENCTO
   Then Print File ARQIMP "\018"
        Print File ARQIMP "\027E"
        Print File ARQIMP "Vencimentos: "
        
        Array DATA [5]
        Array VALOR[5]
        Array FPGT [5]
        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=5
             Then VENC()
                  K=0
          EndIf
          Skip
          If EOT() Then QuitLoop EndIf
        EndWhile
        If K>0
           Then VENC()
        EndIf
        ;Print File ARQIMP "\n"
        Print File ARQIMP "\027F"
EndIf

;************************* IMPRESSAO DO RODAPE *********************************
Print File ARQIMP Spaces(12)+"   /   /     -                                   " + "\n"
Print File ARQIMP Spaces(12)+"-------------------------------------------------" + "\n"
Print File ARQIMP Spaces(12)+"              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(13)
EndIf

For I From 1 to K
  Print File ARQIMP FPGT[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(13)
For I From 1 to K
  Print File ARQIMP DATA[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(13)
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_0380.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()


