;** PDE10   ********************************************************************
;*  Exportacao de dados p/ PlugDash                                            *
;*******************************************************************************
;Desenvolvido  em: 11/06/2019 por Anivel Augusto Rodrigues
;Ultima Alteracao: 10/07/2019 por Anivel Augusto Rodrigues  
;Motivo Alteracao: Ajustes diretorio de trabalho
;Ultima Alteracao: 26/08/2019 por Anivel Augusto Rodrigues  
;Motivo Alteracao: Ajustes CX_0001
;Ultima Alteracao: 29/08/2019 por Anivel Augusto Rodrigues  
;Motivo Alteracao: Ajustes contas a pagar
;Ultima Alteracao: 03/09/2019 por Anivel Augusto Rodrigues  
;Motivo Alteracao: Ajustes fluxo de caixa
;Ultima Alteracao: 11/10/2019 por Anivel Augusto Rodrigues  
;Motivo Alteracao: Ajustes estoque
;Ultima Alteracao: 23/10/2019 por Anivel Augusto Rodrigues  
;Motivo Alteracao: Ajustes estoque, mensagem


;*******************************************************************************
;* Rotina Converte caracter especial e passa para numero                       *
;*******************************************************************************
Proc processaretapa(wi)
private wi

limparodape(0)
ExibeRodape(1,"Etapa "+format("W3,EIC",wi)+"/"+format("W3",strval(tetapas))+" "+rtrim(substr(arrayetapas[wi],42,80)))

play rtrim(substr(arrayetapas[wi],1,40))

EndProc

;*******************************************************************************
;* Rotina Converte caracter especial e passa para numero                       *
;*******************************************************************************
Proc monta_string(s)
private s, sn, i, ch

sn=""
For i from 1 to Len(s)
  ch=SubStr(s,i,1)
  Switch 
   Case Asc(ch)=34  : ch="'" ; ""
  EndSwitch

  sn=sn+ch
EndFor

Return ("\""+sn+"\"")

;;;Return Upper("\""+sn+"\"")
EndProc

;*******************************************************************************
;*  Rotina para mostrar as horas                                               *
;*******************************************************************************
Proc Tempohrs(tini)
private tini,hrs,seg,h,m,s, tfim

tfim=ticks()
if tfim<tini
   then seg=(86400000-tini)
        seg=(seg+tfim)/1000
   else seg=(tfim-tini)/1000
endif

h=seg/3600
m=(h-int(h))*60
s=(m-int(m))*60

hrs=AlinhM(StrVal(int(h)),2)+":"+
    AlinhM(StrVal(int(m)),2)+":"+
    AlinhM(StrVal(Round(s,0)),2)
return hrs

Endproc ; (Tempohrs)

;*******************************************************************************
;* Carregar Lista                                                              *
;*******************************************************************************
Proc AjustaPasta(s)
Private s,ns,i

ns=""
For i From 1 to Len(s)
 If SubStr(s,i,1)=Chr(92)
    Then ns=ns+Chr(92)
 EndIf
 ns=ns+SubStr(s,i,1)
EndFor
Return ns
EndProc

;************************************
;* ROTINA PARA Ler/Gravacao Opcao   *
;************************************
Proc ConfOpcoes(Opcao)
Private Opcao

Switch
  Case Opcao="Ler":
       If IsFile("PDE10.ini")
          Then LeiaINI("PDE10.ini","VSDGPASTA.S.c:\\pdash\\")
       EndIf
       If Not IsAssigned(VSDGPASTA)    Then VSDGPASTA="c:\\pdash\\" endif
  Case Opcao="Gravar":
       FileWrite "PDE10.ini" From GravaExpressao("VSDGPASTA","S",AjustaPasta(VSDGPASTA))
EndSwitch
EndProc ;

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
TECLA=""
MSG_CAMPO=""
If Disparo="OPEN"
   Then AoCriarShowDialog()
        TiraSombraShowDialog()
        
        Campo="TG_EMPRESAS"
EndIf

If Disparo="UPDATE"
   Then Switch
          Case Campo="TG_CONFIG" : 
                                   Play "\\sistemas\\ctarec\\setpasta"
                                   Campo="TG_EMPRESAS"
          
          OtherWise              : Return Temp
        EndSwitch  
EndIf


