\n");
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED');
include "form.messages.php";
return;
}
//
// Process all actions to list and edit records
//
$naction = "";
$CurrentTable = "supplier_order_header";
$DeleteField = "";
$dSeparator = "";
$FormFile = "supplier_orders.form.php";
$TitleText = "Supplier Order";
$OrderBy = "";
$Sequence = 'N';
if (array_key_exists('schedule_num',$_GET)) $schedule_num = intval($_GET['schedule_num']);
if (array_key_exists('schedule_num',$_POST)) $schedule_num = intval($_POST['schedule_num']);
if (array_key_exists('menu_num',$_GET)) $menu_num = intval($_GET['menu_num']);
if (array_key_exists('menu_num',$_POST)) $menu_num = intval($_POST['menu_num']);
if (!isset($schedule_num)) {
$error_message = getLanguageText('ERRORMSG-INVALID-SCHEDULE-NUM');
$action = '';
$naction = '';
header("Location: /?action=$action&naction=$naction$headerArgs&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
$headerArgs = "&schedule_num=$schedule_num";
if (!isset($menu_num)) {
$error_message = getLanguageText('ERRORMSG-NO-MENU-NUM');
$action = 'schedule-reports';
$naction = '';
header("Location: /?action=$action&naction=$naction$headerArgs&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
// Check if this is a valid schedule
$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 = $schedule_num " .
$swhere;
writeLog($query);
$results = db_query($query);
if (db_num_rows($results) == 0) {
$error_message = getLanguageText('ERRORMSG-INVALID-SCHEDULE');
$action = 'schedule-reports';
$naction = '';
header("Location: /?action=$action&naction=$naction$headerArgs&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
$hotlunch_schedule = db_fetch_array($results);
// Check if this is a valid menu
$query = "select * from hotlunch_menu_dates " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"and record_num = $menu_num";
writeLog($query);
$results = db_query($query);
if (db_num_rows($results) == 0) {
echo("
\n");
$error_message = "The requested hot lunch menu is invalid";
include "form.messages.php";
return;
}
$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'];
//
// Update table values - re-post quantities
//
include "supplier_orders.post.php";
$templatePageTitle = getLanguageText('TEXT-SUPPLIER-ORDERS-AND-TOTALS') . " - $menu_title";
if (array_key_exists('naction',$_GET)) $naction = $_GET['naction'];
if (array_key_exists('naction',$_POST)) $naction = $_POST['naction'];
$school_num = $school_record['record_num'];
switch (strtolower($naction)) {
case "update":
$PostCopy = $_POST;
$hotlunch_menu_transactions = new Table("hotlunch_menu_transactions");
foreach ($_POST as $key => $val) {
// Process actual charges values for supplier order items
if (strpos($key,"actual") === 0) {
list($nn,$rnum) = explode('_',$key);
$val = str_replace('$','',$val);
$val = str_replace(',','',$val);
$val = str_replace('..','.',$val);
if (strlen($val) == 0) $val = 0;
// Get supplier_order_items record
$menu_num = intval($_POST["menu_$rnum"]);
$supplier_num = intval($_POST["supplier_$rnum"]);
$supplier_product_num = intval($_POST["product_$rnum"]);
$query = "INSERT INTO supplier_order_items_actual (school_num,schedule_num,menu_num,supplier_num,supplier_product_num,actual_cost) " .
"VALUES ($school_num,$schedule_num,$menu_num,$supplier_num,$supplier_product_num,$val) " .
"ON DUPLICATE KEY UPDATE actual_cost = $val";
writeLog($query);
db_query($query);
}
// Update existing menu date transactions
if (strpos($key,"description_") === 0) {
list($nn,$rnum) = explode('_',$key);
$description = $val;
$type = $_POST["type_$rnum"];
$amount = str_replace('$','',$_POST["amount_$rnum"]);
$amount = str_replace(',','',$amount);
if (strlen($amount) == 0) $amount = 0;
$record_num = $rnum;
// Update the record if there is a description
if (strlen($description)) {
$query = $hotlunch_menu_transactions->BuildUpdate();
db_query($query);
}
else {
$query = "delete from hotlunch_menu_transactions where record_num = $record_num";
db_query($query);
}
}
// Insert new menu date transactions
if (strpos($key,"new_description_") === 0) {
list($n1,$n2,$rnum) = explode('_',$key);
$description = $val;
$type = $_POST["new_type_$rnum"];
$amount = str_replace('$','',$_POST["new_amount_$rnum"]);
$amount = str_replace(',','',$amount);
if (strlen($description) > 0 && strlen($amount) > 0) {
$query = $hotlunch_menu_transactions->BuildInsert();
writeLog($query);
db_query($query);
}
}
}
$query = "select supplier_num,sum(actual_cost) as actual_cost from supplier_order_items_actual " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"and menu_num = $menu_num " .
"group by 1";
writeLog($query);
$results = db_query($query);
while ($row = db_fetch_array($results)) {
$supplier_num = $row['supplier_num'];
$actual_cost = $row['actual_cost'];
$query = "update supplier_order_header " .
"set actual_total = $actual_cost " .
"where school_num = $school_num " .
"and supplier_num = $supplier_num";
writeLog($query);
db_query($query);
}
// Calculate net total for this menu date
$query = "select sum(actual_cost) as amount from supplier_order_items_actual " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"and menu_num = $menu_num";
writeLog($query);
$results = db_query($query);
$row = db_fetch_array($results);
$supplier_costs = sprintf("%0.2f",$row['amount']);
// Get misc expenses
$query = "select sum(amount) as amount from hotlunch_menu_transactions " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"and menu_num = $menu_num " .
"and type = 'Expense'";
writeLog($query);
$results = db_query($query);
$row = db_fetch_array($results);
$other_costs = sprintf("%0.2f",$row['amount']);
// Get misc revenue
$query = "select sum(amount) as amount from hotlunch_menu_transactions " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"and menu_num = $menu_num " .
"and type = 'Revenue'";
writeLog($query);
$results = db_query($query);
$row = db_fetch_array($results);
$other_revenue = sprintf("%0.2f",$row['amount']);
// Get order items revenue
$query = "select sum(extended_price) as amount from hotlunch_order_items " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"and menu_num = $menu_num " .
"and ordered_by_type != 'Concession' " .
"and active != 'N'";
writeLog($query);
$results = db_query($query);
$row = db_fetch_array($results);
$order_revenue = sprintf("%0.2f",$row['amount']);
$net_revenue = $order_revenue + $other_revenue - $supplier_costs - $other_costs;
// Update hotlunch_menu_dates
$query = "update hotlunch_menu_dates " .
"set order_revenue = $order_revenue, " .
"other_revenue = $other_revenue, " .
"supplier_costs = $supplier_costs, " .
"other_costs = $other_costs, " .
"net_revenue = $net_revenue " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"and record_num = $menu_num";
writeLog($query);
db_query($query);
$form_message = "Supplier Orders Have Been Updated";
$naction = "list";
break;
default:
$naction = "list";
break;
}
//
// List all records if naction is "list"
//
if ($naction == "list") {
include "hotlunch_reports.menu.php";
echo("
\n");
$message_width = 900;
include "form.messages.php";
// Get supplier order records for display
if ($supplier_school_num == 0) $supplier_school_num = $school_num;
$query = "select supplier_name,product_name,soh.supplier_num," .
"online_qty,supplier_product_qty,soi.package_qty,qty_ordered,estimated_cost,soi.actual_cost,soi.record_num,sp.record_num AS supplier_product_num " .
"from supplier_order_header as soh, supplier_order_items as soi , suppliers as s, supplier_products as sp " .
"where soh.school_num = $school_num " .
"and soi.school_num = $school_num " .
"and s.school_num = $supplier_school_num " .
"and sp.school_num = $supplier_school_num " .
"and soi.order_num = soh.record_num " .
"and soh.supplier_num = s.record_num " .
"and soh.schedule_num = $schedule_num " .
"and soh.menu_num = $menu_num " .
"and soi.supplier_product_num = sp.record_num " .
"order by 1,2";
writeLog($query);
$results = db_query($query);
// Get misc charges for display
$query = "select * from hotlunch_menu_transactions " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"and menu_num = $menu_num " .
"order by type,description";
writeLog($query);
$results2 = db_query($query);
$count = 0;
$query = "select * from hotlunch_schedules " .
"where school_num = $school_num " .
"and record_num = $schedule_num";
writeLog($query);
$hotlunch_schedule = db_getVal($query);
?>