;** RO_0353B *******************************************************************
;** Romaneio - Modelo Jato de Tinta                                           **
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 23/03/2021 por Bruno Carvalho
;Ultima Alteracao:   /  /     por               
;Motivo Alteracao:  

;Sem Valores
     
;*******************************************************************************
;* ImpressÆo do Cabe‡alho                                                      *
;*******************************************************************************
;ImpressÆo em Folha A4, em duas vias!

Proc RM_0353_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_8LPP

;Print File ARQIMP "\n"
Print File ARQIMP Format("W50,AL","    ")+"Impressao Ax: "+Format("D10",Today())+"-"+Time()+ "\n\n"

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

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

Print File ARQIMP CI_10CPP
Print File ARQIMP CI_8LPP

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("W7,AL",[UF])
Print File ARQIMP "Tel: "
Print File ARQIMP Format("W15,AL",[Telefone])

GetRecord "\\Sistemas\\Cadastro\\Destinat" [Codigo] To Cel
If RetVal And Not IsBlank(Cel["Celular"])
   Then Print File ARQIMP Spaces(01)+"Cel: "
        Print File ARQIMP Format("W15,AL",Cel["Celular"])+"\n"
   Else Print File ARQIMP "\n"
EndIf
   
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                "+"\n"
Print File ARQIMP "------------------------------------------------------------------------------------------------------------------------"+"\n"

EndProc ;(RM_0353_Cabecalho)

;*******************************************************************************
;* Checa linha na area de impressao dos itens                                  *
;*******************************************************************************
Proc RM_0353_Checa_Linha(Nl)
private nl

If (CONTADOR+nl)>(CONTA_ITENS)
   Then RM_0353_RodaPe(True)
        Print File ARQIMP "\f"
        RM_0353_Cabecalho()
        CONTADOR=nl
        Moveto "FaturaI"
   Else CONTADOR=CONTADOR+nl
EndIf
EndProc

;*******************************************************************************
;* ImpressÆo dos Itens                                                         *
;*******************************************************************************
Proc RM_0353_Itens()
       
Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]

CONTA_ITENS = 0

While [Numero da OF]=[FaturaC->Numero da OF]
  CONTA_ITENS = CONTA_ITENS+1
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

Switch
 ;Case CONTA_ITENS <= 2 : CONTA_ITENS  = 2
  Case CONTA_ITENS <=14 : CONTA_ITENS  = 10
  Case CONTA_ITENS > 14 : CONTA_ITENS  = 58 
EndSwitch

CONTADOR = 0

Home
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
While [Numero da OF]=[FaturaC->Numero da OF]
  RM_0353_Checa_Linha(1)
  Print File ARQIMP Spaces(02)+Format("W03,AR",StrVal([Item da OF]))
  Print File ARQIMP Spaces(01)+Format("W11,AL",[Codigo do Item])
  Print File ARQIMP Spaces(01)+Format("W30,AL",[Descricao do Item]) ;62
  Print File ARQIMP Spaces(01)+Format("W31,AL",[Informacoes_Adicionais])
  Print File ARQIMP Spaces(01)+Format("W03,Al",[Unidade Fiscal])
  Print File ARQIMP Format("W11.3,EIC",[Quantidade])
  ;Print File ARQIMP Format("W10.2,EIC",[Preco Unitario])
  ;Print File ARQIMP Spaces(02)+Format("W13.2,EIC",[Preco Total])
  Print File ARQIMP "\n"

  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

For ITEM From CONTADOR To CONTA_ITENS
    If CONTADOR = CONTA_ITENS 
       Then QuitLoop
    EndIf
    Print File ARQIMP "\n"
EndFor 

Print File ARQIMP CI_8LPP
EndProc ;(Itens)

;*******************************************************************************
;* ImpressÆo do Rodap‚                                                         *
;*******************************************************************************
Proc RM_0353_Rodape(QUEBRAFORM)
Private QUEBRAFORM,s

Print File ARQIMP "------------------------------------------------------------------------------------------------------------------------"+"\n"

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


