strelitzia:
The file in question is invoked when an order is placed so that it can generate a pdf invoice to attach to the order confirmation email.
require_once(DIR_FS_CATALOG . 'fpdf/genpdf.php');
>
> The directory 'fpdf' is in the site root, and full content of that file is
> ```
<?php
// Process order totals
if (!isset($order_totals) || isset($order_totals) && !is_array($order_totals) || isset($order_totals) && is_array($order_totals) && sizeof($order_totals) < 1) $order_totals = $order_total_modules->process();
if (is_array($order_totals)) {
foreach ($order_totals as $totals) {
$pdf_totals[$totals['code']] = $totals['value'];
}
}
if (isset($pdf_totals['ot_coupon'])) {
$discount_coupon = number_format($pdf_totals['ot_coupon'], 2, '.', '');
unset($pdf_totals['ot_coupon']);
}
if (isset($pdf_totals['ot_gv'])) {
$gift_certificate = number_format($pdf_totals['ot_gv'], 2, '.', '');
unset($pdf_totals['ot_gv']);
}
$total = number_format($pdf_totals['ot_subtotal'], 2, '.', '');
$sc = number_format($pdf_totals['ot_shipping'], 2, '.', '');
$totaltax = number_format($pdf_totals['ot_tax'], 2, '.', '');
$final = number_format($pdf_totals['ot_total'], 2, '.', '');
unset($pdf_totals['ot_subtotal']);
unset($pdf_totals['ot_shipping']);
unset($pdf_totals['ot_tax']);
unset($pdf_totals['ot_total']);
// Will the products fit on one page?
$next_page = false;
if ((sizeof($this->products) > 8 && !isset($discount_coupon) && !isset($gift_certificate)) || (sizeof($this->products) > 7 && isset($discount_coupon)) || (sizeof($this->products) > 7 && isset($gift_certificate)) || (sizeof($this->products) > 6 && isset($discount_coupon) && isset($gift_certificate))) $next_page = true;
error_reporting(0);
define('FPDF_FONTPATH', DIR_FS_CATALOG . 'fpdf/font/');
require('fpdf.php');
$str = utf8_decode("£");
$err = 0;
$tnow = date('Y-m-d H:i:s');
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$err = 0;
$file = $path.$filename;
$fp = fopen($file, "rb"); //Open it
$data = fread($fp, filesize($file)); //Read it
$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
fclose($fp);
$headers = "From: $name<" . $from_name . ">\n";
$headers .= "Reply-to: <" . $replyto . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-sender: $name<" . $from_name . ">\n";
$headers .= "X-mailer: PHP mail\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $from_name . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message2 = "------=MIME_BOUNDRY_message_parts\n";
$message2 .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message2 .= "Content-Transfer-Encoding: quoted-printable\n";
$message2 .= "\n";
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$message2 .= "$message\n";
$message2 .= "\n";
$message2 .= "------=MIME_BOUNDRY_message_parts--\n";
$message2 .= "\n";
$message2 .= "------=MIME_BOUNDRY_main_message\n";
$message2 .= "Content-Type: application/pdf;\n\tname=\"" . $filename . "\"\n";
$message2 .= "Content-Transfer-Encoding: base64\n";
$message2 .= "Content-Disposition: attachment;\n\tfilename=\"" . $filename . "\"\n\n";
$message2 .= $data; //The base64 encoded message
$message2 .= "\n";
$message2 .= "------=MIME_BOUNDRY_main_message--\n";
if (mail($mailto, $subject, $message2, $headers)) {
//echo("mail send ... OK"); // or use booleans here
} else {
//echo("mail send ... ERROR!");
$err++;
}
return ($err);
}
class PDF extends FPDF
{
function FancyTable($titles,$data,$c,$start)
{
$str = utf8_decode("£");
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.4);
$this->SetFont('Arial','B',8);
//Header
$w=array(108,30,10,18,24);
for($i=0;$i<count($titles);$i++)
$this->Cell($w[$i],5,$titles[$i],1,0,'C',true);
$this->Ln();
//Color and font restoration
$this->SetFillColor(224,235,255);
$this->SetTextColor(0);
$this->SetFont('');
//Data
$fill=false;
$p = 0;
foreach($data as $row)
{
if ($p >= $start) {
$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
$this->Cell($w[1],6,$row[1],'LR',0,'R',$fill);
$this->Cell($w[2],6,$row[2],'LR',0,'L',$fill);
$this->Cell($w[3],6,$str . $row[3],'LR',0,'R',$fill);
$this->Cell($w[4],6,$str . $row[4],'LR',0,'R',$fill);
$this->Ln();
$fill=!$fill;
}
$p++;
if ($c == 0 && $p >= 8) { break; }
}
$this->Cell(array_sum($w),0,'','T');
}
//Page header
function Header()
{
}
//Page footer
function Footer()
{
}
}
//Instanciation of inherited class
$i = 1;
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pi = 1;
require ("invoice_top.php");
// $total = 0.00;
for ($i = 0; $i < sizeof($this->products); $i++) {
$attribute_customid = '';
$attribute_names = array();
$x = strip_tags($this->products[$i]['name']);
// Process attributes
if (isset($this->products[$i]['attributes']) && sizeof($this->products[$i]['attributes']) > 0) {
foreach ($this->products[$i]['attributes'] as $attributes) {
$attribute_names[] = $attributes['option'] . ': ' . $attributes['value'];
$attribute_customid = $_SESSION['pwas_class2']->zen_get_customid($this->products[$i]['id'], array($attributes['value_id']));
}
if (isset($attribute_names) && is_array($attribute_names)) {
$attribute_string = implode(', ', $attribute_names);
$x .= ' ' . $attribute_string;
}
}
$y = $this->products[$i]['qty'];
$z = ($this->products[$i]['final_price'] > 0 ? $this->products[$i]['final_price'] : $this->products[$i]['price']);
$t = $this->products[$i]['tax'];
$m = ($this->products[$i]['model'] == '' && $attribute_customid != '' ? $attribute_customid : $this->products[$i]['model']);
$x = str_replace("<br>", "", $x );
$itemname = "$y x $x";
$totalexvat = $z * $y;
$totalcost = (zen_add_tax($z, $t) * $y) + zen_add_tax($this->products[$i]['onetime_charges'], $t);
$thistax = zen_calculate_tax($z, $t);
$pwt = $z + $thistax;
// $totaltax += zen_calculate_tax($z, $t) * $this->products[$i]['qty'];
$z = number_format($z, 2, '.', '');
$totalcost = number_format($totalcost, 2, '.', '');
$pwt = number_format($pwt, 2, '.', '');
// $totaltax = number_format($totaltax, 2, '.', '');
$totalexvat = number_format($totalexvat, 2, '.', '');
$t = $t . "%";
// $total += $totalcost;
$lines[$i] = "$itemname|$m|$t|$pwt|$totalcost";
}
// $total = number_format($total, 2, '.', '');
// $totaltax = number_format($totaltax, 2, '.', '');
// $final = $total + $sc;
// $final = number_format($final, 2, '.', '');
$pdf->ln(2);
foreach($lines as $line) $data[]=explode('|',chop($line));
$titles = array('Products','Stock Code','Tax','Unit Price', 'Line Total' );
$pdf->FancyTable($titles,$data, 0, 0);
$pdf->ln(1);
$pdf->SetFont('Arial','',12);
$pdf->Cell(0,25,"Payment Method: $payment_method", 0, 0, "L");
$pdf->SetFont('Arial','B',12);
$pdf->Cell(0,25,"Sub Total: " . $str . "$total", 0, 0, "R");$pdf->ln(8);
$pdf->SetFont('Arial','I',12);
if ($sc == "3.5") { $sc = "3.50"; }
if ($sc == "8.5") { $sc = "8.50"; }
if ($sc > 0) {
$pdf->Cell(0,25,"Shipping Cost: " . $str . "$sc", 0, 0, "R");$pdf->ln(8);
}
if (isset($discount_coupon) && $discount_coupon > 0) {
$pdf->Cell(0,25,"Discount Coupon: -" . $str . "$discount_coupon", 0, 0, "R");$pdf->ln(8);
}
$pdf->Cell(0,25,"Includes VAT of " . $str . "$totaltax", 0, 0, "R");$pdf->ln(8);
if (isset($gift_certificate) && $gift_certificate > 0) {
$pdf->Cell(0,25,"Gift Certificates: -" . $str . "$gift_certificate", 0, 0, "R");$pdf->ln(8);
}
$pdf->SetFont('Arial','B',14);
$pdf->Cell(0,25,"Total: " . $str . "$final", 0, 0, "R");
if (!$next_page) {
$pdf->ln(18);
$pdf->SetFont('Arial','B',18);
$pdf->SetTextColor(205, 116, 66);
$pdf->SetX(64);
$pdf->Write(1,"Thank You");
$pdf->SetTextColor(220, 155, 115);
$pdf->Write(1," for your custom!");
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('Arial','B',14);
}
//Label Part
//Higher is higher, lower = lower on the page
$pdf->SetFont('Arial','',8);
$pdf->SetY(-96);
$pdf->SetX(34);
$pdf->Cell(0,5,"Return to: Tudor Rose Patchwork", 0, 1);$pdf->SetX(48);
$pdf->Cell(0,5,"C/o Bacchus Marsh Ltd", 0, 1);$pdf->SetX(48);
$pdf->Cell(0,5,"Unit 1, Station Road", 0, 1);$pdf->SetX(48);
$pdf->Cell(0,5,"Oakley MK43 7RB", 0, 1);$pdf->SetX(48);
$pdf->Cell(0,5,"United Kingdom", 0, 1);
$pdf->SetY(-60);
$pdf->SetX(85);
$pdf->SetFont('Arial','B',16);
$pdf->Cell(0,8,"$delfname $delsname", 0, 1);$pdf->SetX(85);
$pdf->SetFont('Arial','B',14);
if (strlen($delstreet) > 0) { $pdf->Cell(0,5,"$delstreet", 0, 1);$pdf->SetX(85); }
if (strlen($delsuburb) > 0) { $pdf->Cell(0,5,"$delsuburb", 0, 1);$pdf->SetX(85); }
$pdf->Cell(0,5,"$delcity", 0, 1);$pdf->SetX(85);
if (strlen($delstate) > 0) { $pdf->Cell(0,5,"$delstate", 0, 1);$pdf->SetX(85); }
$pdf->Cell(0,5,"$delpostcode", 0, 1);$pdf->SetX(85);
$pdf->Cell(0,5,"$delcountry", 0, 1);
if ($next_page) {
$pdf->AddPage();
$pi = 2;
require ("invoice_top.php");
$pdf->ln(2);
$pdf->FancyTable($titles,$data, 1, 8);
}
if ($next_page) {
$pdf->ln(25);
$pdf->SetFont('Arial','B',18);
$pdf->SetTextColor(205, 116, 66);
$pdf->SetX(64);
$pdf->Write(1,"Thank You");
$pdf->SetTextColor(220, 155, 115);
$pdf->Write(1," for your custom!");
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('Arial','B',14);
}
$pdf->Output(DIR_FS_CATALOG . "download/$zf_insert_id.pdf",F);
$x = DIR_FS_CATALOG . "download/$zf_insert_id.pdf";
if ($err == 0) {
$mailerr == 0;
$my_file = "$zf_insert_id.pdf";
$my_path = DIR_FS_CATALOG . "download/";
$my_name = "Sandra";
$my_mail = "[email protected]";
$my_replyto = "[email protected]";
$my_subject = "PDF for TRP Order No $zf_insert_id";
$my_message = "Tudor Rose order number $zf_insert_id";
$mailerr = mail_attachment($my_file, $my_path, "[email protected]", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
if ($mailerr > 0) {
error_log("$tnow - An error occured while sending the following attachment by email, $zf_insert_id.pdf\n", 3, DIR_FS_CATALOG . "error_log");
} else {
//chmod (DIR_FS_CATALOG . "orders/$zf_insert_id.pdf", 0700) or $err++;
}
} else {
error_log("$tnow - An error occured while modifying the permissions on, $zf_insert_id.pdf\n", 3, DIR_FS_CATALOG . "error_log");
}
So, I don't see if/how perhaps the $_SESSION is affected, but my idea to address this (as a work around which is not always the best way to implement) is to modify:
// $total = 0.00;
for ($i = 0; $i < sizeof($this->products); $i++) {
$attribute_customid = '';
$attribute_names = array();
$x = strip_tags($this->products[$i]['name']);
// Process attributes
if (isset($this->products[$i]['attributes']) && sizeof($this->products[$i]['attributes']) > 0) {
foreach ($this->products[$i]['attributes'] as $attributes) {
$attribute_names[] = $attributes['option'] . ': ' . $attributes['value'];
$attribute_customid = $_SESSION['pwas_class2']->zen_get_customid($this->products[$i]['id'], array($attributes['value_id']));
}
if (isset($attribute_names) && is_array($attribute_names)) {
$attribute_string = implode(', ', $attribute_names);
$x .= ' ' . $attribute_string;
}
}
$y = $this->products[$i]['qty'];
$z = ($this->products[$i]['final_price'] > 0 ? $this->products[$i]['final_price'] : $this->products[$i]['price']);
$t = $this->products[$i]['tax'];
$m = ($this->products[$i]['model'] == '' && $attribute_customid != '' ? $attribute_customid : $this->products[$i]['model']);
$x = str_replace("<br>", "", $x );
$itemname = "$y x $x";
$totalexvat = $z * $y;
$totalcost = (zen_add_tax($z, $t) * $y) + zen_add_tax($this->products[$i]['onetime_charges'], $t);
$thistax = zen_calculate_tax($z, $t);
$pwt = $z + $thistax;
// $totaltax += zen_calculate_tax($z, $t) * $this->products[$i]['qty'];
$z = number_format($z, 2, '.', '');
$totalcost = number_format($totalcost, 2, '.', '');
$pwt = number_format($pwt, 2, '.', '');
// $totaltax = number_format($totaltax, 2, '.', '');
$totalexvat = number_format($totalexvat, 2, '.', '');
$t = $t . "%";
// $total += $totalcost;
$lines[$i] = "$itemname|$m|$t|$pwt|$totalcost";
}
To add the following that is in blue:
// $total = 0.00;
if (!class_exists('products_with_attributes_class_stock')) {
include_once DIR_FS_CATALOG . DIR_WS_CLASSES . 'class.products_with_attributes_class_stock.php';
}
if (empty($_SESSION['pwas_class2'])) {
$_SESSION['pwas_class2'] = new products_with_attributes_class_stock();
}
for ($i = 0; $i < sizeof($this->products); $i++) {
$attribute_customid = '';
$attribute_names = array();
$x = strip_tags($this->products[$i]['name']);
// Process attributes
if (isset($this->products[$i]['attributes']) && sizeof($this->products[$i]['attributes']) > 0) {
foreach ($this->products[$i]['attributes'] as $attributes) {
$attribute_names[] = $attributes['option'] . ': ' . $attributes['value'];
$attribute_customid = $_SESSION['pwas_class2']->zen_get_customid($this->products[$i]['id'], array($attributes['value_id']));
}
if (isset($attribute_names) && is_array($attribute_names)) {
$attribute_string = implode(', ', $attribute_names);
$x .= ' ' . $attribute_string;
}
}
$y = $this->products[$i]['qty'];
$z = ($this->products[$i]['final_price'] > 0 ? $this->products[$i]['final_price'] : $this->products[$i]['price']);
$t = $this->products[$i]['tax'];
$m = ($this->products[$i]['model'] == '' && $attribute_customid != '' ? $attribute_customid : $this->products[$i]['model']);
$x = str_replace("<br>", "", $x );
$itemname = "$y x $x";
$totalexvat = $z * $y;
$totalcost = (zen_add_tax($z, $t) * $y) + zen_add_tax($this->products[$i]['onetime_charges'], $t);
$thistax = zen_calculate_tax($z, $t);
$pwt = $z + $thistax;
// $totaltax += zen_calculate_tax($z, $t) * $this->products[$i]['qty'];
$z = number_format($z, 2, '.', '');
$totalcost = number_format($totalcost, 2, '.', '');
$pwt = number_format($pwt, 2, '.', '');
// $totaltax = number_format($totaltax, 2, '.', '');
$totalexvat = number_format($totalexvat, 2, '.', '');
$t = $t . "%";
// $total += $totalcost;
$lines[$i] = "$itemname|$m|$t|$pwt|$totalcost";
}