Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class BusinessAssociates_SSA_PaymentStatus
    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
            lblPaymenthistory.Visible = False
            lblTotalPaidAmound.Visible = False
            txtChequeNo.Attributes.Add("onkeypress", "return blockNonNumbers(this, event, false, false);")
            txtReceivedAmount.Attributes.Add("onkeyup", "return blockNonNumbers(this, event, true, 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(11).Text = gvQuotationBank.Rows(I).Cells(12).Text Then
                    gvQuotationBank.Rows(I).Cells(0).Text = ""
                Else
                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

    Public Function GenerateCode() As String
        Dim PaymentCode As String
        PaymentCode = ""
        Try
            Dim i As String
            i = "1"
            i = Format(Val(i), "000")
            While True
                PaymentCode = "PAY" & Year(Date.Today) & Format(Month(Date.Today), "00") & Format(Day(Date.Today), "00") & i

                If IsAlreadyPresent(PaymentCode, txtVCompanyID.Text.Trim) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    Return PaymentCode
                    Exit Function
                End If
            End While
        Catch ex As Exception
        End Try
        Return PaymentCode
    End Function

    Public Function IsAlreadyPresent(ByVal PaymentNo As String, ByVal CompanyID As String) As Boolean
        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_CRMPaymentByPaymentNo_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@PaymentNo", SqlDbType.NVarChar, 50))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@PaymentNo").Value = PaymentNo
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = CompanyID

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "Payments")

            If ObjDS.Tables(0).Rows.Count > 0 Then
                Return True
            End If
        Catch ex As Exception
            Return False
        Finally
            cn.Close()
            cn.Dispose()
        End Try

    End Function

    Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        lblErrorData1.Text = ""
        lblErrorData1.Visible = False

        If Not IsNumeric(txtChequeNo.Text) Then
            showmessage("InValid Cheque/D.D. No.", lblErrorData1)
            txtChequeNo.Focus()
            Exit Sub
        End If

        If (DateDiff(DateInterval.Month, System.DateTime.Now, Convert.ToDateTime(txtChequeDate.Text)) > 5 Or DateDiff(DateInterval.Month, System.DateTime.Now, Convert.ToDateTime(txtChequeDate.Text)) < -5) Then
            showmessage("Cheque Date Can not be later/earlier than 5 Month.", lblErrorData1)
            cmdChequeDate.Focus()
            Exit Sub
        End If

        If txtReceivedAmount.Text.IndexOf(".") = 0 Or txtReceivedAmount.Text.IndexOf("0.") = 0 Then
            showmessage("InValid Received Amount.", lblErrorData1)
            txtReceivedAmount.Focus()
            Exit Sub
        End If

        txtPaymentID.Text = System.Guid.NewGuid.ToString

        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("Connection4Voffice"))
        Dim objAdapter As SqlDataAdapter

        cn.Open()


        Try
            objAdapter = New SqlDataAdapter("Insert_CRMPayment_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@PaymentID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@PaymentNo", SqlDbType.NVarChar, 50))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@QuotationID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@PaymentAmount", SqlDbType.Decimal, 18))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@PaymentMode", SqlDbType.Int))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@ChequeNo", SqlDbType.NVarChar, 50))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@PaymentDate", SqlDbType.DateTime))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@ChequeDate", SqlDbType.DateTime))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@BankName", SqlDbType.NVarChar, 100))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@BranchName", SqlDbType.NVarChar, 100))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CityName", SqlDbType.NVarChar, 100))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@UserID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@UserMode", SqlDbType.Int))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 10))

            objAdapter.SelectCommand.Parameters("@PaymentID").Value = txtPaymentID.Text.Trim
            objAdapter.SelectCommand.Parameters("@PaymentNo").Value = lblPaymentNo.Text.Trim
            objAdapter.SelectCommand.Parameters("@QuotationID").Value = txtQuotationID.Text.Trim
            objAdapter.SelectCommand.Parameters("@PaymentAmount").Value = Convert.ToDecimal(txtReceivedAmount.Text)
            objAdapter.SelectCommand.Parameters("@PaymentMode").Value = ddlPaymentType.SelectedValue
            objAdapter.SelectCommand.Parameters("@ChequeNo").Value = txtChequeNo.Text.Trim
            objAdapter.SelectCommand.Parameters("@PaymentDate").Value = System.DateTime.Now
            objAdapter.SelectCommand.Parameters("@ChequeDate").Value = txtChequeDate.Text
            objAdapter.SelectCommand.Parameters("@BankName").Value = txtBankName.Text.Trim
            objAdapter.SelectCommand.Parameters("@BranchName").Value = txtBranch.Text.Trim
            objAdapter.SelectCommand.Parameters("@CityName").Value = txtCity.Text.Trim
            objAdapter.SelectCommand.Parameters("@UserID").Value = txtSSAID.Text.Trim
            objAdapter.SelectCommand.Parameters("@UserMode").Value = 1
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.Trim
            objAdapter.SelectCommand.Parameters("@FormName").Value = "V_CRM_Payment"
            objAdapter.SelectCommand.Parameters("@Mode").Value = "Insert"

            objAdapter.SelectCommand.ExecuteNonQuery()

            If lblBalanceAmount.Text = "0" Then
                AddCRMQuotationPaymentStatus("Paid")
            Else
                AddCRMQuotationPaymentStatus("Part Payment")
            End If

            tblQuotation.Visible = False
            lblQuotationDetail.Visible = False
            SelectQuotationBySearchMode(txtSearchMode.Text)

        Catch ex As Exception
            showmessage("Please Try again Later", lblErrorData1)
        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Sub

    Public Sub AddCRMQuotationPaymentStatus(ByVal PaymentStatus As String)
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("Connection4Voffice"))
        Dim objAdapter As SqlDataAdapter

        cn.Open()


        Try
            objAdapter = New SqlDataAdapter("Insert_CRMQuotationPaymentStatus_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@QuotationID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@ReceivedAmount", SqlDbType.Decimal, 18))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@QuotationStatus", SqlDbType.NVarChar, 50))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@QuotationPaymentStatus", SqlDbType.NVarChar, 50))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 10))

            objAdapter.SelectCommand.Parameters("@QuotationID").Value = txtQuotationID.Text.Trim
            If rbFullPayment.Checked = True Then
                objAdapter.SelectCommand.Parameters("@ReceivedAmount").Value = Convert.ToDecimal(lblQuotationAmount.Text.Trim)
            ElseIf rbPartPayment.Checked = True Then
                objAdapter.SelectCommand.Parameters("@ReceivedAmount").Value = Convert.ToDecimal(txtReceivedAmount.Text.Trim)
            End If

            objAdapter.SelectCommand.Parameters("@QuotationStatus").Value = "Closed"
            objAdapter.SelectCommand.Parameters("@QuotationPaymentStatus").Value = PaymentStatus

            objAdapter.SelectCommand.Parameters("@FormName").Value = "Payment Status"
            objAdapter.SelectCommand.Parameters("@Mode").Value = "Update"

            objAdapter.SelectCommand.ExecuteNonQuery()

        Catch ex As Exception
            Throw ex
        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Sub

    Protected Sub gvQuotationBank_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        lblPaymenthistory.Visible = False
        lblTotalPaidAmound.Visible = False
        gvPayment.Visible = False

        rbFullPayment.Checked = False
        rbPartPayment.Checked = False
        tblQuotation.Visible = True
        lblQuotationDetail.Visible = True
        txtQuotationID.Text = gvQuotationBank.SelectedRow.Cells(1).Text
        lblQuotationNo.Text = gvQuotationBank.SelectedRow.Cells(2).Text
        lblPaymentNo.text = GenerateCode()
        lblQuotationDate.Text = gvQuotationBank.SelectedRow.Cells(3).Text
        lblCompanyName.Text = gvQuotationBank.SelectedRow.Cells(4).Text
        txtCustomerID.Text = gvQuotationBank.SelectedRow.Cells(13).Text
        txtQuotationAmount.Text = gvQuotationBank.SelectedRow.Cells(11).Text
        lblQuotationAmount.Text = gvQuotationBank.SelectedRow.Cells(11).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
        rbPartPayment.Checked = True
        SelectPaymentAmountByQuotationID()

        txtChequeNo.Text = ""
        txtChequeDate.Text = ""
        txtBankName.Text = ""
        txtBranch.Text = ""
        txtCity.Text = ""

        Dim I As Integer

        For I = 0 To gvQuotationBank.Rows.Count - 1
            If gvQuotationBank.Rows(I).Cells(11).Text = gvQuotationBank.Rows(I).Cells(12).Text Then
                gvQuotationBank.Rows(I).Cells(0).Text = ""
            Else
            End If
        Next
        ' lblPayableAmount.Text = (60 * txtQuotationAmount.Text) / 100
        'txtReceivedAmount.Text = lblPayableAmount.Text
        'lblBalanceAmount.Text = lblPayableAmount.Text - txtReceivedAmount.Text

    End Sub

    Protected Sub rbFullPayment_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        SelectPaymentAmountByQuotationID()

        'txtReceivedAmount.Text = lblPayableAmount.Text
        'txtReceivedAmount.Enabled = False
        'lblBalanceAmount.Text = lblPayableAmount.Text - txtReceivedAmount.Text
    End Sub

    Protected Sub rbPartPayment_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        'lblPayableAmount.Text = (60 * txtQuotationAmount.Text) / 100
        SelectPaymentAmountByQuotationID()
        'txtReceivedAmount.Text = lblPayableAmount.Text
        'txtReceivedAmount.Enabled = True
        'lblBalanceAmount.Text = lblPayableAmount.Text - txtReceivedAmount.Text
    End Sub

    Public Sub SelectPaymentAmountByQuotationID()
        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_CRMPaymentAmountByQuotationID_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@QuotationID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@QuotationID").Value = txtQuotationID.Text.Trim
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.Trim

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "Payments")

            If IsDBNull(ObjDS.Tables(0).Rows(0).Item("PayableAmount")) Then

                rbPartPayment.Enabled = True
                If rbFullPayment.Checked = True Then
                    lblPayableAmount.Text = txtQuotationAmount.Text
                    txtReceivedAmount.Text = lblPayableAmount.Text
                    txtReceivedAmount.Enabled = False
                    lblBalanceAmount.Text = txtQuotationAmount.Text - txtReceivedAmount.Text
                Else
                    lblPayableAmount.Text = (60 * txtQuotationAmount.Text) / 100
                    txtReceivedAmount.Text = lblPayableAmount.Text
                    txtReceivedAmount.Enabled = True
                    lblBalanceAmount.Text = txtQuotationAmount.Text - txtReceivedAmount.Text
                End If
                
            Else
                rbFullPayment.Checked = True
                rbPartPayment.Checked = False
                rbPartPayment.Enabled = False
                If rbFullPayment.Checked = True Then
                    lblPayableAmount.Text = txtQuotationAmount.Text - ObjDS.Tables(0).Rows(0).Item("PayableAmount")
                    txtReceivedAmount.Text = lblPayableAmount.Text
                    txtReceivedAmount.Enabled = False
                    lblBalanceAmount.Text = lblPayableAmount.Text - txtReceivedAmount.Text
                Else
                    lblPayableAmount.Text = txtQuotationAmount.Text - ObjDS.Tables(0).Rows(0).Item("PayableAmount")
                    txtReceivedAmount.Text = lblPayableAmount.Text
                    txtReceivedAmount.Enabled = True
                    lblBalanceAmount.Text = lblPayableAmount.Text - txtReceivedAmount.Text
                End If
            End If
        Catch ex As Exception
        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Sub

    Protected Sub txtReceivedAmount_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            lblBalanceAmount.Text = lblQuotationAmount.Text - txtReceivedAmount.Text
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub lnkPayment_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim KID As String
        Dim lnk As LinkButton
        lnk = CType(sender, LinkButton)
        KID = lnk.CommandArgument
        txtQuotationID.Text = KID

        Try
            tblQuotation.Visible = False
            lblQuotationDetail.Visible = False

            lblPaymenthistory.Visible = True
            lblTotalPaidAmound.Visible = True
            gvPayment.Visible = True

            lblErrorData1.Visible = False

            SelectQuotationByQuotationID()
            SelectPaymentByQuotationID()

            Dim I As Integer

            For I = 0 To gvQuotationBank.Rows.Count - 1
                If gvQuotationBank.Rows(I).Cells(11).Text = gvQuotationBank.Rows(I).Cells(12).Text Then
                    gvQuotationBank.Rows(I).Cells(0).Text = ""
                Else
                End If
            Next

        Catch ex As Exception

        End Try
        

    End Sub

    Protected Sub gvPayment_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvPayment.PageIndex = e.NewPageIndex
        ' SelectQuotationBySearchMode(txtSearchMode.Text)
    End Sub

    Public Sub SelectPaymentByQuotationID()
        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_CRMPaymentByQuotationID_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@QuotationID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@QuotationID").Value = txtQuotationID.Text.Trim
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.Trim

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "Payments")

            gvPayment.DataSource = ObjDS
            gvPayment.DataBind()

            Dim J As Integer
            Dim Amount As Decimal
            Amount = 0

            For J = 0 To gvPayment.Rows.Count - 1
                If gvPayment.Rows(J).Cells(3).Text = 1 Then
                    gvPayment.Rows(J).Cells(4).Text = "Cheque"
                ElseIf gvPayment.Rows(J).Cells(3).Text = 2 Then
                    gvPayment.Rows(J).Cells(4).Text = "Demand Draft"
                End If
                If gvPayment.Rows(J).Cells(6).Text = 2 Then
                    gvPayment.Rows(J).Cells(7).Text = FillAdminDetails(gvPayment.Rows(J).Cells(5).Text.ToString) & " (Admin)"
                ElseIf gvPayment.Rows(J).Cells(6).Text = 1 Then
                    gvPayment.Rows(J).Cells(7).Text = FillSSADetails(gvPayment.Rows(J).Cells(5).Text.ToString) & " (SSA)"
                End If
                Amount = Amount + gvPayment.Rows(J).Cells(2).Text
            Next

            lblTotalPaidAmound.Text = "Total Received Amount : " & Amount

        Catch ex As Exception
            Throw ex
        Finally
            cn.Close()
            cn.Dispose()
        End Try

    End Sub

    Public Function FillAdminDetails(ByVal AdminID As String) As String

        Dim Name As String

        Name = ""

        Dim Dr As DataRow
        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_EmployeeByAdminID_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@AdminID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@AdminID").Value = AdminID.ToString
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.ToString

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "EmployeeDetails")

            If ObjDS.Tables(0).Rows.Count > 0 Then
                Dr = ObjDS.Tables(0).Rows(0)
                Name = Dr("V_Emp_Name")
            End If

        Catch ex As Exception

        Finally
            cn.Close()
            cn.Dispose()
        End Try

        Return Name

        ObjDS.Clear()
        ObjDS.Dispose()

    End Function

    Public Function FillSSADetails(ByVal SSAID As String) As String

        Dim Name As String

        Name = ""
        Dim cmd As New Data.SqlClient.SqlCommand()
        Dim con As New Data.SqlClient.SqlConnection
        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")

        Dim Str As String
        Str = "select * From SSA_Master Where SSA_KID='" & SSAID.ToString & "'"
        If con.State = Data.ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        cmd = New SqlCommand(Str, con)
        Dim adp As New SqlDataAdapter
        Dim ds As New DataSet
        Dim Dr As DataRow
        adp.SelectCommand = cmd
        adp.Fill(ds)

        If ds.Tables(0).Rows.Count > 0 Then
            Dr = ds.Tables(0).Rows(0)
            Name = Dr("SSA_FirstName") & "  " & Dr("SSA_LastName")
        End If

        Return Name
        ds.Clear()
        ds.Dispose()

        con.Close()
        con.Dispose()

    End Function

    Public Sub SelectQuotationByQuotationID()
        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_CRMQuotationByQuotationID_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@QuotationID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@QuotationID").Value = txtQuotationID.Text.Trim
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.Trim

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "QuotationBank")

            If ObjDS.Tables(0).Rows.Count > 0 Then
                lblPaymenthistory.Text = "<b>Payment History Of :</b> " & ObjDS.Tables(0).Rows(0).Item("V_CRM_QuotationNo")
            End If

        Catch ex As Exception
            Throw ex
        Finally
            cn.Close()
            cn.Dispose()
        End Try

    End Sub

    Protected Sub gvPayment_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvPayment.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(8).Text = Format$(Convert.ToDateTime(e.Row.Cells(8).Text), "MMM-dd-yyyy")
            e.Row.Cells(10).Text = Format$(Convert.ToDateTime(e.Row.Cells(10).Text), "MMM-dd-yyyy")
        End If
    End Sub
End Class
