;** NP_0322  *******************************************************************
;** Impressao de Nota Promissoria                     ** Moreira & Silva  **   *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 24/09/2012 por Bruno Carvalho
;Ultima Alteracao: 10/10/2012 por Bruno Carvalho  
;Motivo Alteracao: Adicionado "Numero da NF" no Canhoto, "Conta Contabil" e "Data de EmissÆo" 
;Ultima Alteracao: 31/10/2012 por Bruno Carvalho  
;Motivo Alteracao: Adicionado "Conta Contabil" e "Hora" no canhoto da NP 
;Ultima Alteracao: 31/05/2013 por Bruno Carvalho  
;Motivo Alteracao: Ajustes ITEM[] Campo Unit rio -> 8 D¡gitos
;Ultima Alteracao: 02/07/2013 por Bruno Carvalho  
;Motivo Alteracao: Ajustes Monta_Item() 
;Ultima Alteracao: 13/09/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes modo ordem da duplicata numero/letra
;Ultima Alteracao: 05/12/2019 por Bruno Carvalho
;Motivo Alteracao: Adicionado Data de Vencimento na Promissoria 

;*******************************************************************************
; Rotina p/ Montar linha do item                                               *
;*******************************************************************************
Proc Monta_Item() 

Contador_Item = Contador_Item + 1
If DynArraySize(Item)>=Contador_Item
   Then Print File ARQIMP Spaces(2)+Format("W38",SubStr(Item[Contador_Item],4,Len(Item[Contador_Item])))
   Else Print File ARQIMP Spaces(2)+Spaces(38)
EndIf

EndProc ;Monta_Item()

;*******************************************************************************
; Rotina Principal                                                             *
;*******************************************************************************
AGUARDE("Imprimindo Nota Promissoria")

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

Release Vars Item
DynArray Item[]

Print File ARQIMP CI_15CPP
Print File ARQIMP CI_6LPP
;********* Imprime Vencimentos*********
Array dia[12]
Array mes[12]
Array ano[12]

For I From 1 To 12
    dtvencto[i]=BlankDate()
    valorven[i]=BlankNum()
    dia[i] =""
    mes[i] =""
    ano[i] =""
EndFor

HORA=Time()
HORA=Substr(HORA,1,5)

CONTA_CONTABIL=""
Moveto "FaturaC"
Moveto [Codigo]
Ativa_Lookup()
Moveto [Codigo]
Locate [Faturac->Codigo]
If RetVal
   Then If [Conta Contabil]<>""
           Then CONTA_CONTABIL=" - "+Format("W10,AL",[Conta Contabil])
        EndIf   
EndIf
KeyPress "ESC"   

Moveto "FaturaV"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
If RetVal
   Then i=0
        While [Numero da OF] = [FaturaC->Numero da OF]
          i=i+1
          dtvencto[i] = [Data ]
          valorven[i] = [Valor]
          dia[i]      = Day([Data])
          mes[i]      = MESEXT(Month([Data]))
          ano[i]      = Year([Data])
          Skip
          If EOT()
             Then QuitLoop
          Endif
        EndWhile
Endif              

Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
If RetVal
   Then i=0
        While [Numero da OF] = [FaturaC->Numero da OF]
          i=i+1
          Item[i]=AlinhM(StrVal(i),3)+Format("W6",SubStr([Codigo do Item],1,6))+Spaces(1)+Format("W12",SubStr([Descricao do Item],1,12))+
                  Spaces(1)+Format("W2",SubStr([Descricao do Item],29,2))+Spaces(1)+Format("W2",[Unidade Fiscal])+
                  Spaces(1)+Format("W4",[Quantidade])+Format("W8.2,EIC",[Preco Unitario])
          Skip
          If EOT()           
             Then QuitLoop
          Endif
        EndWhile
Endif
Moveto "FaturaC"  

i=1

getsys_modo_ordemdup()

