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

173 lines
6.2 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']);
if (array_key_exists('start_month',$_GET)) $start_month = db_escape_string($_GET['start_month']);
if (array_key_exists('end_month',$_POST)) $end_month = db_escape_string($_POST['end_month']);
$swhere = "";
if (isset($selected_supplier_num)) {
$swhere = "AND record_num = $selected_supplier_num";
}
// Get months to process
$query = "SELECT substring(menu_date,1,7) AS month " .
"FROM hotlunch_menu_dates AS hlmd, hotlunch_order_items AS hloi, hotlunch_schedules AS hls " .
"WHERE hlmd.school_num = $school_num " .
"AND hloi.school_num = $school_num " .
"AND hls.school_num = $school_num " .
"AND hlmd.record_num = hloi.menu_num " .
"AND hls.record_num = hlmd.schedule_num " .
"AND hls.schedule_type IN ('Hot Lunch','Date Sensitive','Other') " .
"GROUP BY 1 " .
"ORDER BY 1 DESC " .
"LIMIT 0,11";
$menu_months = db_query($query);
$i = db_num_rows($menu_months);
$months = array();
while ($row = db_fetch_assoc($menu_months)) {
$month = $row['month'];
if ($month < $start_month || $month > $end_month) continue;
$months[] = $row;
}
for ($i = count($months) - 1; $i >= 0; $i--) {
$headings[] = $months[$i]['month'];
}
$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;
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($months); $i++) {
$month = $headings[$i];
$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.menu_date LIKE '$month%'";
$qty = intval(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");
}
?>