If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = -59: 
          If Campo="TG_NECF"
              Then If Not SDCarrega_F1()
                      Then MENSAGEM(MSG)
                      Else Seleciona_Opcoes("ECF","F1_Temp")
                   EndIf
              Else Dialog_F1()    
          EndIf        
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESAS"      : Campo="NO"
            Case Campo="TG_MESANO"        : Campo="TG_EMPRESAS"

            Case Campo="TG_CONFIG"        : Campo="TG_EMPRESAS"
            Case Campo="YES" Or Campo="NO": Campo="TG_EMPRESAS"
          EndSwitch
     Case (Evento["KeyCode"] <= -104 And Evento["KeyCode"] >= -113) Or 
           Evento["KeyCode"]  = -139 Or  Evento["KeyCode"]  = -140:
           SYS_TECLA_ACIONADA=Evento["KeyCode"]
           Seta_Tecla_Acionada()
           CancelDialog
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_EMPRESAS"   : Campo="TG_MESANO"
                    Case Campo="TG_MESANO"     : Campo="YES"

                    Case Campo="TG_CONFIG"      : 
                         Play "\\sistemas\\ctarec\\setpasta"
                         Campo="TG_EMPRESAS"

                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
 Case Campo="TG_EMPRESAS": MSG_CAMPO="Informe a Empresa"
 Case Campo="TG_CONFIG"  : MSG_CAMPO="Configurar caminho ViseDash"
 Case Campo="TG_OPCOES"  : MensagemCheckBoxes()
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp

EndProc ;SD_Dialog()

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
If Not IsAssigned(SYS_CODIGO_CLIENTE)  Then SYS_CODIGO_CLIENTE=""  EndIf

sys_valor()

LimpAguarde()


;-- defini‡Æo dos processos ----------------------------------------------------
dynarray arrayetapas[]

arrayetapas[01]=format("W40","\\Sistemas\\Mobilets\\SS_0000") +"|"+"Usuarios"
arrayetapas[02]=format("W40","\\Sistemas\\Mobilets\\SS_0001") +"|"+"Informativo"

arrayetapas[03]=format("W40","\\Sistemas\\Mobilets\\VN_0001") +"|"+"FaturamentoDiario"

arrayetapas[04]=format("W40","\\Sistemas\\Mobilets\\VN_0002") +"|"+"FaturamentoMensalValorNotaFiscal"
arrayetapas[05]=format("W40","\\Sistemas\\Mobilets\\VN_0002A")+"|"+"FaturamentoMensalValorMercadoria"
arrayetapas[06]=format("W40","\\Sistemas\\Mobilets\\VN_0002D")+"|"+"FaturamentoMensalDevolu‡äes"         
arrayetapas[07]=format("W40","\\Sistemas\\Mobilets\\VN_0002L")+"|"+"FaturamentoMensalVendas-Devolucoes"  
arrayetapas[08]=format("W40","\\Sistemas\\Mobilets\\VN_0003") +"|"+"FaturamentoFamilia"
arrayetapas[09]=format("W40","\\Sistemas\\Mobilets\\VN_0004") +"|"+"FaturamentoEstado"
arrayetapas[10]=format("W40","\\Sistemas\\Mobilets\\VN_0005") +"|"+"FaturamentoVendedor"
arrayetapas[11]=format("W40","\\Sistemas\\Mobilets\\VN_0006") +"|"+"FaturamentoFormaPagamento"
arrayetapas[12]=format("W40","\\Sistemas\\Mobilets\\VN_0007") +"|"+"FaturamentoCondicaoPagamento"
arrayetapas[13]=format("W40","\\Sistemas\\Mobilets\\VN_0100") +"|"+"FaturamentoIndicador"                

arrayetapas[14]=format("W40","\\Sistemas\\Mobilets\\VN_0110") +"|"+"FaturamentoABCClientes"               
arrayetapas[15]=format("W40","\\Sistemas\\Mobilets\\VN_0120") +"|"+"FaturamentoABCItens"               

arrayetapas[16]=format("W40","\\Sistemas\\Mobilets\\VN_0501") +"|"+"PedVendasDiario"
arrayetapas[17]=format("W40","\\Sistemas\\Mobilets\\VN_0502") +"|"+"PedVendasMensal"
arrayetapas[18]=format("W40","\\Sistemas\\Mobilets\\VN_0505") +"|"+"PedVendasVendedor"
arrayetapas[19]=format("W40","\\Sistemas\\Mobilets\\VN_0510") +"|"+"PedVendasIndicador"                
arrayetapas[20]=format("W40","\\Sistemas\\Mobilets\\VN_0520") +"|"+"PedVendasMetas"

arrayetapas[21]=format("W40","\\Sistemas\\Mobilets\\CX_0001") +"|"+"Conta Corrente"     
arrayetapas[22]=format("W40","\\Sistemas\\Mobilets\\CX_0002") +"|"+"ChequePredatados"
arrayetapas[23]=format("W40","\\Sistemas\\Mobilets\\CX_0003") +"|"+"Fluxodecaixa"   

arrayetapas[24]=format("W40","\\Sistemas\\Mobilets\\CT_0001") +"|"+"Chequeterceiros1"
arrayetapas[25]=format("W40","\\Sistemas\\Mobilets\\CT_0002") +"|"+"Chequeterceiros2"

arrayetapas[26]=format("W40","\\Sistemas\\Mobilets\\CR_0001") +"|"+"TitulosaReceber1"
arrayetapas[27]=format("W40","\\Sistemas\\Mobilets\\CR_0003") +"|"+"TitulosaReceber2"

