Skip to content

Instantly share code, notes, and snippets.

@joomdonation
Created May 9, 2016 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joomdonation/85f8dea6bd20152ccebec9cd00228588 to your computer and use it in GitHub Desktop.
Save joomdonation/85f8dea6bd20152ccebec9cd00228588 to your computer and use it in GitHub Desktop.
public static function generateInvoicePDF($row)
{
self::loadLanguage();
require_once JPATH_ROOT . '/components/com_osmembership/tcpdf/tcpdf.php';
require_once JPATH_ROOT . '/components/com_osmembership/tcpdf/config/lang/eng.php';
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$config = self::getConfig();
$sitename = JFactory::getConfig()->get("sitename");
$query->select('*')
->from('#__osmembership_plans')
->where('id = '. $row->plan_id);
$db->setQuery($query);
$rowPlan = $db->loadObject();
$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('freesans', '', 8);
$pdf->AddPage();
$invoiceOutput = $config->invoice_format;
$replaces = array();
$replaces['user_id'] = $row->user_id;
$replaces['name'] = $row->first_name . ' ' . $row->last_name;
$replaces['email'] = $row->email;
$replaces['organization'] = $row->organization;
$replaces['address'] = $row->address;
$replaces['address2'] = $row->address2;
$replaces['city'] = $row->city;
$replaces['state'] = self::getStateName($row->country, $row->state);
$replaces['zip'] = $row->zip;
$replaces['country'] = $row->country;
$replaces['country_code'] = self::getCountryCode($row->country);
$replaces['phone'] = $row->phone;
$replaces['fax'] = $row->fax;
$replaces['invoice_number'] = self::formatInvoiceNumber($row, $config);
$replaces['invoice_date'] = JHtml::_('date', $row->created_date, $config->date_format);
$replaces['from_date'] = JHtml::_('date', $row->from_date, $config->date_format);
$replaces['to_date'] = JHtml::_('date', $row->to_date, $config->date_format);
$replaces['created_date'] = JHtml::_('date', $row->created_date, $config->date_format);
$replaces['date'] = JHtml::_('date', 'Now', $config->date_format);
$replaces['plan_title'] = $rowPlan->title;
$replaces['short_description'] = $rowPlan->short_description;
$replaces['description'] = $rowPlan->description;
$replaces['transaction_id'] = $row->transaction_id;
$replaces['membership_id'] = self::formatMembershipId($row, $config);
$replaces['end_date'] = $replaces['to_date'];
$replaces['payment_method'] = '';
if ($row->payment_method)
{
$method = os_payments::loadPaymentMethod($row->payment_method);
if ($method)
{
$replaces['payment_method'] = JText::_($method->title);
}
}
$query->clear();
// Support for name of custom field in tags
$query->select('field_id, field_value')
->from('#__osmembership_field_value')
->where('subscriber_id = ' . $row->id);
$db->setQuery($query);
$rowValues = $db->loadObjectList('field_id');
$query->clear();
$query->select('id, name, fieldtype')
->from('#__osmembership_fields AS a')
->where('a.published = 1')
->where('a.is_core = 0');
$db->setQuery($query);
$rowFields = $db->loadObjectList();
for ($i = 0, $n = count($rowFields); $i < $n; $i++)
{
$rowField = $rowFields[$i];
if (isset($rowValues[$rowField->id]))
{
$fieldValue = $rowValues[$rowField->id]->field_value;
if (is_string($fieldValue) && is_array(json_decode($fieldValue)))
{
$fieldValue = implode(', ', json_decode($fieldValue));
}
if ($fieldValue && $rowField->fieldtype == 'Date')
{
try
{
$replaces[$rowField->name] = JHtml::_('date', $fieldValue, $config->date_format, null);
}
catch (Exception $e)
{
$replaces[$rowField->name] = $fieldValue;
}
}
else
{
$replaces[$rowField->name] = $fieldValue;
}
}
else
{
$replaces[$rowField->name] = '';
}
}
if ($row->published == 0)
{
$invoiceStatus = JText::_('OSM_INVOICE_STATUS_PENDING');
}
elseif ($row->published == 1)
{
$invoiceStatus = JText::_('OSM_INVOICE_STATUS_PAID');
}
else
{
$invoiceStatus = JText::_('');
}
$replaces['INVOICE_STATUS'] = $invoiceStatus;
$replaces['ITEM_QUANTITY'] = 1;
$replaces['ITEM_AMOUNT'] = $replaces['ITEM_SUB_TOTAL'] = self::formatCurrency($row->amount, $config);
$replaces['DISCOUNT_AMOUNT'] = self::formatCurrency($row->discount_amount, $config);
$replaces['SUB_TOTAL'] = self::formatCurrency($row->amount - $row->discount_amount, $config);
$replaces['TAX_AMOUNT'] = self::formatCurrency($row->tax_amount, $config);
$replaces['TOTAL_AMOUNT'] = self::formatCurrency($row->gross_amount, $config);
switch ($row->act)
{
case 'renew':
$itemName = JText::_('OSM_PAYMENT_FOR_RENEW_SUBSCRIPTION');
$itemName = str_replace('[PLAN_TITLE]', $rowPlan->title, $itemName);
break;
case 'upgrade':
$itemName = JText::_('OSM_PAYMENT_FOR_UPGRADE_SUBSCRIPTION');
$itemName = str_replace('[PLAN_TITLE]', $rowPlan->title, $itemName);
$sql = 'SELECT a.title FROM #__osmembership_plans AS a '
. 'INNER JOIN #__osmembership_upgraderules AS b '
. 'ON a.id=b.from_plan_id '
. 'WHERE b.id=' . $row->upgrade_option_id;
$db->setQuery($sql);
$fromPlanTitle = $db->loadResult();
$itemName = str_replace('[FROM_PLAN_TITLE]', $fromPlanTitle, $itemName);
break;
default:
$itemName = JText::_('OSM_PAYMENT_FOR_SUBSCRIPTION');
$itemName = str_replace('[PLAN_TITLE]', $rowPlan->title, $itemName);
break;
}
$replaces['ITEM_NAME'] = $itemName;
foreach ($replaces as $key => $value)
{
$key = strtoupper($key);
$invoiceOutput = str_ireplace("[$key]", $value, $invoiceOutput);
}
$v = $pdf->writeHTML($invoiceOutput, true, false, false, false, '');
//Filename
$filePath = JPATH_ROOT . '/media/com_osmembership/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