;** PD_0415G *******************************************************************
;** Imprime a Digitacao do Pedido/Grade c/preco                                *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido  em: 26/04/2018 por Bruno Carvalho            
;Alterado em     :   /  /     por 
;Motivo Alteracao:

;*******************************************************************************
;* Verifica Vencimentos do Pedido                                              *
;*******************************************************************************
Proc Verifica_Vencimentos()
Private wc

DynArray array_dtvencto[]
DynArray array_vrvencto[]
wc=0

If Not IsEmpty("PedidoV")
   Then Moveto "PedidoV"
        Moveto [Numero do Pedido]
        Locate [PedidoC->Numero do Pedido]
        While [Numero do Pedido]=[PedidoC->Numero do Pedido]
          wc=wc+1
          array_dtvencto[wc]=Format("D13",[Data])
          array_vrvencto[wc]=Format("W10.2,EIC",[Valor])
          Skip
          If EOT() Then QuitLoop EndIf
        EndWhile
EndIf

Release Vars wc
EndProc

;*******************************************************************************
;* Imprime Vencimentos do Pedido                                               *
;*******************************************************************************
Proc Imprime_Vencimentos()
Private tit_vencto,linha_dtvencto,linha_vrvencto,wi,wc

tit_vencto    ="Vencimentos.....: "
linha_dtvencto=""
linha_vrvencto=""
wi=0
For wc From 1 to DynArraySize(Array_dtvencto)
  wi=wi+1

  linha_dtvencto=linha_dtvencto+array_dtvencto[wc]+Spaces(02)
  linha_vrvencto=linha_vrvencto+array_vrvencto[wc]+Spaces(02)

  If wi=9
     Then Checa_Linha(2,"")
          Print File ARQIMP tit_vencto+linha_dtvencto+"\n"
          Print File ARQIMP Spaces(18)+linha_vrvencto+"\n"

          wi            =0
          tit_vencto    =Spaces(18)
          linha_dtvencto=""
          linha_vrvencto=""
  EndIf
EndFor
If wi>0
   Then Checa_Linha(2,"")
        Print File ARQIMP tit_vencto+linha_dtvencto+"\n"
        Print File ARQIMP Spaces(18)+linha_vrvencto+"\n"
EndIf

Release Vars tit_vencto,linha_dtvencto,linha_vrvencto,wi,wc
EndProc

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

tamobs=112
For wc From 1 To 2
	Switch
    Case wc=1 : obs="Obs. do Pedido..: "+Tira_QuebraLinha([PedidoC->Observacao do Pedido     ])
    Case wc=2 : obs="Obs. Nota Fiscal: "+Tira_QuebraLinha([PedidoC->Observacao na Nota Fiscal])
	EndSwitch
  
  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

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

    posobs =posobs+tamobs
	EndFor
EndFor

EndProc

;*******************************************************************************
;* Verifica as Grades no Pedido                                                *
;*******************************************************************************
Proc Verifica_Grades()
Private wc,gok

DynArray Array_Grade[]

Moveto "Pedidog"
Moveto [Numero do Pedido]
Locate [Pedidoc->Numero do Pedido]
While [Numero do Pedido]=[Pedidoc->Numero do Pedido]
  gok=False
  For wc From 1 to DynArraySize(Array_Grade)
    If Array_Grade[wc]=[Codigo da Grade]
       Then gok =True
    EndIf
  EndFor
  If Not gok
     Then Array_Grade[DynArraySize(Array_Grade)+1]=Completa([Codigo da Grade],1," ")
  EndIf

  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

Array array_tittm[12]
Moveto "\\Sistemas\\Estoque\\TpGrade"
Moveto [Codigo da Grade]
For wc From 1 to DynArraySize(Array_Grade)
  Locate SubStr(Array_Grade[wc],1,1)
  If RetVal
     Then array_tittm[1] =[Titulo Grade1 ]
          array_tittm[2] =[Titulo Grade2 ]
          array_tittm[3] =[Titulo Grade3 ]
          array_tittm[4] =[Titulo Grade4 ]
          array_tittm[5] =[Titulo Grade5 ]
          array_tittm[6] =[Titulo Grade6 ]
          array_tittm[7] =[Titulo Grade7 ]
          array_tittm[8] =[Titulo Grade8 ]
          array_tittm[9] =[Titulo Grade9 ]
          array_tittm[10]=[Titulo Grade10]
          array_tittm[11]=[Titulo Grade11]
          array_tittm[12]=[Titulo Grade12]
     Else For i From 1 to 12
            array_tittm[i]=""
          EndFor
  EndIf

  Array_Grade[wc]=Array_Grade[wc]+"Ý"+
                  Format("W3,AR",array_tittm[01])+"Ý"+
                  Format("W3,AR",array_tittm[02])+"Ý"+
                  Format("W3,AR",array_tittm[03])+"Ý"+
                  Format("W3,AR",array_tittm[04])+"Ý"+
                  Format("W3,AR",array_tittm[05])+"Ý"+
                  Format("W3,AR",array_tittm[06])+"Ý"+
                  Format("W3,AR",array_tittm[07])+"Ý"+
                  Format("W3,AR",array_tittm[08])+"Ý"+
                  Format("W3,AR",array_tittm[09])+"Ý"+
                  Format("W3,AR",array_tittm[10])+"Ý"+
                  Format("W3,AR",array_tittm[11])+"Ý"+
                  Format("W3,AR",array_tittm[12])+"Ý"
EndFor
Moveto "Pedidog"

Release Vars wc,wi,gok,array_tittm
EndProc

;*******************************************************************************
;* Rotina para Impressao do Cabecalho titulo                                   *
;*******************************************************************************
Proc Cabecalho_Tit()
npag=npag+1

If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf

Moveto "PedidoC"
Print File ARQIMP CI_6LPP+CI_15CPP+"\n"
Print File ARQIMP Fill("-",136)+"\n"
Print File ARQIMP  Completa(NOMEMPRESA,55," ")+"Pedido de Venda"+Spaces(22)+"Impressao: "+Format("D13",Today())+"-"+Time()+
                        "  Pag: "+Format("W3,EIC",npag)+"\n"
Print File ARQIMP Fill("-",136)+"\n"

Print File ARQIMP Spaces(99)+"N§ do Pedido : "+CI_INEG+CI_IEXP+Format("W06.3,AL",[Numero do Pedido])+CI_FNEG+CI_FEXP+"\n\n"

nlin=6
EndProc

;*******************************************************************************
;* Rotina para Impressao do Cabecalho do Pedido                                *
;*******************************************************************************
Proc Cabecalho_Ped()

Cabecalho_Tit()
Moveto "PedidoC"

Print File ARQIMP "Cliente: "
Print File ARQIMP Format("W05,AL",[Codigo])
Print File ARQIMP Format("W88,AL",[Razao Social])
Print File ARQIMP "Ped.Origem: "+[Numero Pedido Origem]+"\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])+"      Tabela: "+[Codigo da Tabela]+"\n"

Switch
  Case [Frete por Conta]="0" : FRETECONTA="SEM FRETE"
  Case [Frete por Conta]="1" : FRETECONTA="EMITENTE"
  Case [Frete por Conta]="2" : FRETECONTA="DESTINATARIO"
  Case [Frete por Conta]="3" : FRETECONTA="TERCEIROS"
EndSwitch

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: "+[Frete por Conta]+" "+FRETECONTA+"\n\n"

nlin=nlin+8

Cabecalho_Item()
EndProc

;*******************************************************************************
;* Rotina para Impressao do Cabecalho do Item                                  *
;*******************************************************************************
Proc Cabecalho_Item()
Private wc,linha

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

Moveto "PedidoG"
For wc From 1 to DynArraySize(Array_Grade)
  If wc=DynArraySize(Array_Grade)
     Then linha=" SeqÝRef   ÝDescricao                                   Ý"+Array_Grade[wc]+" TotalÝUNÝ  PrUnitÝ   PrTotal"
     Else linha="    Ý      Ý                                            Ý"+Array_Grade[wc]+"      Ý  Ý        Ý          "
  EndIf
  Print File ARQIMP linha+"\n"
  nlin=nlin+1
EndFor

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

nlin=nlin+2
EndProc

;*******************************************************************************
;* Rodape para Itens                                                           *
;*******************************************************************************
Proc Rodape(tp)
Private tp
If tp="Item"
   Then Print File ARQIMP " ---------------------------------------------------------------------------------------------------------------------------------------"+"\n"
Endif
Print File ARQIMP "\n"
Print File ARQIMP Format("W132,AR","*** Continua proxima pagina ==> ")+"\f"+"\n"
EndProc

;*******************************************************************************
;* Checa linha na area de impressao dos itens                                  *
;*******************************************************************************
Proc Checa_Linha(nl,tp)
private nl,tp

If (nlin+nl)>=62
   Then Rodape(tp)
        Cabecalho_Tit()
        If tp="Item"
           Then Cabecalho_Item()
        EndIf
EndIf
nlin=nlin+nl
EndProc


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

npag=0
nlin=0

Array array_tqtd[14]  ;** controla total   13=total pecas  14=total valor **
For wi From 1 to 14
  array_tqtd[wi]=0
EndFor

Verifica_Vencimentos()
Verifica_Grades()
Cabecalho_Ped()

