
Imports System.Data
Imports System.Data.SqlClient
Partial Class Admin_pending_payment
    Inherits System.Web.UI.Page
    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
    Dim cmd As New SqlCommand
   

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("AdminID") Is Nothing Then
            Response.Redirect("AdminLogin.aspx?sesn=expire")
            Exit Sub
        End If
        If Not IsPostBack Then
            If Session("AdminID") = Nothing Then
                Response.Redirect("AdminLogin.aspx")
            End If
            fillprdouctgrid()

            rowoption.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

            rowtitle.Visible = False
            rowpayment.Visible = False
        End If

    End Sub
    Function fillprdouctgrid()
        Dim str As String = "SELECT   Distinct SupplierProductInvoice.SupplierProductInvoice_Kid,  SupplierInv.SupplierInv_Code,SupplierInv.SupplierInv_No,convert(nvarchar,SupplierInv.SupplierInv_Date,103) as SupplierInv_Date , SupplierInv.SupplierInv_NetAmount, OrderRequestToSupplierProduct.OrderRequestToSupplierProduct_SupplierId, Supplier_Master.Supplier_Name,SupplierInv.SupplierInv_Kid, SupplierProductInvoice.SupplierProductInvoice_Status,   SupplierInv.SupplierInv_SupplierDispatchProdDetailId as SupplierInv_SDPD FROM   Supplier_Master INNER JOIN  OrderRequestToSupplierProduct ON  Supplier_Master.Supplier_Kid = OrderRequestToSupplierProduct.OrderRequestToSupplierProduct_SupplierId CROSS JOIN  SupplierProductInvoice INNER JOIN  SupplierInv ON SupplierProductInvoice.SupplierProductInvoice_SupplierInvId = SupplierInv.SupplierInv_Kid where SupplierProductInvoice.SupplierProductInvoice_Status='Pending' "
        gvpendingpyment.Columns(1).Visible = True
        gvpendingpyment.Columns(7).Visible = True
        ' gvpendingpyment.Columns(8).Visible = True
        fillgridview(gvpendingpyment, str)
        gvpendingpyment.Columns(1).Visible = False
        gvpendingpyment.Columns(7).Visible = False
        ' gvpendingpyment.Columns(8).Visible = False
    End Function


    Function fillgridview(ByVal gv As GridView, ByVal str As String)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        Dim da As New SqlDataAdapter(str, con)
        Dim ds As New DataSet
        da.Fill(ds)
        gv.DataSource = ds.Tables(0)
        gv.DataBind()
        con.Close()


    End Function
    Protected Sub lnksinvno_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim lnksinvno As LinkButton = CType(sender, LinkButton)
        Dim Str As String = "SELECT     Product_Master.Product_Name, SupplierProductInvoice.SupplierProductInvoice_Quantity, SupplierProductInvoice.SupplierProductInvoice_UnitPrice, " & _
                       " SupplierProductInvoice.SupplierProductInvoice_ShippingCharges, SupplierProductInvoice.SupplierProductInvoice_TotalPrice, SupplierInv_ExciseDuty, SupplierInv_EduCess, SupplierInv_VAT, SupplierInv_CST, SupplierInv_CSTAgainstC, (SupplierInv_ExciseDuty+SupplierInv_EduCess+ SupplierInv_VAT + SupplierInv_CST + SupplierInv_CSTAgainstC) as amt   FROM   SupplierInv INNER JOIN " & _
                       " SupplierProductInvoice ON SupplierInv.SupplierInv_Kid = SupplierProductInvoice.SupplierProductInvoice_SupplierInvId INNER JOIN" & _
                       " Product_Master ON SupplierProductInvoice.SupplierProductInvoice_ProductId = Product_Master.Product_Kid where SupplierInv.SupplierInv_Kid='" & lnksinvno.CommandArgument & "'"
        fillgridview(gvproudctdetails, Str)
        lblvatdetails.Visible = True
        rowvat.Visible = True
        lblinfomsg.Visible = False
        rowtitle.Visible = False
        rowoption.Visible = False
        rowpayment.Visible = False

    End Sub

    Protected Sub gvpendingpyment_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvpendingpyment.PageIndexChanging
         gvpendingpyment.PageIndex = e.NewPageIndex
        fillprdouctgrid()
        rowvat.Visible = False
        rowtitle.Visible = False
        rowpayment.Visible = False
        lblinfomsg.Visible = False
        rowoption.Visible = False
    End Sub



    Protected Sub gvpendingpyment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvpendingpyment.SelectedIndexChanged
        lblinfomsg.Visible = False
        rowtitle.Visible = True
        rowoption.visible = True
        rowpayment.Visible = True
        rowvat.Visible = False
    End Sub

    Protected Sub rdocheque_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdocheque.CheckedChanged, rdodd.CheckedChanged

        If rdocheque.Checked = True Then
            rowmsg.Visible = True
            lblmsg.Text = "Please Enter Cheque Details"
            rowdd.Visible = False
            rowcheck.Visible = True
            rowbank.Visible = True
            rowbranch.Visible = True
            rowday.Visible = True
            rowamount.Visible = True
            rowbtn.Visible = True
            btnsubmit.Visible = True

        ElseIf rdodd.Checked = True Then
            rowmsg.Visible = True
            lblmsg.Text = "Please Enter Demand Draft Details"
            rowcheck.Visible = False
            rowdd.Visible = True
            rowbank.Visible = True
            rowbranch.Visible = True
            rowday.Visible = True
            rowamount.Visible = True
            rowbtn.Visible = True
            btnsubmit.Visible = True
        End If
        txtamt.Text = CType(gvpendingpyment.SelectedRow.Cells(5).FindControl("lblSupplierInv_NetAmount"), Label).Text.Trim
        Fill_Combo("select Bank_Kid,Bank_Name from Bank_Master", ddlbankname)

    End Sub

    Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnsubmit.Click
        Try
            '  Dim orderpkid = ReturnValue("SELECT  SupplierDispatchProductDetails.SupplierDispatchProductDetails_Kid FROM  SupplierDispatchDetails INNER JOIN  SupplierDispatchProductDetails ON   SupplierDispatchDetails.SupplierDispatchDetails_Kid = SupplierDispatchProductDetails.SupplierDispatchProductDetails_SupplierDispatchDetailsId where  SupplierDispatchDetails.SupplierDispatchDetails_OrderRequestToSupplierId='" & kid & "'")
            Dim kid As String
            Dim supplierid As String
            Dim amt As Decimal
            Dim spinvkid As String
            gvpendingpyment.Columns(1).Visible = True
            gvpendingpyment.Columns(8).Visible = True
            gvpendingpyment.Columns(7).Visible = True
            amt = CType(gvpendingpyment.SelectedRow.Cells(5).FindControl("lblSupplierInv_NetAmount"), Label).Text.Trim
            kid = CType(gvpendingpyment.SelectedRow.Cells(8).FindControl("lblSupplierInv_SDPD"), Label).Text.Trim
            supplierid = CType(gvpendingpyment.SelectedRow.Cells(7).FindControl("lblOrderRequestToSupplierProduct_SupplierId"), Label).Text.Trim
            spinvkid = CType(gvpendingpyment.SelectedRow.Cells(1).FindControl("lblSupplierProductInvoice_Kid"), Label).Text.Trim
            gvpendingpyment.Columns(7).Visible = False
            gvpendingpyment.Columns(8).Visible = False
            gvpendingpyment.Columns(1).Visible = False

            con.Open()
            cmd.Connection = con
            cmd.CommandText = "PaymentsToSupplier_Proc"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@PaymentsToSupplier_Kid", SqlDbType.NVarChar, 10).Value = RemoveLiterals(NewPrimaryKey(Me))
            cmd.Parameters.Add("@PaymentsToSupplier_Code", SqlDbType.NVarChar, 10).Value = Generate_Code("select count(*) from PaymentsToSupplier where PaymentsToSupplier_Code='", "STP")
            cmd.Parameters.Add("@PaymentsToSupplier_SupplierDispatchDetailsId", SqlDbType.NVarChar, 10).Value = kid 'orderpkid
            cmd.Parameters.Add("@PaymentsToSupplier_SupplierId", SqlDbType.NVarChar, 10).Value = supplierid
            If rdocheque.Checked = True Then
                cmd.Parameters.Add("@PaymentsToSupplier_Mode", SqlDbType.NVarChar, 20).Value = rdocheque.Text
                cmd.Parameters.Add("@PaymentsToSupplier_Cheque", SqlDbType.NVarChar, 10).Value = txtcheque.Text
            Else
                cmd.Parameters.Add("@PaymentsToSupplier_Mode", SqlDbType.NVarChar, 20).Value = rdodd.Text
                cmd.Parameters.Add("@PaymentsToSupplier_Cheque", SqlDbType.NVarChar, 10).Value = txtdd.Text
            End If

            cmd.Parameters.Add("@PaymentsToSupplier_Amount", SqlDbType.Decimal, 20).Value = Convert.ToDecimal(amt)
            cmd.Parameters.Add("@PaymentsToSupplier_BankId", SqlDbType.NVarChar, 10).Value = ddlbankname.SelectedItem.Value
            cmd.Parameters.Add("@PaymentsToSupplier_BranchId", SqlDbType.NVarChar, 10).Value = ddlbranchname.SelectedItem.Value
            cmd.Parameters.Add("@PaymentsToSupplier_InstrumentNo", SqlDbType.NVarChar, 10).Value = ""
            cmd.Parameters.Add("@PaymentsToSupplier_ChequeDate", SqlDbType.NVarChar).Value = txtdate.Text
            cmd.Parameters.Add("@PaymentsToSupplier_PaymentDate", SqlDbType.NVarChar).Value = Convert.ToDateTime(Format(Date.Now, "MM/dd/yyyy"))
            cmd.Parameters.Add("@PaymentsToSupplier_Status", SqlDbType.NVarChar, 10).Value = "Disbursed"
            cmd.Parameters.Add("@PaymentsToSupplier_FinancialYearId", SqlDbType.NVarChar, 10).Value = Session("g_FinYear")
            cmd.Parameters.Add("@PaymentsToSupplier_CompanyId", SqlDbType.NVarChar, 10).Value = Session("g_Company")
            cmd.Parameters.Add("@PaymentsToSupplier_UserId", SqlDbType.NVarChar, 10).Value = Session("g_User")
            cmd.Parameters.Add("@Formname", SqlDbType.NVarChar, 10).Value = "pending payment"
            cmd.Parameters.Add("@Mode", SqlDbType.NVarChar, 10).Value = "Insert"
            cmd.ExecuteNonQuery()
            Dim str As String = "UPDATE  SupplierProductInvoice SET  SupplierProductInvoice.SupplierProductInvoice_Status='Credited'  where SupplierProductInvoice_Kid ='" & spinvkid & "'"
            cmd = New SqlCommand(str, con)
            cmd.ExecuteNonQuery()
            '  str = "SELECT   Distinct SupplierProductInvoice.SupplierProductInvoice_Kid,  SupplierInv.SupplierInv_No, SupplierInv.SupplierInv_Date, SupplierInv.SupplierInv_NetAmount, OrderRequestToSupplierProduct.OrderRequestToSupplierProduct_SupplierId, Supplier_Master.Supplier_Name,SupplierInv.SupplierInv_Kid, SupplierProductInvoice.SupplierProductInvoice_Status,   SupplierInv.SupplierInv_SupplierDispatchProdDetailId FROM   Supplier_Master INNER JOIN  OrderRequestToSupplierProduct ON  Supplier_Master.Supplier_Kid = OrderRequestToSupplierProduct.OrderRequestToSupplierProduct_SupplierId CROSS JOIN  SupplierProductInvoice INNER JOIN  SupplierInv ON SupplierProductInvoice.SupplierProductInvoice_SupplierInvId = SupplierInv.SupplierInv_Kid where SupplierProductInvoice.SupplierProductInvoice_Status='Pending'"
            fillprdouctgrid()
            'fillgridview(gvpendingpyment, str)
            lblinfomsg.Visible = True
            lblinfomsg.Text = "Payment For Supplier is Credited."
            rowoption.Visible = False
            rowtitle.Visible = False
            clearfield()
            rowvat.Visible = False
            rowtitle.Visible = False
            rowoption.Visible = False
            rowpayment.Visible = False
        Catch ex As Exception
        Finally
            cmd.Dispose()
            con.Close()
            con.Dispose()
        End Try
    End Sub

    Protected Sub ddlbankname_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlbankname.SelectedIndexChanged
        Fill_Combo("select Branch_Kid,Branch_Name from Branch_Master where Branch_BankId='" & ddlbankname.SelectedItem.Value & "'", ddlbranchname)
    End Sub
    Function clearfield()
        txtamt.Text = ""
        txtcheque.Text = ""
        txtdate.Text = ""
        txtdd.Text = ""
        ddlbankname.SelectedIndex = 0
        ddlbranchname.SelectedIndex = 0
    End Function


   
End Class
