﻿Imports System.Web.Configuration
Imports Superbowl.sqlDB

Partial Class KeyAccountDetails
   Inherits System.Web.UI.Page

   Public Shared KeyAccountID As Integer = 0
   Public Shared dwKeyAccount As String = ""
   Public Shared dwPriceGroupCode As String = ""
   Private db As New sqlDB
   Private ds As DataSet
   Private dr, drIni As DataRow

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

      If Not Page.IsPostBack Then
         WebConfigurationManager.AppSettings("strPage") = "KeyAccountDetails"
         If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
            btnSave.Enabled = False
         End If
         Try
            KeyAccountID = Request.QueryString("KAID")
         Catch ex As Exception
            KeyAccountID = 0
         End Try

         If KeyAccountID <> 0 Then
            Try
               drIni = sqlDB.GetRow(db.sql_get_KeyAccount(KeyAccountID))
               dwKeyAccount = drIni!strKeyAccount                                              'get ori KeyAccount for dw
               dwPriceGroupCode = Nz(drIni!strPriceGroupCode, "")

               txtKeyAccount.Text = drIni!strKeyAccount

               strAccpacName.Text = Nz(drIni!strAccpacName, "")
               strAccpacCode.Text = Nz(drIni!strAccpacCode, "")
               txtPriceGroupCode.Text = Nz(drIni!strPriceGroupCode, "")

               chkActive.Checked = drIni!blnActive
               chkOncon.Checked = drIni!blnOncon
               chkRebate.Checked = drIni!blnRebate
               chkToolkit.Checked = drIni!blnToolkit
               chkPayLessInvoice.Checked = drIni!blnPayLessInvoice

               lblLastEdit.Text = Nz(drIni!strLastUser, "unknown") & " - " & SqlDate(Nz(drIni!dtStamp, ""), "d MMMM yyyy HH:mm")

            Catch ex As IndexOutOfRangeException                       'dr = ds.tables(0).rows(0) doesn't exist
               lblMessage.Text = "KeyAccount ID, <font color=black>" & KeyAccountID & "</font>, was not found!"
               drIni = Nothing
            End Try
         Else
            dwKeyAccount = ""
            'txtKeyAccount.ReadOnly = False
         End If
      End If
   End Sub

   'v1.4.3 - 20131106 - Added txtPriceGroupCode and updating strPriceGroupCode for dwdCustomer -christiaan
   Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
      lblMessage.Text = ""

      If txtKeyAccount.Text.Trim() = "" Then lblMessage.Text &= "KeyAccount name required."
      If txtPriceGroupCode.Text.Trim() = "" Then lblMessage.Text &= "Price Group Code required."

      If sqlDB.CountRows(db.sql_get_KeyAccount(txtKeyAccount.Text.Trim, KeyAccountID)) > 0 Then lblMessage.Text &= "KeyAccount """ & txtKeyAccount.Text.Trim & """ already exists!"

      If Not lblMessage.Text.Length > 0 Then

         If KeyAccountID <> 0 Then 'Added strSnellKeyAcc x3 2009/01/16 ; 
            sqlDB.doQueryDS(db.sql_update_KeyAccount(KeyAccountID, txtKeyAccount.Text.Trim, strAccpacName.Text.Trim, strAccpacCode.Text.Trim, txtPriceGroupCode.Text.Trim, CInt(chkOncon.Checked), CInt(chkToolkit.Checked), CInt(chkRebate.Checked), CInt(chkPayLessInvoice.Checked), CInt(chkActive.Checked), Session.Item("User")))
            '2-Update into DW		
            If dwKeyAccount <> txtKeyAccount.Text.Trim Or dwPriceGroupCode <> txtPriceGroupCode.Text.Trim Then

               sqlDB.doQueryDS(db.sql_update_KeyAccount_dw(dwKeyAccount, txtKeyAccount.Text, txtPriceGroupCode.Text))
            End If
         Else
            sqlDB.doQueryDS(db.sql_insert_KeyAccount(txtKeyAccount.Text.Trim, strAccpacName.Text.Trim, strAccpacCode.Text.Trim, txtPriceGroupCode.Text.Trim, CInt(chkOncon.Checked), CInt(chkToolkit.Checked), CInt(chkRebate.Checked), CInt(chkPayLessInvoice.Checked), 1, Session.Item("User")))
         End If

         '	Response.Redirect("KeyAccountList.aspx")
      End If

   End Sub

   Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
      Response.Redirect("KeyAccountList.aspx")
   End Sub

End Class