'CHANGE LOG
' 20120830 - v2.0.0 - project start
' 20140220 - v2.0.1 - added AdvM pda export files
' 20140221 - v2.0.1 - change OnconSurvey process to include new survey questions (update stg insert)
' 20140430 - v2.1.0 - Added chkPartialImport [=True] as AdvM can only provide day files vs mounth to date.
' 20140509 - v2.1.1 - Changes to On/OffconPromo import files and DB - maanie
' 20140902 - v2.1.2 - added AdvM pda Exposure CRM export file - pj
' 20141020 - v2.1.3 - added process to email requisitions on Mondays with no invoice no after 2 weeks of being approved. - Maanie
' 20160128 - v2.1.4 - disabled function, no longer needed [re: RGBC: Cancelling Daily email for UN-Invoiced Requisitions]- pj
' 20160512 -        - re-enabled OffconPromo import - pj
' 20170609 - v2.1.5 - added daily vendor import - maanie
' 20180313 - v2.1.6 - added daily GL Account Code import - maanie
' 20180403 - v2.1.7 - Process class: added 2 new functions to import from view - maanie
'
'
'
'
'
'
'
'

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

Imports Superbowl_Daily_2.Process
Imports Superbowl_Daily_2.Export_Process


Public Class frmMain

   Const VersionDate As String = "28 January 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 = "RGBC Superbowl Daily Version 2." & 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\RGBC\"
      strDownloadDir = "E:\work\RGBC\"
#Else
        'strDownloadDir = "\\Sapphire\Sales\eSalesCharter\"
      strDownloadDir = "\\Sapphire\Sales\ADM\"
