Imports System.Data
Imports System.Data.SqlClient

Partial Class Buyer_TransactionHistory
    Inherits System.Web.UI.Page

    Dim query As String = ""
    Dim bno As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            loaddatagrid()
        End If
    End Sub

    Public Sub loaddatagrid()
        Try
            query = "SELECT dbo.BillingHeader.BillingHeader_Kid " & _
                     ",dbo.BillingHeader.BillingHeader_BillNo " & _
                     ", dbo.BillingHeader.BillingHeader_BuyerID, " & _
                     "dbo.BillingHeader.BillingHeader_NetAmt, " & _
                     "dbo.BillingHeader.BillingHeader_BillDate, PaymentDetails.PaymentDetails_BillAmt " & _
                     "FROM dbo.BillingHeader INNER JOIN dbo.PaymentDetails " & _
                      "ON dbo.BillingHeader.BillingHeader_Kid = dbo.PaymentDetails.PaymentDetails_BillNo " & _
                      "where  BillingHeader.BillingHeader_BuyerID='" & Session("BuyId") & "'" & _
                      "and BillingHeader.BillingHeader_Status = 'dispatched' and Datediff(mm,BillingHeader.BillingHeader_BillDate,getdate()) between 0 and 6 order by BillingHeader.BillingHeader_BillDate "

            Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
            cmd = New SqlCommand(query, Con)
           
            da = New SqlDataAdapter(cmd)
            da.Fill(ds, "Bill-Header")
            If ds.Tables("Bill-Header").Rows.Count <> 0 Then
                GvOrder.DataSource = ds.Tables("Bill-Header")
                GvOrder.DataBind()
                ds.Tables("Bill-Header").Clear()
            Else
                ' Label1.Visible = True
            End If
        Catch ex As Exception
            'Response.Write(ex.Message.ToString)
        Finally
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try
    End Sub

    Public Sub lkbill_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim i As New LinkButton()
        i = CType(sender, LinkButton)
        bno = RemoveLiterals(i.CommandArgument)
        loadbilldata(bno)
    End Sub

    Private Sub loadbilldata(ByVal billno As String)
        Try
            Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
            'Dim cmd As New SqlCommand
            'query = "select Product_Master.Product_Name,BillDetails.BillDetails_ProdQty,BillDetails.BillDetails_ProdRate,BillDetails.BillDetails_ProdAmt,BillDetails.BillDetails_PFCharge,BillDetails.BillDetails_TaxAmt,BillDetails.BillDetails_Freight " & _
            '        "from Product_Master inner join BillDetails on Product_Master.Product_Kid=BillDetails.BillDetails_ProdId " & _
            '        "where BillDetails.BillDetails_BillNo='" & billno & "'"
            'cmd = New SqlCommand(query, Con)
            'da = New SqlDataAdapter(cmd)
            'da.Fill(ds, "Bill-Detail")
            'GvBill.DataSource = ds.Tables("Bill-Detail")
            'GvBill.DataBind()
            'ds.Tables("Bill-Detail").Clear()

            Con.Open()
            'Dim str As String = "Select DISTINCT BillDetails.BillDetails_BillNo, Product_Master.Product_Name, BillDetails.BillDetails_ProdQty, BillDetails.BillDetails_ProdRate,BillDetails.BillDetails_ProdAmt," & _
            '" BillDetails.BillDetails_PFCharge,BillDetails.BillDetails_ShipAddID, BillDetails.BillDetails_TaxAmt, BillDetails.BillDetails_Freight " & _
            '" ,BillDetails.BillDetails_ExciteDuty,BillDetails.BillDetails_EduCess,BillDetails.BillDetails_Vat,BillDetails.BillDetails_CST,BillDetails.BillDetails_CSTAgainst,Product_Master.Product_Kid, AddressBook.AddressBook_Kid, AddressBook.AddressBook_UserName,AddressBook.AddressBook_Add1 " & _
            ' "   FROM     AddressBook INNER JOIN Product_Master INNER JOIN BillDetails ON Product_Master.Product_Kid = BillDetails.BillDetails_ProdId ON " & _
            '" BillDetails.BillDetails_ShipAddID = AddressBook.AddressBook_Kid  AND " & _
            '" BillDetails.BillDetails_BuyerID = AddressBook.AddressBook_BuyerID " & _
            '" WHERE AddressBook.AddressBook_Kid IN (SELECT    BillDetails.BillDetails_ShipAddID  FROM         BillDetails  WHERE        BillDetails.BillDetails_BillNo  ='" & billno & "')"

            Dim str As String = "SELECT DISTINCT BillDetails.BillDetails_BillNo, Product_Master.Product_Name, BillDetails.BillDetails_ProdQty, BillDetails.BillDetails_ProdRate, BillDetails.BillDetails_ProdAmt, BillDetails.BillDetails_PFCharge, BillDetails.BillDetails_ShipAddID, BillDetails.BillDetails_TaxAmt,  BillDetails.BillDetails_Freight, BillDetails.BillDetails_ExciteDuty, BillDetails.BillDetails_EduCess, BillDetails.BillDetails_Vat,BillDetails.BillDetails_CST, BillDetails.BillDetails_CSTAgainst, Product_Master.Product_Kid, AddressBook.AddressBook_UserName,AddressBook.AddressBook_Add1 FROM  Product_Master INNER JOIN  BillDetails ON Product_Master.Product_Kid = BillDetails.BillDetails_ProdId INNER JOIN AddressBook ON BillDetails.BillDetails_ShipAddID = AddressBook.AddressBook_Kid AND  BillDetails.BillDetails_BuyerID = AddressBook.AddressBook_BuyerID WHERE  BillDetails.BillDetails_BillNo ='" & billno & "'"
            Dim cmd As New SqlCommand(str, Con)
            da = New SqlDataAdapter(cmd)
            da.Fill(ds, "Bill-Detail")
            GvBill.Columns(9).Visible = True
            GvBill.Columns(10).Visible = True
            GvBill.Columns(11).Visible = True
            GvBill.DataSource = ds.Tables("Bill-Detail")
            GvBill.DataBind()
            GvBill.Columns(9).Visible = False
            GvBill.Columns(10).Visible = False
            GvBill.Columns(11).Visible = False

            ds.Tables("Bill-Detail").Clear()


        Catch ex As Exception
            'Response.Write(ex.Message.ToString())
        Finally
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try
    End Sub

    
    Protected Sub GvOrder_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GvOrder.SelectedIndexChanged

    End Sub
End Class
