Imports System.Data.SqlClient
Imports System.Data

Partial Class Buyer_OrderTrack
    Inherits System.Web.UI.Page
    Dim status As String
    Shared dt As New DataTable
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Label5.Visible = False

            Page.LoadControl("~/UserControl/buyeruserinfo.ascx")

            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("Image3"), Image).Visible = True
            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("LinkButton3"), LinkButton).Font.Bold = True
            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("LinkButton3"), LinkButton).Font.Size = 10

            If Page.IsPostBack = False Then
                Dim str As String = Session("BuyId")
                ' bindgvorderlist()
                If Not IsNothing(dt) Then
                    dt.Rows.Clear()
                    dt.Columns.Clear()

                End If
                bindgvorderlist()
                createtable()
                txtQty1.Attributes.Add("onkeypress", "return numericonly(event,this);")
                txtsearch.Text = ""
            End If
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub
    Protected Sub createtable()
        dt.Columns.Add("ProductCode", GetType(String))
        dt.Columns.Add("Quantity", GetType(Integer))
    End Sub

    Private Function ReturnProductId(ByVal ProdCode As String) As String
        Dim ProductId As String = ""
        Dim MarketPrice As Decimal = 0.0
        Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
        Con.Open()                                             'Open the Connection
        Dim StrQry As String = "select Product_Kid,Product_MarketPrice from Product_Master where Product_Code='" & ProdCode & "' and Product_Isdeleted='0'"
        Dim cmd As New SqlCommand(StrQry, Con)                 'Define command variable to execute the query
        Dim dr As SqlDataReader                                'Define DataReader variable for storing the output of the query
        dr = cmd.ExecuteReader
        Do While dr.Read
            ProductId = dr(0)
            MarketPrice = dr(1)
        Loop
        Con.Close()
        Return ProductId
    End Function

    Protected Function ShopCartProcedure(ByVal StrMode As String, ByVal qty As Integer, ByVal code As String)
        Try
            Dim strr As String = "select Product_Kid,Product_AtcoPrice from product_Master where Product_Approved='Y' and product_Code='" & code & "'"
            Dim con As New SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            cmd = New SqlCommand(strr, con)
            'con.Open()
            'dr = cmd.ExecuteReader
            Dim da1 As New SqlDataAdapter(cmd)
            Dim ds1 As New DataSet
            If Not da1.Fill(ds1) = Nothing Then
              
                Dim shopid As String = NewPrimaryKey(Me.Page)
                Dim shopCode As String = Generate_Code("select count(*) from ShopCart where ShopCart_Code='", "SC")
                Dim con1 As New SqlConnection                        'Define connection variable for connecting to SQL-Server
                con1.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
                Dim cmd As New SqlCommand
                Dim sqlcol As Data.SqlClient.SqlParameterCollection = cmd.Parameters
                sqlcol.Add("@ShopCart_Kid", SqlDbType.NVarChar, 10).Value = RemoveLiterals(shopid.Trim)
                sqlcol.Add("@ShopCart_Code", SqlDbType.NVarChar, 25).Value = shopCode.Trim
                sqlcol.Add("@ShopCart_ProductId", SqlDbType.NVarChar, 10).Value = ds1.Tables(0).Rows(0).Item(0)
                sqlcol.Add("@ShopCart_BuyerUserId", SqlDbType.NVarChar, 25).Value = Session("BuyId")
                sqlcol.Add("@ShopCart_Quantity", SqlDbType.Int).Value = qty
                sqlcol.Add("@ShopCart_SpecialPrice", SqlDbType.Decimal, 18).Value = ds1.Tables(0).Rows(0).Item(1)
                sqlcol.Add("@ShopCart_FinancialYearId", SqlDbType.NVarChar, 10).Value = ""
                sqlcol.Add("@ShopCart_CompanyId", SqlDbType.NVarChar, 10).Value = Session("ComanyId")
                sqlcol.Add("@ShopCart_UserId", SqlDbType.NVarChar, 10).Value = Session("UserId")
                sqlcol.Add("@FormName", SqlDbType.NVarChar, 50).Value = Me.Page.ToString
                sqlcol.Add("@Mode", SqlDbType.VarChar, 10).Value = StrMode.Trim
                ExecQuery("ShopCart_Proc", sqlcol).StartsWith("True")
                sqlcol = Nothing
                cmd = Nothing
                'Response.Redirect("ShopCart.aspx")
                con1.Close()
                da1.Dispose()
                ds1.Dispose()
                con1.Close()

                Return True
            End If
        Catch ex As Exception
            ex.Message.ToString()
        End Try

    End Function
    Protected Function ReturnValueDouble(ByVal StrQry As String) As String
        Try
            Dim SqlCon As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) ' Declare Connection Variable
            Dim sqlCmd As New SqlCommand(StrQry, SqlCon)    ' Declare Command Variable
            If SqlCon.State = ConnectionState.Open Then
                SqlCon.Close()
            End If

            sqlCmd.CommandType = CommandType.Text           ' Set Command Type 
            SqlCon.Open()
            sqlCmd.ExecuteScalar()                          ' To Execute the command                        
            'If (sqlCmd.ExecuteScalar) = "" Then              'If Return Values is 0" Then
            '    Return ""                                   'Returns ""
            'Else

            Return sqlCmd.ExecuteScalar.ToString        'Else Return Value from database
            ' End If

            ' Dispose connection Object
            SqlCon.Close()
            sqlCmd = Nothing
            SqlCon = Nothing
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Function

    Private Function ReturnShoppingPrice(ByVal ProductCode As String) As String
        Dim flag As Boolean = False
        Dim MarketPrice As Decimal = 0.0
        Dim ProductId As String = ""
        Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
        Con.Open()                                             'Open the Connection
        Dim StrQry As String = "select Product_Kid,Product_AtcoPrice from Product_Master where Product_Code='" & ProductCode & "'and Product_Isdeleted='0' "
        Dim cmd As New SqlCommand(StrQry, Con)                 'Define command variable to execute the query
        Dim dr As SqlDataReader                                'Define DataReader variable for storing the output of the query
        dr = cmd.ExecuteReader
        Do While dr.Read
            ProductId = dr(0)
            MarketPrice = dr(1)
            flag = True
        Loop
        Con.Close()
        If flag = True Then
            Return ProductId & "," & Convert.ToString(MarketPrice)
        ElseIf flag = False Then
            Return "Invalid"
        End If

    End Function

    Private Sub ClearControl()
        txtQty1.Text = ""
        txtCode1.Text = ""
        'txtQty2.Text = ""
        'txtCode2.Text = ""
        'txtQty3.Text = ""
        'txtCode3.Text = ""
        'txtQty4.Text = ""
        'txtCode4.Text = ""
        'txtQty5.Text = ""
        'txtCode5.Text = ""
    End Sub

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        ViewState.Item("tst") = Nothing
        Panel1.Visible = True
        txtsearch.Text = ""
        bindgvproduclist()
    End Sub

    Protected Sub gvproductlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
       
            Dim lbpc As Label = CType(gvproductlist.Rows(gvproductlist.SelectedIndex).FindControl("lblProductCode"), Label)

            'txtCode1.Text = gvproductlist.SelectedRow.Cells(2).Text.Trim
            txtCode1.Text = lbpc.Text
            txtQty1.Text = 1
            Panel1.Visible = True
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub gvproductlist_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvproductlist.PageIndexChanging
        Try
       
            gvproductlist.PageIndex = e.NewPageIndex
            If ViewState.Item("tst") <> Nothing Then

                searchproductlist()
            Else
                bindgvproduclist()
            End If
            Panel1.Visible = True
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub
    Function bindgvproduclist()
        Try
      
            Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
            Con.Open()                                             'Open the Connection
            Dim StrQry As String = "select Product_Name,Product_Code from Product_Master where Product_Isdeleted='0' and Product_Approved='Y' order by Product_Code,Product_Name "
            Dim da As New SqlDataAdapter(StrQry, Con)                 'Define command variable to execute the query
            Dim ds As New DataSet 'Define DataReader variable for storing the output of the query
            da.Fill(ds)
            gvproductlist.DataSource = ds.Tables(0)
            gvproductlist.DataBind()
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Function

    Function bindgvorderlist()
        Try
       
            Gvorderlist.DataSource = dt
            Gvorderlist.DataBind()
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Function
   
    Function searchproductlist()
        Try
            Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
            Con.Open() 'Open the Connection
            Dim search As String
            If ViewState.Item("tst") <> Nothing Then
                search = ViewState.Item("tst").ToString
            Else
                ' Dim txtsearch As TextBox = CType(gvproductlist.HeaderRow.FindControl("txtsearch"), TextBox)
                search = txtsearch.Text
            End If
            Dim StrQry As String = "select Product_Name,Product_Code from Product_Master where Product_Name like  '" & search.Trim & "%" & "' or Product_Code like  '" & search.Trim & "%" & "' and Product_Isdeleted='0' "
            Dim da As New SqlDataAdapter(StrQry, Con)                 'Define command variable to execute the query
            Dim ds As New DataSet 'Define DataReader variable for storing the output of the query
            da.Fill(ds)
            gvproductlist.DataSource = ds.Tables(0)
            gvproductlist.DataBind()
            ViewState.Item("tst") = search.Trim
            'Session("Status") = search.Trim
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Function

   
    Protected Sub Gvorderlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            txtCode1.Text = Gvorderlist.SelectedRow.Cells(1).Text
            txtQty1.Text = Gvorderlist.SelectedRow.Cells(2).Text.Trim


        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub btnsearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnsearch.Click
        Try
            ViewState.Item("tst") = Nothing
            searchproductlist()
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Button1.Click
        Try

            If Gvorderlist.Rows.Count > 0 Then
                If txtCode1.Text <> "" Or txtQty1.Text <> "" Then
                    Dim dr As DataRow = dt.NewRow()
                    ShopCartProcedure("Insert", CInt(txtQty1.Text), txtCode1.Text)
                    dr("ProductCode") = txtCode1.Text
                    dr("Quantity") = txtQty1.Text
                    dt.Rows.Add(dr)
                    bindgvorderlist()
                    ClearControl()
                    Label5.Visible = False
                End If
            Else
                If txtCode1.Text <> "" Or txtQty1.Text <> "" Then
                    Dim dr As DataRow = dt.NewRow()
                    ShopCartProcedure("Insert", CInt(txtQty1.Text), txtCode1.Text)
                    dr("ProductCode") = txtCode1.Text
                    dr("Quantity") = txtQty1.Text
                    dt.Rows.Add(dr)
                    bindgvorderlist()
                    ClearControl()
                    Label5.Visible = False
                Else
                    If txtCode1.Text = "" Or txtQty1.Text = "" Then
                        Label5.Visible = True
                        Label5.Text = "Please Enter Product / Quantity."
                        Exit Sub
                    End If
                End If

            End If

            Response.Redirect("ShopCart.aspx")
            'Else

            'Call BtnOrder_Click(sender, e)
            'If Not IsNothing(dt) Then
            'dt.Rows.Clear()
            'dt.Columns.Clear()
            'End If
            'Response.Redirect("ShopCart.aspx")

            'Label5.Text = "Please Enter Product."
            'Label5.Visible = True
            'End If


        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub BtnOrder_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BtnOrder.Click
        Try

            'If txtQty1.Text <> "" And txtCode1.Text <> "" Then
            '    ShopCartProcedure("Insert", CInt(txtQty1.Text), txtCode1.Text)
            'End If
            Dim flag As Boolean = False
            If txtQty1.Text = "" Then
                ' Label5.Text = "Product Qty Must Be Required."
                ' Label5.Visible = True
                ' Exit Sub
            ElseIf txtCode1.Text = "" Then
                'Label5.Text = "Product Code Must Be Required."
                'Label5.Visible = True
                'Exit Sub
            Else
                Dim productid As String = ReturnValue("select distinct product_kid from product_Master where product_code='" & txtCode1.Text.Trim & "' and product_isdeleted='0'")
                Dim tmpdr() As DataRow = dt.Select("ProductCode='" & txtCode1.Text.Trim & "'")

                If productid = "" Then
                    showmessage("Product Code is not valid please reenter it", Label5)
                    txtCode1.Text = ""
                    Exit Sub
                End If
                If tmpdr.Length > 0 Then
                    showmessage("Product code is already present", Label5)
                    Exit Sub
                End If
                If ShopCartProcedure("Insert", CInt(txtQty1.Text), txtCode1.Text) = True Then
                    Dim dr As DataRow = dt.NewRow()
                    dr("ProductCode") = txtCode1.Text
                    dr("Quantity") = txtQty1.Text
                    dt.Rows.Add(dr)
                    bindgvorderlist()
                    ClearControl()
                    Label5.Visible = False
                Else
                    Label5.Text = "Invalid Product Code."
                    Label5.Visible = True
                End If

            End If

            'If txtQty2.Text <> "" And txtCode2.Text <> "" Then
            '    If ShopCartProcedure("Insert", CInt(txtQty2.Text), txtCode2.Text) = True Then
            '        flag = True
            '    End If
            'Else
            '    Label5.Visible = True
            'End If

            'If txtQty3.Text <> "" And txtCode3.Text <> "" Then
            '    If ShopCartProcedure("Insert", CInt(txtQty3.Text), txtCode3.Text) = True Then
            '        flag = True
            '    End If
            'Else
            '    Label5.Visible = True
            'End If

            'If txtQty4.Text <> "" And txtCode4.Text <> "" Then
            '    If ShopCartProcedure("Insert", CInt(txtQty4.Text), txtCode4.Text) = True Then
            '        flag = True
            '    End If
            'Else
            '    Label5.Visible = True
            'End If

            'If txtQty5.Text <> "" And txtCode5.Text <> "" Then
            '    If ShopCartProcedure("Insert", CInt(txtQty5.Text), txtCode5.Text) = True Then
            '        flag = True
            '    End If
            'Else
            '    Label5.Visible = True
            'End If

            'If flag = True Then
            '    'CreateMessageAlert(Me.Page, "Your Product has sucssesfully added to shopping cart", "strKey")
            '    Response.Redirect("ShopCart.aspx")
            'End If

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub
End Class
