Fulfillment

This project was part of the ICS 321 Database Systems I course at the University of Hawaiʻi. The goal was to automate reordering sold items from vendors to replenish inventory, helping speed up customer deliveries and reduce human error. This process is called fulfillment.

The project used triggers and stored procedures covered earlier in the course. The system monitors sales in real time and automatically generates purchase orders. A confirmation procedure places each order and updates the purchase date.

The system was implemented using MySQL and PHP, and it included features such as sale parameters, and order confirmation.

To place new sale I wrote PHP script https://dmitryg.ics321.org/app_sale.php The script can be called with parameters to insert new sale.

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

header('Content-Type: application/json');

$user = '';  // username you use to log into cPanel
$pass = '';  // password you use to log into cPanel
$host = "localhost";
$database = $user."_QACS";

/*
SET @customer_id='10';
SET @employee_id='5';
SET @sale_date='2019-02-13';
SET @item_id='13';
SET @tax_rate='4.712';
SET @sale_id='0';
CALL ADD_SALE(@customer_id,@employee_id,@sale_date,@item_id,@tax_rate,@sale_id,@sub_total,@total,@err,@msg);
*/
$customer_id = 10;
$employee_id = 5;
$sale_date = '2019-02-13';
$item_id = '13';
$tax_rate = '4.712';
$sale_id = '0';
$sub_total = 0;
$total = 0;

if(isset($_REQUEST['customer_id'])) $customer_id=$_REQUEST['customer_id'];
if(isset($_REQUEST['employee_id'])) $employee_id=$_REQUEST['employee_id'];
if(isset($_REQUEST['sale_date'])) $sale_date=$_REQUEST['sale_date'];
if(isset($_REQUEST['item_id'])) $item_id=$_REQUEST['item_id'];
if(isset($_REQUEST['tax_rate'])) $tax_rate=$_REQUEST['tax_rate'];

$dsn = "mysql:host=$host;dbname=$database";
$pdo = new PDO($dsn, $user, $pass);

/* customer_id, employee_id, sale_date, item_id, tax_rate, sale_id, sub_total, total, err, msg */
$sql = "CALL ADD_SALE(:customer_id,:employee_id,:sale_date,:item_id,:tax_rate,@sale_id,@sub_total,@total,@err,@msg);";

$statement = $pdo->prepare($sql);
$statement->bindValue(":customer_id", $customer_id);
$statement->bindValue(":employee_id", $employee_id);
$statement->bindValue(":sale_date", $sale_date);
$statement->bindValue(":item_id", $item_id);
$statement->bindValue(":tax_rate", $tax_rate);
$statement->execute();

$sql = "SELECT @err AS err, @msg AS msg2";

$statement = $pdo->query($sql);

if ($rec = $statement->fetch()) {
    $err = $rec['err'];
    $msg = $rec['msg2'];
}
$out= json_encode(
[   "msg" => "$msg",
"error" => "$err",
"customer_id" => "$customer_id",
"employee_id" => "$employee_id",
"sale_date" => "$sale_date",
"item_id" => "$item_id",
"tax_rate" => "$tax_rate",
"sale_id" => "$sale_id",
"sub_total" => "$sub_total",
"total" => "$total"
]);
echo $out;

Next step was to create a trigger that would automatically generate a purchase order when a sale of an item is inserted.

CREATE TRIGGER ADD_OrderVendor AFTER INSERT ON SALE_ITEM
FOR EACH ROW BEGIN

SET @EmployeeID = (SELECT EmployeeID FROM SALE WHERE SaleID = NEW.SaleID);

SET @VendorID = (SELECT VendorID FROM ITEM WHERE ItemID = NEW.ItemID);

INSERT INTO ORDER_VENDOR(SaleID, SaleItemID, ItemID, EmployeeID, VendorID) VALUES (NEW.SaleID, NEW.SaleItemID, NEW.ItemID, @EmployeeID, @VendorID);

END

It basically inserts a new record into the ORDER_VENDOR table with the SaleID, SaleItemID, ItemID from the SALE_ITEM table. In addition EmployeeID, and VendorID are retrieved from the SALE and ITEM tables respectively.

The final step was to create a stored procedure that would confirm the order and update the purchase date.

DELIMITER $$
CREATE DEFINER=dmitr321@localhost PROCEDURE ACCEPT_ORDER_VENDOR()
BEGIN

SET @order_id = (SELECT MAX(OrderID) AS last_id FROM ORDER_VENDOR);
SET @current_date = CURDATE();

SET @item_id = (SELECT ItemID FROM ORDER_VENDOR WHERE OrderID=@order_id);

UPDATE ORDER_VENDOR SET OrderDate=@current_date WHERE OrderID = @order_id;

UPDATE ITEM SET PurchaseDate=@current_date WHERE ItemID = @item_id;

END$$
DELIMITER;

This stored procedure retrieves the maximum OrderID from the ORDER_VENDOR table, sets the current date, and updates the OrderDate in the ORDER_VENDOR table and the PurchaseDate in the ITEM table.

PHP script to call the stored procedure: https://dmitryg.ics321.org/confirm_order.php It executes the stored procedure to confirm the order and update the purchase date, then returns the result as JSON.

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

header('Content-Type: application/json');

$user = '';  // username you use to log into cPanel
$pass = '';  // password you use to log into cPanel
$host = "localhost";
$database = $user."_QACS";

$dsn = "mysql:host=$host;dbname=$database";
$pdo = new PDO($dsn, $user, $pass);

$sql = "CALL ACCEPT_ORDER_VENDOR();";

$statement = $pdo->prepare($sql);
$statement->execute();

$sql = "SELECT @err AS err, @msg AS msg2";

$statement = $pdo->query($sql);

if ($rec = $statement->fetch()) {
$err = $rec['err'];
$msg = $rec['msg2'];
}

$out= json_encode(
[   "msg" => "$msg",
"error" => "$err"
]);
echo $out;

The project was a valuable opportunity to apply concepts from class and gain hands-on experience with database design and implementation. The database design and stored procedures could be improved further. I gained experience with triggers, stored procedures, MySQL, and PHP while building a functional database system.

Demo Video