;** Pd_0419 ********************************************************************
;** Imprime a Digitacao do Pedido                            ** STUDIO TEXTIL **
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 07/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 impressÆo dos Itens                                  *
;*******************************************************************************
Proc Checa_Linha(Nl)
Private nl,TB_ANT,TEMP
TB_ANT=Table()

TEMP=False
If (CONTADOR+nl)>(PARAM_NITENS)
   Then RodaPe(True)
        Cabecalho()
        TEMP=True     
EndIf
CONTADOR=CONTADOR+nl
If TB_ANT<>Table() Then Moveto TB_ANT EndIf

Return TEMP

EndProc ;(Checa_Linha)

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc Cabecalho()    
private regitens, regsdestq, Desccomp, ctmedio, s1, s2, s3, kv

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

Moveto "PedidoC"

Print File ARQIMP CI_15CPP
Print File ARQIMP "\n"
Print File ARQIMP fill("-",132)+"\n"
Print File ARQIMP Format("W40,AL",NOMEMPRESA)+Spaces(15)+"Pedido de Venda"+Spaces(32)+"Impressao: "+Format("D13",Today())+"-"+Time()+ "\n"
Print File ARQIMP fill("-",132)+"\n\n"

Print File ARQIMP "Cliente: "
Print File ARQIMP Format("W05,AL",[Codigo])
Print File ARQIMP Format("W97,AL",[Razao Social])
Print File ARQIMP "N§ do Pedido : "+Format("W06.3,AL",[Numero do Pedido])+"\n"

Print File ARQIMP Spaces(14)+Format("W40,AL",[Endereco])
Print File ARQIMP Format("W18,AL",[CGC])+Spaces(07)+"Emissao: "
Print File ARQIMP Format("D13",[Data de Emissao])+"\n"

Print File ARQIMP Spaces(14)+Format("W20,AL",[Bairro])
Print File ARQIMP Format("W20,AL",[CEP])
Print File ARQIMP Format("W25,AL",[IE])
Print File ARQIMP "Entrega: "+Format("D13",[Data de Entrega])+"\n"

Print File ARQIMP Spaces(14)+Format("W20,AL",[Cidade])
Print File ARQIMP Format("W20,AL",[UF])
Print File ARQIMP Format("W25,AL",[Telefone])
Print File ARQIMP "Empresa: "+Format("W05,AL",[Codigo da Empresa])+Format("W20,AL",[Nome Fantasia Empresa])+"\n\n"

Print File ARQIMP "Representante: "+Format("W05,AL",[Codigo do Representante])
Print File ARQIMP Format("W25,AL",[Nome Fantasia Represent])
Print File ARQIMP "Destinacao: "+Format("W04,AL",[Codigo da Destinacao])
Print File ARQIMP Format("W25,AL",[Descricao da Destinacao])
Print File ARQIMP "CFO: "+Format("W09,AL",[CFO])
Print File ARQIMP "TabPreco: "+Format("W06,AL",[Codigo da Tabela])+"\n"

Print File ARQIMP "Transportador: "+Format("W05,AL",[Codigo da Transportadora])
Print File ARQIMP Format("W28,AL",[Nome Fantasia Transport])
Print File ARQIMP "Veiculo: "+Format("W08,AL",[Placa do Veiculo])+"-"+Format("W08,AL",[UF Veiculo])
Print File ARQIMP "Frete por Conta: "+Format("W15,AL",[Frete por Conta])+"\n\n"

;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP "Sq  Item                                                           Desenho   "
Print File ARQIMP "   Quantidade Un PrUnitario    PrTotal   CtMedio PV-CM% "+"\n"
Print File ARQIMP "--- -------------------------------------------------------------- ----------"
Print File ARQIMP "-- ---------- -- ---------- ---------- --------- ------"+"\n"

mesref = AlinhM(StrVal(Month([PedidoC->Data de Emissao])),2)
anoref = StrVal(Year([PedidoC->Data de Emissao]))

