Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Net
Imports Newtonsoft.Json
Partial Class search
Inherits System.Web.UI.Page
Dim connStr As String =
ConfigurationManager.ConnectionStrings("PVFC").ConnectionString
Protected Sub btnSearch_Click(sender As Object, e As EventArgs)
Dim con As New SqlConnection(connStr)
Dim query As String =
"SELECT Product_Id, Product_Description, Product_Finish, Standard_Price " &
"FROM PRODUCT_t WHERE Product_Description LIKE @search"
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@search", "%" & txtSearch.Text & "%")
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
rptProducts.DataSource = dr
rptProducts.DataBind()
con.Close()
End Sub
Protected Sub btnShowAll_Click(sender As Object, e As EventArgs)
Dim con As New SqlConnection(connStr)
Dim query As String =
"SELECT Product_Id, Product_Description, Product_Finish, Standard_Price FROM PRODUCT_t"
Dim cmd As New SqlCommand(query, con)
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
rptProducts.DataSource = dr
rptProducts.DataBind()
con.Close()
End Sub
Protected Sub rptProducts_ItemCommand(source As Object, e As RepeaterCommandEventArgs)
If e.CommandName = "AddToCart" Then
If Session("Customer_Id") Is Nothing Then
Response.Redirect("CustomerLogin.aspx")
Exit Sub
End If
Dim customerId As Integer =
Convert.ToInt32(Session("Customer_Id"))
Dim productId As Integer =
Convert.ToInt32(e.CommandArgument)
Dim txtQty As TextBox =
CType(e.Item.FindControl("txtQty"), TextBox)
If txtQty.Text = "" Then
lblMessage.Text = "Enter quantity."
Exit Sub
End If
Dim quantity As Integer =
Convert.ToInt32(txtQty.Text)
Dim con As New SqlConnection(connStr)
con.Open()
Dim orderId As Integer = 0
Dim checkOrderQuery As String =
"SELECT TOP 1 Order_Id FROM ORDER_t WHERE Customer_Id = @cid AND Order_Date = CAST(GETDATE() AS DATE)"
Dim cmdCheckOrder As New SqlCommand(checkOrderQuery, con)
cmdCheckOrder.Parameters.AddWithValue("@cid", customerId)
Dim result = cmdCheckOrder.ExecuteScalar()
If result Is Nothing Then
Dim newIdQuery As String =
"SELECT ISNULL(MAX(Order_Id),1000)+1 FROM ORDER_t"
Dim cmdNewId As New SqlCommand(newIdQuery, con)
orderId = Convert.ToInt32(cmdNewId.ExecuteScalar())
Dim insertOrderQuery As String =
"INSERT INTO ORDER_t VALUES (@oid, @cid, CAST(GETDATE() AS DATE))"
Dim cmdInsertOrder As New SqlCommand(insertOrderQuery, con)
cmdInsertOrder.Parameters.AddWithValue("@oid", orderId)
cmdInsertOrder.Parameters.AddWithValue("@cid", customerId)
cmdInsertOrder.ExecuteNonQuery()
Else
orderId = Convert.ToInt32(result)
End If
Dim checkLineQuery As String =
"SELECT Ordered_Quantity FROM Order_line_t WHERE Order_Id = @oid AND Product_Id = @pid"
Dim cmdCheckLine As New SqlCommand(checkLineQuery, con)
cmdCheckLine.Parameters.AddWithValue("@oid", orderId)
cmdCheckLine.Parameters.AddWithValue("@pid", productId)
Dim existingQty = cmdCheckLine.ExecuteScalar()
If existingQty IsNot Nothing Then
Dim newQty As Integer =
Convert.ToInt32(existingQty) + quantity
Dim updateQuery As String =
"UPDATE Order_line_t SET Ordered_Quantity = @qty WHERE Order_Id = @oid AND Product_Id = @pid"
Dim cmdUpdate As New SqlCommand(updateQuery, con)
cmdUpdate.Parameters.AddWithValue("@qty", newQty)
cmdUpdate.Parameters.AddWithValue("@oid", orderId)
cmdUpdate.Parameters.AddWithValue("@pid", productId)
cmdUpdate.ExecuteNonQuery()
Else
Dim insertLineQuery As String =
"INSERT INTO Order_line_t VALUES (@oid, @pid, @qty)"
Dim cmdInsertLine As New SqlCommand(insertLineQuery, con)
cmdInsertLine.Parameters.AddWithValue("@oid", orderId)
cmdInsertLine.Parameters.AddWithValue("@pid", productId)
cmdInsertLine.Parameters.AddWithValue("@qty", quantity)
cmdInsertLine.ExecuteNonQuery()
End If
con.Close()
lblMessage.Text = "Product added to YOUR cart!"
End If
If e.CommandName = "Recommend" Then
Dim productId As Integer =
Convert.ToInt32(e.CommandArgument)
Dim apiUrl As String =
"http://localhost:5064/api/recommendations/" & productId
Dim client As New WebClient()
Dim json As String =
client.DownloadString(apiUrl)
Dim recommendations As List(Of String) =
JsonConvert.DeserializeObject(Of List(Of String))(json)
Dim bl As BulletedList =
CType(e.Item.FindControl("blRecommendations"), BulletedList)
bl.DataSource = recommendations
bl.DataBind()
End If
End Sub
End Class
Functionality: Reordering Suggestions for Customer
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Net
Imports Newtonsoft.Json
Partial Class Cart
Inherits System.Web.UI.Page
Private connStr As String =
ConfigurationManager.ConnectionStrings("PVFC").ConnectionString
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadCart()
End If
End Sub
Private Sub LoadCart()
If Session("Customer_Id") Is Nothing Then
Response.Redirect("CustomerLogin.aspx")
Return
End If
Dim customerId As Integer =
Convert.ToInt32(Session("Customer_Id"))
Using conn As New SqlConnection(connStr)
Dim query As String =
"SELECT p.Product_Description, " &
"ol.Ordered_Quantity AS Quantity, " &
"p.Standard_Price AS Price, " &
"(ol.Ordered_Quantity * p.Standard_Price) AS Total " &
"FROM ORDER_t o " &
"INNER JOIN Order_line_t ol ON o.Order_Id = ol.Order_Id " &
"INNER JOIN PRODUCT_t p ON ol.Product_Id = p.Product_Id " &
"WHERE o.Customer_Id = @CustomerId " &
"AND o.Order_Id = (SELECT MAX(Order_Id) FROM ORDER_t WHERE Customer_Id = @CustomerId)"
Using cmd As New SqlCommand(query, conn)
cmd.Parameters.AddWithValue("@CustomerId", customerId)
Dim dt As New DataTable()
Using da As New SqlDataAdapter(cmd)
da.Fill(dt)
End Using
gvCart.DataSource = dt
gvCart.DataBind()
Dim grandTotal As Decimal = 0
For Each row As DataRow In dt.Rows
grandTotal += Convert.ToDecimal(row("Total"))
Next
lblGrandTotal.Text = "Grand Total: " & grandTotal.ToString("C")
End Using
End Using
End Sub
Protected Sub btnReorder_Click(sender As Object, e As EventArgs)
If Session("Customer_Id") Is Nothing Then
Response.Redirect("CustomerLogin.aspx")
Exit Sub
End If
Dim customerId As Integer =
Convert.ToInt32(Session("Customer_Id"))
Dim apiUrl As String =
"http://localhost:5064/api/reorder/" & customerId
Dim client As New WebClient()
Dim json As String =
client.DownloadString(apiUrl)
Dim items As List(Of String) =
JsonConvert.DeserializeObject(Of List(Of String))(json)
blReorder.DataSource = items
blReorder.DataBind()
End Sub
End Class
Functionality: Forecasting Demand for Secondary Item
Imports System.Net
Imports Newtonsoft.Json
Imports System.Data
Partial Class employee_dashboard
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Session("Role") Is Nothing OrElse Session("Role").ToString() <> "Employee" Then
Response.Redirect("EmployeeLogin.aspx")
End If
End Sub
Protected Sub btnLogout_Click(sender As Object, e As EventArgs)
Session.Clear()
Response.Redirect("LoginChoice.aspx")
End Sub
Protected Sub btnForecast_Click(sender As Object, e As EventArgs)
Dim productId As Integer = Convert.ToInt32(txtProductId.Text)
Dim apiUrl As String = "http://localhost:5064/api/forecast/" & productId
Dim client As New WebClient()
Dim json As String = client.DownloadString(apiUrl)
Dim dt As DataTable =
JsonConvert.DeserializeObject(Of DataTable)(json)
gvForecast.DataSource = dt
gvForecast.DataBind()
End Sub
End Class