Commit 3e968bfe authored by Lola Slade's avatar Lola Slade
Browse files

PDF Generation using a template is basically working and inserting tax receipt...

PDF Generation using a template is basically working and inserting tax receipt tokens.  Created a starting point for a default template html file.
parent 22e510d7
......@@ -129,11 +129,18 @@ class CRM_Cdntaxreceipts_Form_Settings extends CRM_Core_Form {
$this->addElement('file', 'receipt_pdftemplate', ts('PDF Template', array('domain' => 'org.civicrm.cdntaxreceipts')), 'size=30 maxlength=60');
$this->addUploadElement('receipt_pdftemplate');
$this->addRule( 'receipt_pdftemplate', ts('File size should be less than %1 MBytes (%2 bytes)', array(1 => $uploadSize, 2 => $uploadFileSize)), 'maxfilesize', $uploadFileSize, array('domain' => 'org.civicrm.cdntaxreceipts') );
$this->addElement(
'select', 'receipt_pdflettertemplate',
ts('PDF Letter Template', array('domain' => 'org.civicrm.cdntaxreceipts')),
array(0 => ts('- do not use -')) + CRM_Core_BAO_MessageTemplate::getMessageTemplates(FALSE),
FALSE, array());
}
else if ( $mode == 'defaults' ) {
$defaults = array(
'receipt_prefix' => CRM_Core_BAO_Setting::getItem(self::SETTINGS, 'receipt_prefix'),
'receipt_authorized_signature_text' => CRM_Core_BAO_Setting::getItem(self::SETTINGS, 'receipt_authorized_signature_text'),
'receipt_pdflettertemplate' => CRM_Core_BAO_Setting::getItem(self::SETTINGS, 'receipt_pdflettertemplate', NULL, 0),
);
return $defaults;
}
......@@ -141,6 +148,7 @@ class CRM_Cdntaxreceipts_Form_Settings extends CRM_Core_Form {
$values = $this->exportValues();
CRM_Core_BAO_Setting::setItem($values['receipt_prefix'], self::SETTINGS, 'receipt_prefix');
CRM_Core_BAO_Setting::setItem($values['receipt_authorized_signature_text'], self::SETTINGS, 'receipt_authorized_signature_text');
CRM_Core_BAO_Setting::setItem($values['receipt_pdflettertemplate'], self::SETTINGS, 'receipt_pdflettertemplate');
$receipt_logo = $this->getSubmitValue('receipt_logo');
$receipt_signature = $this->getSubmitValue('receipt_signature');
......
......@@ -2,31 +2,13 @@
class CRM_Cdntaxreceipts_PDF_Factory {
private static $_singleton = NULL;
/**
* Get or set the single instance of CRM_Cdntaxreceipts_PdfFactory
*
* @param $instance CRM_Core_Resources, new copy of the manager
* @return CRM_Core_Resources
*/
public static function singleton() {
if (self::$_singleton === NULL) {
self::$_singleton = new CRM_Cdntaxreceipts_PDF_Factory();
}
return self::$_singleton;
}
public static function getPDFLib() {
$pdf_template_id = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'pdf_template_id');
//dpm($pdf_template_id);
$pdf_template_id = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_pdflettertemplate', NULL, 0);
if (empty($pdf_template_id)) {
return new CRM_Cdntaxreceipts_PDF_GeneratorOriginal();
}
else {
//TODO: Create a new class to use PDF letter templates and use it here
return new CRM_Cdntaxreceipts_PDF_GeneratorOriginal();
return new CRM_Cdntaxreceipts_PDF_GeneratorPDFLetter($pdf_template_id);
}
}
}
......@@ -3,11 +3,14 @@
abstract class CRM_Cdntaxreceipts_PDF_Generator {
protected $_imageFilesPath;
protected $_imageFilesURL;
function __construct() {
$resources = CRM_Core_Resources::singleton();
$path = $resources->getPath('org.civicrm.cdntaxreceipts', 'img/preview_mode.png');
$this->_imageFilesPath = dirname($path) . '/';
$url = $resources->getUrl('org.civicrm.cdntaxreceipts' , 'img/');
$this->_imageFilesURL = $url;
}
abstract function open();
......
......@@ -47,7 +47,6 @@ class CRM_Cdntaxreceipts_PDF_GeneratorOriginal extends CRM_Cdntaxreceipts_PDF_Ge
$this->pdf->AddPage();
$pdf_variables['pdf_img_files_path'] = $this->_imageFilesPath;
//dpm($pdf_variables);
if ($pdf_variables["is_duplicate"]) {
// print duplicate copy
......@@ -75,7 +74,6 @@ class CRM_Cdntaxreceipts_PDF_GeneratorOriginal extends CRM_Cdntaxreceipts_PDF_Ge
$mymargin_top = $pdf_variables["mymargin_top"];
$is_duplicate = $pdf_variables["is_duplicate"];
$pdf_img_files_path = $pdf_variables["pdf_img_files_path"];
$line_1 = $pdf_variables["line_1"];
$source_funds = $pdf_variables["source_funds"];
$amount = $pdf_variables["amount"];
$display_date = $pdf_variables["display_date"];
......@@ -91,6 +89,8 @@ class CRM_Cdntaxreceipts_PDF_GeneratorOriginal extends CRM_Cdntaxreceipts_PDF_Ge
$issue_type = $pdf_variables["issue_type"];
$receipt_contributions = $pdf_variables['receipt_contributions'];
// Moved this hard coded string in here from Receipt.php
$line_1 = ts("This is your Official Receipt for income tax purposes.", array('domain' => 'org.civicrm.cdntaxreceipts'));
$pdf = $this->pdf;
// Middle center section
if ( $preview_mode ) {
......
......@@ -2,28 +2,128 @@
class CRM_Cdntaxreceipts_PDF_GeneratorPDFLetter extends CRM_Cdntaxreceipts_PDF_Generator {
protected $_imageFilesPath;
protected $_html;
/**
* @var CRM_Core_BAO_MessageTemplate - The CiviCRM message template object
*/
protected $_template;
/**
* @var array - an array of pages
*/
protected $_htmlPages;
/**
* @var text - The HTML string from the CiviCRM message template
*/
protected $_html_message;
protected $_pdfFormat;
function __construct($msgTemplateId) {
parent::__construct();
$this->_htmlPages = array();
// Get template and PDF format
// TODO: Use civicrm_api here
$defaults = array();
$template_finder = new CRM_Core_BAO_MessageTemplate();
$params = array('id' => intval($msgTemplateId));
$template = $template_finder->retrieve($params, $defaults);
$this->_template = $template;
$this->_html_message = $template->msg_html;
$this->_pdfFormat = $format = CRM_Core_BAO_PdfFormat::getById($template->pdf_format_id);
}
function open() {}
function addPage($pdf_variables) {}
/**
* addPage - Function to add a new HTML page to the tax receipt
* Code is heavily borrowed from CRM_Contact_Form_Task_PDFLetterCommon::postProcess
* @param $pdf_variables
* @return bool
*/
function addPage($pdf_variables) {
$skipOnHold = FALSE;
$skipDeceased = TRUE;
// Add image url
$pdf_variables['image_files_url'] = $this->_imageFilesURL;
$contactId = $pdf_variables['contact_id'];
$pdf_variables = self::formatPDFVariables($pdf_variables);
$tokens = array();
CRM_Utils_Hook::tokens($tokens);
$categories = array_keys($tokens) + array('tax_receipt');
$html_message = $this->_html_message;
CRM_Contact_Form_Task_PDFLetterCommon::formatMessage($html_message);
$messageToken = CRM_Utils_Token::getTokens($html_message);
$returnProperties = array();
if (isset($messageToken['contact'])) {
foreach ($messageToken['contact'] as $key => $value) {
$returnProperties[$value] = 1;
}
}
$params = array('contact_id' => $contactId);
list($contact) = CRM_Utils_Token::getTokenDetails($params,
$returnProperties,
$skipOnHold,
$skipDeceased,
NULL,
$messageToken,
'CRM_Cdntaxreceipts_PDF_GeneratorPDFLetter'
);
if (civicrm_error($contact)) {
return FALSE;
}
$contact[$contactId] = $contact[$contactId] + $pdf_variables;
$tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken);
$tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE);
if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
$smarty = CRM_Core_Smarty::singleton();
// also add the contact tokens to the template
$smarty->assign_by_ref('contact', $contact);
$tokenHtml = $smarty->fetch("string:$tokenHtml");
}
$this->_htmlPages[] = $tokenHtml;
//TODO: Should we create an activity? Probably not here though
//self::createActivities($form, $html_message, $form->_contactIds);
}
function closeAndSend($filename) {
CRM_Utils_PDF_Utils::html2pdf($this->_html, $filename, TRUE, $this->_pdfFormat);
CRM_Utils_PDF_Utils::html2pdf($this->_htmlPages, $filename, FALSE, $this->_pdfFormat);
CRM_Utils_System::civiExit(1);
}
function closeAndSave($filename) {
CRM_Utils_PDF_Utils::html2pdf($this->_html, $filename, TRUE, $this->_pdfFormat);
$pdf_bin = CRM_Utils_PDF_Utils::html2pdf($this->_htmlPages, $filename, TRUE, $this->_pdfFormat);
file_put_contents($filename, $pdf_bin);
}
function getDefaultLeftMargin() { return 0;}
function getDefaultLeftMargin() {
return CRM_Core_BAO_PdfFormat::getValue('margin_left', $this->_pdfFormat);
}
function getDefaultTopMargin() { return 0;}
function getDefaultTopMargin() {
return CRM_Core_BAO_PdfFormat::getValue('margin_top', $this->_pdfFormat);
}
static function formatPDFVariables($pdf_variables) {
$formatted = array();
foreach ($pdf_variables as $key => $value) {
$formatted['tax_receipt.' . $key] = $value;
}
return $formatted;
}
}
......@@ -219,6 +219,7 @@ class CRM_Cdntaxreceipts_Receipt {
// Set attachment file name
$pdf_file = $this->getFileName();
// Get contact details
// TODO: I think we can skip this we should already have it
list($displayName, $email) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId);
......@@ -295,7 +296,7 @@ class CRM_Cdntaxreceipts_Receipt {
/**
* process - Calculate some temporary display variables before printing
* and generate an array for original PDF generator,
* and generate an array of varaiables for the PDF generator,
* @return array
*/
function process() {
......@@ -330,16 +331,14 @@ class CRM_Cdntaxreceipts_Receipt {
date('M j, Y', mktime(0, 0, 0, 12, 31, $this->_receive_date));
}
$line_1 = ts("This is your Official Receipt for income tax purposes.", array('domain' => 'org.civicrm.cdntaxreceipts'));
$this->_issued_on = (int) $_SERVER['REQUEST_TIME'];
$this->_ip = $_SERVER['REMOTE_ADDR'];
$pdf_variables = array(
"preview_mode" => $previewMode,
"is_duplicate" => $this->_is_duplicate,
"line_1" => $line_1,
"source_funds" => isset($receipt['source']) ? $receipt['source'] : '',
'contact_id' => $this->_contactId,
'preview_mode' => $previewMode,
'is_duplicate' => $this->_is_duplicate,
'source_funds' => !empty($this->_source) ? $this->_source : '',
"amount" => $this->_receipt_amount,
"display_date" => $display_date,
"display_year" => $display_year,
......@@ -353,6 +352,14 @@ class CRM_Cdntaxreceipts_Receipt {
"address_line_2" => $address_line_2,
"address_line_3" => $address_line_3,
"inkind_values" => isset($this->_inkind_values) ? $this->_inkind_values : array(),
'org_name' => CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_name'),
'org_address_line1' => CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_address_line1', NULL, ''),
'org_address_line2' => CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_address_line2', NULL, ''),
'org_tel' => CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_tel', NULL, ''),
'org_fax' => CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_fax', NULL, ''),
'org_web' => CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_web', NULL, ''),
'org_email' => CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_email', NULL, ''),
'org_charitable_no' => CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_charitable_no', NULL, ''),
"receipt_contributions" => $this->_contributions,
);
......
......@@ -74,7 +74,6 @@ class CRM_Cdntaxreceipts_Receipt_BatchBuilderAnnual extends CRM_Cdntaxreceipts_R
}
}
//dpm($this->_receiptBatch);
return $this->_receiptBatch['toIssue'];
}
......
......@@ -43,7 +43,6 @@ class CRM_Cdntaxreceipts_Receipt_BatchBuilderSingle extends CRM_Cdntaxreceipts_R
$this->_receiptBatchSummary[$key][$method]++;
}
}
//dpm($this->_receiptBatch);
return $this->_receiptBatch['toIssue'];
}
......
......@@ -86,7 +86,6 @@ class CRM_Cdntaxreceipts_Task_IssueTaxReceiptsCommon extends CRM_Contribute_Form
}
}
$batchCounts = $batch->getCounts();
dpm($batchCounts);
return $batchCounts;
}
......
......@@ -160,167 +160,6 @@ function cdntaxreceipts_eligibleAmount( $contributionId ) {
return $deductibleAmount;
}
/**
* issueAnnualTaxReceipt()
* Issues an annual tax receipt for a given year.
* Detects if the receipt is a duplicate and reacts appropriately.
*/
function cdntaxreceipts_issueAnnualTaxReceipt( $contactId, $year, &$collectedPdf = NULL, $previewMode = FALSE ) {
// determine the send method
list( $method, $email ) = cdntaxreceipts_sendMethodForContact($contactId);
// query for duplicate receipt
list( $issued_on, $receipt_id ) = cdntaxreceipts_annual_issued_on($contactId, $year);
// When returning issued_on field:
// returns false for non existing $contribution ID
// returns 0 for existing but old data
// returns timestamp for new data
$is_duplicate = empty($issued_on) ? 0 : 1;
if ( $is_duplicate ) {
// issue an identical receipt
$receipt = cdntaxreceipts_load_receipt($receipt_id);
$receipt['issued_on'] = (int) $_SERVER['REQUEST_TIME'];
$receipt['is_duplicate'] = 1;
$receipt['receive_date'] = $year;
$receipt['issue_method'] = $method;
}
else {
$contributions = cdntaxreceipts_contributions_not_receipted($contactId, $year);
if ( count($contributions) == 0 ) {
return array(FALSE, $method, NULL);
}
// calculate total amount
$receiptContributions = array();
$totalReceipt = 0;
foreach ( $contributions as $c ) {
$receiptContributions[] = array(
'contribution_id' => $c['contribution_id'],
'contribution_amount' => $c['total_amount'],
'receipt_amount' => cdntaxreceipts_eligibleAmount($c['contribution_id']),
'receive_date' => $c['receive_date'],
);
$totalReceipt += cdntaxreceipts_eligibleAmount($c['contribution_id']);
}
// generate a receipt number
$receiptNo = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_prefix') . str_pad($receiptContributions[0]['contribution_id'], 8, 0, STR_PAD_LEFT);
$receipt = array(
'receipt_no' => $receiptNo,
'issued_on' => (int) $_SERVER['REQUEST_TIME'],
'contact_id' => $contactId,
'receipt_amount' => $totalReceipt,
'is_duplicate' => 0,
'issue_type' => 'annual',
'issue_method' => $method,
'receive_date' => $year,
'contributions' => $receiptContributions,
);
}
return cdntaxreceipts_processTaxReceipt($receipt, $collectedPdf, $previewMode);
}
/**
* cdntaxreceipts_issueAggregateTaxReceipt()
* Issues an grouped tax receipt for a given contact, year and list of selected contributions.
* Currently does not issue duplicates. Pass only original contribution ids.
*
* @param $contactId
* @param $year
* @param $contributions
* @param $method
* @param null $collectedPdf
* @param bool $previewMode
* @return array
*/
function cdntaxreceipts_issueAggregateTaxReceipt($contactId, $year, $contributions, $method, &$collectedPdf = NULL,
$previewMode = FALSE ) {
if ( count($contributions) == 0 ) {
return array(FALSE, $method, NULL);
}
/* Two different formats come into this function in the $contributions array
* 1. When issuing for the first time:
* array ($contribution_id => array(
* 'contribution_id' => $id,
* 'contact_id' => $dao->contact_id,
* 'total_amount' => $dao->total_amount,
* 'non_deductible_amount' => $dao->non_deductible_amount,
* 'receive_date' => $dao->receive_date,
* 'receive_year' => $dao->receive_year,
* 'eligible' => $eligible,
* 'receipt_id' => $dao->receipt_id,
* ),);
* 2. When re-issuing:
*
* $index (not contrib id) => array(
* 'contribution_id'
* 'contribution_amount'
* 'receipt_amount'
* 'receive_date'
* );
* */
$contrib_copy = $contributions;
$check_contribution = array_shift($contrib_copy);
list($issued_on, $receipt_id) = cdntaxreceipts_issued_on($check_contribution['contribution_id']);
if (isset($check_contribution['receipt_id']) && $check_contribution['receipt_id'] != 0) {
$is_duplicate = TRUE;
}
else {
$is_duplicate = empty($issued_on) ? 0 : 1;
}
if ( $is_duplicate ) {
$receipt = cdntaxreceipts_load_receipt($receipt_id);
$receipt['issued_on'] = (int) $_SERVER['REQUEST_TIME'];
$receipt['is_duplicate'] = 1;
$receipt['issue_method'] = $method;
$receipt['receive_date'] = $year;
}
else {
// calculate total amount
$receiptContributions = array();
$totalReceipt = 0;
foreach ( $contributions as $c ) {
$receiptContributions[] = array(
'contribution_id' => $c['contribution_id'],
'contribution_amount' => $c['total_amount'],
'receipt_amount' => cdntaxreceipts_eligibleAmount($c['contribution_id']),
'receive_date' => $c['receive_date'],
);
$totalReceipt += cdntaxreceipts_eligibleAmount($c['contribution_id']);
}
// generate a receipt number
$receiptNo = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_prefix') . str_pad($receiptContributions[0]['contribution_id'], 8, 0, STR_PAD_LEFT);
$receipt = array(
'receipt_no' => $receiptNo,
'issued_on' => (int) $_SERVER['REQUEST_TIME'],
'contact_id' => $contactId,
'receipt_amount' => $totalReceipt,
'is_duplicate' => 0,
'issue_type' => 'aggregate',
'issue_method' => $method,
'receive_date' => $year,
'contributions' => $receiptContributions,
);
}
return cdntaxreceipts_processTaxReceipt($receipt, $collectedPdf, $previewMode);
}
/* determines the send method for the specified contact
* returns array, [0] = 'email' or 'print', [1] = email address or NULL
*/
......
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* {
margin:0; padding:0;
}
body{
font: 10px Helvetica, sans-serif;
}
a {
color: #000000;
}
</style>
<title></title>
</head>
<body>
<div id="receipt_background" style="margin: none; padding: none; width: 100%; height: 100%;">
<div id="receipt_wrapper" style="height: 280px;">
<div id="receipt_column1" style="float: left; width: 45%; background-color:#0000FF; height: 100%;">
<div style="height: 40%"><img alt="Logo" src="{tax_receipt.image_files_url}/your-logo.png" /></div>
<div style="height: 30%">&nbsp;</div>
<div style="height: 30%">
<p style="margin: 2px; font-weight: bold;">This is your receipt for income tax purposes.</p>
<p style="margin: 2px;">Canadian Revenue Agency: <a href="http://www.cra-arc.gc.ca/charities">www.cra-arc.gc.ca/charities</a></p>
</div>
</div>
<div id="receipt_column2" style="float: left; width: 10%; background-color:#00FFFF; height: 100%;">
<div style="height: 95%">&nbsp;</div>
<div style="height: 5%">Thank you!</div>
</div>
<div id="receipt_column3" style="float: left; width: 45%; background-color:#FF0000; height: 100%;">
<div id="receipt_org_details" style="text-align: right; font-size: 10px;">
<p>{tax_receipt.org_name}</p>
<p>{tax_receipt.org_address_line1}</p>
<p>{tax_receipt.org_address_line2}</p>
<p>Tel: {tax_receipt.org_tel} Fax: {tax_receipt.org_fax}</p>
<p style="font-style: italic;">Email: {tax_receipt.org_email} Website: {tax_receipt.org_web}</p>
<p style="font-style: italic;">Charitable Registration: {tax_receipt.org_charitable_no}</p>
</div>
<div id="receipt_signature" style="text-align: left;">
<img alt="Authorized signature" src="{tax_receipt.image_files_url}/authorized-signature.png" />
<div id="receipt_signature_label" style="text-align: center; margin: 0px; padding: 0px; border-top: solid 1px #000000; width: 160px;">
<p style="margin: 0px; font-style: italic; font-size: 10px;">Authorized signature</p>
</div>
</div>
</div>
</div>
<div style="text-align: center; padding-top: 5px; clear: both; height: 10px">
<hr align="center" style="text-align: center; margin-left: 3%; max-width: 94%;" /></div>
</div>
</body>
</html>
......@@ -92,10 +92,17 @@
<td class="label">{$form.receipt_pdftemplate.label}</td>
<td class="content">{$form.receipt_pdftemplate.html}
<p class="description">{ts domain='org.civicrm.cdntaxreceipts'}Upload your own PDF template: .pdf{/ts}</p>
{if $receipt_pdftemplate}
{if $receipt_pdftemplate_class}<span class="crm-error">The file {$receipt_pdftemplate} was not found</span>
{else}<p class="label">Current {$form.receipt_pdftemplate.label}: {$receipt_pdftemplate}</p>{/if}
{/if}</td>
{if $receipt_pdftemplate}
{if $receipt_pdftemplate_class}<span class="crm-error">The file {$receipt_pdftemplate} was not found</span>
{else}<p class="label">Current {$form.receipt_pdftemplate.label}: {$receipt_pdftemplate}</p>{/if}
{/if}</td>
</tr>
<tr>
<td class="label">{$form.receipt_pdflettertemplate.label}</td>
<td class="content">{$form.receipt_pdflettertemplate.html}
<p class="description">{ts domain='org.civicrm.cdntaxreceipts'}Use a selected CiviCRM PDF Letter template instead of internal PDF generation.<br />
Note: This does not work with the above PDF background selection.{/ts}</p>
</td>
</tr>
</tbody>
</table>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment