;** PD_0365G *******************************************************************
;** Imprime a Digitacao do Pedido                 ** Fundi‡Æo SÆo Pedro **     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 25/03/2015 por Andrey Fillipe
;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,tit_obs

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

    tit_obs=Spaces(18)
    posobs =posobs+tamobs
	EndFor
EndFor

Release Vars obs,tit_obs,wi,wc,tamobs,nlobs,posobs,tit_obs
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("-",132)+"\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("-",132)+"\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"
Print File ARQIMP Spaces(99)+"PED/Original : "+CI_INEG+CI_IEXP+Format("W06.3,AL",[Numero Pedido Origem])+CI_FNEG+CI_FEXP+"\n\n"
nlin=7
EndProc

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

Moveto "PedidoC"
Cabecalho_Tit()

Print File ARQIMP "Cliente: "
Print File ARQIMP Format("W05,AL",[Codigo])
Print File ARQIMP Format("W97,AL",[Razao Social])+"\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])+"\n\n"
;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"

nlin=nlin+8

Cabecalho_Item()
EndProc

;*******************************************************************************
;* Rotina para Impressao do Cabecalho do Item                                  *
;*******************************************************************************
Proc Cabecalho_Item()

Print File ARQIMP 
  "Item                                                                                               Un Quantidade Unitario      Total"+"\n"
Print File ARQIMP         
  "-------------------------------------------------------------------------------------------------- -- ---------- -------- ----------"+"\n"

nlin=nlin+2
EndProc


;*******************************************************************************
;* Rodape para Itens                                                           *
;*******************************************************************************
Proc Rodape(tp)
Private tp
If tp="Item"
   Then Print File ARQIMP Fill("-",132)+"\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,tb

tb=Table()

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

Moveto tb
EndProc


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

npag=0
nlin=0
SeqItPed=0

Verifica_Vencimentos()
Cabecalho_Ped()

Moveto "PedidoI"
Moveto [Numero do Pedido]
Locate PEDIDO
If Retval Then
While [Numero do Pedido] = PEDIDO
   Checa_Linha(2,"Item")

  ;SeqItPed=SeqItPed+1

  ;Print File ARQIMP Format("W04,AR",StrVal(SeqItPed))+" "

   Print File ARQIMP Format("W04,AR",StrVal([Item do Pedido]))+" "
   Print File ARQIMP Spaces(01)+Format("W11,AL",[Codigo do Item])
   Print File ARQIMP Spaces(01)+Format("W45,AL",[Descricao do Item])
   ;Print File ARQIMP Spaces(21)                                           
   ;Print File ARQIMP Spaces(02)+Format("W05,AR",Substr([Descricao do Item],31,5))
   ;Print File ARQIMP Spaces(01)+Format("W20,AL",[Descricao da Cor_Tecido])
   Print File ARQIMP Spaces(35)+Format("W03,AR",[Unidade Fiscal])
   Print File ARQIMP Format("W11.3,EIC",[Quantidade])
   Print File ARQIMP Format("W09.2,EIC",[Preco Unitario])
   Print File ARQIMP Format("W11.2,EIC",[Preco Total])+"\n"

   If Not IsBlank([Observacao_Item])
      Then TamObs=80
           nlobs=Len([Observacao_Item])/tamobs
           If nlobs-Int(nlobs)>0
              Then nlobs=Int(nlobs)+1
              Else nlobs=Int(nlobs)
           EndIf
           
           posobs=1
           For i from 1 to nlobs
            Checa_Linha(1,"Item")
            Print File ARQIMP Spaces(18)+SubStr([Observacao_Item],posobs,tamobs)+"\n"
            posobs=posobs+tamobs
           EndFor
   EndIf
   
   Print File ARQIMP Fill("-",132)+"\n"
   Skip
   If EOT() Then QuitLoop EndIf
EndWhile
EndIf

;***************** 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("-",132)
Print File ARQIMP CI_10CPP
Print File ARQIMP "\f"
EndProc

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

Seta_FilePrint("PD_0365G")

View "PedidoC"
View "PedidoI"
View "PedidoV"

Moveto "PedidoC"
Moveto [Numero do Pedido]
Locate PEDIDO
IMPPED()
ImpriArq(ARQIMP)

Release Vars  nlin,npag,array_vrvencto,array_dtvencto
              

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