Files
HotLunches/schedule_products.csv.php
2026-10-19 15:22:52 -07:00

135 lines
4.3 KiB
PHP

<?php
$ValidAccess = 2010;
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: /");
exit();
}
$action = "schedule-products-csv";
// Get parameters
$selected_menus = array();
$selected_products = array();
if (array_key_exists('action',$_POST)) $action = $_POST['action'];
if (array_key_exists('schedule_num',$_POST)) $schedule_num = intval($_POST['schedule_num']);
if (array_key_exists('selected_menus',$_POST)) $selected_menus = $_POST['selected_menus'];
if (array_key_exists('selected_products',$_POST)) $selected_products = $_POST['selected_products'];
if (!isset($mode)) $mode = 'detail';
// Ensure that only valid user types have access to this module
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator','Administration Team','Schedule Team','Supplier');
if (checkPermission($AllowedUserTypes) != 1) {
header("Location: /?action=$action&error_code=NOACCESS&schedule_num=$schedule_num");
return;
}
// Validate parameters
if (count($selected_products) == 0) {
header("Location: /?action=$action&error_code=NOPRODUCTS&schedule_num=$schedule_num");
return;
}
if (count($selected_menus) == 0) {
header("Location: /?action=$action&error_code=NOMENUS&schedule_num=$schedule_num&num=$numMenus");
return;
}
$numDates = count($selected_menus);
$numProducts = count($selected_products);
$menuDates = db_escape_string(implode(",",$selected_menus));
$supplierProducts = db_escape_string(implode(",",$selected_products));
// 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 record_num = $schedule_num " .
"and school_num = $school_num $swhere";
$results = db_query($query);
if (db_num_rows($results) == 0) {
header("Location: /?action=$action&error_code=INVALIDSCHED");
return;
}
$hotlunch_schedule = db_fetch_array($results);
// If this is a ScheduleTeam user check if they have permission to access this schedule
if (in_array($_SESSION['UserInfo']['type'],array('Schedule Team','Schedule Volunteer'))) {
$userNum = intval($_SESSION['UserInfo']['record_num']);
$access = db_getVal("SELECT count(*) FROM hotlunch_schedules_people WHERE school_num = $school_num AND schedule_num = $schedule_num AND user_num = $userNum");
if (!$access || $access == 0) {
echo("<br>\n");
$error_message = "You are not allowed access to the requested hot lunch schedule";
include "form.messages.php";
return;
}
}
// Initialize the spreadsheet document type
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header("Content-type: application/octet-stream; charset=utf-8");
header("Content-Disposition: attachment; filename=product-totals.csv");
header('Content-Transfer-Encoding: text');
echo("\xEF\xBB\xBF");
// Get set of products and the quantities
$query = "SELECT product_name,package_qty,package_cost,sum(qty) " .
"FROM hotlunch_order_items AS hloi, hotlunch_menu_products AS hlmp, supplier_products AS sp " .
"WHERE hloi.school_num = $school_num " .
"AND hlmp.school_num = $school_num " .
"AND hlmp.supplier_product_num IN ( $supplierProducts ) " .
"AND hloi.schedule_num = $schedule_num " .
"AND hloi.menu_num IN ( $menuDates ) " .
"AND hloi.menu_item_num = hlmp.menu_item_num " .
"AND hloi.active != 'N' " .
"AND sp.school_num = $supplier_school_num " .
"AND sp.record_num = hlmp.supplier_product_num " .
"GROUP BY 1,2,3 " .
"ORDER BY 1";
$results = db_query($query);
echo("Schedule: " . $hotlunch_schedule['schedule_name'] . "\n");
echo("Selected Products: $numProducts\n");
echo("Selected Dates: $numDates\n");
echo("\n");
echo("PRODUCT,PKG QTY,PKG COST,QTY SOLD,TOTAL COST\n");
// Process the results
while ($row = db_fetch_array($results)) {
// Output the results
if (is_array($row)) {
for ($i = 0; $i < count($row); $i++) {
$value = '';
if (array_key_exists($i, $row)) $value = $row[$i];
echo('"' . $value . '",');
}
echo($row[2] * intval(($row[3] + $row[1] - 1) / $row[1]));
echo("\n");
}
}
?>