Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class Market_Operating_Price
    Inherits System.Web.UI.Page
    Shared g_FinYear As String
    Shared g_User As String
    Shared g_Company As String
    Shared Category(10) As String
    Shared kid As String
    Shared code As String
    Shared i As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("SupplierID") Is Nothing Then
                Response.Redirect("listproduct.aspx?sess=expire")
                Exit Sub
            End If
            If Not IsPostBack Then
                If Session.Item("ProductID") Is Nothing Then
                    Response.Redirect("List_Product.aspx")
                    Exit Sub
                End If
                'g_User = "U1"
                'g_Company = "COM1"
                'g_FinYear = "2007"
                'g_User = Session("g_User").ToString
                'g_Company = Session("g_Company").ToString
                'g_FinYear = Session("g_FinYear").ToString
                btnUpdate.Attributes.Add("onclick", "return UpdateMsg();")
                BtnSave.Attributes.Add("onclick", "return SaveConfirm();")

                Dim catid As String
                catid = ReturnValue("Select Product_CategoryID from Product_Master where Product_Kid= '" & Session("ProductID").ToString() & "'and Product_IsDeleted='0' and Product_CompanyId ='" & Session("g_Company").ToString & "' ")
                i = 0
                FillCategoryName(catid)
                FillUnitCombo("select distinct Unit_Kid,Unit_Name from Unit_Master where Unit_IsDeleted='0' and Unit_CompanyId='" & Session("g_Company").ToString.Trim & "'", ddlProdPriceUnit)
                FillUnitCombo("select distinct Unit_Kid,Unit_Name from Unit_Master where Unit_IsDeleted='0' and Unit_CompanyId='" & Session("g_Company").ToString.Trim & "'", ddlCompPriceUnit)
                ddlProdPriceUnit.Items.FindByText("Rs.").Selected = True
                ddlCompPriceUnit.Items.FindByText("Rs.").Selected = True
                Call CheckRights(Me, Session("g_User").ToString, "Market Operating Price")
                ClearFields()
                getExistingData()
                If Session("AdminID") <> Nothing Then
                    btnUpdate.Visible = True
                    BtnSave.Visible = False
                    EnabeControls(True)
                Else
                    btnUpdate.Visible = False

                End If
            End If


        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")
        End Try
    End Sub
    Public Sub FillUnitCombo(ByVal strQuery As String, ByVal ddl As DropDownList)
        Try
            Dim SqlCon As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))  ' Declare connection Object
            Dim SqlAdapter As New SqlDataAdapter      ' Declare Dataadapter Object
            Dim SqlDataSet As New Data.DataSet        ' Declare Dataset Object
            Dim DT As New Data.DataTable              ' Declare Datatable Oblect
            Dim sqlCmd As New SqlCommand(strQuery, SqlCon) ' Declare Command Object
            Dim i As Integer

            SqlCon.Open()                                 ' Open Connection
            SqlAdapter.SelectCommand = sqlCmd             ' Set command to Adapter
            SqlAdapter.Fill(SqlDataSet, "TableName")      ' Links Adapter with DataSet
            DT = SqlDataSet.Tables("TableName")           ' Get datatable from dataset
            sqlCmd.ExecuteNonQuery()                      ' Executes Command

            ddl.Items.Clear()                             ' Clear the Items of DropDownList  

            For i = 0 To DT.Rows.Count - 1
                ddl.Items.Insert(i, New ListItem(DT.Rows(i).Item(1), DT.Rows(i).Item(0)))  '' Add Items(code,Exact Value) into DropDownList
            Next i
            ' ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(""))     '' Assigning 0 th Index to DropDownList

            SqlCon.Close()                                '' Close Connection Object
            SqlCon.Dispose()                              '' Dispose Connection Oblect
            sqlCmd.Dispose()                              '' Dispose command object
            SqlAdapter.Dispose()                          '' Dispose adapter Object
            SqlDataSet.Dispose()
            DT.Clear() '' Dispose dataset Object
            DT.Dispose()                                  '' Dispose datatable Object
        Catch ex As Exception
            CreateMessageAlert(ddl.Page, ex.Message, "StrKeyVal")     'Display the Error Message
            ddl = Nothing
        End Try
    End Sub
    Public Sub FillCategoryName(ByVal Catid As String)
        Dim dt As DataTable
        dt = ReturnDataTable("Select * From Category_Master Where Category_Kid ='" & Catid & "' and Category_Isdeleted = '0' and Category_CompanyId ='" & Session("g_Company").ToString & "'")
        Dim ParentId As String
        ParentId = dt.Rows(0).Item(3).ToString()
        If ParentId = "Root Category" Then
            'Label1.Text += dt.Rows(0).Item(2).ToString()
            'Dim CategoryName(10) As String
            Dim j As Integer
            Category(i) = dt.Rows(0).Item(2).ToString() & "  >  "
            'Array.Reverse(Category)
            For j = 0 To i
                Label2.Text += Category(i)
                i = i - 1
            Next
            Exit Sub
        Else
            Category(i) = dt.Rows(0).Item(2).ToString() & "  >  "
            i = i + 1
            'Label1.Text += dt.Rows(0).Item(2).ToString() & "  >>  "
            FillCategoryName(dt.Rows(0).Item(3).ToString())
        End If
        dt.Clear()
        'GridView1.DataSource = dt
        'GridView1.DataBind()
    End Sub
    Private Sub getExistingData()
        Dim dt As DataTable = getTable("Select * from MarketOperatingPrice where MarketOperatingPrice_ProductId='" & Session.Item("ProductID").Trim & "' and MarketOperatingPrice_IsDeleted='0' and MarketOperatingPrice_CompanyID='" & Session("g_Company").ToString.Trim & "'", "MOP")
        If dt.Rows.Count > 0 Then
            kid = dt.Rows(0).Item("MarketOperatingPrice_Kid").ToString
            code = dt.Rows(0).Item("MarketOperatingPrice_Code").ToString
            txtMarketOperatingPriceCode.Text = dt.Rows(0).Item("MarketOperatingPrice_Code").ToString
            txtMarketOperatingPricePrprice.Text = dt.Rows(0).Item("MarketOperatingPrice_ProductPrice").ToString
            txtMarketOperatingPriceCrprice.Text = dt.Rows(0).Item("MarketOperatingPrice_CompetitorInPrice").ToString
            txtMarketOperatingPriceSource.Text = dt.Rows(0).Item("MarketOperatingPrice_Source").ToString
            txtMarketOperatingPriceMLeader.Text = dt.Rows(0).Item("MarketOperatingPrice_MarketLeader").ToString
            ddlProdPriceUnit.Items.FindByValue(dt.Rows(0).Item("MarketOperatingPrice_ProductPriceUnit").ToString()).Selected = True
            ddlCompPriceUnit.Items.FindByValue(dt.Rows(0).Item("MarketOperatingPrice_CompetitorInPriceUnit").ToString()).Selected = True
            BtnSave.Visible = False
            EnabeControls(False)
        Else
            GenerateMarketOperatingPriceCode()
            BtnSave.Visible = True
            EnabeControls(True)
        End If
        dt.Clear()
    End Sub
    Private Sub EnabeControls(ByVal bool As Boolean)
        txtMarketOperatingPriceCrprice.Enabled = bool
        txtMarketOperatingPriceMLeader.Enabled = bool
        txtMarketOperatingPricePrprice.Enabled = bool
        txtMarketOperatingPriceSource.Enabled = bool
        ddlCompPriceUnit.Enabled = bool
        ddlProdPriceUnit.Enabled = bool
    End Sub
    Protected Sub ClearFields()
        txtMarketOperatingPriceCode.Text = ""
        txtMarketOperatingPriceCrprice.Text = ""
        txtMarketOperatingPriceMLeader.Text = ""
        txtMarketOperatingPricePrprice.Text = ""
        txtMarketOperatingPriceSource.Text = ""
    End Sub
    Protected Sub GenerateMarketOperatingPriceCode()
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "MOP" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from MarketOperatingPrice where MarketOperatingPrice_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    txtMarketOperatingPriceCode.Text = code
                    Exit Sub
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        'SAve Item in Market Opearatin Price
        Try
            lblError.Visible = True
            If txtMarketOperatingPricePrprice.Text = "" Then
                lblError.Text = "Product Price  can not be null"
                Exit Sub
            End If
            If txtMarketOperatingPriceCrprice.Text = "" Then
                lblError.Text = "Competitor In Price can not be null"
                Exit Sub
            End If
            If txtMarketOperatingPriceMLeader.Text = "" Then
                lblError.Text = "Market Leader  can not be null"
                Exit Sub
            End If
            If txtMarketOperatingPricePrprice.Text = "" Then
                lblError.Text = "product Price can not be null"
                Exit Sub
            End If
            If txtMarketOperatingPriceSource.Text = "" Then
                lblError.Text = "Source can not be null"
                Exit Sub
            End If
            lblError.Visible = False
            Dim cmd As New SqlCommand
            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            cmd.Connection = con
            cmd.CommandText = "MarketOperatingPrice_Proc"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_Kid", SqlDbType.NVarChar)).Value = NewPrimaryKey(Me)
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_Code", SqlDbType.NVarChar)).Value = txtMarketOperatingPriceCode.Text
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_ProductPrice", SqlDbType.Decimal)).Value = Convert.ToDecimal(txtMarketOperatingPricePrprice.Text)
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_ProductPriceUnit", SqlDbType.NVarChar, 10)).Value = ddlProdPriceUnit.SelectedItem.Value
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_CompetitorInPrice", SqlDbType.Decimal)).Value = Convert.ToDecimal(txtMarketOperatingPriceCrprice.Text)
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_CompetitorInPriceUnit", SqlDbType.NVarChar, 10)).Value = ddlCompPriceUnit.SelectedItem.Value
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_Source", SqlDbType.NVarChar)).Value = txtMarketOperatingPriceSource.Text
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_MarketLeader", SqlDbType.NVarChar)).Value = txtMarketOperatingPriceMLeader.Text
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_ProductId", SqlDbType.NVarChar)).Value = Session.Item("ProductID")
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_FinancialYearId", SqlDbType.NVarChar)).Value = Session("g_FinYear").ToString
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_CompanyId", SqlDbType.NVarChar)).Value = Session("g_Company").ToString
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_UserId", SqlDbType.NVarChar)).Value = Session("g_User").ToString
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Market Operating Price"
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.NVarChar, 50)).Value = "Insert"
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            con.Dispose()
            CreateMessageAlert(Me, "Market Operating Price Saved Successfully.", "StrKeyVal")
            lblError.Text = "Record Saved Successfully"
            Response.Redirect("List_Product.aspx")

        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")
        End Try
    End Sub
    Sub Node_Populate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs)
        Try
            If e.Node.ChildNodes.Count = 0 Then
                Select Case (e.Node.Depth)
                    Case 0
                        FillModule(e.Node, "2")
                        Exit Sub
                    Case 1
                        FillMenu(e.Node)
                        Exit Sub
                    Case 2
                        FillSubMenu(e.Node)
                        Exit Sub
                End Select
            End If
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub BtnSkip_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSkip.Click
        Response.Redirect("List_Product.aspx")
    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            lblError.Visible = True
            If txtMarketOperatingPricePrprice.Text = "" Then
                lblError.Text = "Product Price  can not be null"
                Exit Sub
            End If
            If txtMarketOperatingPriceCrprice.Text = "" Then
                lblError.Text = "Competitor In Price can not be null"
                Exit Sub
            End If
            If txtMarketOperatingPriceMLeader.Text = "" Then
                lblError.Text = "Market Leader  can not be null"
                Exit Sub
            End If
            If txtMarketOperatingPricePrprice.Text = "" Then
                lblError.Text = "product Price can not be null"
                Exit Sub
            End If
            If txtMarketOperatingPriceSource.Text = "" Then
                lblError.Text = "Source can not be null"
                Exit Sub
            End If
            lblError.Visible = False
            Dim cmd As New SqlCommand
            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            cmd.Connection = con
            cmd.CommandText = "MarketOperatingPrice_Proc"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_Kid", SqlDbType.NVarChar)).Value = RemoveLiterals(kid.ToString.Trim)
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_Code", SqlDbType.NVarChar)).Value = code.ToString.Trim
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_ProductPrice", SqlDbType.Decimal)).Value = Convert.ToDecimal(txtMarketOperatingPricePrprice.Text)
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_ProductPriceUnit", SqlDbType.NVarChar, 10)).Value = ddlProdPriceUnit.SelectedItem.Value
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_CompetitorInPrice", SqlDbType.Decimal)).Value = Convert.ToDecimal(txtMarketOperatingPriceCrprice.Text)
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_CompetitorInPriceUnit", SqlDbType.NVarChar, 10)).Value = ddlCompPriceUnit.SelectedItem.Value
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_Source", SqlDbType.NVarChar)).Value = txtMarketOperatingPriceSource.Text
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_MarketLeader", SqlDbType.NVarChar)).Value = txtMarketOperatingPriceMLeader.Text
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_ProductId", SqlDbType.NVarChar)).Value = Session.Item("ProductID")
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_FinancialYearId", SqlDbType.NVarChar)).Value = Session("g_FinYear").ToString
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_CompanyId", SqlDbType.NVarChar)).Value = Session("g_Company").ToString
            cmd.Parameters.Add(New SqlParameter("@MarketOperatingPrice_UserId", SqlDbType.NVarChar)).Value = Session("g_User").ToString
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Market Operating Price"
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.NVarChar, 50)).Value = "Update"
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            CreateMessageAlert(Me, "Market Operating Price Updated Successfully.", "StrKeyVal")
            lblError.Text = "Record Saved Successfully"
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")
        End Try
    End Sub
End Class
