;** RO_0412 ********************************************************************
;** Imprime a Digitacao do Romaneio                                            *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 09/05/2011 por Bruno Carvalho
;Ultima Altera‡Æo: 10/05/2018 por Bruno Carvalho
;Motivo Alteracao: Adicinado Respons vel -> cliente controla pelo "FaturaIC" 
;Ultima Altera‡Æo: 14/05/2018 por Bruno Carvalho
;Motivo Alteracao: Adicionado Total de Pe‡as (Quantidade) nos totalizadores
;Ultima Altera‡Æo: 10/09/2019 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes data de emissao

;*******************************************************************************
;* Rotina para Impressao do Cabecalho                                          *
;*******************************************************************************
Proc Cabecalho()

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

Print File ARQIMP CI_10CPP
Print File ARQIMP CI_6LPP

Print File ARQIMP "\027F"
Print File ARQIMP Format("W50,AL","CONTROLE INTERNO")+"Impressao: "+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"
Print File ARQIMP "\020"

Print File ARQIMP "Cliente : "+Format("W05,AL",[FaturaC->Codigo])+Format("W46,AL",[FaturaC->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",[FaturaC->Endereco])
Print File ARQIMP Format("W18,AL",[FaturaC->CGC])+"\n"

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

Print File ARQIMP Spaces(10)+Format("W20,AL",[FaturaC->Cidade])
Print File ARQIMP Format("W20,AL",[FaturaC->UF])
Print File ARQIMP Format("W25,AL",REG_PESSOA["Celular"])+"\n"

Print File ARQIMP "Operacao: "
Print File ARQIMP Chr(27)+Chr(71)
Print File ARQIMP Format("W49,AL",""+[FaturaC->Codigo da Destinacao]+" "+[FaturaC->Descricao da Destinacao])+
                  "Emissao: "+Format("D13",[FaturaC->Data de Emissao])+"\n"
Print File ARQIMP Chr(27)+Chr(72)

Print File ARQIMP "Vendedor: "+Format("W05,AL",[FaturaC->Codigo do Representante])
Print File ARQIMP Format("W25,AL",[FaturaC->Nome Fantasia Represent])
If DynArraySize(REPRESENTANTE_RATEIO)>0
   Then For ii From 1 To DynArraySize(REPRESENTANTE_RATEIO)
          If ii>1
             Then Print File ARQIMP Spaces(53)
             Else Print File ARQIMP "Responsavel: " 
          EndIf
          Print File ARQIMP REPRESENTANTE_RATEIO[ii]
          Print File ARQIMP "\n"     
        Endfor
   Else Print File ARQIMP "\n"
EndIf

Print File ARQIMP "\015"
Print File ARQIMP "---------------------------------------------------------------------------------------------------------------------"+"\n"
Print File ARQIMP Spaces(02)+"Item                                                                          Un  Quantidade  Unitario        Total"+"\n"
Print File ARQIMP "---------------------------------------------------------------------------------------------------------------------"+"\n"

EndProc ;Cabecalho() 

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

Cabecalho() 

Moveto TABELA_TEMP
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
TOT      = 0
QTD      = 0
SeqItPed = 0
CONTADOR = 0

While [Numero da OF]=[FaturaC->Numero da OF]
  Print File ARQIMP Spaces(01)+Format("W03,AL",StrVal([Sequencia]))
  Print File ARQIMP Spaces(01)+Format("W11,AL",[Codigo do Item])
  Print File ARQIMP Spaces(01)+Format("W62,AL",[Descricao do Item])
  Print File ARQIMP Spaces(01)+Format("W03,Al",[Unidade Fiscal])

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

  Print File ARQIMP Format("W10.2,EIC",[Preco Unitario])
  Print File ARQIMP Format("W13.2,EIC",[Preco Total])+"\n"
  TOT = TOT + [Preco Total]
  QTD = QTD + [Qtd1]
  Skip
  If EOT() Then QuitLoop EndIf
  CONTADOR = CONTADOR + 1
  
  If CONTADOR >= PARAM_LINHAS_ITENS
     Then Rodape("QUEBRA_LINHA")
          Print File ARQIMP "\f"
          CONTADOR = 0
          Cabecalho() 
          Moveto TABELA_TEMP
  EndIf
EndWhile

If CONTADOR < 12
   Then For ITEM From CONTADOR To 12
          Print File ARQIMP "\n"
        EndFor
EndIf

EndProc ;Itens()

;*******************************************************************************
;* Rotina para Impressao do Cabecalho                                          *
;*******************************************************************************
Proc Rodape(PARAM)
Private PARAM

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

If PARAM = "QUEBRA_LINHA"
   Then Print File ARQIMP Spaces(87)+"Total..............  *******  "+"\n"
        Print File ARQIMP Spaces(87)+"Quantidade.........  *******  "+"\n"
        Print File ARQIMP "          *** FAVOR MANTER O ENDERECO E O TELEFONE ATUALIZADOS ***"
        Print File ARQIMP Spaces(21)+"Desconto  **   %...  *******  "+"\n"
        Print File ARQIMP Spaces(87)+"Frete..............  *******  "+"\n"
        Print File ARQIMP Spaces(87)+"Total Liquido......  *******  "+"\n\n"
 
        Print File ARQIMP "Cond. Pagto: "+[FaturaC->Descricao CondPagto]
        Print File ARQIMP "   NP: "+StrVal([FaturaC->Numero de Parcelas])
        Print File ARQIMP "   CD: "+[FaturaC->Composicao de Dias]+"\n"

        Print File ARQIMP "OBS.: "
        Print File ARQIMP Format("W75,AL",SubStr([FaturaC->Observacao na Nota Fiscal],001,73))+"\n\n"
        
        VENCTO()
        
        Print File ARQIMP "\n\n" 
        Print File ARQIMP "\020"
        Print File ARQIMP Format("W117,AC","** CONTINUA NA PROXIMA FOLHA") + "\n"
        
   Else Print File ARQIMP Spaces(87)+"Total.............."+Format("W11.2,EIC",TOT)+"\n"   
        Print File ARQIMP Spaces(87)+"Quantidade........."+Format("W11,EIC",QTD)+"\n"
        Print File ARQIMP "          *** FAVOR MANTER O ENDERECO E O TELEFONE ATUALIZADOS ***"
        Print File ARQIMP Spaces(21)+"Desconto"+Format("W06.2,EIC",[FaturaC->Taxa de Desconto]) + "%..."
        Print File ARQIMP Format("W12.2,EIC",[FaturaC->Valor Desconto])+"\n"
        Print File ARQIMP Spaces(87)+"Frete.............."+Format("W11.2,EIC",[FaturaC->Valor do Frete])+"\n"
        Print File ARQIMP Spaces(87)+"Total Liquido......"+Format("W11.2,EIC",[FaturaC->Valor Nota Fiscal])+"\n\n"
 
        Print File ARQIMP "Cond. Pagto: "+[FaturaC->Descricao CondPagto]
        Print File ARQIMP "   NP: "+StrVal([FaturaC->Numero de Parcelas])
        Print File ARQIMP "   CD: "+[FaturaC->Composicao de Dias]+"\n"

        Print File ARQIMP "OBS.: "
        Print File ARQIMP Format("W75,AL",SubStr([FaturaC->Observacao na Nota Fiscal],001,73))+"\n\n"
        
        VENCTO()
        
        Print File ARQIMP "\027F"

        ;************************* 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"
EndIf

EndProc ;Rodape()

;*******************************************************************************
;* 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 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 para Verificar Referˆncia                                            *
;*******************************************************************************
Proc Verifica()

WSITEM = [Codigo do Item]
WSDES  = "" 
WSUNI  = ""

Moveto [Codigo do Item]
If Not BUSCA_LOOKUP("Codigo do Item",[Codigo do Item],"Item n„o Cadastrado !")
   Then Return
EndIf

WSREF=REG_BUSCA_LOOKUP["Referencia"]
WSDES=REG_BUSCA_LOOKUP["Descricao do Item"]
WSUNI=REG_BUSCA_LOOKUP["Unidade Fiscal"]   

EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
Proc Seleciona()
Do_It!

TABELA_TEMP=Cria_Tabela("","Numero da OF(A6*),Sequencia(S*),Referencia(A20*),Codigo do Item(A10),Codigo da Cor_Tecido(A4),"+
"Preco Unitario($),Descricao do Item(A100),Descricao da Cor_Tecido(A20),Qtd1(S),Qtd2(S),Qtd3(S),Qtd4(S),Qtd5(S),Qtd6(S),Qtd7(S),Qtd8(S),Qtd9(S),"+
"Qtd10(S),Qtd11(S),Qtd12(S),Quantidade(N),Unidade Fiscal(A4),Preco Total($),Aliquota de IPI(N),Aliquota de ICMS(N),Peso Liquido(N),Peso Embalagem(N),Volumes(N)")

Adiciona_Indice_Secundario(TABELA_TEMP,"Indftgrd","Codigo do Item,Codigo da Cor_Tecido,Preco Unitario,Numero da OF,Sequencia")

CoEditKey
{TableAdd} {FaturaIC}
CONTA=0
DynArray REPRESENTANTE_RATEIO[]

Moveto "FaturaIC"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
If Retval
   Then While [Numero da OF]=[FaturaC->Numero da OF]
          CONTA = CONTA + 1
          REPRESENTANTE_RATEIO[CONTA] = Format("W4",[Codigo do Representante])+" "+Format("W20",[Nome Fantasia Represent])
          Skip
          If EOT() Then QuitLoop EndIf
        EndWhile
EndIf
Release Vars CONTA

{TableAdd} TypeIn TABELA_TEMP Enter
SEQG=1
Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
While [Numero da OF]=[FaturaC->Numero da OF]
Verifica()
Moveto "FaturaI"
 
  WSPRU  = [Preco Unitario]
  WSQTD  = [Quantidade    ]

  Moveto TABELA_TEMP
  Moveto [Codigo do Item]
  Locate WSITEM
  If Not RetVal
     Then End
          Down
          [Numero da OF           ]=[FaturaI->Numero da OF]
          [Sequencia              ]=SEQG
          [Codigo do Item         ]=WSITEM
          [Descricao do Item      ]=WSDES
          [Unidade Fiscal         ]=WSUNI
          [Qtd1                   ]=WSQTD
          [Referencia             ]=WSREF
          [Preco Unitario         ]=WSPRU
          SEQG=SEQG+1
     Else [Qtd1]=[Qtd1]+WSQTD     
  EndIf
  [Preco Total]=[Qtd1]*[Preco Unitario]

  UnLockRecord

  Moveto "FaturaI"
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

EndProc ;Seleciona()

;*******************************************************************************
;* Rotina de Vencimentos                                                       *
;*******************************************************************************
Proc VENCTO()        

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

Array DATA[5]
Array VALOR[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])
  If K=5
     Then VENC()
          K=0
  EndIf
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile
If K>0
   Then VENC()
EndIf
        
EndProc ;Proc VENCTO()

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
ArqImp=PrivDir()+"Ro_0217.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf
AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)

PARAM_LINHAS_ITENS = 54
GetRecord "\\Sistemas\\Cadastro\\Destinat" [FaturaC->Codigo] To REG_PESSOA

Seleciona()
Itens()
Rodape("")

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

Do_It!
Moveto TABELA_TEMP
ClearImage
Delete TABELA_TEMP
Moveto "FaturaIC"
ClearImage

Moveto "FaturaC"
CoEditKey

Release Vars VALOR,DATA,REPRESENTANTE_RATEIO,CONTA
Release Procs SELECIONA,VENC, cabecalho, Itens, Rodape, Vencto
LIMPAGUARDE()
