Imports System.Data
Imports System.Data.SqlClient
Partial Class BusinessAssociates_SSA_ViewProspect
    Inherits System.Web.UI.Page
    Dim QueryStr As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            txtVCompanyID.Text = "f6b59677-9946-489c-8628-465872369428"
            txtSSAID.Text = Session("SSAID").ToString
            tblCustomer.Visible = False
            FillGrid()
        End If
    End Sub

    Private Sub BindCategory()
        Try
            Dim DsCategory As New DataSet

            QueryStr = "select Category_Kid,Category_Name from Category_Master where Category_ParentId='Root Category' and Category_IsDeleted='0' order by category_name asc"

            DsCategory = SelectCategory(QueryStr)

            If DsCategory.Tables(0).Rows.Count > 0 Then
                chkCategory.DataSource = DsCategory
                chkCategory.DataBind()
            Else
                chkCategory.Items.Clear()
            End If
        Catch ex As Exception
            ex.Message.ToString()
        End Try
    End Sub

    Public Shared Function SelectCategory(ByVal Query As String) As DataSet
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet

        cn.Open()

        Try
            objAdapter = New SqlDataAdapter(Query, cn)
            objAdapter.SelectCommand.CommandType = CommandType.Text

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "Category")

            Return ObjDS

        Catch ex As Exception
            Throw ex
        Finally
            cn.Close()
            cn.Dispose()
        End Try

    End Function

    Public Sub FillGrid()
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("Connection4Voffice"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet

        cn.Open()

        Try
            objAdapter = New SqlDataAdapter("Select_CRMCustomerBySSAIDAndMode_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CustomerMode", SqlDbType.Int))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@SSAID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@CustomerMode").Value = 1
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.Trim
            objAdapter.SelectCommand.Parameters("@SSAID").Value = txtSSAID.Text.Trim

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "CustomerDetails")

            gvCustomer.DataSource = ObjDS
            gvCustomer.DataBind()

            ObjDS.Clear()
            ObjDS.Dispose()

            Dim J As Integer

            For J = 0 To gvCustomer.Rows.Count - 1

                If gvCustomer.Rows(J).Cells(3).Text = "1" Then
                    gvCustomer.Rows(J).Cells(4).Text = "Government"
                ElseIf gvCustomer.Rows(J).Cells(3).Text = "2" Then
                    gvCustomer.Rows(J).Cells(4).Text = "Private"
                End If
            Next

        Catch ex As Exception

        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Sub

    Protected Sub gvCustomer_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvCustomer.PageIndex = e.NewPageIndex
        FillGrid()
    End Sub

    Public Sub BindCity()
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("Connection4Voffice"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet

        cn.Open()

        Try
            objAdapter = New SqlDataAdapter("Select_CityByState_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@StateID", SqlDbType.Int))

            objAdapter.SelectCommand.Parameters("@StateID").Value = -1
            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "Location_City")

            If ObjDS.Tables(0).Rows.Count > 0 Then
                ddlCity.DataSource = ObjDS
                ddlCity.DataBind()
                ddlCity.Items.Insert(0, New ListItem("Select", "-1"))
            Else
                ddlCity.Items.Clear()
                ddlCity.Items.Insert(0, New ListItem("Select", "-1"))
            End If

            ObjDS.Clear()
            ObjDS.Dispose()

        Catch ex As Exception

        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Sub

    Protected Sub gvCustomer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        tblCustomer.Visible = True
        txtLeadID.Text = gvCustomer.SelectedRow.Cells(1).Text
        txtAdminID.Text = gvCustomer.SelectedRow.Cells(16).Text
        txtCompanyName.Text = Server.HtmlDecode(gvCustomer.SelectedRow.Cells(2).Text)
        ddlType.SelectedValue = Server.HtmlDecode(gvCustomer.SelectedRow.Cells(3).Text)
        BindCity()
        ddlCity.SelectedValue = gvCustomer.SelectedRow.Cells(6).Text
        txtContactPerson.Text = Server.HtmlDecode(gvCustomer.SelectedRow.Cells(5).Text)
        txtAddress.Text = IIf(gvCustomer.SelectedRow.Cells(11).Text = "&nbsp;", "", Server.HtmlDecode(gvCustomer.SelectedRow.Cells(11).Text))
        txtEmailID.Text = IIf(gvCustomer.SelectedRow.Cells(10).Text = "&nbsp;", "", Server.HtmlDecode(gvCustomer.SelectedRow.Cells(10).Text))
        txtPhone.Text = IIf(gvCustomer.SelectedRow.Cells(9).Text = "&nbsp;", "", Server.HtmlDecode(gvCustomer.SelectedRow.Cells(9).Text))
        txtRemarks.Text = IIf(gvCustomer.SelectedRow.Cells(13).Text = "&nbsp;", "", Server.HtmlDecode(gvCustomer.SelectedRow.Cells(13).Text))
        txtRequirement.Text = IIf(gvCustomer.SelectedRow.Cells(12).Text = "&nbsp;", "", Server.HtmlDecode(gvCustomer.SelectedRow.Cells(12).Text))

        BindCategory()

        Dim CategoryStr As String

        CategoryStr = Server.HtmlDecode(gvCustomer.SelectedRow.Cells(18).Text) & "-"
        txtCategory.Text = Server.HtmlDecode(gvCustomer.SelectedRow.Cells(18).Text)

        Dim j As Integer
        For j = 0 To chkCategory.Items.Count - 1
            If InStr(CategoryStr, chkCategory.Items(j).Value & "-") Then
                chkCategory.Items(j).Selected = True
            End If
        Next

        ' txtQuotationAmt.Text = gvCustomer.SelectedRow.Cells(11).Text
        Dim dob As Date
        dob = gvCustomer.SelectedRow.Cells(8).Text


    End Sub

    Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Try
            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim cmdQuotation As New Data.SqlClient.SqlCommand()
            Dim con As New Data.SqlClient.SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("Connection4Voffice")

            If txtPhone.Text.Trim = "" And txtEmailID.Text.Trim = "" Then
                showmessage("Please Enter Phone Or Email Address.", lblErrorData1)
                txtPhone.Focus()
                Exit Sub
            End If

            Dim Cat As String

            Cat = ""
            Dim j As Integer
            For j = 0 To chkCategory.Items.Count - 1
                If chkCategory.Items(j).Selected = True Then
                    Cat += chkCategory.Items(j).Value & "-"
                End If
            Next

            txtCategory.Text = Cat.Trim("-")

            If txtCategory.Text.Trim = "" Then
                showmessage("Please Select Atleast One Category.", lblErrorData1)
                Exit Sub
            End If

            If rbGenQuotation.Checked = True Then

                If txtLastDate.Text = "" Then
                    showmessage("Last Date Of Quotation Submission Can not be null.", lblErrorData1)
                    ibtndob.Focus()
                    Exit Sub
                End If

                If IsDate(txtLastDate.Text) Then
                    showmessage("Invalid Last Date Of Quotation Submission.", lblErrorData1)
                    ibtndob.Focus()
                    Exit Sub
                End If


                If txtQuotationAmt.Text = "" Then
                    showmessage("Quotation Amount Can not be null.", lblErrorData1)
                    txtQuotationAmt.Focus()
                    Exit Sub
                End If

                If ddlOrderStatus.SelectedValue = "-1" Then
                    showmessage("Please Select Order Status.", lblErrorData1)
                    ddlOrderStatus.Focus()
                    Exit Sub
                End If

                If ddlPayment.SelectedValue = "-1" Then
                    showmessage("Please Select Invoice Payment Status.", lblErrorData1)
                    ddlPayment.Focus()
                    Exit Sub
                End If

            End If

            cmd.CommandText = "Insert_CRMCustomer_Proc"
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.Connection = con

            con.Open()
            cmd.Parameters.Add("@CustomerID", Data.SqlDbType.VarChar).Value = txtLeadID.Text.Trim

            Dim LeadDate As Date

            LeadDate = System.DateTime.Now

            cmd.Parameters.Add("@LeadDate", Data.SqlDbType.DateTime).Value = Format(LeadDate, "MMM-dd-yyyy")

            cmd.Parameters.Add("@CompanyName", Data.SqlDbType.NVarChar, 100).Value = txtCompanyName.Text.Trim
            If ddlType.SelectedIndex > 0 Then
                cmd.Parameters.Add("@CompanyType", Data.SqlDbType.NVarChar, 100).Value = ddlType.SelectedValue
            End If

            If ddlCity.SelectedIndex > 0 Then
                cmd.Parameters.Add("@CityID", Data.SqlDbType.NVarChar, 100).Value = ddlCity.SelectedValue
            End If

            cmd.Parameters.Add("@ContactName", Data.SqlDbType.NVarChar, 50).Value = txtContactPerson.Text.Trim

            cmd.Parameters.Add("@PhoneNo", Data.SqlDbType.NVarChar, 50).Value = txtPhone.Text.Trim

            cmd.Parameters.Add("@EmailAddress", Data.SqlDbType.NVarChar, 50).Value = txtEmailID.Text.Trim

            cmd.Parameters.Add("@CompanyAddress", Data.SqlDbType.NVarChar, 50).Value = txtAddress.Text.Trim

            cmd.Parameters.Add("@Requirements", Data.SqlDbType.NVarChar, 50).Value = txtRequirement.Text.Trim

            cmd.Parameters.Add("@Remarks", Data.SqlDbType.NVarChar, 50).Value = txtRemarks.Text.Trim

            cmd.Parameters.Add("@CategoryName", Data.SqlDbType.NVarChar, 500).Value = txtCategory.Text.Trim

            cmd.Parameters.Add("@CustomerMode", Data.SqlDbType.NVarChar, 50).Value = 2   'For Lead

            cmd.Parameters.Add("@SSAID", Data.SqlDbType.NVarChar, 50).Value = txtSSAID.Text.Trim

            cmd.Parameters.Add("@AdminID", Data.SqlDbType.NVarChar, 50).Value = txtAdminID.Text.Trim

            cmd.Parameters.Add("@CompanyID", Data.SqlDbType.NVarChar, 50).Value = txtVCompanyID.Text.Trim

            cmd.Parameters.Add("@FormName", Data.SqlDbType.NVarChar, 100).Value = Me.Title.ToString
            cmd.Parameters.Add("@Mode", Data.SqlDbType.NVarChar, 100).Value = "Update"

            cmd.ExecuteNonQuery()

            con.Close()
            con.Dispose()

            If rbGenQuotation.Checked = True Then
                con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("Connection4Voffice")

                cmdQuotation.CommandText = "Insert_CRMQuotation_Proc"
                cmdQuotation.CommandType = Data.CommandType.StoredProcedure
                cmdQuotation.Connection = con

                con.Open()

                cmdQuotation.Parameters.Add("@QuotationID", Data.SqlDbType.VarChar).Value = System.Guid.NewGuid.ToString
                cmdQuotation.Parameters.Add("@QuotationNo", Data.SqlDbType.VarChar).Value = "QUOT20080706001"
                cmdQuotation.Parameters.Add("@QuotationDate", Data.SqlDbType.VarChar).Value = System.DateTime.Now.Date
                cmdQuotation.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = txtLeadID.Text.Trim
                cmdQuotation.Parameters.Add("@QuotationAmount", SqlDbType.Decimal).Value = txtQuotationAmt.Text.Trim
                cmdQuotation.Parameters.Add("@ReceivedAmount", SqlDbType.Decimal).Value = "0.0"
                cmdQuotation.Parameters.Add("@ApprovalStatus", SqlDbType.Int).Value = 0
                cmdQuotation.Parameters.Add("@QuotationStatus", SqlDbType.NVarChar, 50).Value = ddlOrderStatus.SelectedValue.Trim
                cmdQuotation.Parameters.Add("@QuotationPaymentStatus", SqlDbType.NVarChar, 50).Value = ddlPayment.SelectedValue.Trim
                cmdQuotation.Parameters.Add("@QuotationLastDate", SqlDbType.DateTime).Value = txtLastDate.Text
                cmdQuotation.Parameters.Add("@CompanyID", SqlDbType.VarChar).Value = txtVCompanyID.Text.Trim
                cmdQuotation.Parameters.Add("@SSAID", SqlDbType.VarChar).Value = txtSSAID.Text.Trim
                cmdQuotation.Parameters.Add("@QuotationMode", SqlDbType.Int).Value = 1
                cmdQuotation.Parameters.Add("@FormName", SqlDbType.NVarChar, 50).Value = "QuotationBankSSA"
                cmdQuotation.Parameters.Add("@Mode", SqlDbType.VarChar, 10).Value = "Insert"

                cmdQuotation.ExecuteNonQuery()

                con.Close()
                con.Dispose()

            End If
            ClearFields()

            showmessage("Record Added Successfully.", lblErrorData1)
            FillGrid()
            tblCustomer.Visible = False
        Catch ex As Exception
            showmessage("Please try again later.", lblErrorData1)
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Sub

    Protected Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        tblCustomer.Visible = False
    End Sub

    Public Sub ClearFields()
        txtCompanyName.Text = ""
        ddlType.SelectedValue = "-1"
        ddlCity.SelectedValue = "-1"
        txtContactPerson.Text = ""
        txtAddress.Text = ""
        txtEmailID.Text = ""
        txtPhone.Text = ""
        txtRemarks.Text = ""
        txtRequirement.Text = ""
        txtQuotationAmt.Text = ""
    End Sub

End Class
