Imports System.Data
Imports System.Data.SqlClient

Partial Class Buyer_PaymentDetails

    Inherits System.Web.UI.Page
    Shared amt As Double = 0.0      'variable to calculate net amount.
     
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        amt = 0.0
       
        Try

            Dim multipleshipTable As New DataTable
            If Page.IsPostBack = False Then
                hiderows()
                If Session("singleship") <> Nothing Then   'check value of session for single shipping
                    lblAddress.Visible = True   
                    lblAddress.Text = Session("singleship")
                    Session("singleship") = Nothing

                    multipleshipTable = Session("SingleShippTabel")
                    GvMultipleShip.Visible = True

                    GvMultipleShip.Columns(1).Visible = True
                    GvMultipleShip.Columns(4).Visible = True
                    '  GvMultipleShip.Columns(7).Visible = True
                    '  GvMultipleShip.Columns(8).Visible = True
                    '   GvMultipleShip.Columns(6).Visible = True
                    '  GvMultipleShip.Columns(9).Visible = True
                    Dim i As Integer = 0
                    For i = 0 To multipleshipTable.Rows.Count - 1
                        amt = amt + multipleshipTable.Rows(i).Item(12).ToString.Trim    'calculate mat
                    Next
                    GvMultipleShip.DataSource = multipleshipTable
                    GvMultipleShip.DataBind()

                    GvMultipleShip.Columns(1).Visible = False
                    GvMultipleShip.Columns(4).Visible = False
                    '   GvMultipleShip.Columns(7).Visible = False
                    '   GvMultipleShip.Columns(8).Visible = False
                    '   GvMultipleShip.Columns(6).Visible = False
                    ' GvMultipleShip.Columns(9).Visible = False

                    ' txtamt.Text = Request.QueryString("Amt")
                    txtamt.Text = FormatNumber(amt)
                    Session("SingleShippTabel") = Nothing

                Else

                    ' for multiple shipping
                    multipleshipTable = Session("Shippdetail")
                    Dim i As Integer = 0
                    For i = 0 To multipleshipTable.Rows.Count - 1
                        amt = amt + multipleshipTable.Rows(i).Item(12).ToString.Trim ' calculate net amount for multiple shipping
                    Next

                    GvMultipleShip.Visible = True
                    GvMultipleShip.Columns(1).Visible = True
                    ' GvMultipleShip.Columns(3).Visible = True
                    'GvMultipleShip.Columns(7).Visible = True
                    '  GvMultipleShip.Columns(8).Visible = True
                    ' GvMultipleShip.Columns(6).Visible = True

                    GvMultipleShip.DataSource = multipleshipTable
                    GvMultipleShip.DataBind()
                    GvMultipleShip.Columns(1).Visible = False
                    'GvMultipleShip.Columns(3).Visible = False
                    ' GvMultipleShip.Columns(7).Visible = False
                    ' GvMultipleShip.Columns(8).Visible = False
                    'GvMultipleShip.Columns(6).Visible = False
                    rowmsg.Visible = False
                    rowdd.Visible = False
                    rowcheck.Visible = False
                    rowbank.Visible = False
                    rowbranch.Visible = False
                    rowday.Visible = False
                    rowamount.Visible = False
                    rowbtn.Visible = False
                    'LoadValues()
                    'txtamt.Text = Request.QueryString("Amt")
                    txtamt.Text = FormatNumber(amt, 2)
                    Session("Shippdetail") = Nothing
                End If

            End If
        Catch ex As Exception
            txtamt.Text = ex.Message.ToString
        End Try
