316 lines
9.7 KiB
PHP
316 lines
9.7 KiB
PHP
<?php
|
|
$ValidAccess = 2010;
|
|
|
|
require_once("include/globals.php");
|
|
|
|
// Guarded session handling to avoid warnings and header issues
|
|
if (!empty($sessionName) && session_status() === PHP_SESSION_NONE) {
|
|
session_name($sessionName);
|
|
}
|
|
if (session_status() === PHP_SESSION_NONE || session_id() === '') {
|
|
session_start();
|
|
}
|
|
|
|
|
|
//
|
|
// Ensure that the user is logged in
|
|
//
|
|
if (SessionActive() != 1 || !UserLoggedIn()) {
|
|
echo("Invalid Access");
|
|
exit();
|
|
}
|
|
|
|
|
|
//
|
|
// Setup the PDF
|
|
//
|
|
// Use dejavusans as reliable default - it's bundled with TCPDF and supports bold/italic
|
|
$document_font = strtolower(trim($school_record['document_font'] ?? ''));
|
|
if ($document_font === '' || $document_font === 'helvetica') {
|
|
$document_font = 'dejavusans';
|
|
}
|
|
|
|
$document_font_weight = "";
|
|
$document_text_color = "#000000";
|
|
$document_font_size = 9;
|
|
|
|
|
|
// Set parameters for this document
|
|
$max_rows = 185;
|
|
$max_width = 255;
|
|
$left_col = 10;
|
|
$last_col = 265;
|
|
|
|
$start_row = 10;
|
|
$incr = 4.5;
|
|
|
|
$logo_url = $school_record['logo_url'];
|
|
$logo_image = "images/" . $school_record['logo_image'];
|
|
$logo_row = $max_rows + 5;
|
|
|
|
$report_title = getLanguageText('TEXT-SCHEDULE-ORDER-PAYMENT-SUMMARY');
|
|
$report_subtitle = $school_record['school_name'];
|
|
$document_name = $report_title;
|
|
|
|
|
|
//
|
|
// Setup PDF document
|
|
//
|
|
require("tcpdf/config/lang/eng.php");
|
|
require("tcpdf/tcpdf.php");
|
|
|
|
class PDF extends TCPDF {
|
|
function Header() {
|
|
global $incr, $left_col, $start_row, $title_width, $document_font, $document_font_size, $report_title, $report_subtitle;
|
|
|
|
$this->SetFont($document_font,"B",$document_font_size + 1);
|
|
$this->SetFillColor(hexdec("bb"),hexdec("bb"),hexdec("bb"));
|
|
|
|
// Report title
|
|
$this->SetXY($left_col,$start_row);
|
|
$this->Cell($title_width,$incr,$report_title,'0',1,'L',true);
|
|
}
|
|
|
|
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);
|
|
|
|
// 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);
|
|
|
|
// Get parameters
|
|
$twhere = "";
|
|
if (array_key_exists('twhere',$_GET) && strlen($_GET['twhere'])) $twhere = $_GET['twhere'];
|
|
if (array_key_exists('selected_schedules',$_GET) && strlen($_GET['selected_schedules']) > 0) {
|
|
$snums = $_GET['selected_schedules'];
|
|
$selected_schedules = explode(',',$snums);
|
|
}
|
|
else {
|
|
// Get set of schedules to display
|
|
$results = db_query("SELECT distinct schedule_num,schedule_name FROM hotlunch_order_header AS hloh, hotlunch_schedules AS hs " .
|
|
"WHERE hloh.school_num = $school_num " .
|
|
"AND hs.school_num = $school_num " .
|
|
"AND hloh.schedule_num = hs.record_num " .
|
|
"AND (payment_method IS NOT NULL AND payment_method != '') " .
|
|
$twhere .
|
|
"ORDER BY 2");
|
|
$selected_schedules = array();
|
|
while ($row = db_fetch_array($results)) {
|
|
$selected_schedules[] = $row['schedule_num'];
|
|
}
|
|
}
|
|
|
|
$school_name = $school_record['school_name'];
|
|
$school_num = $school_record['record_num'];
|
|
|
|
|
|
// Setup configuration for this document
|
|
// $max_rows = 250;
|
|
$columns = array(15,65,85,105,130,150,175,195);
|
|
$column_headers = array('Payment Method','Count','%','Total','%','Paid','%');
|
|
$column_align = array('L','C','C','R','C','R','C');
|
|
$column_fields = array('payment_method','num','p1','total','p2','paid','p3');
|
|
$column_format = array('none','none','none','money','none','money','none');
|
|
$start_row = 20;
|
|
$incr = 4;
|
|
|
|
|
|
$pdf->SetFillColor(hexdec("ee"),hexdec("ee"),hexdec("ee"));
|
|
$pdf->addPage();
|
|
$curr_row = $start_row + $incr * 2;
|
|
|
|
for ($i = 0; $i < count($selected_schedules); $i++) {
|
|
$schedule_num = $selected_schedules[$i];
|
|
$schedule_name = db_getVal("SELECT schedule_name FROM hotlunch_schedules WHERE school_num = $school_num AND record_num = $schedule_num");
|
|
if (!$schedule_name) continue;
|
|
|
|
// Get totals first
|
|
$query = "SELECT count(*) AS num, sum(order_total) AS total, sum(discount_amount) AS discount, sum(amount_paid) AS paid " .
|
|
"FROM hotlunch_order_header " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND schedule_num = $schedule_num " .
|
|
"AND status NOT IN ('Inactive','Deleted','Concession')";
|
|
$results = db_query($query);
|
|
$row = db_fetch_array($results);
|
|
$total_count = $row['num'];
|
|
$total_orders = $row['total'];
|
|
$total_paid = $row['paid'];
|
|
|
|
// Define Query
|
|
$query = "SELECT payment_method, count(*) AS num, sum(order_total) AS total, sum(discount_amount) AS discount, sum(amount_paid) AS paid " .
|
|
"FROM hotlunch_order_header " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND schedule_num = $schedule_num " .
|
|
"AND status NOT IN ('Inactive','Deleted','Concession') " .
|
|
"GROUP BY 1 " .
|
|
"ORDER BY 1";
|
|
$summary = db_query($query);
|
|
|
|
$rowsNeeded = db_num_rows($summary) + 3 * $incr;
|
|
if ($curr_row + $rowsNeeded > $max_rows) {
|
|
$pdf->addPage();
|
|
$curr_row = $start_row + $incr * 2;
|
|
}
|
|
|
|
|
|
|
|
// Print schedule name
|
|
$curr_col = $columns[0];
|
|
$width = 300;
|
|
$pdf->SetXY($curr_col,$curr_row);
|
|
$pdf->SetFont($document_font,"B",$document_font_size);
|
|
$pdf->Cell($width,$incr,$schedule_name,'0',1,'L',false);
|
|
$curr_row += $incr;
|
|
|
|
// Print the column headers
|
|
$pdf->SetFillColor(hexdec("cc"),hexdec("cc"),hexdec("cc"));
|
|
for ($c = 0; $c < count($column_headers); $c++) {
|
|
$curr_col = $columns[$c];
|
|
$width = $columns[$c + 1] - $columns[$c];
|
|
$text = $column_headers[$c];
|
|
$align = $column_align[$c];
|
|
$pdf->SetXY($curr_col,$curr_row);
|
|
$pdf->Cell($width,$incr,$text,'1',1,$align,true);
|
|
}
|
|
|
|
$pdf->SetFillColor(hexdec("ee"),hexdec("ee"),hexdec("ee"));
|
|
$curr_row += $incr;
|
|
|
|
$totals = array('',0,'',0,'',0,'');
|
|
$count = 0;
|
|
while ($row = db_fetch_array($summary)) {
|
|
extract($row);
|
|
|
|
if (strlen($payment_method) == 0) {
|
|
// Derive label without relying on missing status; use paid amount as hint
|
|
if ((float)$row['paid'] == 0.0) $payment_method = 'Unpaid Orders';
|
|
else $payment_method = 'Unknown';
|
|
}
|
|
|
|
$row['payment_method'] = $payment_method;
|
|
|
|
if ($curr_row > $max_rows) {
|
|
$pdf->addPage();
|
|
$curr_row = $start_row + $incr * 2;
|
|
}
|
|
|
|
// Calculate the percentages
|
|
$row['p1'] = sprintf("%.1f",($total_count == 0) ? 0 : round($row['num'] / $total_count * 1000) / 10) . '%';
|
|
$row['p2'] = sprintf("%.1f",($total_orders == 0) ? 0 : round($row['total'] / $total_orders * 1000) / 10) . '%';
|
|
$row['p3'] = sprintf("%.1f",($total_paid == 0) ? 0 : round($row['paid'] / $total_paid * 1000) / 10) . '%';
|
|
|
|
$bg = false;
|
|
if ($count % 2) $bg = true;
|
|
|
|
// Print the report line for each record
|
|
for ($c = 0; $c < count($column_headers); $c++) {
|
|
$curr_col = $columns[$c];
|
|
$width = $columns[$c + 1] - $columns[$c];
|
|
$align = $column_align[$c];
|
|
$data = $row[$column_fields[$c]];
|
|
|
|
if ($c == 1 || $c == 3 || $c == 5) $totals[$c] += $data;
|
|
|
|
switch ($column_format[$c]) {
|
|
case 'money':
|
|
$text = '$' . number_format((float)$data,2);
|
|
break;
|
|
|
|
case 'none':
|
|
$text = preg_replace('/^ */','',$data);
|
|
break;
|
|
}
|
|
|
|
$pdf->SetXY($curr_col,$curr_row);
|
|
$pdf->Cell($width,$incr,$text,'1',1,$align,$bg);
|
|
}
|
|
|
|
$count++;
|
|
$curr_row += $incr;
|
|
}
|
|
|
|
// Print totals
|
|
$pdf->SetFillColor(hexdec("cc"),hexdec("cc"),hexdec("cc"));
|
|
for ($c = 0; $c < count($column_headers); $c++) {
|
|
$data = $totals[$c];
|
|
$curr_col = $columns[$c];
|
|
$width = $columns[$c + 1] - $columns[$c];
|
|
$align = $column_align[$c];
|
|
|
|
switch ($column_format[$c]) {
|
|
case 'money':
|
|
$text = '$' . number_format((float)$data,2);
|
|
break;
|
|
|
|
case 'none':
|
|
$text = preg_replace('/^ */','',$data);
|
|
break;
|
|
}
|
|
|
|
$pdf->SetXY($curr_col,$curr_row);
|
|
$pdf->Cell($width,$incr,$text,'1',1,$align,true);
|
|
}
|
|
|
|
$curr_row += $incr * 3;
|
|
}
|
|
|
|
|
|
|
|
// Output the completed PDF
|
|
$filename = "schedule-order-summary.pdf";
|
|
|
|
// Ensure no prior output corrupts headers/PDF generation
|
|
if (function_exists('ob_get_level')) {
|
|
while (ob_get_level()) { ob_end_clean(); }
|
|
}
|
|
|
|
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");
|
|
|
|
exit();
|
|
|
|
?>
|