Files
HotLunches/hotlunch.order_details.summary.generate.php
2026-10-19 15:22:52 -07:00

336 lines
11 KiB
PHP

<?php
$ValidAccess = 2010;
require_once("include/globals.php");
// Guarded session handling to avoid warnings and align with other modules
if (session_status() === PHP_SESSION_NONE) {
if (!empty($sessionName)) {
session_name($sessionName);
}
session_start();
}
//
// Ensure that the user is logged in
//
if (SessionActive() != 1 || !UserLoggedIn()) {
header("Location: /?action=order-details&schedule_num=$schedule_num&menu_num=$menu_num&error_code=NOACCESS");
exit();
}
// Check permissions
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator','Application Volunteer','Schedule Volunteer','Administration Team','Schedule Team','Supplier');
if (checkPermission($AllowedUserTypes) != 1) {
header("Location: /?action=order-details&schedule_num=$schedule_num&menu_num=$menu_num&error_code=NOACCESS");
exit();
}
//
// Get and validate the parameters
//
if (array_key_exists('schedule_num',$_POST)) $schedule_num = intval($_POST['schedule_num']);
if (array_key_exists('menu_num',$_POST)) $menu_num = intval($_POST['menu_num']);
if (array_key_exists('document_format',$_POST)) $document_format = $_POST['document_format'];
if (!isset($schedule_num)) {
header("Location: /?action=order-details&schedule_num=$schedule_num&menu_num=$menu_num&error_code=NOSCHED");
exit();
}
if (!isset($menu_num)) {
header("Location: /?action=order-details&schedule_num=$schedule_num&menu_num=$menu_num&error_code=NOMENU");
exit();
}
// Check if this is a valid schedule
$query = "select * from hotlunch_schedules " .
"where school_num = $school_num " .
"and record_num = $schedule_num";
$results = db_query($query);
if (db_num_rows($results) == 0) {
header("Location: /?action=order-details&schedule_num=$schedule_num&menu_num=$menu_num&error_code=INVALIDSCHED");
exit();
}
$hotlunch_schedule = db_fetch_array($results);
$schedule_name = $hotlunch_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) {
header("Location: /?action=order-details&schedule_num=$schedule_num&menu_num=$menu_num&error_code=NOACCESS");
exit();
}
}
// 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";
$results = db_query($query);
if (db_num_rows($results) == 0) {
header("Location: /?action=order-details&schedule_num=$schedule_num&menu_num=$menu_num&error_code=INVALIDMENU");
exit();
}
$hotlunch_menu_date = db_fetch_array($results);
$menu_title = $hotlunch_menu_date['menu_title'];
$menu_date = date("D M j Y",strtotime($hotlunch_menu_date['menu_date']));
// Determine which items have been selected
$selected_items = array();
$query = "select * from hotlunch_menu_items " .
"where school_num = $school_num " .
"and menu_num = $menu_num " .
"order by sequence";
$results = db_query($query);
while ($row = db_fetch_array($results)) {
$record_num = $row['record_num'];
if (array_key_exists("item_$record_num", $_POST) && $_POST["item_$record_num"] == 'Y') $selected_items[] = $record_num;
}
if (count($selected_items) == 0) {
header("Location: /?action=order-details&schedule_num=$schedule_num&menu_num=$menu_num&error_code=NOITEMS");
exit();
}
$school_name = $school_record['school_name'];
$order_by = "division";
$numClasses = db_getVal("SELECT count(*) FROM classes WHERE school_num = $base_school_num");
if ($numClasses == 0) {
db_query("UPDATE people SET class_num = 3, meal_location = 1 WHERE school_num = $base_school_num AND people_type = 'Student'");
}
//
// Orders to print
//
$items = implode(',',$selected_items);
$query = "select distinct hloi.ordered_by_type,p.first_name,p.last_name,p.class_num,c.division,c.sequence,c.grade,hloi.order_num " .
"from hotlunch_order_header as hloh, hotlunch_order_items as hloi, hotlunch_menu_items as hlmi, " .
"people as p left join classes as c on c.school_num = $base_school_num and p.class_num = c.record_num left join people as p1 on c.teacher1 = p1.record_num " .
"where hloh.record_num = hloi.order_num " .
"and hloh.status not in ('Inactive','Deleted') " .
"and hloi.ordered_by_num = p.record_num " .
"and hloi.school_num = $school_num " .
"and hlmi.school_num = $school_num " .
"and p.school_num = $base_school_num " .
"and hloi.menu_item_num in ($items) " .
"and hloi.menu_item_num = hlmi.record_num " .
"and hloi.active != 'N' " .
"order by $order_by";
$orders = db_query($query);
// Initialize the PDF document
$document_font = "Arial";
$document_font_weight = "";
$document_text_color = "#000000";
$document_font_size = 9;
$incr = 11;
//
// Setup PDF document
//
define('FPDF_FONTPATH',"font/");
require("include/fpdi.php");
$pdf= new fpdi('P','pt','Letter');
$pdf->addPage();
$pdf->SetAutoPageBreak(true,10);
$font_parm = $document_font_weight;
$font_red = hexdec(substr(str_replace("#","",$document_text_color),0,2));
$font_green = hexdec(substr(str_replace("#","",$document_text_color),2,2));
$font_blue = hexdec(substr(str_replace("#","",$document_text_color),4,2));
$pdf->SetTextColor($font_red,$font_green,$font_blue);
// Set parameters for this document
$start_row = 10;
$max_rows = 700;
$curr_row = $start_row;
$new_page = true;
$left_col = 10;
$title_col = 200;
$qty_col = 50;
$item_col = 75;
$items_col = 20;
$pdf->SetFont("Arial",$font_parm,$document_font_size);
$pdf->SetFillColor(hexdec("cc"),hexdec("cc"),hexdec("cc"));
$fill = true;
if ($document_format == 'report') $fill = false;
$prev_div = -1;
// Process each record
while ($entry = db_fetch_array($orders)) {
extract($entry);
if ($division != $prev_div) {
$class_info = "Staff";
if (isset($class_num) && $class_num > 0) $class_info = getClassInfo($class_num,'external',0);
$prev_div = $division;
if ($curr_row != $start_row) $curr_row = $max_rows;
}
if ($curr_row >= $max_rows - $incr * 2) {
$pdf->addPage();
$curr_row = $start_row;
$new_page = true;
}
// Display titles in report format on each page
if ($new_page && $document_format == 'report') {
$pdf->SetFont("Arial",'B',$document_font_size + 1);
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell(200,$incr + 2,$school_name,'0',1,'L',true);
$pdf->SetXY($title_col,$curr_row);
$pdf->Cell(400,$incr + 2,"$class_info $menu_title, $menu_date",'0',1,'R',true);
$new_page = false;
$curr_row += $incr + 10;
$pdf->SetFont("Arial",$font_parm,$document_font_size);
}
// Display student name
$name = trim($name);
$pdf->SetXY($left_col,$curr_row);
$pdf->Cell(200,$incr,$name,'0',1,'L',$fill);
if ($document_format == 'receipt') {
$pdf->SetXY($title_col,$curr_row);
$pdf->Cell(400,$incr,"$class_info $menu_title, $menu_date",'0',1,'R',$fill);
}
// Get order items for this order
$query = "select hloi.*,hlmi.item_title,hlmi.sequence from hotlunch_order_items as hloi, hotlunch_menu_items as hlmi " .
"where hloi.school_num = $school_num " .
"and hlmi.school_num = $school_num " .
"and hloi.order_num = $order_num " .
"and hloi.menu_item_num in ($items) " .
"and hloi.menu_item_num = hlmi.record_num " .
"and hloi.active != 'N' " .
"order by sequence";
$ordered_items = db_query($query);
// Display the items
switch ($document_format) {
case 'receipt':
while ($row = db_fetch_array($ordered_items)) {
extract($row);
// Check for item options
$options = "";
$options_ordered = trim($options_ordered);
$options2_ordered = trim($options2_ordered);
$options3_ordered = trim($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)";
}
$curr_row += $incr;
$pdf->SetXY($qty_col,$curr_row);
$pdf->Cell(20,$incr,$qty,'0',1,'R',false);
$pdf->SetXY($item_col,$curr_row);
$pdf->Cell(200,$incr,"$item_title $options",'0',1,'L',false);
}
$curr_row += 30;
break;
case 'report':
$items_ordered = "";
$line = 1;
while ($row = db_fetch_array($ordered_items)) {
extract($row);
// Check for item options
$options = "";
$options_ordered = trim($options_ordered);
$options2_ordered = trim($options2_ordered);
$options3_ordered = trim($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)";
}
if (strlen($items_ordered) > 0) {
if ((strlen($items_ordered) + strlen("$item_title $options")) > (140 * $line)) {
$items_ordered .= "\n";
$line++;
}
else {
$items_ordered .= " // ";
}
}
if ($qty > 1) $items_ordered .= "$qty x ";
$items_ordered .= "$item_title $options";
}
$curr_row += $incr;
$pdf->SetFont("Arial",$font_parm,$document_font_size - 1);
$pdf->SetXY($items_col,$curr_row);
$pdf->MultiCell(0,$incr,$items_ordered,'0','L',false);
$pdf->SetFont("Arial",$font_parm,$document_font_size);
$curr_row = $pdf->GetY();
break;
}
$curr_row += $incr;
}
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=order-details-summary.pdf");
header('Content-Transfer-Encoding: binary');
$pdf->Output("order-details-summary.pdf","D");
?>