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

553 lines
15 KiB
PHP

<?php
$ValidAccess = 2010;
require_once("include/globals.php");
//
// Ensure that the user is logged in
//
if (SessionActive() != 1 || !UserLoggedIn()) {
echo("Invalid Access");
exit();
}
// Ensure that only valid user types have access to this module
$swhere = "";
$AllowedUserTypes = array('SuperUser');
if (checkPermission($AllowedUserTypes) != 1) {
$swhere = "AND school_num = $school_num ";
}
//
// Ensure that there is an invoice number passed
//
if (!isset($invoice_num)) {
if (array_key_exists('invoice_num',$_GET) && intval($_GET['invoice_num']) > 0) $invoice_num = intval($_GET['invoice_num']);
}
if (!isset($invoice_num)) {
echo("<p>Error: Invoice Number is Not Set</p>\n");
return;
}
//
// Check that this is a valid invoice number
//
$query = "select * from invoice_header where record_num = $invoice_num $swhere";
$results = db_query($query);
if (db_num_rows($results) == 0) {
echo("<p>Error: Invoice Number Not Found</p>\n");
return;
}
$invoice = db_fetch_array($results);
$school_num = $invoice['school_num'];
//
// Get school record
//
$query = "select * from schools where record_num = '$school_num'";
$results = db_query($query);
if (db_num_rows($results) == 0) {
echo("<p>Error: School Not Found - $school_num</p>\n");
return;
}
$school = db_fetch_array($results);
extract($invoice);
extract($school);
$school_name = trim($school_name);
$invoice_total = $total;
$balance_owing = $invoice_total - $amount_paid;
$companyHeader = "EZS Software Inc. (Hot Lunches)";
$companyName = "EZS Software Inc.";
if ($invoice_date < '2015-10-01') {
$companyName = "Hot Lunches Online";
$companyHeader = "Hot Lunches Online";
}
//
// Initialize 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 = 10;
$start_row = 55;
$max_rows = 240;
$left_col = 15;
$date_col = 121;
$amount_col = 145;
$num_col = 150;
$col = $left_col;
$row = $start_row;
$incr = 4;
//
// Setup the PDF libraries
//
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() {
global $incr, $document_font, $document_font_size, $balance_owing, $companyName, $companyHeader;
// Print the form
$this->SetTextColor(0,0,0);
$this->SetFillColor(hexdec("dd"),hexdec("dd"),hexdec("dd"));
$this->SetLineWidth(0.3);
$this->SetDrawColor(0,0,0);
// Header image
$this->Image("images/document-header.jpg",0,0,220);
// Heading Text
$this->SetFont($document_font,"B",$document_font_size + 3);
$left_col = 13;
$right_col = 105;
$width = $right_col - $left_col;
$text_row = 35;
$this->SetXY($left_col,$text_row);
$title_text = "INVOICE";
if ($balance_owing <= 0) $title_text = "RECEIPT";
$this->Cell($width,$incr + 2,$title_text,'0',0,'C',false,'',1);
// Form Text
$this->SetFont($document_font,"B",$document_font_size + 1);
// Customer Box
$left_col = 13;
$right_col = 110;
$top_row = 47;
$middle_row = $top_row + 7;
$bottom_row = $top_row + 35;
// Draw Box
$this->printBox($left_col,$top_row,$right_col,$bottom_row);
// Middle Line
$this->Line($left_col,$middle_row,$right_col,$middle_row);
// Add Heading Text
$this->SetXY($left_col + 1,$top_row + 1);
$width = $right_col - $left_col - 2;
$this->Cell($width,$incr + 1,"Customer",'0',0,'L',false,'',1);
// Date and Invoice # Box
$left_col = 120;
$right_col = 200;
$top_row = 47;
$middle_row = $top_row + 7;
$bottom_row = $top_row + 14;
// Draw Box
$this->printBox($left_col,$top_row,$right_col,$bottom_row);
// Middle Line
$this->Line($left_col,$middle_row,$right_col,$middle_row);
// Add Heading Text
$this->SetXY($left_col + 1,$top_row + 1);
$width = $right_col - $left_col - 2;
$this->Cell($width,$incr + 1,"Date",'0',0,'L',false,'',1);
// Middle vertical line
$center_col = $left_col + 40;
$this->Line($center_col,$top_row,$center_col,$bottom_row);
$this->SetXY($center_col + 1,$top_row + 1);
$this->Cell($width,$incr + 1,"Invoice #",'0',0,'L',false,'',1);
// Mailing Address
$this->SetFont($document_font,"B",$document_font_size + 2);
$left_col = 122;
$row = 64;
$this->SetXY($left_col,$row);
$this->Cell($width,$incr + 1,$companyHeader,'0',0,'L',false,'',1);
$this->SetFont($document_font,"B",$document_font_size + 1);
$row += $incr;
$this->SetXY($left_col,$row);
$this->Cell($width,$incr + 1,"c/o 981 Noons Creek Close",'0',0,'L',false,'',1);
$row += $incr;
$this->SetXY($left_col,$row);
$this->Cell($width,$incr + 1,"Port Moody, BC",'0',0,'L',false,'',1);
$row += $incr;
$this->SetXY($left_col,$row);
$this->Cell($width,$incr + 1,"V3H 4J5",'0',0,'L',false,'',1);
$this->SetFont($document_font,"B",$document_font_size + 1);
// Due Date and Contact Person Boxes
$left_col = 13;
$right_col = 200;
$top_row = 85;
$middle_row = $top_row + 7;
$bottom_row = $top_row + 14;
// Draw Box
$this->printBox($left_col,$top_row,$right_col,$bottom_row);
// Middle Line
$this->Line($left_col,$middle_row,$right_col,$middle_row);
// Add Heading Text
$this->SetXY($left_col + 1,$top_row + 1);
$width = $right_col - $left_col - 2;
$this->Cell($width,$incr + 1,"Due Date",'0',0,'L',false,'',1);
// First vertical line
$center_col = $left_col + 50;
$this->Line($center_col,$top_row,$center_col,$bottom_row);
$this->SetXY($center_col + 1,$top_row + 1);
$this->Cell($width,$incr + 1,"Contact Person",'0',0,'L',false,'',1);
// Second vertical line
$center_col = $left_col + 140;
$this->Line($center_col,$top_row,$center_col,$bottom_row);
$this->SetXY($center_col + 1,$top_row + 1);
$this->Cell($width,$incr + 1,"Next Renewal",'0',0,'L',false,'',1);
// Main Item Box
$left_col = 13;
$right_col = 200;
$top_row = 103;
$middle_row = $top_row + 7;
$bottom_row = $top_row + 140;
// Draw Box
$this->printBox($left_col,$top_row,$right_col,$bottom_row);
// Middle Line
$this->Line($left_col,$middle_row,$right_col,$middle_row);
// Add Heading Text
$this->SetXY($left_col + 1,$top_row + 1);
$width = $right_col - $left_col - 2;
$this->Cell($width,$incr + 1,"Description",'0',0,'L',false,'',1);
// Totals horizontal line
$line_row = $top_row + 112;
$this->Line($left_col,$line_row,$right_col,$line_row);
// First vertical line
$center_col = $left_col + 140;
$this->Line($center_col,$top_row,$center_col,$bottom_row);
$this->SetXY($center_col + 1,$top_row + 1);
$this->Cell(30,$incr + 1,"Amount",'0',0,'R',false,'',1);
// Footer message
$this->SetXY($left_col,244);
if ($balance_owing > 0) {
$this->MultiCell(200,$incr,"Thank You for Your Business\nPlease Make Checks Payable to \"$companyName\"",'0','C',false,1,'','',true,0,false,true);
}
else {
$this->MultiCell(200,$incr,"Thank You for Your Business\n\"$companyName\"",'0','C',false,1,'','',true,0,false,true);
}
$this->SetFont($document_font,"B",$document_font_size);
$this->SetXY($left_col,257);
$this->MultiCell(200,$incr,"981 Noons Creek Close, Port Moody, BC, Canada V3H 4J5\nGST # 80267 2329",'0','C',false,1,'','',true,0,false,true);
}
function printBox($left,$top,$right,$bottom) {
// Top Line
$this->Line($left,$top,$right,$top);
// Bottom Line
$this->Line($left,$bottom,$right,$bottom);
// Left Line
$this->Line($left,$top,$left,$bottom);
// Right Line
$this->Line($right,$top,$right,$bottom);
}
function Footer() {
global $incr, $left_col, $title_width, $max_rows, $document_font, $document_font_size, $document_name, $logo_image, $logo_url, $logo_row;
return;
// 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(20,$logo_row + 9);
$this->SetFont($document_font,"B",$document_font_size);
$this->Cell($title_width,$incr,$logo_url,'0',1,'L');
}
}
$pdf= new PDF('P', 'mm', 'Letter', 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();
$pdf->SetFont($document_font,"B",$document_font_size);
//
// Output school name and address
//
$name = $pac_name;
if (strlen($bill_to_address) == 0) $bill_to_address = $address;
if (strlen($bill_to_city) == 0) $bill_to_city = $city;
if (strlen($bill_to_province) == 0) $bill_to_province = $province;
if (strlen($bill_to_postal) == 0) $bill_to_postal = $postal;
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$name,0,1,"L",0);
$row += $incr;
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$bill_to_address,0,1,"L",0);
$row += $incr;
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,"$bill_to_city, $bill_to_province",0,1,"L",0);
$row += $incr;
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$bill_to_postal,0,1,"L",0);
//
// Invoice date and number
//
$col = $date_col;
$row = $start_row;
$pdf->SetXY($col,$row);
$inv_date = date("M j, Y",strtotime($invoice_date));
$pdf->Cell(0,5,$inv_date,0,1,"L",0);
$inv_num = sprintf("%06d-HL",$invoice_num + 500);
$col = $date_col + 42;
$pdf->SetXY($col,$row);
$pdf->Cell(18,5,$inv_num,0,1,"R",0);
//
// Due date, etc...
//
$col = $left_col;
$row = $start_row + 38;
$pdf->SetXY($col,$row);
$due_date = date("M j, Y",strtotime($due_date));
$due_date = "Due on Receipt";
$pdf->Cell(0,5,$due_date,0,1,"L",0);
// Get contact name
$contact_name = db_getVal("SELECT contact_name FROM school_contact_list WHERE school_num = $school_num AND sequence = 1");
$col = 64;
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$contact_name,0,1,"L",0);
$renewal = date("M j, Y",strtotime($renewal_date));
$col = $amount_col + 10;
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$renewal,0,1,"L",0);
//
// Invoice line items
//
$query = "select * from invoice_items where invoice_num = $invoice_num order by sequence";
$results = db_query($query);
if (db_num_rows($results)) {
$row = $start_row + 56;
$total = 0;
while ($invoice_item = db_fetch_array($results)) {
extract($invoice_item);
$col = $left_col;
$pdf->SetXY($col,$row);
$pdf->Cell(138,5,$description,0,1,"L",false,'',1);
if (isset($amount) && $amount != 0.00) {
$col = $amount_col;
$pdf->SetXY($col,$row);
if ($amount > 0) $amt = $school_record['currency_symbol'] . number_format($amount,2);
else $amt = "- $currencySymbol" . number_format(abs($amount),2);
$pdf->Cell(40,5,$amt,0,1,"R",0);
}
$total += $amount;
$row += $incr + 2;
}
// Print School Info
$pdf->SetFont($document_font,"B",$document_font_size - 1);
$row += $incr + 1;
$col = $left_col + 5;
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$school_name,0,1,"L",0);
$row += $incr + 1;
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$url,0,1,"L",0);
$row += $incr + 1;
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$pac_name,0,1,"L",0);
if ($include_school_names == 'Y' && $school['reseller'] != 'hotlunch') {
$reseller = $school['reseller'];
$results = db_query("SELECT school_name FROM schools WHERE status = 'Active' AND reseller = '$reseller' ORDER BY school_name");
$scount = 0;
$names = "";
$row += $incr + 1;
$col = $left_col + 10;
$pdf->SetFont($document_font,"",$document_font_size - 2);
while ($r = db_fetch_assoc($results)) {
$name = $r['school_name'];
if (strlen($names) > 0) $names .= ", ";
$names .= $name;
$scount++;
if ($scount > 1) {
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$names,0,1,"L",0);
$names = "";
$row += $incr;
$scount = 0;
}
}
if ($scount > 0) {
$pdf->SetXY($col,$row);
$pdf->Cell(0,5,$names,0,1,"L",0);
$names = "";
}
}
// Print message
$row += $incr * 3;
$col = $left_col + 5;
$pdf->SetXY($col,$row);
$pdf->SetFont($document_font,"B",$document_font_size - 2);
$today = date("Y-m-d",time());
$msg = "Please Note Our Address:\n - 981 Noons Creek Close, Port Moody, BC, V3H 4J5";
$pdf->MultiCell(120, $incr, $msg, '0','L',false);
// Print totals
$row = 220;
$col = 120;
if ($charge_gst == 'Y') {
$row = 216;
$pdf->SetXY($col,$row);
$pdf->SetFont($document_font,"B",$document_font_size);
$pdf->Cell(40,5,"SubTotal :",0,1,"L",0);
$pdf->SetXY($amount_col,$row);
$pdf->Cell(40,5,$currencySymbol . number_format($total,2),0,1,"R",0);
$row += $incr * 1.1;
$pdf->SetXY($col,$row);
$pdf->Cell(40,5,$tax_code . " :",0,1,"L",0);
$pdf->SetXY($amount_col,$row);
$pdf->Cell(40,5,$currencySymbol . number_format($gst,2),0,1,"R",0);
$row += $incr * 1.5;
}
$pdf->SetXY($col,$row);
$pdf->SetFont($document_font,"B",$document_font_size);
$pdf->Cell(40,5,"TOTAL :",0,1,"L",0);
$pdf->SetXY($amount_col,$row);
$pdf->Cell(40,5,$currencySymbol . number_format($total + $gst,2),0,1,"R",0);
if ($amount_paid > 0) {
$row += $incr * 1.5;
$pdf->SetXY($col,$row);
$pdf->SetFont($document_font,"B",$document_font_size);
$pdf->Cell(40,5,"Payments :",0,1,"L",0);
$pdf->SetXY($amount_col,$row);
$pdf->Cell(40,5,"- " . $currencySymbol . number_format($amount_paid,2),0,1,"R",0);
$row += $incr * 1.1;
$pdf->SetXY($col,$row);
$pdf->SetFont($document_font,"B",$document_font_size);
$pdf->Cell(40,5,"BALANCE :",0,1,"L",0);
$pdf->SetXY($amount_col,$row);
$balance = ($total + $gst) - $amount_paid;
if ($balance < 0) {
$pdf->Cell(40,5,"- " . $currencySymbol . number_format($balance,2),0,1,"R",0);
}
else {
$pdf->Cell(40,5,$currencySymbol . number_format($balance,2),0,1,"R",0);
}
}
}
// Output the completed PDF
$file_name = str_replace("/","-",$school_name);
$file_name = normalizeString($inv_num . "." . $file_name . ".pdf");
if ($balance_owing <= 0) $file_name = normalizeString($inv_num . "-receipt-$school_name.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=$file_name");
header('Content-Transfer-Encoding: binary');
if (file_exists("invoices/$file_name")) unlink("invoices/$file_name");
$pdf->Output("invoices/$file_name","F");
$pdf->Output("$file_name","D");
return;
?>