;** NE_0377 ********************************************************************
;** Imprime a Digitacao do Romaneio                                            *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 10/03/2016 por Anivel Augusto Rodrigues
;Ultima Alteracao: 16/03/2016 por Anivel Augusto Rodrigues 
;Motivo Alteracao: Ajustes Valores da Tabela
;Ultima Alteracao: 27/07/2017 por Anivel Augusto Rodrigues 
;Motivo Alteracao: Ajustes referencia             
;Ultima Alteracao: 12/09/2017 por Bruno Carvalho 
;Motivo Alteracao: Adicionado Peso Liquido e Peso Total

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

getrecord "\\sistemas\\cadastro\\destinat" [notaec->Codigo] to regdestinat

QUEBRA   = False
CONTADOR = 0

;************************** IMPRESSAO DO CABECALHO *****************************
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf
Moveto "NotaEC"
;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 "\027E"
Print File ARQIMP Spaces(50)+"Data Emissao: " + Format("D13",[Data de Emissao])+"\n"
Print File ARQIMP "\027F"
Print File ARQIMP Format("W50,AL","CONTROLE INTERNO - "+[NotaEC->Codigo da Empresa])+"Impressao: "+Format("D10",Today())+"-"+Time()+ "\n"

Print File ARQIMP "\020"
;Print File ARQIMP "\014"
Print File ARQIMP "\027E"
Print File ARQIMP Spaces(60)+"NE N§ "+[Tipo do Documento]+"/"+Format("W06.3,AL",[NotaEC->Numero do Documento])+"\n"

Print File ARQIMP "Pessoa  : "+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",regdestinat["Endereco"])
Print File ARQIMP Format("W18,AL",[CGC])+"\n"

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

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

Print File ARQIMP "Operacao: "
Print File ARQIMP Chr(27)+Chr(71)
Print File ARQIMP Format("W40",[Tipo de Operacao]+" "+[Descricao da Operacao])
Print File ARQIMP Chr(27)+Chr(72)
Print File ARQIMP "\n"

Print File ARQIMP "\015"         ;Imprime 17 Caracteres por polegada

;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP "---------------------------------------------------------------------------------------------------------------------------------"+"\n"
Print File ARQIMP "  Item                                          Referencia Peso Liq.  Peso Total Un  Quantidade  Unitario        Total      Pecas"+"\n"
Print File ARQIMP "---------------------------------------------------------------------------------------------------------------------------------"+"\n"
                  ;XX123X1234567890X123456789012345678901234567890X1234567890X123456789X12345678901X
EndProc ;Cabecalho()

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc IMPFAT()
private UN_TIPO, UN_ITENS, UN_QTD, CONT_UN_TIPO, CONT_UN_ITENS, PECAS,
        ACHOU_UNIDADE, indice, REFERENCIA 

Cabecalho()

DynArray UN_TIPO[]
DynArray UN_QTD[]
DynArray UN_ITENS[]          

CONT_UN_TIPO  = 0
CONT_UN_ITENS = 0
                       
Moveto "NotaEI"
scan
  PECAS      = BlankNum()
  REFERENCIA = ""
  PESO_LIQ   = BlankNum()
  PESO_TOTAL = BlankNum()
  
  GetRecord "\\Sistemas\\Estoque\\Itens" [Codigo do Item] To REG_ITEM
  If RetVal
     Then PESO_LIQ   = REG_ITEM["Peso Liquido"] 
          REFERENCIA = REG_ITEM["Referencia"]
          If REG_ITEM["Unidade Fiscal"]="KG" 
             Then If REG_ITEM["Peso Liquido"] > 0 And REG_ITEM["Peso Liquido"] <> BlankNum()
                     Then PECAS = NDecimal(NDecimal([Quantidade],3)/REG_ITEM["Peso Liquido"],3)
                  EndIf
          EndIf   
  EndIf   
  
  If PESO_LIQ>0
     Then PESO_TOTAL = PESO_LIQ * [Quantidade]
  EndIf     
  
  ;---- Verifica Tipos de Unidade ---------------------------------------------- 
  ACHOU_UNIDADE = False
  For i From 1 To DynArraySize(UN_TIPO)
    If UN_TIPO[i]=[Unidade Fiscal]
       Then ACHOU_UNIDADE = True
            indice=i
            QuitLoop
    EndIf   
  EndFor
  
  If Not ACHOU_UNIDADE
     Then CONT_UN_TIPO    = CONT_UN_TIPO + 1
          indice          = CONT_UN_TIPO
          UN_TIPO[indice] = [Unidade Fiscal]
          UN_QTD[indice]  = blanknum()
  EndIf         
  
  ;---- Item -------------------------------------------------------------------
  CONT_UN_ITENS = CONT_UN_ITENS + 1
  
  UN_ITENS[CONT_UN_ITENS] =                           Format("W03,AL",[Unidade Fiscal])                          
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] + Spaces(02)+Format("W03,AL",[Sequencia do Lancamento])
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] + Spaces(01)+Format("W11,AL",[Codigo do Item])
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] + Spaces(01)+Format("W41,AL",Format("W30,AL",[Descricao do Item])+Spaces(1)+REFERENCIA)
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] +            Format("W9.3,EIC",PESO_LIQ)
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] + Spaces(01)+Format("W11.3,EIC",PESO_TOTAL)
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] + Spaces(01)+Format("W03,Al",[Unidade Fiscal])
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] + Format("W11.3,EIC",[Quantidade])
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] + Format("W10.2,EIC",[Preco Unitario])
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] + Format("W13.2,EIC",[Preco Total])+Spaces(1)
  UN_ITENS[CONT_UN_ITENS] = UN_ITENS[CONT_UN_ITENS] + Format("W10.3,EIC",PECAS)

  UN_QTD[indice]=UN_QTD[indice]+[Quantidade]

