;** RO_0392 ********************************************************************
;** Imprime a Digitacao do Romaneio                         ** FB TEXTIL **    *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido  em: 11/01/2017 por Rafael Matos
;Alteracao:    em: 21/02/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Vencimentos
;Alteracao:    em: 21/02/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Alterado Coluna de Quantidade.MT para Rendimento (Fator de ConversÆo)
;Alteracao:    em: 06/04/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes email            

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

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

Moveto "FaturaC"
Pega_Endereco_Fatura([Numero da OF])

getrecord "\\sistemas\\cadastro\\Destinat" [Faturac->Codigo] to regdestinat
if retval
   then email=regdestinat["Endereco_Eletronico"]
   else email=""
endif

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 "\n"
 
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\n"

Print File ARQIMP "\027E"+"Entrega : "+"\027F"+Format("W40,AL",VSD_E_Endereco)+"\n"
Print File ARQIMP Spaces(10)+Format("W20,AL",VSD_E_Bairro)+Format("W20,AL",VSD_E_CEP)+"\n" 
Print File ARQIMP Spaces(10)+Format("W20,AL",VSD_E_Cidade)+Format("W20,AL",VSD_E_UF)+"\n"
Print File ARQIMP "\027E"+"Transp. : "+"\027F"+Format("W05,AL",[Codigo da Transportadora])+Format("W46,AL",[Nome Fantasia Transport])+"\n"

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

Print File ARQIMP CI_8LPP
Print File ARQIMP "\027P"         ;Imprime 10 Caracteres por polegada

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("W20,AL",[UF])
Print File ARQIMP Format("W25,AL",[Telefone])+"\n"

Print File ARQIMP "Operacao:  "
Print File ARQIMP Chr(27)+Chr(71)
Print File ARQIMP ""+Format("W39,AL",[Codigo da Destinacao]+" "+[Descricao da Destinacao])
Print File ARQIMP Chr(27)+Chr(72)
Print File ARQIMP email+"\n"

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                                                                     Rend. Un Quantidade  Unitario        Total"+"\n"
Print File ARQIMP "---------------------------------------------------------------------------------------------------------------------"+"\n"

Moveto "FaturaI"  
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
QUANT    = 0
TOT      = 0
CONTADOR = 0

Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
QUANT=0
TOT  =0
PARAM_TAMANHO_DESCR=63

While [Numero da OF]=[FaturaC->Numero da OF]

  desc=[Descricao do Item]
 
  FATCON = 1
  UNEMB  = ""
  FATOR_CONVERSAO = "1"
  GetRecord "\\Sistemas\\Estoque\\Itens" [Codigo do Item] To REG_ITEM
  If ReTval
     Then FATOR_CONVERSAO = REG_ITEM["Fator Conversao Embalagem"]
          desc=desc+Spaces(1)+REG_ITEM["Descricao Complementar"]
          UNEMB=REG_ITEM["Unidade Embalagem"]
          If IsBlank(REG_ITEM["Fator Conversao Embalagem"])
             Then FATCON=1
             Else FATCON=REG_ITEM["Fator Conversao Embalagem"]
          EndIf
  EndIf
  PARAM_TAMANHO_DESCR=50

  Numero_de_Linhas=Int(Len(Desc)/PARAM_TAMANHO_DESCR)
	If (Len(Desc)/PARAM_TAMANHO_DESCR)>Numero_de_Linhas
     Then Numero_de_Linhas=Numero_de_Linhas+1
  EndIf

	LINHA_ITEM = Spaces(02)+Format("W04,AL",[Item da OF])+Spaces(01)+Format("W11,AL",[Codigo do Item])

  pos_inicial=1
  For nl From 1 To Numero_de_Linhas
     LINHA_ITEM = LINHA_ITEM + Format("W50,AL",SubStr(Desc,pos_inicial,PARAM_TAMANHO_DESCR))
     If nl<Numero_de_Linhas
        Then Print File ARQIMP LINHA_ITEM + "\n"
             LINHA_ITEM = Spaces(18)
             pos_inicial=pos_inicial+PARAM_TAMANHO_DESCR
     EndIf
  EndFor    
          
  ;If Not IsBlank(UNEMB)
  ;   Then LINHA_ITEM=LINHA_ITEM+Format("W11.2,EIC",([Quantidade]/FATCON))
  ;EndIf

  LINHA_ITEM=LINHA_ITEM+Format("W11.2,EIC",FATOR_CONVERSAO)

	LINHA_ITEM=LINHA_ITEM+Spaces(2)+Format("W03,AL",[Unidade Fiscal])
  LINHA_ITEM=LINHA_ITEM+Format("W10.3,EIC",[Quantidade])
  LINHA_ITEM=LINHA_ITEM+Format("W10.2,EIC",[Preco Unitario])
  LINHA_ITEM=LINHA_ITEM+Format("W13.2,EIC",[Preco Total])

	Print File ARQIMP LINHA_ITEM+"\n"
 ; Print File ARQIMP Fill("-",54)+"\n"

	QUANT=QUANT+[Quantidade]
	TOT=TOT+[Preco Total]
	Skip
	If EOT() Then QuitLoop EndIf
