;** NP_0322  *******************************************************************
;** Impressao de Nota Promissoria                     ** Moreira & Silva  **   *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 07/01/2013 por Bruno Carvalho
;Ultima Alteracao: 23/01/2013 por Bruno Carvalho 
;Motivo Alteracao: Ajustes p/ quando nĈo houver nenhum vencimento

;*******************************************************************************
; 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("W37",SubStr(Item[Contador_Item],4,Len(Item[Contador_Item])))
   Else Print File ARQIMP Spaces(2)+Spaces(37)
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
    dia[i] =""
    mes[i] =""
    ano[i] =""
EndFor

Array dtvencto[12]
Array valorven[12]

If Not IsEmpty("OrcaCPG")
   Then Moveto "OrcaCPG"
        Home
        
        COMP_DIAS=[Composicao de Dias]
        NP=Search("/",COMP_DIAS)
        Switch 
          Case NP > 0 : 
               i=0
               While NP > 0
                  i=i+1
                  COMP_DIAS = SubStr(COMP_DIAS,NP+1,Len(COMP_DIAS))
                  NP=Search("/",COMP_DIAS) 
               EndWhile
               Array nparcela[i+1]
               
               COMP_DIAS=[Composicao de Dias]
               For Contador From 1 To ArraySize(nparcela)
                 NP = Search("/",COMP_DIAS)
                 PARC = SubStr(COMP_DIAS,1,NP-1)
                 COMP_DIAS = SubStr(COMP_DIAS,NP+1,Len(COMP_DIAS))
                 If Contador=ArraySize(nparcela)
                    Then PARC=COMP_DIAS
                 EndIf    
                 nparcela[Contador]=PARC
               EndFor
        
               VR_NF       = ([Valor Nota Fiscal]/ArraySize(nparcela))
               VR_NF_RESTO = [Valor Nota Fiscal]-(VR_NF*ArraySize(nparcela))
               If VR_NF_RESTO > 0
                   Then VR_NF_RESTO = VR_NF+VR_NF_RESTO
                   Else VR_NF_RESTO = VR_NF
               EndIf   
                
               For Contador From 1 To ArraySize(nparcela)
                 dtvencto[Contador]=[OrcaC->Data de Emissao]+NumVal(nparcela[Contador])
                 If Contador=ArraySize(nparcela)
                    Then valorven[Contador]=VR_NF_RESTO
                    Else valorven[Contador]=VR_NF  
                 EndIf
                 dia[i]      = Day(dtvencto[Contador])
                 mes[i]      = MESEXT(Month(dtvencto[Contador]))
                 ano[i]      = Year(dtvencto[Contador])          
               Endfor
                
          Case NP = 0 And ([Numero de Parcelas]=BlankNum() Or [Numero de Parcelas]=1) :
                Array nparcela[1]    
        
                valorven[1]=[Valor Nota Fiscal]
                
                If [Composicao de Dias]=""
                   Then DIAS=0
                   Else DIAS=[Composicao de Dias]
                EndIf
                dtvencto[1]=[OrcaC->Data de Emissao]+NumVal(DIAS)      
                dia[1]= Day(dtvencto[1])
                mes[1]= MESEXT(Month(dtvencto[1]))
                ano[1]= Year(dtvencto[1]) 
        
          Case NP = 0 And ([Numero de Parcelas]<>BlankNum() And [Numero de Parcelas]>1) :
               NP=[Numero de Parcelas]
               Array nparcela[NP]
                
                VR_NF       = ([Valor Nota Fiscal]/NP)    
                VR_NF_RESTO = [Valor Nota Fiscal]-(VR_NF*NP)
                If VR_NF_RESTO > 0
                   Then VR_NF_RESTO = VR_NF+VR_NF_RESTO
                   Else If VR_NF_RESTO < 0
                           Then VR_NF_RESTO=VR_NF_RESTO+VR_NF
                           Else VR_NF_RESTO=VR_NF 
                        EndIf
                EndIf   
        
                If [Composicao de Dias]=""
                   Then DIAS=30
                   Else DIAS=NumVal([Composicao de Dias])
                EndIf
        
                For Contador From 1 To NP
                  If Contador = 1 
                     Then DATA=DIAS
                     Else DATA=DATA+DIAS
                  EndIf    
                  dtvencto[Contador]=[OrcaC->Data de Emissao]+DATA
                  If Contador=NP
                     Then valorven[Contador]=VR_NF_RESTO
                     Else valorven[Contador]=VR_NF  
                  EndIf   
                  dia[Contador]      = Day(dtvencto[Contador])
                  mes[Contador]      = MESEXT(Month(dtvencto[Contador]))
                  ano[Contador]      = Year(dtvencto[Contador])             
                Endfor
        EndSwitch  
EndIf

For I From 1 To 12
  If Not IsAssigned(valorven[i])
     Then valorven[i]=BlankNum()
          dtvencto[i]=BlankDate() 
  EndIf        
EndFor

If Not IsAssigned(nparcela[1])
   Then Array nparcela[1] 
        nparcela[1]="1"
EndIf        

If valorven[1]=BlankNum()  Then valorven[1]=[Valor Nota Fiscal] EndIf 
If dtvencto[1]=BlankDate() Then dtvencto[1]=Today()             EndIf 
   
HORA=Time()
HORA=Substr(HORA,1,5)

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

Moveto "OrcaI"
i=0
While [Numero do Orcamento] = [OrcaC->Numero do Orcamento]
  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])+Spaces(1)+Format("W6.2,EIC",[Preco Unitario])
  Skip
  If EOT()           
     Then QuitLoop
  Endif
EndWhile

Moveto "OrcaC"  

i=1

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"+"\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(4)
                         
      NOTA=Format("W14,AR",([Numero do Orcamento]+"  "))
      Print File ARQIMP NOTA+Spaces(18)+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(6)+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(6)+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(6)+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(6)+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(6)+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(6)+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(11)+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(11)+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(11)+Format("W40",[Endereco])+"\n" 
      
      
      ; Item 13
      ;                 999999 XXXXXXXXXXXXXXX XX 9999 999.99        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      DATAE=Tira_CharNum(StrVal([Data de Emissao]))
      DATAE=SubStr(DATAE,1,2)+"/"+SubStr(DATAE,3,2)+"/"+SubStr(DATAE,5,4)
      Print File ARQIMP Spaces(13)
      Monta_Item()
      Print File ARQIMP Spaces(11)+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(11)+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()
Do_It!