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

163 lines
5.8 KiB
PHP

<?php
$ValidAccess = 2010;
require_once("include/globals.php");
//
// Ensure that the user is logged in
//
if (SessionActive() != 1 || !UserLoggedIn()) {
header("Location: /?action=$action&error_code=NOACCESS");
exit();
}
// Check permissions
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator','Administration Team','Schedule Team','Supplier');
if (checkPermission($AllowedUserTypes) != 1) {
header("Location: /?action=$action&error_code=NOACCESS");
return;
}
// Get parameters to script
unset($selected_supplier_num);
if (array_key_exists('selected_supplier',$_GET)) $selected_supplier_num = intval($_GET['selected_supplier']);
if (array_key_exists('selected_supplier',$_POST)) $selected_supplier_num = intval($_POST['selected_supplier']);
$selected_schedules = array();
if (array_key_exists('selected_schedules',$_GET)) $selected_schedules = $_GET['selected_schedules'];
$snums = implode(',',$selected_schedules);
$swhere = "";
if (isset($selected_supplier_num)) {
$swhere = "AND record_num = $selected_supplier_num";
}
$Schedules = array();
$SchedulesSequence = array();
$query = "SELECT * FROM hotlunch_schedules WHERE school_num = $school_num AND record_num IN ($snums) ORDER BY end_order_datetime";
$results = db_query($query);
while ($row = db_fetch_assoc($results)) {
$snum = $row['record_num'];
$Schedules[$snum] = $row;
$SchedulesSequence[] = $row;
}
for ($i = count($SchedulesSequence) - 1; $i >= 0; $i--) {
$headings[] = $SchedulesSequence[$i]['schedule_name'];
}
$headings[] = getLanguageText('TEXT-TOTAL-ITEMS');
$headings[] = "";
$headings[] = getLanguageText('TEXT-PACKAGE-COST');
$headings[] = getLanguageText('TEXT-PACKAGE-QUANTITY');
$headings[] = getLanguageText('TEXT-TOTAL-COST');
$headings[] = getLanguageText('TEXT-PRICE');
$headings[] = getLanguageText('TEXT-TOTAL-REVENUE');
$headings[] = getLanguageText('TEXT-PROFIT');
$fname = genFileName("supplier-product-sales.csv");
// 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=$fname");
header('Content-Transfer-Encoding: text');
echo("\xEF\xBB\xBF");
// Get selected supplier(s)
$suppliers = db_query("SELECT * FROM suppliers WHERE school_num = $supplier_school_num and status = 'Active' $swhere ORDER BY supplier_name");
while ($row = db_fetch_array($suppliers)) {
extract($row);
$supplier_num = $record_num;
// Get supplier products
$products = db_query("SELECT * " .
"FROM supplier_products " .
"WHERE school_num = $supplier_school_num " .
"AND supplier_num = $supplier_num " .
"AND status = 'Active' " .
"AND product_type = 'Product' " .
"ORDER BY sequence");
$num_products = db_num_rows($results) . ' ' . getLanguageText('TEXT-PRODUCTS');
$count = 0;
$curr_row = $max_rows + 10;
if ($num_products > 0) {
echo(str_replace('"','""',$supplier_name)."\n");
echo(",");
for ($i = 0; $i < count($headings); $i++) {
echo(str_replace('"','""',$headings[$i]).",");
}
echo("\n");
while ($row = db_fetch_assoc($products)) {
echo('"'.str_replace('"','""',$row['product_name']).'",');
$product_num = $row['record_num'];
$total = 0;
for ($i = 0; $i < count($SchedulesSequence); $i++) {
$snum = $SchedulesSequence[$i]['record_num'];
$query = "SELECT sum(qty) FROM hotlunch_order_items AS hloi, hotlunch_menu_products AS hlmp, hotlunch_menu_items AS hlmi, hotlunch_menu_dates AS hlmd, hotlunch_order_header AS hloh " .
"WHERE hloi.school_num = $school_num " .
"AND hlmi.school_num = $school_num " .
"AND hlmd.school_num = $school_num " .
"AND hlmp.school_num = $school_num " .
"AND hloi.school_num = $school_num " .
"AND hloh.school_num = $school_num " .
"AND hloh.schedule_num = hloi.schedule_num " .
"AND hloi.order_num = hloh.record_num " .
"AND hloi.ordered_by_num = hloh.ordered_by_num " .
"AND hloh.status NOT IN ('Inactive','Deleted') " .
"AND hloi.menu_item_num = hlmp.menu_item_num " .
"AND hloi.active = 'Y' " .
"AND hlmp.supplier_product_num = $product_num " .
"AND hlmp.menu_item_num = hlmi.record_num " .
"AND hlmi.menu_num = hlmd.record_num " .
"AND hlmd.schedule_num = $snum";
$qty = db_getVal($query);
$total += $qty;
echo("$qty,");
}
echo("$total,");
echo(",");
echo('"'.$currencySymbol.number_format($row['package_cost'],2).'",');
echo(number_format($row['package_qty'],0).",");
$units = intval(($total + $row['package_qty'] - 1) / $row['package_qty']);
$cost = $units * $row['package_cost'];
echo('"'.$currencySymbol.number_format($cost,2).'",');
echo('"'.$currencySymbol.number_format($row['standard_online_price'],2).'",');
$revenue = $total * $row['standard_online_price'];
echo('"'.$currencySymbol.number_format($revenue,2).'",');
$profit = $revenue - $cost;
echo('"'.$currencySymbol.number_format($profit,2).'",');
$count++;
echo("\n");
}
}
echo("\n");
}
?>