344 lines
13 KiB
PHP
344 lines
13 KiB
PHP
<?php
|
|
require_once("include/globals.php");
|
|
|
|
if (SessionActive() != 1 || ! UserLoggedIn()) {
|
|
header("Location: /");
|
|
exit();
|
|
}
|
|
|
|
|
|
// Ensure that only valid user types have access to this module
|
|
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator','Application Volunteer','Schedule Volunteer','Administration Team','Schedule Team','Supplier','Financial Team','Schedule Reporting');
|
|
if (checkPermission($AllowedUserTypes) != 1) {
|
|
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED');
|
|
$action = '';
|
|
$naction = '';
|
|
header("Location: /?action=$action&naction=$naction$headerArgs&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
|
|
exit();
|
|
}
|
|
|
|
|
|
if (!isset($curr_menu_num) && isset($menu_num)) $curr_menu_num = $menu_num;
|
|
|
|
// Open log file
|
|
$fname = "supplier-orders.txt";
|
|
$fp = fopen($fname, "w");
|
|
|
|
|
|
$school_name = $school_record['school_name'];
|
|
fwrite($fp,"School: $school_name\n");
|
|
fwrite($fp,"Menu Number: $curr_menu_num\n");
|
|
|
|
|
|
|
|
// Get the schedule record
|
|
$swhere = "";
|
|
if ($_SESSION['UserInfo']['type'] == 'Supplier') $swhere = " and supplier_num = " . $_SESSION['UserInfo']['supplier_num'];
|
|
$query = "select * from hotlunch_schedules " .
|
|
"where school_num = $school_num " .
|
|
"and record_num in ($schedule_num) " .
|
|
"and status = 'Active' $swhere";
|
|
$results = db_query($query);
|
|
$hotlunch_schedule = db_fetch_array($results);
|
|
fwrite($fp,"Schedule Record:\n");
|
|
fwrite($fp,"$query\n");
|
|
fwrite($fp,"\n");
|
|
|
|
|
|
// Get the menu date record
|
|
$query = "select * from hotlunch_menu_dates " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and record_num = $curr_menu_num";
|
|
$results = db_query($query);
|
|
$hotlunch_menu_date = db_fetch_array($results);
|
|
$delivery_date = $hotlunch_menu_date['menu_date'];
|
|
$menu_title = $hotlunch_menu_date['menu_title'];
|
|
if (is_null($delivery_date) || strlen($delivery_date) == 0) $delivery_date = $hotlunch_schedule['end_order_date'];
|
|
fwrite($fp,"Menu Date Record: - $menu_title ($curr_menu_num)\n");
|
|
fwrite($fp,"$query\n");
|
|
fwrite($fp,"\n");
|
|
|
|
|
|
// Get all menu items for this menu date
|
|
$query = "select * from hotlunch_menu_items " .
|
|
"where school_num = $school_num " .
|
|
"and menu_num = $curr_menu_num " .
|
|
"order by sequence";
|
|
$menu_items = db_query($query);
|
|
$num_items = db_num_rows($menu_items);
|
|
fwrite($fp,"Menu Items Query: $num_items\n");
|
|
fwrite($fp,"$query\n");
|
|
fwrite($fp,"\n");
|
|
|
|
|
|
// Set estimated totals to zero
|
|
if (isset($delivery_date) && strlen($delivery_date) > 0) {
|
|
$query = "update supplier_order_header " .
|
|
"set estimated_total = 0, " .
|
|
"delivery_date = '$delivery_date' " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num";
|
|
}
|
|
else {
|
|
$query = "update supplier_order_header " .
|
|
"set estimated_total = 0 " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num";
|
|
}
|
|
// db_query("START TRANSACTION");
|
|
db_query($query);
|
|
// db_query("COMMIT");
|
|
|
|
// Check if there is an order item record for this supplier/schedule/menu/item
|
|
$query = "delete from supplier_order_items " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num";
|
|
db_query($query);
|
|
fwrite($fp,"Delete Supplier Order Items:\n");
|
|
fwrite($fp,"$query\n");
|
|
fwrite($fp,"\n");
|
|
|
|
|
|
// Setup table objects
|
|
$supplier_order_header = new Table("supplier_order_header");
|
|
$supplier_order_items = new Table("supplier_order_items");
|
|
|
|
// Define other values
|
|
$order_date = date("Y-m-d",time());
|
|
$totals = array();
|
|
|
|
$item_num = 0;
|
|
if (db_num_rows($menu_items) > 0) {
|
|
$count = 0;
|
|
|
|
while ($row = db_fetch_array($menu_items)) {
|
|
extract($row);
|
|
$menu_item_num = $record_num;
|
|
fwrite($fp,"$item_title ============================\n\n");
|
|
|
|
// Get set of products for this menu item
|
|
$query = "select * from hotlunch_menu_products " .
|
|
"where school_num = $school_num " .
|
|
"and menu_item_num = $menu_item_num";
|
|
$supplier_products = db_query($query);
|
|
fwrite($fp,"Get Supplier Products: $menu_item_num\n");
|
|
fwrite($fp,"$query\n");
|
|
fwrite($fp,"\n");
|
|
|
|
if (db_num_rows($supplier_products) > 0) {
|
|
while ($product = db_fetch_array($supplier_products)) {
|
|
extract($product);
|
|
|
|
// Get product name
|
|
$product_name = db_getVal("select product_name from supplier_products where record_num = $supplier_product_num");
|
|
fwrite($fp,"$product_name\n");
|
|
|
|
$bgclass = "data-even";
|
|
if ($count % 2) $bgclass = "data-odd";
|
|
$count++;
|
|
|
|
// Get supplier record
|
|
$query = "select supplier_num,product_name,package_qty,package_cost,supplier_name " .
|
|
"from supplier_products as sp, suppliers as s " .
|
|
"where sp.school_num = $supplier_school_num " .
|
|
"and s.school_num = $supplier_school_num " .
|
|
"and sp.record_num = $supplier_product_num " .
|
|
"and sp.supplier_num = s.record_num";
|
|
$results = db_query($query);
|
|
if (db_num_rows($results)) {
|
|
$row = db_fetch_array($results);
|
|
extract($row);
|
|
}
|
|
else continue;
|
|
|
|
// Get qty ordered online
|
|
$query = "select sum(qty) as ordered " .
|
|
"from hotlunch_order_items as hloi, hotlunch_menu_products as hlmp, hotlunch_order_header as hloh " .
|
|
"where hloi.school_num = $school_num " .
|
|
"and hloh.school_num = $school_num " .
|
|
"and hlmp.school_num = $school_num " .
|
|
"and hloi.schedule_num = $schedule_num " .
|
|
"and hloi.menu_num = $curr_menu_num " .
|
|
"and hloi.menu_item_num = hlmp.menu_item_num " .
|
|
"and hloi.menu_item_num = $menu_item_num " .
|
|
"and hloi.order_num = hloh.record_num " .
|
|
"and hloi.active != 'N' " .
|
|
"and hloh.status NOT IN ('Inactive','Deleted') " .
|
|
"and hlmp.supplier_product_num = $supplier_product_num";
|
|
$results = db_query($query);
|
|
$row = db_fetch_assoc($results);
|
|
$online_qty = $row['ordered'];
|
|
|
|
fwrite($fp,"$query\n");
|
|
fwrite($fp,"online_qty: $online_qty\n");
|
|
fwrite($fp,"supplier_product_qty: $supplier_product_qty\n");
|
|
if (strlen($online_qty) == 0) $online_qty = 0;
|
|
|
|
// Calculate the qty of packages that is required and the price
|
|
$oqty = $online_qty * $supplier_product_qty;
|
|
$online_qty = $oqty;
|
|
fwrite($fp,"online_qty: $online_qty\n");
|
|
if (intval($package_qty) == 0) $package_qty = 1;
|
|
$qty_ordered = intval(($oqty + $package_qty - 1) / $package_qty);
|
|
$estimated_cost = $qty_ordered * $package_cost;
|
|
|
|
// Check if there is an order header for this supplier/schedule/menu
|
|
$query = "select * from supplier_order_header " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num " .
|
|
"and supplier_num = $supplier_num";
|
|
$results = db_query($query);
|
|
|
|
// Create an order header if required
|
|
if (db_num_rows($results) == 0) {
|
|
$status = 'Created';
|
|
$estimated_total = $estimated_cost;
|
|
$query = $supplier_order_header->BuildInsert();
|
|
fwrite($fp,"===================\nCreate Order Header\n");
|
|
fwrite($fp,"$query\n");
|
|
db_query($query);
|
|
$order_num = db_insert_id();
|
|
}
|
|
else {
|
|
$row = db_fetch_array($results);
|
|
$order_num = $row['record_num'];
|
|
$query = "update supplier_order_header " .
|
|
"set estimated_total = estimated_total + $estimated_cost " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num " .
|
|
"and supplier_num = $supplier_num";
|
|
// db_query("START TRANSACTION");
|
|
db_query($query);
|
|
// db_query("COMMIT");
|
|
fwrite($fp,"===================\nUpdate Order Header\n");
|
|
fwrite($fp,"$query\n");
|
|
}
|
|
|
|
// Check if there is an order item record for this supplier/schedule/menu/item
|
|
$query = "select * from supplier_order_items " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num " .
|
|
"and supplier_num = $supplier_num " .
|
|
"and supplier_product_num = $supplier_product_num";
|
|
$results = db_query($query);
|
|
fwrite($fp,"$query\n");
|
|
|
|
// Create a supplier_order_items record if required
|
|
if (db_num_rows($results) == 0) {
|
|
$actual_cost = 0;
|
|
$query = $supplier_order_items->BuildInsert();
|
|
db_query($query);
|
|
fwrite($fp,"$query\n");
|
|
}
|
|
else {
|
|
$row = db_fetch_array($results);
|
|
$record_num = $row['record_num'];
|
|
$actual_cost = $row['actual_cost'];
|
|
$online_qty += $row['online_qty'];
|
|
|
|
// Calculate the qty of packages that is required and the price
|
|
$qty_ordered = intval(($online_qty + $package_qty - 1) / $package_qty);
|
|
$estimated_cost = $qty_ordered * $package_cost;
|
|
if ($package_qty == 0) $package_qty = 1;
|
|
|
|
$query = "update supplier_order_items " .
|
|
"set online_qty = $online_qty, " .
|
|
"supplier_product_qty = 1, " .
|
|
"package_qty = $package_qty, " .
|
|
"qty_ordered = $qty_ordered, " .
|
|
"estimated_cost = $estimated_cost " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num " .
|
|
"and supplier_num = $supplier_num " .
|
|
"and supplier_product_num = $supplier_product_num";
|
|
db_query($query);
|
|
fwrite($fp,"$query\n");
|
|
}
|
|
|
|
fwrite($fp,"\n\n");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Verify supplier_order_header and supplier_order_items records
|
|
|
|
// Validate the order header records for this schedule/menu
|
|
$query = "select * from supplier_order_header " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num";
|
|
$order_headers = db_query($query);
|
|
|
|
// Check each order_header entry
|
|
if (db_num_rows($order_headers)) {
|
|
while ($row = db_fetch_array($order_headers)) {
|
|
$supplier_num = $row['supplier_num'];
|
|
|
|
$query = "select hlmp.* from hotlunch_menu_items as hlmi,hotlunch_menu_products as hlmp,supplier_products as sp " .
|
|
"where hlmp.school_num = $school_num " .
|
|
"and hlmi.school_num = $school_num " .
|
|
"and sp.school_num = $supplier_school_num " .
|
|
"and hlmi.menu_num = $curr_menu_num " .
|
|
"and hlmi.record_num = hlmp.menu_item_num " .
|
|
"and hlmp.supplier_product_num = sp.record_num " .
|
|
"and sp.supplier_num = $supplier_num";
|
|
$results = db_query($query);
|
|
|
|
if (db_num_rows($results) == 0) {
|
|
$query = "delete from supplier_order_header " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num " .
|
|
"and supplier_num = $supplier_num";
|
|
db_query($query);
|
|
// echo("<p>$query</p>\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Validate the supplier_order_items
|
|
$query = "select * from supplier_order_items " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num";
|
|
$order_items = db_query($query);
|
|
|
|
// Check each order_item entry
|
|
if (db_num_rows($order_items)) {
|
|
while ($row = db_fetch_array($order_items)) {
|
|
$supplier_product_num = $row['supplier_product_num'];
|
|
|
|
$query = "select hlmp.* " .
|
|
"from hotlunch_menu_products as hlmp, hotlunch_menu_items as hlmi " .
|
|
"where hlmp.school_num = $school_num " .
|
|
"and hlmi.school_num = $school_num " .
|
|
"and hlmi.menu_num = $curr_menu_num " .
|
|
"and hlmp.menu_item_num = hlmi.record_num " .
|
|
"and hlmp.supplier_product_num = $supplier_product_num";
|
|
$results = db_query($query);
|
|
|
|
if (db_num_rows($results) == 0) {
|
|
$query = "delete from supplier_order_items " .
|
|
"where school_num = $school_num " .
|
|
"and schedule_num = $schedule_num " .
|
|
"and menu_num = $curr_menu_num " .
|
|
"and supplier_product_num = $supplier_product_num";
|
|
db_query($query);
|
|
// echo("<p>$query</p>\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
?>
|