endscan

For un From 1 To DynArraySize(UN_TIPO)
  
  For iten From 1 To DynArraySize(UN_ITENS)
     If Rtrim(UN_TIPO[un]) = Rtrim(SubStr(UN_ITENS[iten],1,3))
        Then Print File ARQIMP SubStr(UN_ITENS[iten],4,Len(UN_ITENS[iten]))+"\n"
             CONTADOR = CONTADOR + 1
     EndIf
     If CONTADOR>=20
        Then QUEBRA = True
             Rodape()
             Cabecalho()
     EndIf
  EndFor
  
  Print File ARQIMP CI_INEG+spaces(71)+Format("W13,AL","TOTAL POR "+Rtrim(UN_TIPO[un])+":") + Format("W11.3,EIC",UN_QTD[un])+CI_FNEG+"\n\n"
  CONTADOR = CONTADOR + 2
EndFor  

For ITEM From CONTADOR To 20
  Print File ARQIMP "\n"
EndFor     

QUEBRA = False
Rodape()        

EndProc ;IMPFAT()   

;*******************************************************************************
;* Rotina para Imprimir o Rodape                                               *
;*******************************************************************************
Proc Rodape()
private TC, CT_UTILIZADO, OBS

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

;********************************** CALCULO TC *********************************
TC = ""
CT_UTILIZADO = BlankNum()

;************************** IMPRESSAO DOS TOTAIS *******************************
Moveto "NotaEC"

Print File ARQIMP Format("W66","Tipo Pagto: "+[Descricao Tipo Pagamento])
                               
If QUEBRA
   Then Print File ARQIMP Spaces(21)+"Total............... *******   "+"\n"
   Else Print File ARQIMP Spaces(21)+"Total..............."+Format("W11.2,EIC",[Valor Mercadoria])+"\n"
EndIf   

OBS = Ltrim(Rtrim(SubStr([Observacao],01,72)))
If Search("\n",OBS) > 0 
   Then Pos = Search("\n",OBS)
        OBS = SubStr(OBS,1,Pos-1) + SubStr(OBS,1,Pos-1)
EnDIf

If OBS <> "" And Len(OBS)>1   
  Then Print File ARQIMP "OBS.: "                                                
       Print File ARQIMP Format("W78,AL",Rtrim(SubStr([Observacao],01,78)))
  Else Print File ARQIMP Spaces(84)
EndIf
Release VARS Pos,OBS       

If QUEBRA
   Then Print File ARQIMP Spaces(3)+"Desconto  **  "+"...... *******   "+"\n"
   Else Print File ARQIMP Spaces(3)+"Desconto"+Format("W06.2,EIC",blanknum()) + "......"
        Print File ARQIMP Format("W11.2,EIC",[Valor Desconto])+"\n"
EndIf   

;---- IMPRESSAO DE TITULO DE CREDITO -----------------------------
If Rtrim(TC)<>""
   Then TC = ".................: " + TC
EndIf   
If SubStr(TC,1,84)<>""
   Then Print File ARQIMP Chr(27)+Chr(71)
        Print File ARQIMP Completa(SubStr(TC,1,84),84," ")
        Print File ARQIMP Chr(27)+Chr(72)
   Else Print File ARQIMP Spaces(84)
EndIf    