Print File ARQIMP "\n"
If QUEBRAFORM
   Then ;Print File ARQIMP Spaces(90)+"Total..............  *******"+"\n"
        ;Print File ARQIMP Spaces(90)+"Frete..............  *******"+"\n"
        ;Print File ARQIMP Spaces(90)+"Desconto ***** %...  *******"+"\n"
        ;Print File ARQIMP Spaces(90)+"Total Liquido......  *******"+"\n\n"
        Print File ARQIMP "\n\n\n\n\n"
        
   Else OBS_1 = ""
        OBS_2 = ""
        
        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)
                    OtherWise  : QuitLoop
                  EndSwitch
                  OBS   = Substr(OBS,PosI+1,Len(OBS))
                  PosI  = Search("\n",OBS)
                  If PosI=0
                     Then OBS_2 = OBS
                          QuitLoop
                  EndIf    
                EndWhile              
                
                ;Apenas duas linhas de OBS, e se a primeira linha for maior que 73 caracteres, 
                ;a segunda linha ser  a sequencia destes caracteres
                If SubStr(OBS_1,74,73)<>"" Then OBS_2 = SubStr(OBS_1,74,73) EndIf
                If SubStr(OBS_1,01,73)<>"" Then OBS_1 = SubStr(OBS_1,01,73) EndIf
                
           Else OBS_1 = Ltrim(Rtrim(SubStr([Observacao na Nota Fiscal],01,73)))
                OBS_2 = Ltrim(Rtrim(SubStr([Observacao na Nota Fiscal],74,73)))
        EndIf
   
        If OBS_1<>""
           Then s=Completa(Spaces(02)+"OBS.: "+OBS_1,83," ")
           Else s=Spaces(83)
        EndIf        
        ;s=s+Spaces(07)+"Total..............  *******" 
        s=s+"\n"                                          
        Print File ARQIMP s
                                             
        If OBS_2<>""
           Then s=Completa(Spaces(08)+OBS_2,83," ")
           Else s=Spaces(83)
        EndIf        
        ;s=s+Spaces(07)+"Frete..............  *******"
        s=s+"\n"                                          
        Print File ARQIMP s

        ;Print File ARQIMP Spaces(90)+"Desconto ***** %...  *******"+"\n"
        ;Print File ARQIMP Spaces(90)+"Total Liquido......  *******"+"\n\n"
        
        Print File ARQIMP "\n\n\n"
EndIF

;Print File ARQIMP "Cond. Pagto: "+[Descricao CondPagto]
;Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
;Print File ARQIMP "   CD: "+[Composicao de Dias]+"\n\n"
Print File ARQIMP "\n\n"

;********************* IMPRESSAO DOS VENCIMENTOS **************************
Print File ARQIMP "\018"
Print File ARQIMP "\027E"
;Print File ARQIMP "Vencimentos: "

;Array DATA[5]
;Array VALOR[5]
;Array FORMA_PG[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])
;  FORMA_PG[K] = Format("W2,AR,EIC",[Forma de Pagamento])
;  If K=5
;     Then RM_0353_VENC()
;          K=0
;  EndIf
;  Skip
;  If EOT() Then QuitLoop EndIf
;EndWhile
;If K>0
;   Then VENC()
;EndIf
Print File ARQIMP "\n\n\n\n"
Print File ARQIMP "\027F"

If QUEBRAFORM
   Then Print File ARQIMP "\n\n\n\n\n"
   Else Print File ARQIMP Spaces(12)+"   /   /     -                                   " + "\n"
        Print File ARQIMP Spaces(12)+"-------------------------------------------------" + "\n"
        Print File ARQIMP Spaces(12)+"              Data e Assinatura"+"\n"
        Print File ARQIMP "\n\n\n\n"
        Print File ARQIMP CI_6LPP
        Print File ARQIMP CI_10CPP
EndIf   

EndProc ;RM_0353_Rodape

;*******************************************************************************
;* Rotina de Impressao dos Vencimentos                                         *
;*******************************************************************************
Proc RM_0353_VENC()
If KV>0
   Then Print File ARQIMP Spaces(13)
EndIf

If IMPRIMINDO_PARCELAS
   Then Print File ARQIMP DATA[NP]+Spaces(02)+"\n"
        Print File ARQIMP Spaces(13)+VALOR[NP]+Spaces(02) "\n"
        Print File ARQIMP "   FP: "+FORMA_PG[NP]+"\n\n"
        return
EndIF

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"
Print File ARQIMP Spaces(13)
For I From 1 to K
  Print File ARQIMP "FP: "+FORMA_PG[I]+Spaces(06)
EndFor
Print File ARQIMP "\n\n"

KV=KV+1
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
ArqImp=PrivDir()+"Ro_0353B.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)

For Wc From 1 To 2 

  IMPRIMINDO_PARCELAS=FALSE
  NParc=[FaturaC->Numero de Parcelas]
  If NParc<1 Then NParc=1 EndIf

  For NP From 1 to NParc
    If [FaturaV->Forma de Pagamento]="NP"
       Then IMPRIMINDO_PARCELAS=TRUE
    EndIf
    RM_0353_Cabecalho()
    RM_0353_Itens()
    RM_0353_Rodape(False) 
    If IMPRIMINDO_PARCELAS=False Then QuitLoop EndIf
  EndFor

  If CONTA_ITENS = 14 And Wc = 2
     Then ;Print File ARQIMP "\f"
     Else If CONTA_ITENS = 58 And Wc = 1
             Then Print File ARQIMP "\f"
          EndIf
  EndIf
  
EndFor
NUMCOPIA=1
ImpriArq(ARQIMP)

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