Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class BusinessAssociates_SSA_QuotationStatus
    Inherits System.Web.UI.Page

    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
            FillCustomer()
            lblQuotationBank.Visible = False
            lblQuotationDetail.visible = False
            gvQuotationBank.Visible = False
            tblQuotation.Visible = False
        End If
    End Sub

    Public Sub FillCustomer()
        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 = 2
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.Trim
            objAdapter.SelectCommand.Parameters("@SSAID").Value = txtSSAID.Text.Trim

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "CustomerDetails")

            If ObjDS.Tables(0).Rows.Count > 0 Then
                ddlCustomer.DataSource = ObjDS
                ddlCustomer.DataBind()
                ddlCustomer.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 cmdQuoNoSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

        lblQuotationBank.Visible = True
        gvQuotationBank.Visible = True

        ddlCustomer.SelectedValue = "-1"
        txtDateFrom.Text = ""
        txtDateTo.Text = ""
        tblQuotation.Visible = False
        lblQuotationDetail.Visible = False

        txtSearchMode.text = 1
        SelectQuotationBySearchMode(txtSearchMode.text)
    End Sub

    Protected Sub cmdCustSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        lblQuotationBank.Visible = True
        gvQuotationBank.Visible = True

        txtQuotationNo.Text = ""
        txtDateFrom.Text = ""
        txtDateTo.Text = ""
        tblQuotation.Visible = False
        lblQuotationDetail.Visible = False

        txtSearchMode.text = 2
        SelectQuotationBySearchMode(txtSearchMode.text)
    End Sub

    Protected Sub cmdDateSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        lblQuotationBank.Visible = True
        gvQuotationBank.Visible = True

        txtQuotationNo.Text = ""
        ddlCustomer.SelectedValue = "-1"
        tblQuotation.Visible = False
        lblQuotationDetail.Visible = False

        txtSearchMode.Text = 3
        SelectQuotationBySearchMode(txtSearchMode.Text)
    End Sub

    Public Sub SelectQuotationBySearchMode(ByVal SearchMode As Integer)
        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_CRMQuotationBySSAIDAndSearchMode_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@QuotationNo", SqlDbType.NVarChar, 50))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CustomerID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@DateFrom", SqlDbType.DateTime))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Dateto", SqlDbType.DateTime))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@SearchMode", SqlDbType.Int))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@SSAID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@QuotationNo").Value = txtQuotationNo.Text.Trim
            objAdapter.SelectCommand.Parameters("@CustomerID").Value = IIf(ddlCustomer.SelectedValue = "-1", "{00000000-0000-0000-0000-000000000000}", ddlCustomer.SelectedValue)
            objAdapter.SelectCommand.Parameters("@DateFrom").Value = IIf(txtDateFrom.Text.Trim = "", "01/01/1999", txtDateFrom.Text.Trim)
            objAdapter.SelectCommand.Parameters("@DateTo").Value = IIf(txtDateTo.Text.Trim = "", "01/01/1999", txtDateTo.Text.Trim)
            objAdapter.SelectCommand.Parameters("@SearchMode").Value = SearchMode  'Pass 1 For By QuotationNo , 2 For By CustomerID , 3 For By Date 
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.Trim
            objAdapter.SelectCommand.Parameters("@SSAID").Value = txtSSAID.Text.Trim

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "QuotationBank")

            gvQuotationBank.DataSource = ObjDS
            gvQuotationBank.DataBind()


            Dim I As Integer

            For I = 0 To gvQuotationBank.Rows.Count - 1
                If gvQuotationBank.Rows(I).Cells(10).Text = 0 Then   'Approved'
                    gvQuotationBank.Rows(I).Cells(11).Text = "Not Approved"
                ElseIf gvQuotationBank.Rows(I).Cells(10).Text = 1 Then   'Approved'
                    gvQuotationBank.Rows(I).Cells(11).Text = "Approved"
                ElseIf gvQuotationBank.Rows(I).Cells(10).Text = 2 Then   'Pending'
                    gvQuotationBank.Rows(I).Cells(11).Text = "Pending"
                ElseIf gvQuotationBank.Rows(I).Cells(10).Text = 3 Then    'Rejected'
                    gvQuotationBank.Rows(I).Cells(11).Text = "Rejected"
                End If
            Next
        Catch ex As Exception

        Finally
            cn.Close()
            cn.Dispose()
        End Try

    End Sub

    Protected Sub gvQuotationBank_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvQuotationBank.PageIndex = e.NewPageIndex
        SelectQuotationBySearchMode(txtSearchMode.text)
    End Sub

    Protected Sub gvQuotationBank_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvQuotationBank.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(3).Text = Format$(Convert.ToDateTime(e.Row.Cells(3).Text), "MMM-dd-yyyy")
        End If
    End Sub

    Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        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")

        Try

            cmdQuotation.CommandText = "Insert_CRMQuotation_Proc"
            cmdQuotation.CommandType = Data.CommandType.StoredProcedure
            cmdQuotation.Connection = con

            con.Open()

            cmdQuotation.Parameters.Add("@QuotationID", Data.SqlDbType.VarChar).Value = txtQuotationID.Text.Trim
            cmdQuotation.Parameters.Add("@QuotationNo", Data.SqlDbType.VarChar).Value = lblQuotationNo.Text.Trim
            cmdQuotation.Parameters.Add("@QuotationDate", Data.SqlDbType.VarChar).Value = txtQuotationDate.Text.Trim
            cmdQuotation.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = txtCustomerID.Text.Trim
            cmdQuotation.Parameters.Add("@QuotationAmount", SqlDbType.Decimal).Value = txtQuotationAmount.Text.Trim
            cmdQuotation.Parameters.Add("@ReceivedAmount", SqlDbType.Decimal).Value = txtReceivedAmount.Text.Trim
            cmdQuotation.Parameters.Add("@QuotationStatus", SqlDbType.NVarChar, 50).Value = ddlOrderStatus.SelectedValue.Trim
            cmdQuotation.Parameters.Add("@ApprovalStatus", SqlDbType.Int).Value = txtApprovalStatus.text
            cmdQuotation.Parameters.Add("@QuotationPaymentStatus", SqlDbType.NVarChar, 50).Value = txtPayment.text.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 = "Update"

            cmdQuotation.ExecuteNonQuery()

            con.Close()
            con.Dispose()

            tblQuotation.Visible = False
            lblQuotationDetail.Visible = False
            SelectQuotationBySearchMode(txtSearchMode.Text)

        Catch ex As Exception
            showmessage("Please Try again Later.", lblErrorData1)
            Exit Sub
        End Try
    End Sub

    Protected Sub cmdApprove_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Try
            Dim script As String

            script = "window.open(""{0}"", ""{1}"", ""{2}"");"

            script = String.Format(script, "sendquotation.aspx?type=approval&qid=" & txtQuotationID.Text.Trim, "_blank", "status=no,scollbars=yes,toolbars=no,width=500,height=600,right=300,left=0,top=0")

            ScriptManager.RegisterStartupScript(Page, GetType(Page), "Redirect", script, True)

            '    Session("CRMProspectsId") = txtQuotationID.Text
            '    Call Redirect("QuotationDetails.aspx", "_blank", "")
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub gvQuotationBank_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        tblQuotation.Visible = True
        lblQuotationDetail.Visible = True
        txtQuotationID.Text = gvQuotationBank.SelectedRow.Cells(1).Text
        lblQuotationNo.Text = gvQuotationBank.SelectedRow.Cells(2).Text
        lblQuotationDate.Text = gvQuotationBank.SelectedRow.Cells(3).Text
        lblCompanyName.Text = gvQuotationBank.SelectedRow.Cells(4).Text
        ddlOrderStatus.SelectedValue = IIf(gvQuotationBank.SelectedRow.Cells(5).Text = "&nbsp;", "-1", gvQuotationBank.SelectedRow.Cells(5).Text)
        txtCustomerID.Text = gvQuotationBank.SelectedRow.Cells(13).Text
        txtQuotationAmount.Text = gvQuotationBank.SelectedRow.Cells(14).Text
        txtReceivedAmount.Text = gvQuotationBank.SelectedRow.Cells(18).Text
        txtLastDate.Text = gvQuotationBank.SelectedRow.Cells(16).Text
        txtPayment.Text = gvQuotationBank.SelectedRow.Cells(15).Text
        txtQuotationDate.Text = gvQuotationBank.SelectedRow.Cells(17).Text
        txtApprovalStatus.Text = gvQuotationBank.SelectedRow.Cells(10).Text
    End Sub

    Protected Sub cmdSendQuot_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Try
            Dim script As String

            script = "window.open(""{0}"", ""{1}"", ""{2}"");"

            script = String.Format(script, "sendquotation.aspx?qid=" & txtQuotationID.Text.Trim, "_blank", "status=no,scollbars=yes,toolbars=no,width=500,height=600,right=300,left=0,top=0")

            ScriptManager.RegisterStartupScript(Page, GetType(Page), "Redirect", script, True)

            '    Session("CRMProspectsId") = txtQuotationID.Text
            '    Call Redirect("QuotationDetails.aspx", "_blank", "")
        Catch ex As Exception

        End Try
    End Sub
End Class