End Sub

    ' this is for enable or disable the row for bill mode cheque or draft
    Private Sub rdocheque_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdocheque.CheckedChanged, rdodd.CheckedChanged, rdoapi.CheckedChanged, rdopaycredit.CheckedChanged
        If rdocheque.Checked = True Then
            rowmsg.Visible = True
            lblmsg.Text = "Please Enter Cheque Details"
            rowdd.Visible = False
            rowcheck.Visible = True
            rowcheck.Cells(0).BgColor = "white"
            rowcheck.Cells(1).BgColor = "white"
            rowbank.Visible = True
            rowbank.Cells(0).BgColor = "whitesmoke"
            rowbank.Cells(1).BgColor = "whitesmoke"
            rowbranch.Visible = True
            rowbranch.Cells(0).BgColor = "white"
            rowbranch.Cells(1).BgColor = "white"
            rowday.Visible = True
            rowday.Cells(0).BgColor = "whitesmoke"
            rowday.Cells(1).BgColor = "whitesmoke"
            rowamount.Visible = True
            rowamount.Cells(0).BgColor = "white"
            rowamount.Cells(1).BgColor = "white"
            rowbtn.Visible = True
            rowbtn.Cells(0).BgColor = "whitesmoke"
            btnsubmit.Visible = True
            btnsubmit1.Visible = False

        ElseIf rdodd.Checked = True Then
            rowmsg.Visible = True
            lblmsg.Text = "Please Enter Demand Draft Details"
            rowcheck.Visible = False
            rowdd.Visible = True
            rowdd.Cells(0).BgColor = "white"
            rowdd.Cells(1).BgColor = "white"
            rowbank.Visible = True
            rowbank.Cells(0).BgColor = "whitesmoke"
            rowbank.Cells(1).BgColor = "whitesmoke"
            rowbranch.Visible = True
            rowbranch.Cells(0).BgColor = "white"
            rowbranch.Cells(1).BgColor = "white"
            rowday.Visible = True
            rowday.Cells(0).BgColor = "whitesmoke"
            rowday.Cells(1).BgColor = "whitesmoke"
            rowamount.Visible = True
            rowamount.Cells(0).BgColor = "white"
            rowamount.Cells(1).BgColor = "white"
            rowbtn.Visible = True
            rowbtn.Cells(0).BgColor = "whitesmoke"
            btnsubmit1.Visible = False
            btnsubmit.Visible = True
        ElseIf rdoapi.Checked = True Then

            rowmsg.Visible = True
            lblmsg.Text = "Performa Invoice is being sent to you through  email, your purchase order will be executed after receipt of your payment."
            btnsubmit1.Visible = True
            rowdd.Visible = False
            rowcheck.Visible = False
            rowbank.Visible = False
            rowbranch.Visible = False
            rowday.Visible = False
            rowamount.Visible = False
            rowbtn.Visible = True
            btnsubmit.Visible = False
        ElseIf rdopaycredit.Checked = True Then

            rowmsg.Visible = True
            lblmsg.Text = "To sell products at the lowest possible price is our concern and hence the advice our valuable customer to pay through other option than online payment option. If you still want to pay through payment gateway it will cut you 2.5 extra. This facility will be add soon."
            btnsubmit1.Visible = True
            rowdd.Visible = False
            rowcheck.Visible = False
            rowbank.Visible = False
            rowbranch.Visible = False
            rowday.Visible = False
            rowamount.Visible = False
            rowbtn.Visible = False
            btnsubmit.Visible = False
            btnsubmit1.Visible = False





        End If
    End Sub