#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
      ' 20140509 - v2.1.1 - Changes to On/OffconPromo import files and DB: filename changed - maanie
      'proNew = New Process("Offcon Promo", "OffconPromos.csv", strDownloadDir, "strCustomerNo,strBrand,strDateStart,strDateEnd")
      proNew = New Process("Offcon Promo", "OffPromoSurvey.csv", strDownloadDir, "strCustomerNo,strBrand,strPromoDate")
      lstImport.Add(New KeyValuePair(Of String, Process)(proNew.Name, proNew))

      proNew = New Process("Offcon Survey", "OffPremiseSurvey.csv", strDownloadDir, "strCustomerNo,strProdCode,strDateSurveyed")
      lstImport.Add(New KeyValuePair(Of String, Process)(proNew.Name, proNew))

      ' 20140509 - v2.1.1 - Changes to On/OffconPromo import files and DB: filename changed - maanie
      'proNew = New Process("Oncon Promo", "OnconPromos.csv", strDownloadDir, "strCustomerNo,strBrand,strDatePromo")
      proNew = New Process("Oncon Promo", "OnPromoSurvey.csv", strDownloadDir, "strCustomerNo,strBrand,strDatePromo")
      lstImport.Add(New KeyValuePair(Of String, Process)(proNew.Name, proNew))

      proNew = New Process("Oncon Survey", "OnPremiseSurvey.csv", strDownloadDir, "strCustomerNo,strBrand,strDateSurveyed")
      lstImport.Add(New KeyValuePair(Of String, Process)(proNew.Name, proNew))

      proNew = New Process("Rep Calls", "eSalesCharter_CallsAndOrders.csv", strDownloadDir, "strDate,RepID")
      lstImport.Add(New KeyValuePair(Of String, Process)(proNew.Name, proNew))

      ' 20170609 - v2.1.5 - added daily vendor import - maanie
      proNew = New Process("Vendor", "apvendors_.csv", "vwAPVendors", "VendorID") 'path = view name = vwAPVendors
      lstImport.Add(New KeyValuePair(Of String, Process)(proNew.Name, proNew))

      ' 20180313 - v2.1.6 - added daily GL Account Code import - maanie
      proNew = New Process("GL Code", "GLCodes.csv", "vwGLAccounts", "GLCode") 'path = view name = vwGLAccounts
      lstImport.Add(New KeyValuePair(Of String, Process)(proNew.Name, proNew))

      'exports ' 20140220 - v2.0.1 - added AdvM pda export files
      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))

      ' 20140902 - v2.1.2 - added AdvM pda Exposure CRM export file - pj
      dr = db.getRow("SELECT * FROM sysQuery WHERE ID = 37")
      proExport = New Export_Process("AdvPDA ExpCRM", "exposure_pda_crm.csv", strDownloadDir, dr!memQuery)
      lstExport.Add(New KeyValuePair(Of String, Export_Process)(proExport.Name, proExport))

      dr = db.getRow("SELECT * FROM sysQuery WHERE ID = 36")
      proExport = New Export_Process("AdvPDA PROD", "pda_prod.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)
      '  check mstPeriods, ensure 30months rolling forward
      '  log DW records counts
      '  backup dwf[Off|On]conPromo on mondays
      '
      '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)
      'dw:
      '           process DW
      '           log Process(DW)
      '     }}
      '
      'POST PROCESS:
      '  update dwdCustomer from mstCustomer [log]
      '  *check/update dwdCustomer region & region order  'needed for FC, should be part of update dwdCustomer
      '  update SC
      '  recalc SC
      '
      'OUTPUT:
      '  produce SalesCharterQueries exports
      '  generate BFBE exports
      '
      'FINAL:
      '  email BFBE exports
      '  Batch End() (finalise logging)
      '  email Log
      '  email FSR un-invoiced list ' 20141020 - v2.1.3 - added process to email requisitions on Mondays with no invoice no after 2 weeks of being approved. - Maanie
      '
      '*********************************************************************************************************************


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

      '  check mstPeriods, ensure 30months rolling forward
      CheckPeriods()

      '  log DW records counts
      LogDWD()

      '  backup dwf[Off|On]conPromo on mondays
      'BackupMintPromos() ' not needed anymore

      '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 "Offcon Promo"
                  Dim proOffconPromo = New OffconPromo_Process(proCurrent)
                  proOffconPromo.Run()

               Case "Offcon Survey"
                  Dim proOffconSurvey = New OffconSurvey_Process(proCurrent)
                  proOffconSurvey.Run()

               Case "Oncon Promo"
                  Dim proOnconPromo = New OnconPromo_Process(proCurrent)
                  proOnconPromo.Run()

               Case "Oncon Survey"
                  Dim proOnconSurvey = New OnconSurvey_Process(proCurrent)
                  proOnconSurvey.Run()

               Case "Rep Calls"
                  Dim proRepCalls = New RepCalls_Process(proCurrent)
                  proRepCalls.Run()

               Case "Vendor"  ' 20170609 - v2.1.5 - added daily vendor import - maanie
                  Dim proVendor = New VendorImport_Process(proCurrent)
                  proVendor.Run()

               Case "GL Code"  ' 20180313 - v2.1.6 - added daily GL Account Code import - maanie
                  Dim proGLCode = New GLCodeImport_Process(proCurrent)
                  proGLCode.Run()
            End Select
         End If 'eoIfChecked

      Next 'eoFE Process


      'POST PROCESS:
      '  update dwdCustomer from mstCustomer [log]
      '  *check/update dwdCustomer region & region order  'needed for FC, should be part of update dwdCustomer
      '  update SC
      '  recalc SC
      '
      '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 CRM" '20140220 - v2.0.1 - added AdvM pda export files
                  proCurrent.Run()

               Case "AdvPDA ExpCRM" ' 20140902 - v2.1.2 - added AdvM pda Exposure CRM export file - pj
                  proCurrent.Run()

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

      '  generate BFBE exports
      '
      'FINAL:
      '  email BFBE exports

      '  Batch End() (finalise logging)
      'move stg to dlyImportLog
      WriteLog("info", "Superbowl Daily 2", "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")

      'send email
      EmailLog()

      'email FSR un-invoiced list 
      '20141020 - v2.1.3 - added process to email requisitions on Mondays with no invoice no after 2 weeks of being approved. - Maanie
      '20160128 - v2.1.4 - disabled function, no longer needed [re: RGBC: Cancelling Daily email for UN-Invoiced Requisitions]- pj
      'EmailUnIinvoicedRequisitions()

   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

   Public Sub Button1_Click(sender As Object, e As EventArgs)
      '20140206 - testing - dwayne
      ds = db.doQuery("SELECT * FROM dwdCustomer")


      Dim myExport As New dataExport(ds.Tables(0), "c:\temp\info.csv")
      'dataExport.Execute()

   End Sub

   Private Sub btnEmailRequisitions_Click(sender As Object, e As EventArgs) Handles btnEmailRequisitions.Click
      EmailUnIinvoicedRequisitions()
   End Sub
End Class