CONTADOR = 15

EndProc ;(Cabecalho)

;*******************************************************************************
;*                                                                             *
;*******************************************************************************
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

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

Moveto "Answer"

refant=[Referencia]
TOTREF=0
TOTGER=0
Cont=1
Scan

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

   Checa_Linha(1)
   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  
   
   getrecord "\\Sistemas\\Estoque\\SdEstq" [PedidoC->Codigo da Empresa],[Answer->Codigo do Item],anoref,mesref to regsdestq
   if Not retval
      then ctmedio  =0
      else ctmedio  =regsdestq["Custo Total Atual"]/regsdestq["Saldo Atual"]
  endif
   
  nl=len(Desccomp)/41
  if (len(Desccomp)/41) > int(nl) or nl=0
     then nl=int(nl)+1
  endif
  
  posi=1
  s=Format("W03,AR",strval([Item do Pedido]))+" "+Format("W10,AL",[Referencia])+Format("W10,AL",[Codigo do Item])+Spaces(1)
  for ii from 1 to nl
    Checa_Linha(1)
    Moveto "Answer"
    s=s+Format("W42,AL",Substr(Desccomp,posi,41))
    posi=posi+41
    if Substr(Desccomp,posi,41)<>""
       then Print File ARQIMP s+"\n"
            s=Spaces(25)
    endif    
  endfor
  Print File ARQIMP s
  Print File ARQIMP Format("W12",Descricao_Cor)+Spaces(1)
  Print File ARQIMP Format("W10.1,EIC",[Quantidade])
  Print File ARQIMP Format("W03,AR",[Unidade Fiscal])
  Print File ARQIMP Format("W11.2,EIC",[Preco Unitario])
  Print File ARQIMP Format("W11.2,EIC",[Preco Total])
  Print File ARQIMP Format("W10.2,EIC",ctmedio)
  Print File ARQIMP Format("W07.1,EIC",100-((ctmedio/[Preco Unitario])*100))+"\n"
  Cont=Cont+1
  
  TOTREF=TOTREF+[Quantidade]
EndScan
SomarReferencia()

Checa_Linha(1)
Print File ARQIMP "\n"

TOTREF=TOTGER
refant=""
SomarReferencia()

;For ITEM From CONTADOR To PARAM_NITENS
;    Print File ARQIMP "\n"
;EndFor
  
EndProc ;(Itens)

;*******************************************************************************
;* ImpressÆo do Rodap‚                                                         *
;*******************************************************************************
Proc Rodape(QUEBRAFORM)
Private QUEBRAFORM, Vrfrete, lVrfrete,s1,s2,s3

;********************* IMPRESSAO DOS TOTAIS **************************
Moveto "PedidoC"
If Not QUEBRAFORM                                 
   Then If Not Checa_Linha(10) 
           Then Print File ARQIMP fill("-",132)+"\n"
        EndIf
        Print File ARQIMP "Mecadorias         Desconto     Liquido    Frete    Seguro   Desp.Aces         IPI       Total     BC.ICMS        ICMS"+"\n"
        ;;9.999.999,99888.88%777.777,776.666.666,6644.444,44111.111,112.222.222,223.333/333,334.444.444,445.555.555,551.111.111,11
        Print File ARQIMP Format("W10.2,EIC",[Valor Mercadoria])
        Print File ARQIMP Format("W06.2,EIC",[Taxa de Desconto])+"%"
        Print File ARQIMP Format("W10.2,EIC",[Valor do Desconto])
        Print File ARQIMP Format("W12.2,EIC",[Valor Mercadoria Liquido])
        Print File ARQIMP Format("W09.2,EIC",[Valor do Frete])
        Print File ARQIMP Format("W10.2,EIC",[Valor do Seguro])
        Print File ARQIMP Format("W012.2,EIC",[Valor Despesas Acessorias])
        Print File ARQIMP Format("W012.2,EIC",[Valor IPI])
        Print File ARQIMP Format("W012.2,EIC",[Valor Nota Fiscal])
        Print File ARQIMP Format("W012.2,EIC",[Valor BCICMS])
        Print File ARQIMP Format("W012.2,EIC",[Valor ICMS])+"\n\n"
   Else Print File ARQIMP fill("-",132)+"\n"
        Print File ARQIMP "\n"+"Continua ->"+"\n"
        Print File ARQIMP "\n"