Public sub insertdata1
end sub


    ' function to inseret the data in PaymentDetails table.
    Public Sub insertdata()
        Try
            Dim dateofsubmit, strsql As String
            Dim inttemp As Integer = 0
            'dateofsubmit = drpdd.SelectedItem.Text & "/" & drpmm.SelectedItem.Text & "/" & drpyyyy.SelectedItem.Text
            dateofsubmit = txtdate.Text.Trim
            'strsql = "insert into PaymentDetails(BillNo,UserId,BillDate,BillAmt,ModeofPayment,BankName,BankBranch,ChqNo,chqDate,chqAmt,DDNo,DDDate,DDAmt,Status) values(@a1,@a2,@a3,@a4,@a5,@a6,@a7,@a8,@a9,@a10,@a11,@a12,@a13,@a14)"
          
            Dim kid As String = NewPrimaryKey(Me.Page)      ' generate new kid
            kid = RemoveLiterals(kid)                       ' function to remove &lt and & &gt from kid
            If con.State = ConnectionState.Closed Then
                con.Open()
            End If
            Dim code As String = Generate_Code("select count(*) from PaymentDetails where PaymentDetails_code='", "PR")
            strsql = "insert into PaymentDetails(PaymentDetails_Kid,PaymentDetails_code,PaymentDetails_BillNo,PaymentDetails_BuyerId,PaymentDetails_BillDate,PaymentDetails_BillAmt,PaymentDetails_ModeofPayment,PaymentDetails_BankName,PaymentDetails_BankBranch,PaymentDetails_ChqNo,PaymentDetails_chqDate,PaymentDetails_chqAmt,PaymentDetails_DDNo,PaymentDetails_DDDate,PaymentDetails_DDAmt,PaymentDetails_Status) values(@b1,@b2,@a1,@a2,@a3,@a4,@a5,@a6,@a7,@a8,@a9,@a10,@a11,@a12,@a13,@a14)"
            cmd = New SqlCommand()
            cmd.Connection = con


            cmd.CommandText = strsql
            If rdocheque.Checked = True Then
                cmd.Parameters.AddWithValue("@b1", kid)
                cmd.Parameters.AddWithValue("@b2", code)
                cmd.Parameters.AddWithValue("@a1", ds.Tables("Header").Rows(0).Item("BillingHeader_Kid"))
                cmd.Parameters.AddWithValue("@a2", ds.Tables("Header").Rows(0).Item("BillingHeader_BuyerID"))
                cmd.Parameters.AddWithValue("@a3", ds.Tables("Header").Rows(0).Item("BillingHeader_BillDate"))
                cmd.Parameters.AddWithValue("@a4", Convert.ToDecimal(txtamt.Text))       ' bill amt
                cmd.Parameters.AddWithValue("@a5", "Cheque")                ' bill mode
                cmd.Parameters.AddWithValue("@a6", txtbank.Text.Trim)       ' bank name
                cmd.Parameters.AddWithValue("@a7", txtbranch.Text.Trim)     'branch name
                cmd.Parameters.AddWithValue("@a8", txtcheque.Text.Trim)     'cheque no
                cmd.Parameters.AddWithValue("@a9", dateofsubmit)            ' cheque date
                cmd.Parameters.AddWithValue("@a10", Convert.ToDecimal(txtamt.Text))      ' cheque amt
                cmd.Parameters.AddWithValue("@a11", "0")
                cmd.Parameters.AddWithValue("@a12", "0")
                cmd.Parameters.AddWithValue("@a13", inttemp)
                cmd.Parameters.AddWithValue("@a14", "PD")
                clearvalues()
            ElseIf rdodd.Checked = True Then
                cmd.Parameters.AddWithValue("@b1", kid)
                cmd.Parameters.AddWithValue("@b2", code)
                cmd.Parameters.AddWithValue("@a1", ds.Tables("Header").Rows(0).Item("BillingHeader_Kid"))    ' bill no
                cmd.Parameters.AddWithValue("@a2", ds.Tables("Header").Rows(0).Item("BillingHeader_BuyerID"))   ' buyer id 
                cmd.Parameters.AddWithValue("@a3", ds.Tables("Header").Rows(0).Item("BillingHeader_BillDate"))  ' bill date
                cmd.Parameters.AddWithValue("@a4", Convert.ToDecimal(txtamt.Text))
                cmd.Parameters.AddWithValue("@a5", "DD")                'bill mode
                cmd.Parameters.AddWithValue("@a6", txtbank.Text.Trim)   'bank name  
                cmd.Parameters.AddWithValue("@a7", txtbranch.Text.Trim) 'branch name
                cmd.Parameters.AddWithValue("@a8", "0")                 ' cheque no 
                cmd.Parameters.AddWithValue("@a9", "0")                 'cheque date
                cmd.Parameters.AddWithValue("@a10", inttemp)            'cheque amt
                cmd.Parameters.AddWithValue("@a11", txtdd.Text.Trim)    'dd no
                cmd.Parameters.AddWithValue("@a12", dateofsubmit)       'dd date
                cmd.Parameters.AddWithValue("@a13", Convert.ToDecimal(txtamt.Text))        'dd amt
                cmd.Parameters.AddWithValue("@a14", "PD")               'status
                clearvalues()

            End If

            cmd.ExecuteNonQuery()
            con.Close()
        Catch ex As Exception
            Response.Write(ex.Message.ToString)
        End Try
    End Sub

    ' function to clear the fields.
    Public Sub clearvalues()
        txtdd.Text = ""
        txtcheque.Text = ""
        txtbank.Text = ""
        txtbranch.Text = ""
        txtamt.Text = ""
    End Sub

    ' this is the code for submit button

Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnsubmit.Click
        lblerror.Visible = False
        If Not IsDate(txtdate.Text) Then        ' check for valid date
            lblerror.Visible = True
            lblerror.Text = "Please Enter Valid Date!"
            If Session("singleship") = Nothing Then
                rowamount.Visible = True
                rowbtn.Visible = True
            End If
            Exit Sub
        End If
        insertdata()            ' function to save the record in paymentDetails table
        If ds.Tables("Header").Rows.Count > 0 Then      'check for the any row available in billheader or not
            Dim strBNo As String = ds.Tables("Header").Rows(0).Item("BillingHeader_BillNo")
            'ds.Tables("Header").Clear()
            'ds.Tables("BD").Clear()
            'ds.Tables("Address").Clear()
            Dim con1 As New SqlConnection
            con1.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            con1.Open()
            'delete the items from the shopcart.
            Dim str As String = "delete  FROM ShopCart where ShopCart_BuyerUserId ='" & Session("BuyId") & "'"
            Dim da As New SqlDataAdapter
            cmd = New SqlCommand(str, con1)
            cmd.ExecuteNonQuery()
            con1.Close()
            cmd.Dispose()
            da.Dispose()
            con1.Dispose()

            Response.Redirect("../Buyer/reporttest.aspx?No='" & strBNo.Trim & "'")
        End If
    End Sub
    Protected Sub GvMultipleShip_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvMultipleShip.RowDataBound
        If e.Row.RowType = DataControlRowType.Footer Then
            Dim l1 As Label = CType(e.Row.Cells(4).FindControl("lblnetamount"), Label)
            l1.Text = FormatNumber(amt, 2)      'bind net amount to lable of gridview foot.
        End If
    End Sub
    ' function to hide the rows.
    Function hiderows()
        rowmsg.Visible = False
        rowdd.Visible = False
        rowcheck.Visible = False
        rowbank.Visible = False
        rowbranch.Visible = False
        rowday.Visible = False
        rowamount.Visible = False
        rowbtn.Visible = False
        Return 0
    End Function

   
   
    Protected Sub btnsubmit1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If ds.Tables("Header").Rows.Count > 0 Then      'check for the any row available in billheader or not
            Dim strBNo As String = ds.Tables("Header").Rows(0).Item("BillingHeader_BillNo")
            'ds.Tables("Header").Clear()
            'ds.Tables("BD").Clear()
            'ds.Tables("Address").Clear()
            Dim con1 As New SqlConnection
            con1.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            con1.Open()
            'delete the items from the shopcart.
            Dim str As String = "delete  FROM ShopCart where ShopCart_BuyerUserId ='" & Session("BuyId") & "'"
            Dim da As New SqlDataAdapter
            cmd = New SqlCommand(str, con1)
            cmd.ExecuteNonQuery()
            con1.Close()
            cmd.Dispose()
            da.Dispose()
            con1.Dispose()

            Response.Redirect("../Buyer/reporttest.aspx?No='" & strBNo.Trim & "'")
        End If
    End Sub
End Class
