﻿Imports System.Web.Configuration
Imports Superbowl.sqlDB
Imports Superbowl.AuditEntity

Partial Public Class ProductDataDimensionDetails
    Inherits System.Web.UI.Page

    Private Shared strProdCode As String = "0"
    Private db As New sqlDB
    Private ds As DataSet
    Private dr, drIni As DataRow
    Private s As String
    Private Shared aeFormer, aeLatter As AuditEntity

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblMessage.Text = ""

        If Not Page.IsPostBack Then
            WebConfigurationManager.AppSettings("strPage") = "ProductDataDimensionDetails"
            If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
                btnSave.Enabled = False
            End If
            Try
                strProdCode = Nz(Request.QueryString("PID"), "0")
            Catch ex As Exception
                strProdCode = "0"
            End Try

            If strProdCode <> "0" And strProdCode.Length > 0 Then
                'load controls				
                Try
                    drIni = db.GetRow(db.sql_get_ProductDataDimension(strProdCode))    'breaks if no rows found

                    sdsBrand.SelectParameters("BrandID").DefaultValue = Nz(drIni!refBrandID, 0)

                    ddlProdCode.SelectedValue = strProdCode
                    ddlProdCode.Enabled = False
                    strAccpacCode.Text = Nz(drIni!strAccpacCode, "")
                    strVendorCode.Text = Nz(drIni!strVendorCode, "")
                    strDescription.Text = Nz(drIni!strDescription, "")
                    intABVPerc.Text = Nz(drIni!intABVPerc, "0")
                    intImportCertNo.Text = Nz(drIni!intImportCertNo, "0")

                    intPalletBase.Text = Nz(drIni!intPalletBase, "0")
                    intPalletLayer.Text = Nz(drIni!intPalletLayer, "0")
                    intPalletTotal.Text = Nz(drIni!intPalletTotal, "0")

                    intBottleWeight.Text = Nz(drIni!intBottleWeight, "0")
                    intVAPGiftUnitWeight.Text = Nz(drIni!intVAPGiftUnitWeight, "0")
                    intCaseWeight.Text = Nz(drIni!intCaseWeight, "0")
                    intPalletWeight.Text = Nz(drIni!intPalletWeight, "0")

                    intBottleHeight.Text = Nz(drIni!intBottleHeight, "0")
                    intVAPGiftUnitHeight.Text = Nz(drIni!intVAPGiftUnitHeight, "0")
                    intCaseHeight.Text = Nz(drIni!intCaseHeight, "0")
                    intPalletHeight.Text = Nz(drIni!intPalletHeight, "0")

                    intBottleWidth.Text = Nz(drIni!intBottleWidth, "0")
                    intVAPGiftUnitWidth.Text = Nz(drIni!intVAPGiftUnitWidth, "0")
                    intCaseWidth.Text = Nz(drIni!intCaseWidth, "0")
                    intPalletWidth.Text = Nz(drIni!intPalletWidth, "0")

                    intBottleLength.Text = Nz(drIni!intBottleLength, "0")
                    intVAPGiftUnitLength.Text = Nz(drIni!intVAPGiftUnitLength, "0")
                    intCaseLength.Text = Nz(drIni!intCaseLength, "0")
                    intPalletLength.Text = Nz(drIni!intPalletLength, "0")

                    strBottleBarcode.Text = Nz(drIni!strBottleBarcode, "0")
                    strVAPGiftUnitBarcode.Text = Nz(drIni!strVAPGiftUnitBarcode, "0")
                    strCaseBarcode.Text = Nz(drIni!strCaseBarcode, "0")
                    strPalletBarcode.Text = Nz(drIni!strPalletBarcode, "0")

                    'Net Wight columns - dewald
                    intBottleNetWeight.Text = Nz(drIni!intBottleNetWeight, "0")
                    intVAPGiftUnitNetWeight.Text = Nz(drIni!intVAPGiftUnitNetWeight, "0")
                    intCaseNetWeight.Text = Nz(drIni!intCaseNetWeight, "0")
                    intPalletNetWeight.Text = Nz(drIni!intPalletNetWeight, "0")

                    ddlBrand.SelectedValue = Nz(drIni!refBrandID, 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 = "Product Data and Dimensions, <font color=black>" & strProdCode & "</font>, was not found!"
                    drIni = Nothing
                End Try
            Else
                ddlProdCode.Enabled = True
            End If

        End If
    End Sub

    Private Sub ProductDetails_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

    End Sub

    Protected Sub ddlProdCode_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlProdCode.SelectedIndexChanged
        If db.CountRows("SELECT * FROM mstProductDataDimension WHERE strProdCode = '" & sqlDB.CleanString(ddlProdCode.SelectedValue) & "' ") > 0 Then
            Response.Redirect("ProductDataDimensionDetails.aspx?PID=" & sqlDB.CleanString(ddlProdCode.SelectedValue))
        Else
            strDescription.Focus()
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click

        lblMessage.Text = ""
        Dim s As String

        'v.1.2.4
        strProdCode = Nz(Request.QueryString("PID"), "")
        If strProdCode = "" And Not ddlProdCode.SelectedValue.Trim = "" Then
            strProdCode = "0" 'new record!
        End If

        dr = db.GetRow(db.sql_get_ProductDataDimension(strProdCode))
        If Not dr Is Nothing Then
            aeFormer = New AuditEntity(dr, "mstProductDataDimension")
        End If

        If ddlProdCode.SelectedIndex < 1 Then lblMessage.Text &= "<BR>SAP Code Required"
        'If strType.Text.Trim.Length = 0 Then lblMessage.Text &= "<BR>Product Type Required"
        'If strProductDesc.Text.Trim.Length = 0 Or strLongProdDesc.Text.Trim.Length = 0 Then lblMessage.Text &= "<BR>Product Description Required"
        If ddlBrand.SelectedIndex < 1 Then lblMessage.Text &= "<BR>Brand Required"

        If Not lblMessage.Text.Length > 0 Then          'redundant validation check	
            Try
                '1- Update/insert into MST
                If strProdCode <> "0" And strProdCode.Length > 0 Then 'Update
                    strProdCode = ddlProdCode.SelectedValue

                    Try
                        'v1.4.6 - 20160920 - Added strCategory to product details + removed category from product list filter - maanie
                        sqlDB.doQueryDS(db.sql_update_ProductDataDimension(strProdCode, ddlBrand.SelectedValue, strDescription.Text, strAccpacCode.Text, strVendorCode.Text, intABVPerc.Text, intImportCertNo.Text, intPalletBase.Text, intPalletLayer.Text, intPalletTotal.Text, intBottleWeight.Text, intVAPGiftUnitWeight.Text, intCaseWeight.Text, intPalletWeight.Text, intBottleHeight.Text, intVAPGiftUnitHeight.Text, intCaseHeight.Text, intPalletHeight.Text, intBottleWidth.Text, intVAPGiftUnitWidth.Text, intCaseWidth.Text, intPalletWidth.Text, intBottleLength.Text, intVAPGiftUnitLength.Text, intCaseLength.Text, intPalletLength.Text, strBottleBarcode.Text, strVAPGiftUnitBarcode.Text, strCaseBarcode.Text, strPalletBarcode.Text, Session.Item("User"), intBottleNetWeight.Text, intVAPGiftUnitNetWeight.Text, intCaseNetWeight.Text, intPalletNetWeight.Text))

                        'v1.2.4 - added AdvAudit 20090508
                        dr = db.GetRow(db.sql_get_ProductDataDimension(strProdCode))
                        aeLatter = New AuditEntity(dr, "mstProductDataDimension")
                        AuditEntity.Audit(aeFormer, aeLatter, Session.Item("User")) 'v1.4.5 - 20160913 - Fixed logged In user & savings (i.e use session instead) - maanie
                    Catch ex As Exception
                        lblMessage.Text = "<BR>Update Failed.<BR><BR>" & ex.Message
                        Throw (New Exception("Could not write to datawarehouse"))
                    End Try

                Else 'Insert
                    Try
                        'v1.4.6 - 20160920 - Added strCategory to product details + removed category from product list filter - maanie
                        'strProdCode, refBrandID, strProductDesc, intPack, intSize, dblPrice, strLongProdDesc, strType, strType2, intShortCode, strPriceSeg, strCategory, blnActive, dblFlat, cwsicat, strCategory2, strPrincipalMaterialRefNum, strPrincipalProdDesc, strBottleBarcode, strCaseBarcode, strGiftboxBarcode, intDisplayOrder, strBrandVarietal, QF
                        sqlDB.doQueryDS(db.sql_insert_ProductDataDimension(ddlProdCode.SelectedValue, ddlBrand.SelectedValue, strDescription.Text, strAccpacCode.Text, strVendorCode.Text, intABVPerc.Text, intImportCertNo.Text, intPalletBase.Text, intPalletLayer.Text, intPalletTotal.Text, intBottleWeight.Text, intVAPGiftUnitWeight.Text, intCaseWeight.Text, intPalletWeight.Text, intBottleHeight.Text, intVAPGiftUnitHeight.Text, intCaseHeight.Text, intPalletHeight.Text, intBottleWidth.Text, intVAPGiftUnitWidth.Text, intCaseWidth.Text, intPalletWidth.Text, intBottleLength.Text, intVAPGiftUnitLength.Text, intCaseLength.Text, intPalletLength.Text, strBottleBarcode.Text, strVAPGiftUnitBarcode.Text, strCaseBarcode.Text, strPalletBarcode.Text, Session.Item("User"), intBottleNetWeight.Text, intVAPGiftUnitNetWeight.Text, intCaseNetWeight.Text, intPalletNetWeight.Text))
                        strProdCode = sqlDB.CleanString(ddlProdCode.SelectedValue)

                    Catch ex As Exception
                        lblMessage.Text = "<BR>Insert Failed.<BR><BR>" & ex.Message
                        Throw (New Exception("Could not write to datawarehouse"))
                    End Try
                End If

                '2-Update/insert into DW
                Dim Keys As String() = {"strProdCode"}
                Dim Values As String() = {strProdCode}

                ds = sqlDB.getDW("dwdProductDataDimension", Keys, Values)
                If ds.Tables(0).Rows.Count > 0 Then  'update dw
                    Dim fields As String = ""
                    fields = sqlDB.Update_OuterDwd(sqlDB.doQueryDS(db.sql_get_ProductDataDimension_dw(strProdCode)).Tables(0)) 'build up the "SET bla = $bla
                    If WebConfigurationManager.AppSettings("blnDebug") = True Then lblMessage.Text &= "<BR>" & db.sql_update_ProductDataDimension_dw(strProdCode, fields)
                    sqlDB.doQueryDS(db.sql_update_ProductDataDimension_dw(strProdCode, fields))
                Else  'insert dw
                    sqlDB.doQueryDS(db.sql_insert_ProductDataDimension_dw(strProdCode))
                End If

                If WebConfigurationManager.AppSettings("blnDebug") = False Then Response.Redirect("ProductDataDimensionList.aspx")
            Catch ex As Exception
                lblMessage.Text &= "<BR>" & ex.Message & "<BR>" & ex.StackTrace
            End Try
        End If
    End Sub

    Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClose.Click
        Response.Redirect("ProductDataDimensionList.aspx")
    End Sub

End Class