arrayetapas[28]=format("W40","\\Sistemas\\Mobilets\\CP_0001") +"|"+"TitulosaPagar1"  
arrayetapas[29]=format("W40","\\Sistemas\\Mobilets\\CP_0003") +"|"+"TitulosaPagar2" 

arrayetapas[30]=format("W40","\\Sistemas\\Mobilets\\CL_0001") +"|"+"CarteiraClientes"
arrayetapas[31]=format("W40","\\Sistemas\\Mobilets\\CL_0002") +"|"+"CarteiraClientesComCompra"
arrayetapas[32]=format("W40","\\Sistemas\\Mobilets\\CL_0003") +"|"+"CarteiraClientesVendedor"
arrayetapas[33]=format("W40","\\Sistemas\\Mobilets\\CL_0004") +"|"+"CarteiraClientesGerencia"

arrayetapas[34]=format("W40","\\Sistemas\\Mobilets\\ES_0002") +"|"+"Saldo Estoque, EstoqueMinimo, ItemSemVenda.."           

arrayetapas[35]=format("W40","\\Sistemas\\Mobilets\\VN_0008") +"|"+"FaturamentoGerencia"           
arrayetapas[36]=format("W40","\\Sistemas\\Mobilets\\VN_0508") +"|"+"PedVendasGerencia"

;-------------------------------------------------------------------------------


ConfOpcoes("Ler")
VSDGEMPRESA  =""
VSDGMESANO   = AlinhM(StrVal(Month(Today())),2)+"."+AlinhM(StrVal(Year(Today())),4)
VSDGDTINI    = BlankDate()
VSDGDTFIM    = BlankDate()


;;VSDGEMPRESA="0001"
;;VSDGMESANO   = "05.2019"


While True
 ShowDialog "Exportador de dados p/ ViseDash  - VersÆo 1.0" Proc "SD_Dialog"
   Trigger "OPEN","ARRIVE","UPDATE"
    Key "TAB","ReverseTab","ENTER","ESC","F1",
                      -152,-31,-32,-25,-20,-49,-83,
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140    

     @ 01,00   Height 22 Width 80

    Label  @ 00,00 "Empresa"  For "TG_EMPRESAS"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESAS" To VSDGEMPRESA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMEMP" To VSDGNOMEMP

    @ 02,01 ?? "Caminho ViseDash"
    @ 03,02 ?? VSDGPASTA
    PaintCanvas Attribute 121 03,01,03,33

    Label  @ 04,09 "Mˆs/Ano"  For "TG_MESANO"
    Accept @ 05,09  Width 10 "A07"  Picture "##.####" Tag "TG_MESANO" To VSDGMESANO



    @ 14,01 ?? "** Aten‡Æo **" 
    @ 15,01 ?? " - Verifique se o INTEGRADOR do PlugBot esta carregado;" 
    @ 16,01 ?? " - Ao exportar estoque/invent rio, lembre de reprocessar estoque primeiro."

    Pushbutton @ 18,25 width 11 "~O~k"         OK     Default Value "Ok" Tag "YES"        To BottonValue
    Pushbutton @ 18,38 width 11 "~C~ancela"    CANCEL                    Tag "NO"         To BottonValue
    Pushbutton @ 18,61 width 14 "Con~f~igurar"                           Tag "TG_CONFIG"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  
  If IsBlank(VSDGEMPRESA)
     Then fastmsg("Empresa inv lida !")
          Loop
  EndIf                                                            

  If Direxists(VSDGPASTA)=0
     Then fastmsg("Caminho especificado n„o existe !")
          Loop
  EndIf

  tini=ticks()  
  
  View "\\Sistemas\\Cadastro\\Empresas"
  Moveto [Codigo da Empresa]
  Locate VSDGEMPRESA
  If RetVal
     Then CNPJ_Empr=[CGC Empresa    ]
            UF_Empr=[UF Empresa     ]
            TP_Empr=[Tipo da Empresa]
     Else CNPJ_Empr=""
            UF_Empr=""
            TP_Empr=""
  EndIf
  ClearAll
  
  {Tools} {Net} {AutoRefresh} {60}
  
  VSDGMES=SubStr(VSDGMESANO,1,2)
  VSDGANO=SubStr(VSDGMESANO,4,4)
  
  VSDGDTINI=DateVal("01."+VSDGMES+"."+VSDGANO)
  if month(today())<>numval(VSDGMES)
     then VSDGDTFIM=Ultimo_Dia(VSDGMES+VSDGANO)
          VSDGHRFIM="24:00"
     else VSDGDTFIM=today()
          VSDGHRFIM=substr(time(),1,5)
  endif           

  Aguarde("Processando dados") 

  netapas=0
  tetapas=dynarraysize(arrayetapas)
               
  for wcc from 1 to tetapas

    ;;if wcc<34  then loop endif

    processaretapa(wcc)
  endfor

  ConfOpcoes("Gravar")

  LimpAguarde()
  LimpAguardes()
  
  Mensagem("Fim do processo !")
  quitloop
     
EndWhile

ClearAll
Release Vars All
Release Procs All