If QUEBRA
   Then Print File ARQIMP Spaces(3)+"DespAces............ *******   "+"\n"   
   Else Print File ARQIMP Spaces(3)+"DespAces............"+Format("W11.2,EIC",[Valor Despesas Acessorias])+"\n"
EndIf    

If SubStr(TC,85,66)<>""
   Then Print File ARQIMP Chr(27)+Chr(71)
        Print File ARQIMP Spaces(18)+Completa(SubStr(TC,85,66),66," ")
        Print File ARQIMP Chr(27)+Chr(72)
   Else Print File ARQIMP Spaces(84)
EndIf

If QUEBRA
   Then Print File ARQIMP Chr(27)+Chr(71)
        Print File ARQIMP Spaces(3)+"Total Liquido....... *******   "+"\n"
        Print File ARQIMP Chr(27)+Chr(72)   
   Else Print File ARQIMP Chr(27)+Chr(71)
        Print File ARQIMP Spaces(3)+"Total Liquido......."+Format("W11.2,EIC",([Valor Nota Fiscal]))+"\n"
        Print File ARQIMP Chr(27)+Chr(72)
EndIf      
         
If SubStr(TC,150,66)<>""
   Then Print File ARQIMP Spaces(18)+Completa(SubStr(TC,150,66),66," ")
   Else Print File ARQIMP Spaces(84)
EndIf

If QUEBRA
   Then Print File ARQIMP Spaces(3)+".................... *******   "+"\n"
        Print File ARQIMP Spaces(87)+".................... *******   "+"\n\n"   
   Else Print File ARQIMP Spaces(3)+"...................."  + Format("W11.2,EIC",blanknum())+"\n"
        Print File ARQIMP Spaces(87)+"...................." + Format("W11.2,EIC",blanknum())+"\n\n"
EndIf                                
                 
;************************ IMPRESSAO DOS VENCIMENTOS ****************************
Array DATA [9]
Array VALOR[9]
Array FPGT [9]
QUEBRA_VENCTO = False
K=0
KV=0

switch
  case [Notaec->Tipo de Pagamento]="V":
    Moveto "NotaECV"

    If Not QUEBRA_VENCTO
       Then Print File ARQIMP "\027E"
            Print File ARQIMP "Vencimentos: "
       Else Print File ARQIMP SpaceS(13)
    EndIf   

    K=K+1
    DATA[K]  = Format("D13",[NotaEC->Data de Emissao])
    VALOR[K] = Format("W10.2,AR,EIC",[Valor])
    FPGT[K]  = Format("W10","..: "+"")
   
  case [Notaec->Tipo de Pagamento]="P":
    Moveto "NotaECP"
    scan
      If K = 0
         Then ;Print File ARQIMP "\018"
              If Not QUEBRA_VENCTO
                 Then Print File ARQIMP "\027E"
                      Print File ARQIMP "Vencimentos: "
                 Else Print File ARQIMP SpaceS(13)
              EndIf   
      EndIf        
      K=K+1
      DATA[K]  = Format("D13",[Data de Vencimento])
      VALOR[K] = Format("W10.2,AR,EIC",[Valor do Documento])
      FPGT[K]  = Format("W10","FP: "+[Tipo do Pagamento])
      If K=9
         Then QUEBRA_VENCTO=True
              VENC()
              K=0
      EndIf
    Endscan
     
endswitch
If K>0
   Then VENC()
EndIf
Print File ARQIMP "\027F"

Moveto "NotaEC"

;************************* IMPRESSAO DO RODAPE *********************************
If Not QUEBRA
   Then Print File ARQIMP Format("W129,AC","   /   /     -                                   ")+ "\n"
        Print File ARQIMP Format("W129,AC","-------------------------------------------------") + "\n"
        Print File ARQIMP Format("W129,AC","Data e Assinatura")+"\n"        
EndIf

Print File ARQIMP CI_6LPP
Print File ARQIMP CI_10CPP
Print File ARQIMP "\f"
        
EndProc

;*******************************************************************************
;* Rotina de Impressao dos Vencimentos                                         *
;*******************************************************************************
Proc VENC()
private i

If KV>0
   Then If Not QUEBRA_VENCTO
           Then Print File ARQIMP Spaces(13)
        EndIf   
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"

KV=KV+1
EndProc

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

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

AGUARDE("Imprimindo Romaneio -> "+[NotaEC->Numero do Documento])
IMPFAT()

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

Release Vars QUEBRA, CONTADOR, DATA, VALOR, FPGT, QUEBRA_VENCTO, K, KV
Release Procs IMPFAT, VENC, Cabecalho, Rodape
LIMPAGUARDE()
