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

354 lines
12 KiB
PHP

<?php
$ValidAccess = 2010;
require_once("include/globals.php");
// Start session only if none active to avoid "session_name()" and "session_start()" warnings
if (function_exists('session_status') && session_status() == PHP_SESSION_NONE) {
if (isset($sessionName) && strlen($sessionName)) session_name($sessionName);
session_start();
}
// Ensure commonly-used variables exist to avoid undefined notices
if (!isset($hotlunch_schedule) || !is_array($hotlunch_schedule)) $hotlunch_schedule = array();
$action = "menu-details";
// startLog("hotlunch_orders.print.list.special.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','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 = intval($_GET['schedule_num']);
if (!isset($schedule_num)) {
header("Location: /?action=$action&error_code=NOACCESS");
exit();
}
// Verify the schedule_num is a valid and active 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=$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,60,75,95,115,135,195,215,240,270);
$alignment = array('L','L','R','R','R','L','L','L','L');
$titles = array(getLanguageText('TEXT-NAME'),
getLanguageText('TEXT-TYPE'),
getLanguageText('TEXT-COST')." ",
getLanguageText('TEXT-ORDER')." ",
getLanguageText('TEXT-DISCOUNT')." ",
getLanguageText('TEXT-ACCOUNT'),
getLanguageText('TEXT-CATEGORY'),
getLanguageText('TEXT-STATUS'),
getLanguageText('TEXT-ORDER-DATE')
);
$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 report parameters
$ReportTitle = getLanguageText('TEXT-SPECIAL-PRICE-ORDERS') . " (" . $GlobalText['Student'] . ") - $schedule_name";
$orderby = "ORDER BY last_name, first_name";
$mode = "student";
// Recalculate all orders in schedule
$query = "SELECT record_num FROM hotlunch_order_header WHERE school_num = $school_num AND schedule_num = $schedule_num";
$results = db_query($query);
while ($row = db_fetch_assoc($results)) {
$order_num = $row['record_num'];
$orderData = calcOrderTotal($order_num,$schedule_num,'N');
}
// 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.order_cost,hloh.category_savings,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 " .
"AND hloh.schedule_num = $schedule_num " .
"AND p.user_num = u.record_num " .
"AND hloh.price_category != 'Regular' " .
"GROUP BY 1,2,3,4,5,6,7,8,9,10,11 " .
$orderby;
$results = db_query($query);
if (db_num_rows($results) == 0) {
header("Location: /?action=schedule-reports&schedule_num=$schedule_num&error_code=NOORDERS");
exit();
}
$total = 0;
$savings = 0;
$costs = 0;
$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,"B",$document_font_size);
// Titles
for ($cnum = 0; $cnum < count($titles); $cnum++) {
$curr_col = $columns[$cnum];
$title = $titles[$cnum];
$align = $alignment[$cnum];
$width = $columns[$cnum + 1] - $columns[$cnum];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$title,'0',1,$align,true,'',1);
}
$pdf->SetFont($document_font,"",$document_font_size);
$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_cost'],2) . " ",'0',1,'R',$bg,'',1);
$costs += $row['order_cost'];
$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'];
$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['category_savings'],2) . " ",'0',1,'R',$bg,'',1);
$savings += $row['category_savings'];
// 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);
$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($costs,2) . " ",'0',1,'R',true,'',1);
$width = $columns[4] - $columns[3];
$curr_col = $columns[3];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,$school_record['currency_symbol'] . number_format($total,2) . " ",'0',1,'R',true,'',1);
$width = $columns[5] - $columns[4];
$curr_col = $columns[4];
$pdf->SetXY($curr_col,$curr_row);
$pdf->Cell($width,$incr,'-'.$school_record['currency_symbol'] . number_format($savings,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");
?>