;** VN_0501 ********************************************************************
;*  Grafico de Ped.Vendas Diario                                               *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 14/06/2019 por Anivel Augusto Rodrigues
;Ultima Alteracao: 12/09/2019 por Anivel Augusto Rodrigues      
;Motivo Alteracao: Ajustes filewrite inicial
;Ultima Alteracao: 23/10/2019 por Anivel Augusto Rodrigues  
;Motivo Alteracao: Ajustes mensagem

;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc VN_0501_SelecionaDados()
ClearAll

{Ask} TypeIn "\\Sistemas\\Vendas\\PedidoC" Enter
Moveto [Codigo da Empresa      ]       TypeIn "~VSDGEMPRESA.."
Moveto [Data de Emissao        ] check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
Moveto [Descricao da Destinacao]       TypeIn "..VEND.. Or ..SAIDA.. Or ..SERV.."
Moveto [Situacao do Pedido     ]       TypeIn ".."
Moveto [Valor Nota Fiscal      ]       TypeIn "Calc Sum as Valor Total"
Do_It!
ClearAll
VN_0501_AdicionaDados("VENDAS")

EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc VN_0501_AdicionaDados(tipo)
private tipo
ClearAll

View tabeladados
Edit "Answer"
Scan
   If IsBlank([Data de Emissao]) Then Loop EndIf

   Moveto tabeladados
   Moveto [Tipo]
   Locate tipo, [Answer->Data de Emissao]
   If retVal
      Then [Valor  ]=[Valor  ]+[Answer->Valor Total]
   EndIf

   Moveto "Answer"
EndScan
Do_It!
ClearAll

EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc VN_0501_CriaRegistros()
private DIA, wi, wc

DIA=DAY(VSDGDTFIM)
Edit tabeladados
For wi From 1 to DIA
    End Down
    [Tipo   ]="VENDAS"
    [Data   ]=DateVal(VSDGMES+"/"+strval(wi)+"/"+VSDGANO)
    [Extenso]=StrVal(wi)+"/"+substr(MesExt(numval(VSDGMES)),1,3)
    [Valor  ]=0
EndFor
Do_It!
ClearAll

EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc VN_0501_GravaCsv()
private s, arquivo, arqnew, inicio, NR, vartemp

Aguarde("Aguarde, Exportando Ped.Vendas Di rias")

ClearAll

arquivo=VSDGPASTA+"tmp0000.txt"
arqnew =VSDGPASTA+"VN_0501.csv"
         
NR=NRecords(tabeladados)
inicio=true

FileWrite        arquivo from "label,bar,value\n"    

Edit tabeladados 
Scan
  Barra(NR, RecNo())
  AGUARDE(BARRA_STRING)

  s=  monta_string([Tipo    ])+","
  s=s+monta_string([Extenso ])+","
  s=s+strval([Valor         ])+"\n"

  FileWrite append arquivo from s    
  
EndScan
Do_It!
ClearAll

FileRead  arquivo to   vartemp
FileWrite arqnew  from vartemp    

LimpAguarde()

EndProc

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************

tabeladados=Cria_Temp(PrivDir())
cria_tabela(tabeladados,"Tipo(A20*),Data(D*),Extenso(A10),Valor(N)")

VN_0501_CriaRegistros()
VN_0501_SelecionaDados()
VN_0501_GravaCsv()

Delete tabeladados

ClearAll
Release Vars tabeladados
Release Procs VN_0501_SelecionaDados, VN_0501_AdicionaDados, VN_0501_CriaRegistros, VN_0501_GravaCsv