EndWhile

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

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

Print File ARQIMP Spaces(87)+"Total.............."+Format("W11.2,EIC",TOT)+"\n"

Print File ARQIMP "***FAVOR REVISAR OS TECIDOS,NAO ACEITAMOS RECLAMACAO,NEM DEVOLUCAO"+ Spaces(21) + 
"Desconto"+Format("W06.2,EIC",[Taxa de Desconto]) + "%..." + Format("W12.2,EIC",[Valor Desconto]) + "\n"
Print File ARQIMP " APOS O CORTE DOS MESMOS***"

;-----------------------------------------------------------

Print File ARQIMP Spaces(60)+"Total Liquido......"+Format("W11.2,EIC",[Valor Mercadoria Liquido])+"\n\n"

Print File ARQIMP "Obs....: "+Format("W110,AL",SubStr([Observacao na Nota Fiscal],001,110))+"\n"
If Substr([Observacao na Nota Fiscal],111,110)<>""
   Then Print File ARQIMP Spaces(10)+Format("W110,AL",SubStr([Observacao na Nota Fiscal],111,110))+"\n\n"
   Else Print File ARQIMP "\n"
EndIf     
Print File ARQIMP "Numero da OF...: "+Format("W6,AL",[Numero da OF])+"\n"
Print File ARQIMP "Qtde.de Volumes: "+Format("W4,EIC",[Numero de Volumes])                                                         
             
Print File ARQIMP "Cond. Pagto: "+[Descricao CondPagto]
Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
Print File ARQIMP "   CD: "+[Composicao de Dias]+"\n"

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

s1="Vencimentos....: "
s2="                 "
s3="                 "
KV=0
for K from 1 to 24

 if isblank(DATA[k])
    then quitloop
 endif    
 
 if kv=8
    then Print File ARQIMP s1+"\n"
         Print File ARQIMP s2+"\n"
         Print File ARQIMP s3+"\n"

         kv=0
         s1="                 "
         s2="                 "
         s3="                 "
 endif

 kv=kv+1
 s1=s1+spaces(08)+FPGT[K]          +Spaces(02)
 s2=s2+Format("D13",DATA[k])       +Spaces(02)
 s3=s3+Format("W10.2,EIC",VALOR[k])+Spaces(02)

endfor
if kv>0
  then Print File ARQIMP s1+"\n"
       Print File ARQIMP s2+"\n"
       Print File ARQIMP s3+"\n"

       kv=0
       s1=""
       s2=""
       s3=""
endif

Print File ARQIMP "\027F"+"\n"

;************************* 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"
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()+"Ro_0233.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf

Array  DATA[24]
Array VALOR[24]
Array FPGT [24]

for K from 1 to 24
   DATA[k]=blankdate()
  VALOR[k]=blanknum()
   FPGT[K]=""
endfor

K=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]=[Data ]
  VALOR[K]=[Valor]
   FPGT[K]=[Forma de Pagamento]
  
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile
Moveto "Faturac"

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)
IMPFAT()

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

Release Vars VALOR,DATA,FPGT,K,KV
Release Procs IMPFAT,VENC
LIMPAGUARDE()


