Skip to content

Commit

Permalink
Merge pull request civicrm#7 from rohankatkar/VAT_Issue_388
Browse files Browse the repository at this point in the history
VAT-388  Added action for Print or Email Invoice. Created template for invoice.
  • Loading branch information
pradpnayak committed Jun 20, 2014
2 parents dbe4bf6 + 69c22cd commit 013cc8e
Show file tree
Hide file tree
Showing 13 changed files with 1,058 additions and 42 deletions.
406 changes: 406 additions & 0 deletions CRM/Contribute/Form/Task/Invoice.php

Large diffs are not rendered by default.

101 changes: 59 additions & 42 deletions CRM/Contribute/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,46 +151,15 @@ function setDefaultValues() {
*/
public function postProcess() {
// get all the details needed to generate a receipt
$contribIDs = implode(',', $this->_contributionIds);

$details = CRM_Contribute_Form_Task_Status::getDetails($contribIDs);

$baseIPN = new CRM_Core_Payment_BaseIPN();

$message = array();
$template = CRM_Core_Smarty::singleton();

$params = $this->controller->exportValues($this->_name);

$createPdf = FALSE;
if ($params['output'] == "pdf_receipt") {
$createPdf = TRUE;
}

$excludeContactIds = array();
if (!$createPdf) {
$returnProperties = array(
'email' => 1,
'do_not_email' => 1,
'is_deceased' => 1,
'on_hold' => 1,
);

list($contactDetails) = CRM_Utils_Token::getTokenDetails($this->_contactIds, $returnProperties, FALSE, FALSE);
$suppressedEmails = 0;
foreach ($contactDetails as $id => $values) {
if (empty($values['email']) || !empty($values['do_not_email']) ||
CRM_Utils_Array::value('is_deceased', $values) || !empty($values['on_hold'])) {
$suppressedEmails++;
$excludeContactIds[] = $values['contact_id'];
}
}
}
$elements = self::getElements();

foreach ($details as $contribID => $detail) {
foreach ($elements['details'] as $contribID => $detail) {
$input = $ids = $objects = array();

if (in_array($detail['contact'], $excludeContactIds)) {
if (in_array($detail['contact'], $elements['excludeContactIds'])) {
continue;
}

Expand All @@ -204,13 +173,11 @@ public function postProcess() {
$ids['participant'] = CRM_Utils_Array::value('participant', $detail);
$ids['event'] = CRM_Utils_Array::value('event', $detail);

if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) {
if (!$elements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
CRM_Core_Error::fatal();
}

$contribution = &$objects['contribution'];
// CRM_Core_Error::debug('o',$objects);


// set some fake input values so we can reuse IPN code
$input['amount'] = $contribution->total_amount;
Expand All @@ -226,7 +193,7 @@ public function postProcess() {
// CRM_Core_Error::debug('input',$input);

$values = array();
$mail = $baseIPN->sendMail($input, $ids, $objects, $values, FALSE, $createPdf);
$mail = $elements['baseIPN']->sendMail($input, $ids, $objects, $values, FALSE, $elements['createPdf']);

if ($mail['html']) {
$message[] = $mail['html'];
Expand All @@ -238,17 +205,18 @@ public function postProcess() {
// reset template values before processing next transactions
$template->clearTemplateVars();
}
if ($createPdf) {

if ($elements['createPdf']) {
CRM_Utils_PDF_Utils::html2pdf($message,
'civicrmContributionReceipt.pdf',
FALSE,
$params['pdf_format_id']
$elements['params']['pdf_format_id']
);
CRM_Utils_System::civiExit();
}
else {
if ($suppressedEmails) {
$status = ts('Email was NOT sent to %1 contacts (no email address on file, or communication preferences specify DO NOT EMAIL, or contact is deceased).', array(1 => $suppressedEmails));
if ($elements['suppressedEmails']) {
$status = ts('Email was NOT sent to %1 contacts (no email address on file, or communication preferences specify DO NOT EMAIL, or contact is deceased).', array(1 => $elements['suppressedEmails']));
$msgTitle = ts('Email Error');
$msgType = 'error';
}
Expand All @@ -260,5 +228,54 @@ public function postProcess() {
CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
}
}

/**
* declaration of common variables for Invoice and PDF
*
* @access public
*
* @return array array of common elements
*
*/
public function getElements() {
$pdfElements = array();

$pdfElements['contribIDs'] = implode(',', $this->_contributionIds);

$pdfElements['details'] = CRM_Contribute_Form_Task_Status::getDetails($pdfElements['contribIDs']);

$pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();

$pdfElements['params'] = $this->controller->exportValues($this->_name);

$pdfElements['createPdf'] = FALSE;
if ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt") {
$pdfElements['createPdf'] = TRUE;
}

$excludeContactIds = array();
if (!$pdfElements['createPdf']) {
$returnProperties = array(
'email' => 1,
'do_not_email' => 1,
'is_deceased' => 1,
'on_hold' => 1,
);

list($contactDetails) = CRM_Utils_Token::getTokenDetails($this->_contactIds, $returnProperties, FALSE, FALSE);
$pdfElements['suppressedEmails'] = 0;
foreach ($contactDetails as $id => $values) {
if (empty($values['email']) || !empty($values['do_not_email']) ||
CRM_Utils_Array::value('is_deceased', $values) || !empty($values['on_hold'])) {
$suppressedEmails++;
$pdfElements['suppressedEmails'] = $suppressedEmails;
$excludeContactIds[] = $values['contact_id'];
}
}
}
$pdfElements['excludeContactIds'] = $excludeContactIds;

return $pdfElements;
}
}

5 changes: 5 additions & 0 deletions CRM/Contribute/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ static function &tasks() {
'class' => 'CRM_Contribute_Form_Task_PDFLetter',
'result' => FALSE,
),
9 => array(
'title' => ts('Print or Email Contribution Invoices'),
'class' => 'CRM_Contribute_Form_Task_Invoice',
'result' => FALSE,
),
);

//CRM-4418, check for delete
Expand Down
3 changes: 3 additions & 0 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ static function sendTemplate($params) {
$params['attachments'] = array();
}
$params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $format);
if (isset($params['tplParams']['email_comment'])) {
$params['html'] = $params['tplParams']['email_comment'];
}
}

$sent = CRM_Utils_Mail::send($params);
Expand Down
24 changes: 24 additions & 0 deletions CRM/Price/BAO/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL
li.participant_count,
li.price_field_value_id,
li.financial_type_id,
li.tax_amount,
pfv.description";

$fromClause = "
Expand Down Expand Up @@ -180,7 +181,10 @@ static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL
'financial_type_id' => $dao->financial_type_id,
'membership_type_id' => $dao->membership_type_id,
'membership_num_terms' => $dao->membership_num_terms,
'tax_amount' => $dao->tax_amount,
);
$lineItems[$dao->id]['tax_rate'] = CRM_Price_BAO_LineItem::calculateTaxRate($lineItems[$dao->id]);
$lineItems[$dao->id]['subTotal'] = $lineItems[$dao->id]['qty'] * $lineItems[$dao->id]['unit_price'];
}
return $lineItems;
}
Expand Down Expand Up @@ -418,4 +422,24 @@ static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'con
}
}
}

/**
* Calculate tax rate in percentage
*
* @param $lineItemId an assoc array of lineItem
*
* @return tax rate
*
* @access public
* @static
*/
public static function calculateTaxRate($lineItemId) {
if ($lineItemId['html_type'] == 'Text') {
$tax = (($lineItemId['line_total'] - ($lineItemId['unit_price'] * $lineItemId['qty'])))/($lineItemId['unit_price'] * $lineItemId['qty'])*100;
}
else {
$tax = (($lineItemId['line_total'] - $lineItemId['unit_price'])/$lineItemId['unit_price']) * 100;
}
return $tax;
}
}
Loading

0 comments on commit 013cc8e

Please sign in to comment.