;** PD_0419S *******************************************************************
;** Imprime a Digitacao do Pedido                            ** STUDIO TEXTIL **
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido  em: 04/08/2018 por Bruno Carvalho
;Ultima Alteracao: 10/08/2018 por Anivel Augusto Rodrigues  
;Motivo Alteracao: Ajustes quebra por referencia e totais        

;*******************************************************************************
;* Checa linha na area de impressao dos itens                                  *
;*******************************************************************************
Proc Checa_Linha(param)
private param

If (CONTADOR+param)>(PARAM_NITENS)
   Then Rodape()
        Cabecalho()
        Moveto "Answer"     
EndIf
CONTADOR=CONTADOR+param

EndProc

;*******************************************************************************
;*                                                                             *
;*******************************************************************************
Proc SomarReferencia()

Checa_Linha(1)
Print File ARQIMP spaces(50)+Ci_INEG+Format("W30,AL","Total "+refant+".. ")
Print File ARQIMP Format("W10.1,EIC",TOTREF)+CI_FNEG+"\n"

TOTGER=TOTGER+TOTREF

refant=[Referencia]
TOTREF=0
endproc

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc Cabecalho();
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf
Moveto "PedidoC"
Print File ARQIMP chr(27)+chr(67)+chr(33) ;Imprime 33 Linhas por pagina
Print File ARQIMP CI_10CPP
Print File ARQIMP CI_8LPP

DATA=StrVal([Data de Emissao])
If Len(DATA)=9 Then DATA="0"+DATA EndIf
DATA = SubStr(DATA,1,2) +"/"+ SubStr(DATA,4,2) +"/"+ SubStr(DATA,7,4)

Print File ARQIMP "\015"          ;Imprime 17 Caracteres por polegada
Print File ARQIMP Fill("-",130)+"\n"

Print File ARQIMP Spaces(57)+"Pedido Expedicao"+Spaces(16)+"EMISSAO: "+DATA+Spaces(2)+"ORCAMENTO N§: "+Format("W7,AL",[Numero do Pedido])+"\n"
Print File ARQIMP Fill("-",130)+"\n"
Print File ARQIMP "\027E"+" Cliente.: "+"\027F"+Format("W15,AL",[Razao Social])+"\n"

Print File ARQIMP "----------------------------------------------------------------------------------------------------------------------------------"+"\n"
Print File ARQIMP "\027E" + "Quantidade   UN  Codigo  Descricao                      Desenho     Composi‡Æo       O b s e r v a ‡ Æ o                " +"\027F"  +"\n"
Print File ARQIMP "----------------------------------------------------------------------------------------------------------------------------------"+"\n"

CONTADOR = 0

EndProc ;(Cabecalho)                           

;*******************************************************************************
;* ImpressÆo dos Itens                                                         *
;*******************************************************************************
Proc Itens()
private refant

Moveto "Answer"

refant=[Referencia]
TOTREF=0
TOTGER=0

CONTADOR = 0

