Files
HotLunches/hotlunch_orders.print.list.all.php
2026-10-19 15:22:52 -07:00

437 lines
14 KiB
PHP

<?php
$ValidAccess = 2010;
require_once("include/globals.php");
// Start session only if none active to avoid warnings when index.php already started a session
if (function_exists('session_status') && session_status() == PHP_SESSION_NONE) {
if (isset($sessionName) && strlen($sessionName)) session_name($sessionName);
session_start();
}
// Initialize commonly-used variables to avoid undefined variable notices
if (!isset($total)) $total = 0.0;
if (!isset($hotlunch_schedule) || !is_array($hotlunch_schedule)) $hotlunch_schedule = array();
$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");
exit();
}
// Get parameters to script
if (array_key_exists('schedule_num',$_GET)) $schedule_num = db_escape_string(intval($_GET['schedule_num']));
if (array_key_exists('show',$_GET)) $show = db_escape_string($_GET['show']);
if (array_key_exists('show_status',$_GET)) $show_status = db_escape_string($_GET['show_status']);
if (!isset($schedule_num)) {
header("Location: /?action=$action&error_code=NOACCESS");
exit();
}
// 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");
exit();
}
$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) {
header("Location: /?action=$action&error_code=NOACCESS");
exit();
}
}
//
// 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 = 190;
$title_col = 10;
$columns = array(10,80,95,115,195,215,240,270);
$start_row = 10;
$curr_row = $start_row;
$incr = 5;
//
// Setup PDF document
//
require("tcpdf/config/lang/eng.php");
require("tcpdf/tcpdf.php");
$logo_url = $school_record['logo_url'];
$logo_image = "images/" . $school_record['logo_image'];
$logo_row = $max_rows + 5;
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, $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');
$this->SetXY($left_col,$logo_row + $incr);
$this->Cell($title_width,$incr - 1,"$school_name",'0',1,'R','',1);
$this->Image($logo_image,10,$logo_row, 50);
$this->SetXY(20,$logo_row + 9);
$this->SetFont($document_font,"B",$document_font_size);
$this->Cell($title_width,$incr,$logo_url,'0',1,'L');
}
}
// Force exact physical size for Letter: 8.5 x 11 inches => 279.4mm x 215.9mm (landscape)
$pageFormat = array(279.4, 215.9);
$pdf= new PDF('L', 'mm', $pageFormat, true, 'UTF-8', false);
$pdf->addPage();
$font_red = hexdec("00");
$font_green = hexdec("00");
$font_blue = hexdec("00");
$pdf->SetTextColor($font_red,$font_green,$font_blue);
$school_name = $school_record['school_name'];
$document_name = getLanguageText('TEXT-ORDERS-SUMMARY');
// Set the status condition if required
$ReportTitle = getLanguageText('TEXT-ORDERS-SUMMARY') . " - (" . $GlobalText['Student'] . ") - $schedule_name";
$orderby = "ORDER BY last_name, first_name";
$mode = "student";
if (array_key_exists('sortby',$_GET)) {
switch ($_GET['sortby']) {
case 'student':
default:
$ReportTitle = getLanguageText('TEXT-ORDERS-SUMMARY') . " (" . $GlobalText['Student'] . ") - $schedule_name";
$orderby = "ORDER BY last_name, first_name";
$mode = "student";
break;
case 'student-class':
$ReportTitle = getLanguageText('TEXT-ORDERS-SUMMARY') . " (" . $GlobalText['Class'] . ") - $schedule_name";
$orderby = "ORDER BY sequence,last_name, first_name";
$mode = "student-class";
break;
case 'account':
$ReportTitle = getLanguageText('TEXT-ORDERS-SUMMARY') . " (" . getLanguageText('TEXT-ACCOUNT') . ") - $schedule_name";
$orderby = "ORDER BY userid,last_name, first_name";
$mode = "account";
break;
case 'date':
$ReportTitle = getLanguageText('TEXT-ORDERS-SUMMARY') . " (" . getLanguageText('TEXT-DATE') . ") - $schedule_name";
$orderby = "ORDER BY added_date, last_name, first_name";
$mode = "student";
break;
}
}
$pwhere = "AND p.people_type IN ('Staff','Student','Customer') ";
if (isset($show) && $show == "All") $pwhere = "";
unset($delivered);
if (array_key_exists('delivered',$_GET) && strlen($_GET['delivered'])) $delivered = $_GET['delivered'];
if (array_key_exists('delivered',$_POST) && strlen($_POST['delivered'])) $delivered = $_POST['delivered'];
$dwhere = "";
if (isset($delivered)) {
$dwhere = "AND delivered = '$delivered' ";
if ($delivered == 'Y') $ReportTitle .= " - " . getLanguageText('TEXT-DELIVERED');
else $ReportTitle .= " - " . getLanguageText('TEXT-NOT-DELIVERED');
}
$show_where = "";
if (isset($show_status) && strlen($show_status)) $show_where = "AND hloh.status = '$show_status' ";
// Get orders for this schedule
$query = "SELECT p.last_name,p.first_name,class_num,sequence,people_type,hloh.price_category,hloh.order_total,hloh.status,p.user_num,hloh.record_num,userid,hloh.payment_method,hloh.added_date,sum(ua.transaction_amount) as balance,delivered " .
"FROM people AS p LEFT JOIN classes AS c ON p.class_num = c.record_num, hotlunch_order_header AS hloh, " .
"users AS u LEFT JOIN user_account AS ua ON ua.school_num = $school_num AND ua.user_num = u.record_num AND (ua.deleted != 'Y' OR ua.deleted IS NULL) " .
"WHERE p.school_num = $base_school_num " .
"AND hloh.school_num = $school_num " .
"AND hloh.status NOT IN ('Inactive','Deleted') " .
"AND p.record_num = hloh.ordered_by_num " .
$pwhere .
$dwhere .
$show_where .
"AND hloh.schedule_num = $schedule_num " .
"AND p.user_num = u.record_num " .
"GROUP BY 1,2,3,4,5,6,7,8,9,10,11 " .
$orderby;
$results = db_query($query);
if (db_num_rows($results) == 0) {
echo($query); exit();
header("Location: /?error_code=NOORDERS");
exit();
}
$curr_row = $max_rows + 1;
$page = 1;
// Process all the orders found for this page
while ($row = db_fetch_array($results)) {
if ($curr_row > $max_rows) {
if ($page > 1) $pdf->addPage();
$curr_row = $start_row;
$pdf->SetFillColor(hexdec("cc"),hexdec("cc"),hexdec("cc"));
$pdf->SetFont($document_font,"B",$document_font_size + 1);
$pdf->SetXY($title_col,$curr_row);
$pdf->Cell(260,$incr,$ReportTitle,'0',1,'L',true,'',1);
$curr_row += $incr + 2;
$page++;
$count = 0;
$pdf->SetXY($title_col,$curr_row);
$pdf->SetFont($document_font,"",$document_font_size);
// Titles
$cnum = 0;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,getLanguageText('TEXT-NAME'),'0',1,'L',true,'',1);
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,getLanguageText('TEXT-TYPE'),'0',1,'L',true,'',1);
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,getLanguageText('TEXT-ORDER')." ",'0',1,'R',true,'',1);
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
switch ($mode) {
case 'account':
$pdf->Cell($width,$incr,getLanguageText('TEXT-ACCOUNT'),'0',1,'L',true,'',1);
break;
case 'student-class':
$pdf->Cell($width,$incr,$GlobalText['Class'],'0',1,'L',true,'',1);
break;
case 'student':
$pdf->Cell($width,$incr,getLanguageText('TEXT-ACCOUNT'),'0',1,'L',true,'',1);
break;
}
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,getLanguageText('TEXT-CATEGORY'),'0',1,'L',true,'',1);
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,getLanguageText('TEXT-STATUS'),'0',1,'L',true,'',1);
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,getLanguageText('TEXT-ORDER-DATE'),'0',1,'L',true,'',1);
$curr_row += $incr;
}
$name = $row['last_name'] . ', ' . $row['first_name'];
$pdf->SetFillColor(hexdec("ee"),hexdec("ee"),hexdec("ee"));
$bg = true;
if ($count % 2) $bg = false;
$cnum = 0;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$name,'0',1,'L',$bg,'',1);
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$row['people_type'],'0',1,'L',$bg,'',1);
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$school_record['currency_symbol'] . number_format($row['order_total'],2) . " ",'0',1,'R',$bg,'',1);
$total += $row['order_total'];
// Display account or name depending on sort order
switch ($mode) {
case 'account':
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$row['userid'],'0',1,'L',$bg,'',1);
break;
case 'student-class':
// Get the class info
$class = $row['class_num'];
$class_info = getClassInfo($class,'internal');
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$class_info,'0',1,'L',$bg,'',1);
break;
case 'student':
// Get the name on this account
$unum = $row['user_num'];
$uname = db_getVal("select concat(first_name,' ',last_name) from users where school_num = $base_school_num and record_num = $unum");
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$uname,'0',1,'L',$bg,'',1);
break;
}
// $cnum++;
// $curr_col = $columns[$cnum];
// $width = $columns[$cnum + 1] - $columns[$cnum];
// $pdf->SetXY($curr_col,$curr_row);
// $pdf->Cell($width,$incr,$school_record['currency_symbol'] . number_format($row['balance'],2) . " ",'0',1,'R',$bg,'',1);
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$row['price_category'],'0',1,'L',$bg,'',1);
$dtext = "";
if (isset($hotlunch_schedule['schedule_type']) && $hotlunch_schedule['schedule_type'] != 'Hot Lunch' && in_array($siteType,array('hotlunch','yearbook','tickets','fundraising'))) {
$dtext = " (" . $row['delivered'] . ")";
}
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$row['status'].' - '.$row['payment_method'] . $dtext,'0',1,'L',$bg,'',1);
$cnum++;
$curr_col = $columns[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,dateString('date',strtotime($row['added_date'])),'0',1,'L',$bg,'',1);
$curr_row += $incr;
$count++;
}
// Total
$pdf->SetFillColor(hexdec("cc"),hexdec("cc"),hexdec("cc"));
$pdf->SetFont($document_font,"B",$document_font_size);
$pdf->SetXY($title_col,$curr_row);
$pdf->Cell(260,$incr," ",'0',1,'L',true,'',1);
$width = $columns[1] - $columns[0];
$curr_col = $columns[0];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,"Total:",'0',1,'R',true,'',1);
$width = $columns[3] - $columns[2];
$curr_col = $columns[2];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$school_record['currency_symbol'] . number_format($total,2) . " ",'0',1,'R',true,'',1);
// Output the completed PDF
$filename = "order-summary.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");
?>