Skip to content

Instantly share code, notes, and snippets.

@joomdonation
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save joomdonation/294ce675005cd223672c to your computer and use it in GitHub Desktop.

Select an option

Save joomdonation/294ce675005cd223672c to your computer and use it in GitHub Desktop.
<?php
public static function generateInvoicePDF($row)
{
self::loadLanguage();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$config = self::getConfig();
$sitename = JFactory::getConfig()->get("sitename");
$query->select('*')
->from('#__eb_events')
->where('id = ' . (int) $row->event_id);
$db->setQuery($query);
$rowEvent = $db->loadObject();
require_once JPATH_ROOT . "/components/com_eventbooking/tcpdf/tcpdf.php";
require_once JPATH_ROOT . "/components/com_eventbooking/tcpdf/config/lang/eng.php";
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($sitename);
$pdf->SetTitle('Invoice');
$pdf->SetSubject('Invoice');
$pdf->SetKeywords('Invoice');
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(PDF_MARGIN_LEFT, 0, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('times', '', 8);
$pdf->AddPage();
if ($config->multiple_booking)
{
$invoiceOutput = $config->invoice_format_cart;
}
else
{
$invoiceOutput = $config->invoice_format;
}
if ($config->multiple_booking)
{
$rowFields = self::getFormFields($row->id, 4);
}
elseif ($row->is_group_billing)
{
$rowFields = self::getFormFields($row->event_id, 1);
}
else
{
$rowFields = self::getFormFields($row->event_id, 0);
}
$form = new RADForm($rowFields);
$data = self::getRegistrantData($row, $rowFields);
$form->bind($data);
$form->buildFieldsDependency();
$replaces = self::buildTags($row, $form, $rowEvent, $config);
$replaces['invoice_number'] = self::formatInvoiceNumber($row->invoice_number, $config);
$replaces['invoice_date'] = date($config->date_format);
if ($row->published == 0)
{
$invoiceStatus = JText::_('EB_INVOICE_STATUS_PENDING');
}
elseif ($row->published == 1)
{
$invoiceStatus = JText::_('EB_INVOICE_STATUS_PAID');
}
else
{
$invoiceStatus = JText::_('EB_INVOICE_STATUS_UNKNOWN');
}
$replaces['INVOICE_STATUS'] = $invoiceStatus;
unset($replaces['total_amount']);
unset($replaces['discount_amount']);
unset($replaces['tax_amount']);
if ($config->multiple_booking)
{
$sql = 'SELECT a.title, a.event_date, a.individual_price, b.* FROM #__eb_events AS a INNER JOIN #__eb_registrants AS b ' . ' ON a.id = b.event_id ' .
' WHERE b.id=' . $row->id . ' OR b.cart_id=' . $row->id;
$db->setQuery($sql);
$rowEvents = $db->loadObjectList();
$subTotal = $replaces['amt_total_amount'];
$taxAmount = $replaces['amt_tax_amount'];
$discountAmount = $replaces['amt_discount_amount'];
$total = $replaces['amt_amount'];
$regex = '#<tr id="template-row">(.*?)</tr>#s';
$invoiceOutput = preg_replace_callback($regex, function ($match) use ($rowEvents, $config)
{
$rowTemplate = '
<tr id="template-row">
' . $match[1] . '
</tr>
';
$string = '';
$rowIndex = 0;
foreach ($rowEvents as $rowEvent)
{
$rowItem = $rowTemplate;
$rowItem = str_replace('&nbsp;', ++$rowIndex, $rowItem);
$rowItem = str_replace('[ITEM_NAME]', $rowEvent->title, $rowItem);
$rowItem = str_replace('[NUMBER_REGISTRANTS]', $rowEvent->number_registrants, $rowItem);
$rowItem = str_replace('[ITEM_AMOUNT]', EventbookingHelper::formatAmount($rowEvent->individual_price, $config), $rowItem);
$rowItem = str_replace('[ITEM_SUB_TOTAL]', EventbookingHelper::formatAmount($rowEvent->total_amount, $config), $rowItem);
$string .= $rowItem;
}
return $string;
}, $invoiceOutput);
/*$replaces['EVENTS_LIST'] = EventbookingHelperHtml::loadCommonLayout(
JPATH_ROOT . '/components/com_eventbooking/emailtemplates/invoice_items.php',
array(
'rowEvents' => $rowEvents,
'subTotal' => $subTotal,
'taxAmount' => $taxAmount,
'discountAmount' => $discountAmount,
'total' => $total,
'config' => $config));
*/
$replaces['SUB_TOTAL'] = EventbookingHelper::formatCurrency($subTotal, $config);
$replaces['DISCOUNT_AMOUNT'] = EventbookingHelper::formatCurrency($discountAmount, $config);
$replaces['TAX_AMOUNT'] = EventbookingHelper::formatCurrency($taxAmount, $config);
$replaces['TOTAL_AMOUNT'] = EventbookingHelper::formatCurrency($total, $config);
}
else
{
$replaces['ITEM_QUANTITY'] = 1;
$replaces['ITEM_AMOUNT'] = $replaces['ITEM_SUB_TOTAL'] = self::formatCurrency($row->total_amount, $config);
$replaces['DISCOUNT_AMOUNT'] = self::formatCurrency($row->discount_amount, $config);
$replaces['SUB_TOTAL'] = self::formatCurrency($row->total_amount - $row->discount_amount, $config);
$replaces['TAX_AMOUNT'] = self::formatCurrency($row->tax_amount, $config);
$replaces['TOTAL_AMOUNT'] = self::formatCurrency($row->total_amount - $row->discount_amount + $row->tax_amount, $config);
$itemName = JText::_('EB_EVENT_REGISTRATION');
$itemName = str_replace('[EVENT_TITLE]', $rowEvent->title, $itemName);
$replaces['ITEM_NAME'] = $itemName;
}
foreach ($replaces as $key => $value)
{
$key = strtoupper($key);
$invoiceOutput = str_replace("[$key]", $value, $invoiceOutput);
}
$invoiceOutput = self::convertImgTags($invoiceOutput);
$v = $pdf->writeHTML($invoiceOutput, true, false, false, false, '');
//Filename
$filePath = JPATH_ROOT . '/media/com_eventbooking/invoices/' . $replaces['invoice_number'] . '.pdf';
$pdf->Output($filePath, 'F');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment