﻿
Imports System.Web.Configuration
Imports Superbowl.sqlDB

Partial Public Class ExportsDetails
  Inherits System.Web.UI.Page

  Public Shared ExportID As Double = 0
  Public Shared dwExportName 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") = "ExportsDetails"
      If WebConfigurationManager.AppSettings("ULEVEL") < 3 Then
        btnSave.Enabled = False
      End If
      Try
        ExportID = CDbl(Request.QueryString("EID"))
      Catch ex As Exception
        ExportID = 0
      End Try

      If ExportID <> 0 Then
        Try
          drIni = sqlDB.GetRow(db.sql_get_Export(ExportID))

          txtCustomer.Text = drIni!mstCustomer
          If db.Nz(drIni!strCustomerNo, "") <> "" Then
            ddlCustomer.SelectedValue = Nz(drIni!mstCustomer, "0")
          End If

          If db.Nz(drIni!strProdCode, "") <> "" Then
            ddlBrand.SelectedValue = Nz(drIni!refBrandID, 0)
            ddlProduct.SelectedValue = Nz(drIni!mstProdCode, "0")
          End If

          txtProduct.Text = drIni!mstProdCode

          ddlPeriod.SelectedValue = Nz(drIni!mstPeriod, "- Select -")

          txtCountry.Text = Nz(drIni!strCountry, "")
          ddlCountry.SelectedValue = Nz(drIni!strCountry, "- Select -")

          chkProcessed.Checked = Nz(drIni!blnProcessed, False)
          chkProcessed.Text = New Date(Nz(drIni!refBatch, Date.Now.Ticks)).ToString("d MMMM yyyy")
          If drIni!blnProcessed = True Then
            btnSave.Enabled = False
            btnSaveNew.Enabled = False
          End If

          dblUnits.Text = Nz(drIni!dblUnits, 0)
          dblGSV.Text = Nz(drIni!dblGSV, 0.0)
          dblNSV.Text = Nz(drIni!dblNSV, 0.0)
          dblNettEx.Text = Nz(drIni!dblNettEx, 0.0)

          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 = "Export ID, <font color=black>" & ExportID & "</font>, was not found!"
          drIni = Nothing
        End Try
      Else
        ddlPeriod.SelectedValue = Date.Now.ToString("yyyyMM")
      End If
    End If
  End Sub

  Protected Sub ddlCustomer_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCustomer.SelectedIndexChanged
    If ddlCustomer.SelectedIndex > 0 Then txtCustomer.Text = ddlCustomer.SelectedValue Else txtCustomer.Text = ""
    ddlBrand.Focus()
  End Sub

  Protected Sub ddlBrand_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlBrand.SelectedIndexChanged
    sdsBrandProduct.FilterExpression = "refBrandID = 0 OR refBrandID = " & ddlBrand.SelectedValue
    ddlProduct.Focus()
  End Sub

  Protected Sub ddlProduct_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlProduct.SelectedIndexChanged
    If ddlProduct.SelectedIndex > 0 Then txtProduct.Text = ddlProduct.SelectedValue Else txtProduct.Text = ""
    ddlPeriod.Focus()
  End Sub

  Protected Sub ddlCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCountry.SelectedIndexChanged
    If ddlCountry.SelectedIndex > 0 Then txtCountry.Text = ddlCountry.SelectedValue Else txtCountry.Text = ""
    dblUnits.Focus()
  End Sub

  Protected Sub dblUnits_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dblUnits.TextChanged
    If txtProduct.Text.Trim.Length > 0 And (dblGSV.Text = "0.0" Or dblGSV.Text.Trim.Length = 0) Then
      Try
        dr = db.GetRow("SELECT * FROM mstProduct WHERE strProdCode = '" & CleanString(txtProduct.Text.Trim) & "'")
        dblGSV.Text = Nz(dr!dblPrice, 0.0) * CDbl(dblUnits.Text)
        dblNSV.Focus()

      Catch ex As Exception
        dblGSV.Focus()
      End Try
    End If
  End Sub

  Public Function Save() As Boolean
    lblMessage.Text = ""

    If txtCustomer.Text.Trim() = "" Then lblMessage.Text &= "Customer Number required.<BR>"
    If txtProduct.Text.Trim() = "" Then lblMessage.Text &= "Product Code required.<BR>"
    If ddlPeriod.SelectedIndex < 1 Then lblMessage.Text &= "Period required.<BR>"
    If txtCountry.Text.Trim() = "" Then lblMessage.Text &= "Export to Country required.<BR>"

    Try

      If Not lblMessage.Text.Length > 0 Then

        If ExportID <> 0 Then
          dr = db.GetRow(db.sql_get_Export(ExportID))
          If dr!blnProcessed = True Then
            Throw New DataException("Can not Save Export. This record was been processed already.")
          Else
                  sqlDB.doQueryDS(db.sql_update_Export(ExportID, ddlPeriod.SelectedValue, txtCustomer.Text.Trim, txtProduct.Text.Trim, dblUnits.Text.Trim, dblGSV.Text.Trim, dblNSV.Text.Trim, dblNettEx.Text.Trim, txtCountry.Text.Trim, Session.Item("User")))

          End If
        Else
          'strBatch '909099' is superbowl manual saves
          '         '999090' is daily app imported
               sqlDB.doQueryDS(db.sql_insert_Export(ddlPeriod.SelectedValue, txtCustomer.Text.Trim, txtProduct.Text.Trim, dblUnits.Text.Trim, dblGSV.Text.Trim, dblNSV.Text.Trim, dblNettEx.Text.Trim, txtCountry.Text.Trim, "909099", Session.Item("User")))
        End If

        Return True
        'Response.Redirect("ExportsList.aspx", True)
      End If
    Catch sdex As DataException
      lblMessage.Text = sdex.Message
      Return False
    Catch ex As Exception
      lblMessage.Text = ex.Message & "<BR>" & ex.StackTrace
      Return False
    End Try
  End Function

  Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If Save() Then
      Response.Redirect("ExportsList.aspx", True)
    End If
  End Sub

  Protected Sub btnSaveNew_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSaveNew.Click
    If Save() Then
      ExportID = 0
      ddlCustomer.SelectedIndex = 0
      txtCustomer.Text = ""
      dblUnits.Text = "1"
      dblGSV.Text = "0.0"
      dblNSV.Text = "0.0"
      dblNettEx.Text = "0.0"
      chkProcessed.Checked = False
    End If

  End Sub

  Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
    Response.Redirect("ExportsList.aspx")
  End Sub

End Class