Moveto "Pedidog"
Moveto [Numero do Pedido]
Locate [Pedidoc->Numero do Pedido]
While [Numero do Pedido]=[Pedidoc->Numero do Pedido]

   Checa_Linha(1,"Item")

   Print File ARQIMP Format("W04,AR",StrVal([Item do Pedido]))+"Ý"
   Print File ARQIMP Format("W06,AL",[Referencia])+"Ý"
   Print File ARQIMP Format("W23,AL",[Descricao do Item])+"/"+Format("W20,AL",[Descricao da Cor_Tecido])+"Ý"
   Print File ARQIMP Format("W01,AL",[Codigo da Grade])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd1])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd2])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd3])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd4])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd5])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd6])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd7])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd8])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd9])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd10])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd11])+"Ý"
   Print File ARQIMP Format("W03,EIC",[Qtd12])+"Ý"
   Print File ARQIMP Format("W06,EIC",[Qtd1]+[Qtd2 ]+[Qtd3 ]+[Qtd4 ]+
                                      [Qtd5]+[Qtd6 ]+[Qtd7 ]+[Qtd8 ]+
                                      [Qtd9]+[Qtd10]+[Qtd11]+[Qtd12])+"Ý"
   Print File ARQIMP Format("W02,AL",[Unidade Fiscal])+"Ý"
   Print File ARQIMP Format("W08.2,EIC",[Preco Unitario])+"Ý"
   Print File ARQIMP Format("W10.2,EIC",[Preco Total])+"\n"

   array_tqtd[01]=array_tqtd[01]+[Qtd1 ]
   array_tqtd[02]=array_tqtd[02]+[Qtd2 ]
   array_tqtd[03]=array_tqtd[03]+[Qtd3 ]
   array_tqtd[04]=array_tqtd[04]+[Qtd4 ]
   array_tqtd[05]=array_tqtd[05]+[Qtd5 ]
   array_tqtd[06]=array_tqtd[06]+[Qtd6 ]
   array_tqtd[07]=array_tqtd[07]+[Qtd7 ]
   array_tqtd[08]=array_tqtd[08]+[Qtd8 ]
   array_tqtd[09]=array_tqtd[09]+[Qtd9 ]
   array_tqtd[10]=array_tqtd[10]+[Qtd10]
   array_tqtd[11]=array_tqtd[11]+[Qtd11]
   array_tqtd[12]=array_tqtd[12]+[Qtd12]

   array_tqtd[14]=array_tqtd[14]+[Preco Total]

   Skip
   If EOT() Then QuitLoop EndIf
EndWhile

;************** IMPRESSAO DOS TOTAIS DAS GRADES DO PEDIDO ****************
For wi From 1 to 12
  If array_tqtd[wi]=0 then array_tqtd[wi]=BlankNum() EndIf
  array_tqtd[13]=array_tqtd[13]+array_tqtd[wi]
EndFor

Checa_Linha(3,"")
Print File ARQIMP " ---------------------------------------------------------+---+---+---+---+---+---+---+---+---+---+---+---+------+-----------+----------"+"\n"

Print File ARQIMP Spaces(58)+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[01])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[02])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[03])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[04])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[05])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[06])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[07])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[08])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[09])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[10])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[11])+"Ý"
Print File ARQIMP Format("W03,EIC",array_tqtd[12])+"Ý"
Print File ARQIMP Format("W06,EIC",array_tqtd[13])+"Ý"
Print File ARQIMP Spaces(11)+"Ý"
Print File ARQIMP Format("W10.2,EIC",array_tqtd[14])+"\n"

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


;***************** IMPRESSAO DOS TOTAIS DO PEDIDO  ***************
Moveto "PedidoC"

Checa_Linha(3,"")
Print File ARQIMP CI_6LPP+CI_15CPP+"\n"
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("W12.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"

Checa_Linha(1,"")
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 *******************************
Imprime_Vencimentos()

;********************* IMPRESSAO DO RODAPE *************************************
Imprime_Observacoes()

Checa_Linha(5,"")
Print File ARQIMP "Local p/ Entrega: "+Format("W20,AL",[PedidoC->Endereco de Entrega])+"\n"

Print File ARQIMP Spaces(18)+Format("W15,AL",[PedidoC->Bairro de Entrega])+Format("W11,AL",[PedidoC->CEP de Entrega])
Print File ARQIMP Spaces(40)+"______________________________________________"+"\n"

Print File ARQIMP Spaces(18)+Format("W15,AL",[PedidoC->Cidade de Entrega])+Format("W03,AL",[PedidoC->UF de Entrega])
Print File ARQIMP Spaces(60)+"Data/Assinatura Responsavel"+"\n\n"

Print File ARQIMP Fill("-",136)
Print File ARQIMP CI_10CPP
Print File ARQIMP "\f"          
EndProc




;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AGUARDE("Imprimindo Pedido -> "+PEDIDO)

Seta_FilePrint("Pd_0222G")
Switch
	Case OK="A": ARQIMP = DIREPORT+ARQUIVO+".TXT"
EndSwitch


;****** Adicionando tabelas para trabalho **************************************
View NoWindow "\\Sistemas\\Estoque\\TpGrade"
View "Pedidog"

CoEditKey

{TableAdd} {PedidoV}
;*******************************************************************************


ImpPed()
ImpriArq(ARQIMP)


;****** Liberando tabelas de trabalho ******************************************
Do_It!
Moveto "PedidoV" ClearImage

;*******************************************************************************

LimpAguarde()

Release Vars  nlin,npag,array_vrvencto,array_dtvencto,array_grade,
              array_tqtd

Release Procs Seta_FilePrint,ImpPed,ImpriArq,
              Cabecalho_Ped,Cabecalho_Item,Cabecalho_Tit,Rodape,
              Imprime_Vencimentos,Imprime_Observacoes,
              Checa_Linha,
              Verifica_Vencimentos,Verifica_Grades

