Files
HotLunches/hotlunch_order.receipts.print.php
T
2026-10-19 15:22:52 -07:00

528 lines
19 KiB
PHP

<?php
$ValidAccess = 2010;
require_once("include/globals.php");
// Start session only if none active. Avoid changing session name when a session
// is already active (prevents "Session name cannot be changed" warning).
if (function_exists('session_status') && session_status() == PHP_SESSION_NONE) {
if (isset($sessionName) && strlen($sessionName)) session_name($sessionName);
session_start();
}
$action = "menu-details";
//
// 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','Application Volunteer','Schedule Volunteer','Administration Team','Schedule Team','Supplier','Financial Team');
if (checkPermission($AllowedUserTypes) != 1) {
header("Location: /?action=$action&error_code=NOACCESS");
return;
}
// Get parameters to script
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 (!isset($schedule_num)) {
header("Location: /?action=$action&error_code=NOACCESS");
return;
}
unset($order_num);
if (array_key_exists('order_num',$_GET)) {
$order_num = intval($_GET['order_num']);
$selected_user = db_getVal("SELECT user_num FROM hotlunch_order_header AS hlh,people AS p WHERE hlh.school_num = $school_num AND ordered_by_num = p.record_num AND hlh.record_num = $order_num");
}
// Verify the schedule_num is a valid and active schedule
$swhere = "";
if ($_SESSION['UserInfo']['type'] == 'Supplier') $swhere = " and supplier_num = " . $_SESSION['UserInfo']['supplier_num'];
$today = date("Y-m-d",time());
$query = "select * from hotlunch_schedules " .
"where school_num = $school_num " .
"and record_num = $schedule_num " .
"$swhere";
$results = db_query($query);
if (db_num_rows($results) == 0) {
header("Location: /?action=$action&error_code=INVALIDSCHED");
return;
}
$schedule = db_fetch_array($results);
$schedule_name = $schedule['schedule_name'];
// 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;
}
}
//
// Setup the PDF
//
$document_font = $school_record['document_font'];
if (strlen($document_font) == 0) $document_font = "Helvetica";
$document_font_weight = "";
$document_text_color = "#000000";
$document_font_size = 9;
// Set parameters for this document
$max_rows = 240;
$left_col = 5;
$line_width = 190;
$item_col = 5;
$item_width = 140;
$qty_col = $item_col + $item_width;
$qty_width = 13;
$price_col = $qty_col + $qty_width;
$price_width = 15;
$sign_col = $price_col + $price_width;
$sign_width = 4;
$extended_col = $sign_col + $sign_width;
$extended_width = 18;
$start_row = 5;
$curr_row = $start_row;
$incr = 4.5;
$logo_url = $school_record['logo_url'];
$logo_image = "images/" . $school_record['logo_image'];
$logo_row = $max_rows + 10;
//
// Setup PDF document
//
require("tcpdf/config/lang/eng.php");
require("tcpdf/tcpdf.php");
$school_name = $school_record['school_name'];
$document_name = getLanguageText('TEXT-SCHEDULE-RECEIPTS');
class PDF extends TCPDF {
function Header() {
return;
}
function Footer() {
global $incr, $left_col, $title_width, $max_rows, $document_font, $document_font_size, $document_name, $logo_image, $logo_url, $logo_row, $footer_text, $school_name;
// Print the date
$this->SetFont($document_font,"",$document_font_size - 1);
$this->SetXY($left_col,$logo_row);
$date = dateString('datetime');
$this->Cell($title_width,$incr - 1,"$document_name: $date",'0',1,'R',false,'',1);
$this->SetXY($left_col,$logo_row + $incr);
$this->Cell($title_width,$incr - 1,"$school_name",'0',1,'R','',1);
// Print the footer text
if (strlen($footer_text) > 0) {
$this->SetXY($left_col,$logo_row + $incr);
$this->Cell($title_width,$incr - 1,$footer_text,'0',1,'R',false,'',1);
}
$this->Image($logo_image,30,$logo_row, 50);
}
}
// Force exact physical size for Letter: 8.5 x 11 inches => 215.9mm x 279.4mm (portrait)
$pageFormat = array(215.9, 279.4);
$pdf= new PDF('P', 'mm', $pageFormat, true, 'UTF-8', false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$font_red = hexdec("00");
$font_green = hexdec("00");
$font_blue = hexdec("00");
$pdf->SetTextColor($font_red,$font_green,$font_blue);
$pdf->SetFont($document_font,"",$document_font_size);
$pdf->addPage();
// Get menu dates info
$query = "select * from hotlunch_menu_dates " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"order by sequence,menu_date";
$results = db_query($query);
$menu_dates = array();
while ($row = db_fetch_array($results)) {
$menu_dates[] = $row;
}
// Get menu items for each menu date
$menu_items = array();
for ($i = 0; $i < count($menu_dates); $i++) {
// Ensure this slot is initialized to an array so later code can safely
// count() and iterate even when there are no items for a menu date.
$menu_items[$i] = array();
$menu_num = $menu_dates[$i]['record_num'];
$results = db_query("SELECT * FROM hotlunch_menu_items WHERE school_num = $school_num AND menu_num = $menu_num ORDER BY sequence");
while ($row = db_fetch_array($results)) {
$menu_items[$i][] = $row;
}
}
$ReportTitle = "Schedule Receipt - $schedule_name";
if (!isset($selected_user)) {
$users = db_query("SELECT * FROM users WHERE school_num = $base_school_num AND type IN ('Staff','Parent','Participant','Guest','Application Volunteer','Administration Team') ORDER BY type,last_name,first_name");
}
else {
$users = db_query("SELECT * FROM users WHERE school_num = $base_school_num AND record_num = $selected_user");
}
$page = 0;
while ($user = db_fetch_array($users)) {
extract($user);
$school_num = $school_record['record_num'];
$user_num = $record_num;
$user_name = "$first_name $last_name";
// Get set of people linked to this account
$results = db_query("SELECT * FROM people WHERE school_num = $base_school_num AND user_num = $user_num ORDER BY last_name,first_name");
if (db_num_rows($results) == 0) continue;
$people = "";
$curr_people = array();
while ($row = db_fetch_array($results)) {
if (strlen($people) > 0) $people .= ",";
$people .= $row['record_num'];
$curr_people[] = $row;
}
if (count($curr_people) == 0) continue;
// Determine if there are any orders for these people
$query = "SELECT count(*) FROM hotlunch_order_header WHERE school_num = $school_num AND schedule_num = $schedule_num AND ordered_by_num IN ($people) AND status NOT IN ('Inactive','Deleted')";
$num_orders = db_getVal($query);
if ($num_orders == 0) continue;
// Process each person found
for ($p = 0; $p < count($curr_people); $p++) {
$row = $curr_people[$p];
$curr_person = $row['record_num'];
$curr_class = $row['class_num'];
$class_info = 'Staff';
if (is_numeric($curr_class)) $class_info = getClassInfo($curr_class);
$order_total = 0;
// Check if this person has an order
$results = db_query("SELECT * FROM hotlunch_order_header WHERE school_num = $school_num AND schedule_num = $schedule_num AND ordered_by_num = $curr_person AND status NOT IN ('Inactive','Deleted')");
if (db_num_rows($results) == 0) continue;
while ($oh = db_fetch_array($results)) {
$order_num = $oh['record_num'];
$status = $oh['status'];
$startNewPage = true;
// Process each menu date for this person
for ($i = 0; $i < count($menu_dates); $i++) {
$menu_date = $menu_dates[$i];
// Guard access: if no items exist for this menu date, use an
// empty array so subsequent count() and loops are safe.
$curr_items = isset($menu_items[$i]) && is_array($menu_items[$i]) ? $menu_items[$i] : array();
$menuTitlePrinted = false;
$menu_num = $menu_dates[$i]['record_num'];
$query = "SELECT max(length(sp.supplier_code)) " .
"FROM hotlunch_menu_products AS hlmp,hotlunch_menu_items AS hlmi,supplier_products AS sp " .
"WHERE hlmp.school_num = $school_num " .
"AND hlmi.school_num = $school_num " .
"AND hlmp.menu_item_num = hlmi.record_num " .
"AND hlmi.menu_num = $menu_num " .
"AND sp.school_num = $supplier_school_num " .
"AND sp.record_num = hlmp.supplier_product_num";
$max_length = db_getVal($query);
$code_offset = 0;
if ($max_length > 0) {
$code_offset = 18;
if (isset($supplier_code) && $max_length > 6) $code_offset = strlen($supplier_code) * 3;
}
// Process each menu item for this person and menu date
for ($j = 0; $j < count($curr_items); $j++) {
$curr_menu_item = $curr_items[$j];
$item_num = $curr_menu_item['record_num'];
if ($curr_row >= $max_rows) $startNewPage = true;
if ($startNewPage) {
if ($page > 0) $pdf->addPage();
$page++;
$curr_row = $start_row;
// Print header
$pdf->SetFillColor(hexdec("aa"),hexdec("aa"),hexdec("aa"));
$pdf->SetFont($document_font,"B",$document_font_size + 1);
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell($line_width,$incr + 1,$ReportTitle,'0',1,'L',true,'',1);
$curr_row += $incr + 2;
$pdf->SetFont($document_font,"B",$document_font_size);
$pdf->SetXY($left_col,$curr_row);
$phone = "";
if (strlen($home_phone) > 0) $phone = " - $home_phone";
$pdf->Cell($line_width,$incr,"$type Account: $userid - $user_name $phone",'0',1,'L',false,'',1);
$curr_row += $incr - 1;
// Print current person info
$pdf->SetXY($left_col,$curr_row);
$pname = '';
$ptype = '';
if (array_key_exists('first_name', $row)) $pname = $row['first_name'] . ' ' . $row['last_name'];
if (array_key_exists('people_type', $row)) $ptype = $row['people_type'];
$pdf->Cell($line_width,$incr,"Order For: $pname - $ptype Order Number: $order_num Status: $status",'0',1,'L',false,'',1);
// Display class info if this is not a staff person
if ($class_info != 'Staff' && $class_info != 'Customer') {
$curr_row += $incr - 1;
$pdf->SetFont($document_font,"",$document_font_size);
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell($line_width,$incr,$class_info,'0',1,'L',false,'',1);
}
if ($ptype == 'Customer') {
if ($school_record['customer_address'] == 'Y') {
$curr_row += $incr - 1;
$pdf->SetFont($document_font,"",$document_font_size);
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell($line_width,$incr,$row['cust_address'].", ".$row['cust_city'].", ".$row['cust_province']." ".$row['cust_country']." ".$row['cust_postal'],'0',1,'L',false,'',1);
}
if ($school_record['customer_phone'] == 'Y') {
$curr_row += $incr - 1;
$pdf->SetFont($document_font,"",$document_font_size);
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell($line_width,$incr,$row['cust_phone'],'0',1,'L',false,'',1);
}
if ($school_record['customer_email'] == 'Y') {
$curr_row += $incr - 1;
$pdf->SetFont($document_font,"",$document_font_size);
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell($line_width,$incr,$row['cust_email'],'0',1,'L',false,'',1);
}
}
$curr_row += $incr + 2;
$startNewPage = false;
$line = 0;
}
// Determine if there is an order for this item
$results = db_query("SELECT * FROM hotlunch_order_items WHERE school_num = $school_num AND ordered_by_num = $curr_person AND menu_item_num = $item_num AND active != 'N'");
if (db_num_rows($results) == 0) continue;
$row = db_fetch_array($results);
$qty = $row['qty'];
$net = $row['extended_price'];
$order_total += $net;
if (!$menuTitlePrinted) {
$pdf->SetFillColor(hexdec("bb"),hexdec("bb"),hexdec("bb"));
$pdf->SetFont($document_font,"B",$document_font_size);
$curr_row += 3;
$pdf->SetXY($left_col,$curr_row);
$menu_title = $menu_date['menu_title'];
$pdf->Cell($line_width,$incr,$menu_title,'0',1,'L',true,'',1);
$curr_row += $incr;
$menuTitlePrinted = true;
}
$pdf->SetFillColor(hexdec("dd"),hexdec("dd"),hexdec("dd"));
$pdf->SetFont($document_font,"",$document_font_size);
$fill = false;
if ($line % 2) $fill = true;
$line++;
// Print each item ordered
$pdf->SetFont($document_font,"",$document_font_size);
// Check for item options
$options = "";
$options_ordered = trim($row['options_ordered']);
$options2_ordered = trim($row['options2_ordered']);
$options3_ordered = trim($row['options3_ordered']);
if (strlen($options2_ordered)) $options_ordered .= "\n$options2_ordered";
if (strlen($options3_ordered)) $options_ordered .= "\n$options3_ordered";
if (strlen($options_ordered) > 0) {
$options = trim(str_replace("\n",", ",trim($options_ordered)));
if (strlen($options) > 0) $options = " ($options)";
}
// Check if this item has a supplier code and if so display it
if ($code_offset > 0) {
$product_num = db_getVal("SELECT supplier_product_num FROM hotlunch_menu_products WHERE school_num = $school_num AND menu_item_num = $item_num AND product_sequence = 1");
$supplier_code = db_getVal("SELECT supplier_code FROM supplier_products WHERE school_num = $supplier_school_num AND record_num = $product_num");
if (strlen($supplier_code) > 0) $supplier_code .= ":";
$pdf->SetXY($item_col,$curr_row);
$pdf->Cell($code_offset,$incr,$supplier_code,'0',1,'L',$fill,'',1);
}
// Item description
$item_title = $curr_menu_item['item_title'];
if ($schedule['schedule_receipts_description'] == 'Y' && strlen($curr_menu_item['item_description']) > 0) {
$description = $curr_menu_item['item_description'];
$description = htmlspecialchars_decode($description,ENT_QUOTES);
$maxLength = 70;
if (strlen($description) > $maxLength) {
$words = explode(' ',$description);
$description = $words[0];
for ($w = 1; $w < count($words); $w++) {
if (strlen($description) + strlen($words[$w]) + 1 > $maxLength) break;
$description .= ' ' . $words[$w];
}
$description .= "...";
}
$item_title .= " - " . $description;
}
$pdf->SetXY($item_col + $code_offset,$curr_row);
$pdf->Cell($item_width - $code_offset,$incr,$item_title . $options,'0',1,'L',$fill,'',1);
// Item quantity
$pdf->SetXY($qty_col,$curr_row);
$pdf->Cell($qty_width,$incr,"$qty x",'0',1,'R',$fill,'',1);
// Item price
$pdf->SetXY($price_col,$curr_row);
$pdf->Cell($price_width,$incr,$currencySymbol . number_format($curr_menu_item['online_price'],2),'0',1,'R',$fill,'',1);
// Extended price
$pdf->SetXY($sign_col,$curr_row);
$pdf->Cell($sign_width,$incr,'=','0',1,'L',$fill,'',1);
$pdf->SetXY($extended_col,$curr_row);
$pdf->Cell($extended_width,$incr,$currencySymbol . number_format($net,2),'0',1,'R',$fill,'',1);
$curr_row += $incr;
}
}
// Print order total
$pdf->SetFillColor(hexdec("aa"),hexdec("aa"),hexdec("aa"));
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell($line_width - $extended_width,$incr,"Order Total: ",'0',1,'R',true,'',1);
$pdf->SetXY($extended_col,$curr_row);
$pdf->Cell($extended_width,$incr,$currencySymbol . number_format($order_total,2),'0',1,'R',true,'',1);
$curr_row += $incr + 1;
// Calculate taxes on the order
if ($schedule['tax_rate'] > 0) {
$tax_amount = $order_total * $schedule['tax_rate'] / 100;
$grand_total = $order_total + $tax_amount;
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell($line_width - $extended_width,$incr,$schedule['tax_description'] . ": ",'0',1,'R',true,'',1);
$pdf->SetXY($extended_col,$curr_row);
$pdf->Cell($extended_width,$incr,$currencySymbol . number_format($tax_amount,2),'0',1,'R',true,'',1);
$curr_row += $incr + 1;
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell($line_width - $extended_width,$incr,"Total: ",'0',1,'R',true,'',1);
$pdf->SetXY($extended_col,$curr_row);
$pdf->Cell($extended_width,$incr,$currencySymbol . number_format($grand_total,2),'0',1,'R',true,'',1);
$curr_row += $incr + 1;
}
}
}
}
// Output the completed PDF
$filename = "schedule-receipt.pdf";
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/pdf');
header("Content-Disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
$pdf->Output($filename,"D");
?>