From 9161952ce3d2fa9ca59954ac5f24eafacbec4a26 Mon Sep 17 00:00:00 2001 From: Pratiksha Dubey Date: Thu, 24 Jul 2014 17:37:31 +0530 Subject: [PATCH] VAT-498 Automatically attach Invoice PDF to confirmation receipt based on the global setting --- CRM/Admin/Form/Preferences/Contribute.php | 6 ++++++ CRM/Contribute/BAO/ContributionPage.php | 26 +++++++++++++++++++++++ CRM/Contribute/Form/AdditionalInfo.php | 13 ++++++++++++ CRM/Core/BAO/MessageTemplate.php | 7 ++++++ CRM/Event/BAO/Event.php | 9 ++++++++ CRM/Event/Form/Participant.php | 12 +++++++++++ CRM/Event/Form/Registration/Confirm.php | 1 + 7 files changed, 74 insertions(+) diff --git a/CRM/Admin/Form/Preferences/Contribute.php b/CRM/Admin/Form/Preferences/Contribute.php index af3d257d7d..e9e4469ecb 100644 --- a/CRM/Admin/Form/Preferences/Contribute.php +++ b/CRM/Admin/Form/Preferences/Contribute.php @@ -54,6 +54,11 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences { 'html_type' => 'select', 'weight' => 7, ), + 'is_email_pdf' => array( + 'html_type' => 'checkbox', + 'title' => ts('Automatically email invoice when user purchases online'), + 'weight' => 5, + ), ), ); parent::preProcess(); @@ -82,6 +87,7 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences { 'Exclusive' => ts('Show [tax term] exclusive price - i.e. '.$config->defaultCurrencySymbol.'100.00 + '.$config->defaultCurrencySymbol.'20.00 [tax term]') ) ); + $this->add('checkbox', 'is_email_pdf', ts('Automatically email invoice when user purchases online')); parent::buildQuickForm(); } diff --git a/CRM/Contribute/BAO/ContributionPage.php b/CRM/Contribute/BAO/ContributionPage.php index aa284dc44e..4f950c963e 100644 --- a/CRM/Contribute/BAO/ContributionPage.php +++ b/CRM/Contribute/BAO/ContributionPage.php @@ -393,6 +393,15 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio $sendTemplateParams['toEmail'] = $email; $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_receipt', $values); $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $values); + //send email with pdf invoice + $template = CRM_Core_Smarty::singleton( ); + $taxAmt = $template->get_template_vars('dataArray'); + $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings'); + $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); + if (count($taxAmt) > 0 && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) { + $sendTemplateParams['isEmailPdf'] = True; + $sendTemplateParams['contributionId'] = $values['contribution_id']; + } list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); } @@ -886,5 +895,22 @@ LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm } return $sctJson; } + + /** + * Generate html for pdf in confirmation receipt email attachment + * @param int $contributionId Contribution Page Id + * @param int $userID contact id for contributor + * @return array $pdfHtml + */ + static function addInvoicePdfToEmail($contributionId,$userID) { + $contributionID = array($contributionId); + $contactId = array($userID); + $pdfParams = array( + 'output' => 'pdf_invoice', + 'forPage' => 'confirmpage' + ); + $pdfHtml= CRM_Contribute_Form_Task_Invoice::printPDF($contributionID, $pdfParams, $contactId); + return $pdfHtml; + } } diff --git a/CRM/Contribute/Form/AdditionalInfo.php b/CRM/Contribute/Form/AdditionalInfo.php index a086cdeb99..24d4f4fa91 100644 --- a/CRM/Contribute/Form/AdditionalInfo.php +++ b/CRM/Contribute/Form/AdditionalInfo.php @@ -473,6 +473,18 @@ class CRM_Contribute_Form_AdditionalInfo { $form->assign('receive_date', CRM_Utils_Date::processDate($params['receive_date'])); } + $template = CRM_Core_Smarty::singleton( ); + $taxAmt = $template->get_template_vars('dataArray'); + $eventTaxAmt = $template->get_template_vars('totalTaxAmount'); + $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings'); + $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); + if ((count($taxAmt) > 0 || !empty($eventTaxAmt)) && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) { + $isEmailPdf = True; + } + else { + $isEmailPdf = ''; + } + list($sendReceipt, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( array( 'groupName' => 'msg_tpl_workflow_contribution', @@ -484,6 +496,7 @@ class CRM_Contribute_Form_AdditionalInfo { 'toEmail' => $contributorEmail, 'isTest' => $form->_mode == 'test', 'PDFFilename' => ts('receipt').'.pdf', + 'isEmailPdf' => $isEmailPdf, ) ); diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php index 2c065d7538..b5d9804ce5 100644 --- a/CRM/Core/BAO/MessageTemplate.php +++ b/CRM/Core/BAO/MessageTemplate.php @@ -492,6 +492,13 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { } $config = CRM_Core_Config::singleton(); + if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) { + $pdfHtml = CRM_Contribute_BAO_ContributionPage::addInvoicePdfToEmail($params['contributionId'],$params['contactId']); + if (empty($params['attachments'])) { + $params['attachments'] = array(); + } + $params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf',$pdfHtml, $format) ; + } $pdf_filename = ''; if ($config->doNotAttachPDFReceipt && $params['PDFFilename'] && diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index 99128fa82f..f50f36c31f 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -1191,6 +1191,15 @@ WHERE civicrm_event.is_active = 1 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_confirm', $values['event'] ); + // append invoice pdf to email + $template = CRM_Core_Smarty::singleton( ); + $taxAmt = $template->get_template_vars('totalTaxAmount'); + $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings'); + $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); + if ($taxAmt && (isset($invoicing) && isset($prefixValue['is_email_pdf'])) ) { + $sendTemplateParams['isEmailPdf'] = True; + $sendTemplateParams['contributionId'] = $values['contributionId']; + } CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); } } diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 06a73a5b26..6f05eb2532 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -1684,6 +1684,18 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task { $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails); } + //send email with pdf invoice + $template = CRM_Core_Smarty::singleton( ); + $taxAmt = $template->get_template_vars('dataArray'); + $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', + $this->_id, 'contribution_id', 'participant_id' + ); + $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings'); + $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); + if (count($taxAmt) > 0 && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) { + $sendTemplateParams['isEmailPdf'] = True; + $sendTemplateParams['contributionId'] = $contributionId; + } list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); if ($mailSent) { $sent[] = $contactID; diff --git a/CRM/Event/Form/Registration/Confirm.php b/CRM/Event/Form/Registration/Confirm.php index e44af41128..d32bf2a153 100644 --- a/CRM/Event/Form/Registration/Confirm.php +++ b/CRM/Event/Form/Registration/Confirm.php @@ -608,6 +608,7 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { $value['eventID'] = $this->_eventId; $value['item_name'] = $value['description']; } + $this->_values['contributionId'] = $value['contributionID'] ; //CRM-4453. if (!empty($value['is_primary'])) { -- 2.25.1