EndIf

Print File ARQIMP "Cond. Pagamento.: "+[Descricao CondPagto]
Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
Print File ARQIMP "   CD: "+[Composicao de Dias]
Print File ARQIMP "   N.Volumes.: "+LTrim(Format("W06,EIC",[PedidoC->Numero de Volumes]))
Print File ARQIMP "   Peso Liquido: "+LTrim(Format("W11.3,EIC",[PedidoC->Peso Liquido]))
Print File ARQIMP "   Peso Bruto: "+LTrim(Format("W11.3,EIC",[PedidoC->Peso Bruto]))+"\n"

;************************ IMPRESSAO DOS VENCIMENTOS ****************************
s1="Vencimentos.....: "
s2="                  "
s3="                  "
KV=0
for K from 1 to 36

 if isblank(DATA[k])
    then quitloop
 endif    
 
 if kv=10
    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 "\n"

;************************* IMPRESSAO DO RODAPE ********************************
If Not QUEBRAFORM      
   Then Moveto "PedidoC"
        Print File ARQIMP "Obs. do Pedido..: "+Format("W110,AL",SubStr([Observacao do Pedido],001,110))+"\n"
        If Substr([Observacao do Pedido],111,110)<>""
           Then Print File ARQIMP Spaces(18)+Format("W110,AL",SubStr([Observacao do Pedido],111,110))+"\n"
        EndIf
        
        Print File ARQIMP "Obs. Nota Fiscal: "+Format("W100,AL",SubStr([Observacao na Nota Fiscal],001,100))+"\n\n"
        
        Print File ARQIMP "Local p/ Entrega: "+Format("W20,AL",[Endereco de Entrega])+"\n"
        Print File ARQIMP Spaces(18)+Format("W15,AL",[Bairro de Entrega])+Format("W11,AL",[CEP de Entrega])
        Print File ARQIMP Spaces(40)+"_______________________________________________"+"\n"
        Print File ARQIMP Spaces(18)+Format("W15,AL",[Cidade de Entrega])+Format("W03,AL",[UF de Entrega])
        Print File ARQIMP Spaces(59)+"Data/Assinatura Responsavel"+"\n\n"
        Print File ARQIMP fill("-",132)      
EndIf

If CONTA_PAGINA=1
   Then CONTA_PAGINA = CONTA_PAGINA + 1
        Print File ARQIMP "\n"
   Else Print File ARQIMP "\f"
        CONTA_PAGINA = 1
EndIf   
        
EndProc

;*******************************************************************************
;* Rotina de Impressao dos Vencimentos                                         *
;*******************************************************************************
Proc VENC()
If RecNo()>9
   Then Print File ARQIMP  Spaces(20)
EndIf
For I From 1 to K
  Print File ARQIMP Spaces(03)+DATA[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(25)
For I From 1 to K
  Print File ARQIMP VALOR[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"
EndProc

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

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

CONTA_PAGINA = 1

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
Switch
  Case K > 30 : NLINHA = 15
  Case K > 20 : NLINHA = 10
  Case K > 10 : NLINHA = 5
  OtherWise   : NLINHA = 0
EndSwitch 
                
PARAM_NITENS = 80 - NLINHA  ;20     

{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(False) 

ImpriArq(ARQIMP)

Release Vars PARAM_NITENS,VALOR,DATA,FPGT,K,TOTREF,TOTGER
Release Procs Checa_Linha,Cabecalho,Itens,Rodape,VENC,SomarReferencia