Scan

   if [Referencia]<>refant
      then SomarReferencia()
   endif   


  CODITEM=[Codigo do Item]
  While SubStr(CODITEM,1,1)="0"
    CODITEM=SubStr(CODITEM,2,Len(CODITEM))
  EndWhile

  GetRecord "\\Sistemas\\Estoque\\Iten_tec" [Codigo do Item] to REG_ITEN_TEC
  If RetVal
    Then COMPOSICAO = REG_ITEN_TEC["Composicao"]
    Else COMPOSICAO = ""
  EndIf

  Descricao_Cor = [Descricao da Cor_Tecido]   ;Pegar Answer
  Desccomp      = [Descricao do Item]
  getrecord "\\Sistemas\\Estoque\\Itens" [Answer->Codigo do Item] to regitens
  If Not RetVal
     Then Desccomp      = ""
          Descricao_Cor = ""
     Else ;Descricao_Cor=regitens["Descricao da Cor_Tecido"]
          If Not IsBlank(regitens["Descricao Complementar"])
             Then Desccomp=regitens["Descricao do Item"] + " " + regitens["Descricao Complementar"]  
         EndIf          
  EndIf
  ;If Descricao_Cor<>""
  ;   Then If Desccomp<>""  
  ;           Then Desccomp = Desccomp + " " + Descricao_Cor
  ;           Else Desccomp = Descricao_Cor
  ;        EnDIf
  ;EndIf
 
  nl=len(Desccomp)/30
  if (len(Desccomp)/30) > int(nl) or nl=0
     then nl=int(nl)+1
  endif
  
  Checa_Linha(nl+1)
  posi=1
  s=Format("W12.3,AL",[Quantidade])+" "+Format("W03,Al",[Unidade Fiscal])+" "+Format("W06,AR",CODITEM)+"  "
  for ii from 1 to nl  
    Moveto "Answer"
    s=s+Format("W30,AL",Ltrim(Rtrim(Substr(Desccomp,posi,30))))
    posi=posi+30
    if Substr(Desccomp,posi,30)<>""
       then Print File ARQIMP s + "\n"
            s=Spaces(25)
    endif    
  endfor
  
  LINHA_ITEM =              s
  LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W11,AL",Descricao_Cor)
  LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W16,AL",COMPOSICAO)
  LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W35,AL",[Observacao_Item])
  
  Print File ARQIMP LINHA_ITEM + "\n"
  Print File ARQIMP Fill("-",130)+"\n"
  
  TOTREF=TOTREF+[Quantidade]
EndScan
SomarReferencia()

Checa_Linha(1)
Print File ARQIMP "\n"

TOTREF=TOTGER
refant=""
SomarReferencia()

Imprime_Observacoes()

;Apenas para meia folha
If PARAM_NITENS<=40
   Then For ITEM From CONTADOR To PARAM_NITENS
            Print File ARQIMP "\n"
        EndFor
EndIf          

EndProc ;(Itens)

;*** Imprime Rodape ************************************************************
Proc Rodape()

If CONTA_PAGINA=1
   Then CONTA_PAGINA = CONTA_PAGINA + 1
        If PARAM_NITENS > 40
           Then Print File ARQIMP "\f" 
           Else Print File ARQIMP "\n\n\n\n"
        EndIf   
   Else Print File ARQIMP "\f"
        CONTA_PAGINA = 1
EndIf   

EndProc ;Rodape

;*******************************************************************************
;* Imprime Observacoes do Pedido                                               *
;*******************************************************************************
Proc Imprime_Observacoes()
Private obs,tamobs,nlobs,posobs,i

obs="Observacoes: "+[PedidoC->Observacao do Pedido]

tamobs=130

nlobs=Len(obs)/tamobs
If nlobs-Int(nlobs)>0
	 Then nlobs=Int(nlobs)+1
	 Else nlobs=Int(nlobs)
EndIf
If nlobs=0 Then nlobs=1 EndIf

Checa_Linha(nlobs)
posobs=1
For i from 1 to nlobs
  Print File ARQIMP CI_INEG+SubStr(obs,posobs,tamobs)+CI_FNEG+"\n"
  posobs =posobs+tamobs
EndFor

Release Vars obs,tamobs,nlobs,posobs,i

EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AGUARDE("Imprimindo Pedido -> "+[Numero do Pedido])

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

CONTA_PAGINA = 1   

Moveto "PedidoC"

Array  DATA[36]
Array VALOR[36]
Array FPGT [36]

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

K=0
Moveto "PedidoV"
Scan
  K=K+1
 DATA[K]=[Data ]
VALOR[K]=[Valor]
 FPGT[K]=[Forma de Pagamento]  
EndScan
PARAM_NITENS = 80         

{Ask} {PedidoI} Check
Moveto [Numero do Pedido] TypeIn "~PEDIDO"
Do_It!
Sort "Answer" On "Referencia","Descricao do Item","Descricao da Cor_Tecido","Item do Pedido"
                
Cabecalho()
Itens()
Rodape()

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

Release Vars CONTADOR,PARAM_NITENS,DATA,LINHA_ITEM,COMPOSICAO,CODITEM,ITEM,TOTREF,TOTGER
Release Procs Cabecalho,Itens,Rodape,Checa_Linha,Imprime_Observacoes,SomarReferencia
LIMPAGUARDE()