While valorven[i] <> BlankNum()
      
      Contador_Item = 0
      
      ESCEXT(valorven[i])
      VALOR_POR_EXTENSO=EXTENSO
      
      DatExt(dtvencto[i])
      DATA_POR_EXTENSO=EXTENSO
                                                
      ;Print File ARQIMP "\n"
      Print File ARQIMP Spaces(16)+"Codigo Descricao       UN Qtde PrUnit"+Spaces(34)+Format("D13",dtvencto[i])+"\n"

      ;  Cliente  XXXX  Codigo Descricao      UN Qtde PrUnit    99999999999999                 XX/XX/XXXX                9999999.99
      Print File ARQIMP "Cliente  "+Format("W4",[Codigo])
      Monta_Item()
      Print File ARQIMP Spaces(5)
                         
      
      switch
        case sys_modo_ordemdup="N":
          If Not IsBlank(dtvencto[2])
             Then NOTA=Format("W8,AR",(NOTAFISC+"/"+strval(i)))
             Else NOTA=Format("W10,AR",(NOTAFISC+"  "))           
          EndIf
        
        otherwise                 :
          If Not IsBlank(dtvencto[2])
             Then NOTA=Format("W8,AR",(NOTAFISC+"/"+CHR(65+i-1)))
             Else NOTA=Format("W10,AR",(NOTAFISC+"  "))           
          EndIf
      
      endswitch
      
      Print File ARQIMP NOTA+Spaces(14)+Format("W49.2,EIC",valorven[i])+"\n"
  
      ;  Vendedor XXXX  ----- -------------- -- ---- ------    99999999999999                 XX/XX/XXXX                9999999.99
      Print File ARQIMP "Vendedor "+Format("W4",[Codigo do Representante])
      Monta_Item()
      Print File ARQIMP "\n"
      
      ;Item 1
      ;  Vr Nota:       999999 XXXXXXXXXXXXXXX XX 9999 999.99
      Print File ARQIMP "Vr Nota:     "
      Monta_Item()
      Print File ARQIMP "\n"
      
      ; Item 2
      ;  9999999999.99  999999 XXXXXXXXXXXXXXX XX 9999 999.99     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Print File ARQIMP Completa(LTrim(Format("W13.2,EIC",[Valor Nota Fiscal])),13," ")
      Monta_Item() 
      Print File ARQIMP Spaces(5)+SubStr(DATA_POR_EXTENSO,01,78)+"\n"
      
      ; Item 3
      ;  Vr Vencto:     999999 XXXXXXXXXXXXXXX XX 9999 999.99    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Print File ARQIMP "Vr Vencto:   "
      Monta_Item() 
      Print File ARQIMP Spaces(5)+SubStr(DATA_POR_EXTENSO,79,34)+"\n"

      ; Item 4
      ;  9999999999.99  999999 XXXXXXXXXXXXXXX XX 9999 999.99    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Print File ARQIMP Completa(LTrim(Format("W13.2,EIC",valorven[i])),13," ")
      Monta_Item() 
      Print File ARQIMP Spaces(5)+Format("W40",[Nome Fantasia Empresa])+"\n"   
      
      ; Item 5
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99    
      Print File ARQIMP "Data Vento:  "
      Monta_Item() 
      Print File ARQIMP "\n"

      ; Item 6
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      DATA=Format("D13",dtvencto[i])
      If Len(Ltrim(Rtrim(DATA))) = 9
         Then DATA="0"+Ltrim(DATA)
      EndIf   
      Print File ARQIMP Format("W10",DATA)+Spaces(3)
      Monta_Item()
      Print File ARQIMP Spaces(5)+SubStr(VALOR_POR_EXTENSO,2,77)+"\n"   

      ; Item 7
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Print File ARQIMP Format("W13,AL",Ltrim(RTrim(NOTA)))
      Monta_Item()
      Print File ARQIMP Spaces(5)+SubStr(VALOR_POR_EXTENSO,79,77)+"\n"  


      ; Item 8
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Print File ARQIMP Format("W5,AL",HORA)+Spaces(8)
      Monta_Item()
      Print File ARQIMP Spaces(5)+SubStr(VALOR_POR_EXTENSO,156,77)+"\n" 

      ; Item 9
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99    
      Print File ARQIMP Format("W10,AL",SubStr(CONTA_CONTABIL,4,10))+Spaces(3)
      Monta_Item()
      Print File ARQIMP "\n" 
      
      ; Item 10
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Print File ARQIMP Spaces(13)
      Monta_Item()
      Print File ARQIMP Spaces(10)+Rtrim(Format("W40",[Razao Social]))+CONTA_CONTABIL+"\n" 
                                                                                          
      
      ; Item 11
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99        XXXXXXXXXXXXXXXXXX
      Print File ARQIMP Spaces(13)
      Monta_Item()
      Print File ARQIMP Spaces(10)+Format("W18",[CGC])+Spaces(25)+HORA+"\n" 
      
      
      ; Item 12
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Print File ARQIMP Spaces(13)
      Monta_Item()
      Print File ARQIMP Spaces(10)+Format("W40",[Endereco])+"\n" 
      
      
      ; Item 13
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      
      DATAE=Tira_CharNum(StrVal([Data de Emissao]))
      If Len(DATAE)=7
         Then DATAE="0"+DATAE
      EndIf   
      
      DATAE=SubStr(DATAE,1,2)+"/"+SubStr(DATAE,3,2)+"/"+SubStr(DATAE,5,4)
      Print File ARQIMP Spaces(13)
      Monta_Item()
      Print File ARQIMP Spaces(10)+Format("W40",[Bairro])+Spaces(3)+DATAE+"\n" 
      
      
      ; Item 14
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Print File ARQIMP Spaces(13)
      Monta_Item()
      Print File ARQIMP Spaces(10)+Format("W40",[Cidade])+"\n" 
      
      Print File ARQIMP "\n\n\n\n\n"

      i=i+1

EndWhile

Print File ARQIMP CI_10CPP
Print File ARQIMP CI_6LPP

UnlockRecord

;---- Imprime Arquivo Impressao ------------------------------------------------
NUMCOPIA=1
ImpriArq(ARQIMP)

LIMPAGUARDE()
