'CHANGE LOG
' 20151021 - v0.0.0 - project start
' 20151124 - v0.0.0 - Gauteng Main Market >> Gauteng - pj
' 20160525 - v1.1.1 - Added email log - maanie
'
'
'
'
'
'
'
'
'
'
'

Imports SNELL_CronDaily.SystemFunctions
Imports SNELL_CronDaily.ProjectFunctions
Imports SNELL_CronDaily.db
Imports System.Net.Mail

Imports SNELL_CronDaily.Process
Imports SNELL_CronDaily.Export_Process


Public Class frmMain

   Const VersionDate As String = "31 May 2016"

   Public lstImport As List(Of KeyValuePair(Of String, Process))
   Public lstExport As List(Of KeyValuePair(Of String, Export_Process))
   Dim ds As DataSet
   Dim dr As DataRow

   Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      'ini
      lblVersion.Text = "SNELL CronDaily Version 1." & My.Application.Info.Version.Build.ToString() & "." & My.Application.Info.Version.Revision.ToString() & " - " & frmMain.VersionDate
      Dim proNew As Process
      Dim proExport As Export_Process
      Dim strDownloadDir As String


#If DEBUG Then
      strDownloadDir = "D:\Documents\Work Stuff\ESnell\"
#Else
        strDownloadDir = "C:\SAPREPORTS\" '[re: SQL Server 2014 Express and SQL Server Management Studio 2014 Express  - Installed]
#End If

      'set dirs
      My.Settings.dirImports = System.AppDomain.CurrentDomain.BaseDirectory() & "Imports\"
      My.Settings.dirImported = System.AppDomain.CurrentDomain.BaseDirectory() & "Imported\"
      My.Settings.dirExports = System.AppDomain.CurrentDomain.BaseDirectory() & "Exports\"


      lblDirImports.Text = My.Settings.dirImports
      lblDirImported.Text = My.Settings.dirImported
      lblDirExports.Text = My.Settings.dirExports

      'data ini
      lstImport = New List(Of KeyValuePair(Of String, Process))
      lstExport = New List(Of KeyValuePair(Of String, Export_Process))

      'imports
      proNew = New Process("Customer", "pda_crm.txt", strDownloadDir, "ENTITY_CODE")
      lstImport.Add(New KeyValuePair(Of String, Process)(proNew.Name, proNew))

      proNew = New Process("Product", "pdaprod.txt", strDownloadDir, "ITEM_CODE")
      lstImport.Add(New KeyValuePair(Of String, Process)(proNew.Name, proNew))

      'exports '
      'dr = db.getRow("SELECT * FROM sysQuery WHERE ID = 35")
      'proExport = New Export_Process("AdvPDA CRM", "pda_crm.csv", strDownloadDir, dr!memQuery)
      'lstExport.Add(New KeyValuePair(Of String, Export_Process)(proExport.Name, proExport))

      'form ini

      If My.Settings.Debug = False Then
         Run()
         Me.Close()
      End If


   End Sub

   Private Sub Run()
      'process flow:
      'START UP:
      '  Batch Start() (prep logging, start logging)
      '
      'IMPORT:
      '  foreach Process selected{
      '     Run Process():
      '     {
      'ini:
      '        log Process(Start)
      '        Copy down files [log Process(Copied)]
      '        if valid
      '           clear stg
      'import:
      '           import stg
      '           log Process(Imported)
      'master:
      '           *process Master
      '           *log Process(Master)
      '     }}
      '
      'POST PROCESS:
      '
      'OUTPUT:
      '
      'FINAL:
      '  Batch End() (finalise logging)
      '
      '*********************************************************************************************************************

      ' 20160525 - v1.1.1 - Added email log - maanie - send email BEFORE LOG TRUNCATE!
      EmailLog()

      'START UP:
      '  Batch Start() (prep logging, start logging)
      db.doQuery("TRUNCATE TABLE stgSuperbowlLog")
      WriteLog("info", "SNELL CronDaily", "Batch Start", "Batch Start: " & Date.Now.ToString("u"))


      'IMPORT:
      '  foreach Process selected{
      '     Run Process():

      For Each proPair In lstImport
         Dim proCurrent As Process = proPair.Value
         Dim strProcess As String = proPair.Key

         Dim chkProcess As New CheckBox
         Try
            chkProcess = Me.Controls.Find("chk" & proCurrent.ID, True)(0)
         Catch ex As Exception
            chkProcess.Checked = False
         End Try

         Dim barProcess As New ProgressBar
         Try
            barProcess = Me.Controls.Find("bar" & proCurrent.ID, True)(0)
            proCurrent.barProcess = barProcess
         Catch ex As Exception
         End Try


         If chkProcess.Checked = True Then

            Select Case strProcess
               Case "Customer"
                  Dim proCustomer = New Customer_Process(proCurrent)
                  proCustomer.Run()

               Case "Product"
                  Dim proProduct = New Product_Process(proCurrent)
                  proProduct.Run()

            End Select
         End If 'eoIfChecked

      Next 'eoFE Process


      'POST PROCESS:
      '
      'OUTPUT:
      '  produce SalesCharterQueries exports
      For Each proPair In lstExport
         Dim proCurrent As Export_Process = proPair.Value
         Dim strProcess As String = proPair.Key

         Dim chkProcess As New CheckBox
         Try
            chkProcess = Me.Controls.Find("chk" & proCurrent.ID, True)(0)
         Catch ex As Exception
            chkProcess.Checked = False
         End Try

         Dim barProcess As New ProgressBar
         Try
            barProcess = Me.Controls.Find("bar" & proCurrent.ID, True)(0)
            proCurrent.barProcess = barProcess
         Catch ex As Exception
         End Try


         If chkProcess.Checked = True Then

            Select Case strProcess
               'Case "AdvPDA PROD" '20140220 - v2.0.1 - added AdvM pda export file
               '   proCurrent.Run()
            End Select
         End If
      Next

      'FINAL:

      '  Batch End() (finalise logging)
      'move stg to dlyImportLog
      WriteLog("info", "SNELL CronDaily", "Batch Completed", "Batch Completed: " & Date.Now.ToString("u"))
      'db.doQuery("INSERT INTO logSuperbowl (strStamp, dtLog, strType, strProcess, strAction, txtLog) " & _
      '"SELECT strStamp, dtLog, strType, strProcess, strAction, txtLog FROM stgSuperbowlLog")

   End Sub


   '*** HANDLES ***

   Private Sub btnRun_Click(sender As Object, e As EventArgs) Handles btnRun.Click

      Me.Run()

   End Sub

   Private Sub btnEmailLog_Click(sender As Object, e As EventArgs) Handles btnEmailLog.Click

      EmailLog()

   End Sub

End Class
