3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 * This class provides the functionality to email a group of
25 class CRM_Contribute_Form_Task_Invoice
extends CRM_Contribute_Form_Task
{
27 * Are we operating in "single mode", i.e. updating the task of only
28 * one specific contribution?
32 public $_single = FALSE;
35 * Gives all the statues for conribution.
38 public $_contributionStatusId;
41 * Gives the HTML template of PDF Invoice.
44 public $_messageInvoice;
47 * This variable is used to assign parameters for HTML template of PDF Invoice.
50 public $_invoiceTemplate;
56 public $_selectedOutput;
59 * Build all the data structures needed to build the form.
61 public function preProcess() {
62 $id = CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE);
64 $this->_contributionIds
= [$id];
65 $this->_componentClause
= " civicrm_contribution.id IN ( $id ) ";
66 $this->_single
= TRUE;
67 $this->assign('totalSelectedContributions', 1);
69 // set the redirection after actions
70 $contactId = CRM_Utils_Request
::retrieve('cid', 'Positive', $this, FALSE);
71 $url = CRM_Utils_System
::url('civicrm/contact/view/contribution',
72 "action=view&reset=1&id={$id}&cid={$contactId}&context=contribution&selectedChild=contribute"
75 CRM_Core_Session
::singleton()->pushUserContext($url);
81 // check that all the contribution ids have status Completed, Pending, Refunded, or Partially Paid.
82 $this->_contributionStatusId
= CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
83 $status = ['Completed', 'Pending', 'Refunded', 'Partially paid'];
85 foreach ($this->_contributionStatusId
as $key => $value) {
86 if (in_array($value, $status)) {
90 $Id = implode(",", $statusId);
91 $query = "SELECT count(*) FROM civicrm_contribution WHERE contribution_status_id NOT IN ($Id) AND {$this->_componentClause}";
92 $count = CRM_Core_DAO
::singleValueQuery($query);
94 CRM_Core_Error
::statusBounce(ts('Please select only contributions with Completed, Pending, Refunded, or Partially Paid status.'));
97 // we have all the contribution ids, so now we get the contact ids
98 parent
::setContactIDs();
99 $this->assign('single', $this->_single
);
101 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
102 $urlParams = 'force=1';
103 if (CRM_Utils_Rule
::qfKey($qfKey)) {
104 $urlParams .= "&qfKey=$qfKey";
107 $url = CRM_Utils_System
::url('civicrm/contribute/search', $urlParams);
111 'title' => ts('Search Results'),
115 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
117 $this->_selectedOutput
= CRM_Utils_Request
::retrieve('select', 'String', $this);
118 $this->assign('selectedOutput', $this->_selectedOutput
);
120 CRM_Contact_Form_Task_EmailCommon
::preProcessFromAddress($this);
121 if ($this->_selectedOutput
== 'email') {
122 CRM_Utils_System
::setTitle(ts('Email Invoice'));
125 CRM_Utils_System
::setTitle(ts('Print Contribution Invoice'));
130 * Build the form object.
132 public function buildQuickForm() {
133 $this->preventAjaxSubmit();
134 if (CRM_Core_Permission
::check('administer CiviCRM')) {
135 $this->assign('isAdmin', 1);
138 $this->add('select', 'from_email_address', ts('From'), $this->_fromEmails
, TRUE);
139 if ($this->_selectedOutput
!= 'email') {
140 $this->addElement('radio', 'output', NULL, ts('Email Invoice'), 'email_invoice');
141 $this->addElement('radio', 'output', NULL, ts('PDF Invoice'), 'pdf_invoice');
142 $this->addRule('output', ts('Selection required'), 'required');
143 $this->addFormRule(['CRM_Contribute_Form_Task_Invoice', 'formRule']);
146 $this->addRule('from_email_address', ts('From Email Address is required'), 'required');
149 $this->add('wysiwyg', 'email_comment', ts('If you would like to add personal message to email please add it here. (If sending to more then one receipient the same message will be sent to each contact.)'), [
157 'name' => $this->_selectedOutput
== 'email' ?
ts('Send Email') : ts('Process Invoice(s)'),
162 'name' => ts('Cancel'),
168 * Global validation rules for the form.
170 * @param array $values
173 * list of errors to be posted back to the form
175 public static function formRule($values) {
178 if ($values['output'] == 'email_invoice' && empty($values['from_email_address'])) {
179 $errors['from_email_address'] = ts("From Email Address is required");
186 * Process the form after the input has been submitted and validated.
188 public function postProcess() {
189 $params = $this->controller
->exportValues($this->_name
);
190 self
::printPDF($this->_contributionIds
, $params, $this->_contactIds
);
194 * Process the PDf and email with activity and attachment on click of Print Invoices.
196 * @param array $contribIDs
198 * @param array $params
199 * Associated array of submitted values.
200 * @param array $contactIds
203 public static function printPDF($contribIDs, &$params, $contactIds) {
204 // get all the details needed to generate a invoice
205 $messageInvoice = [];
206 $invoiceTemplate = CRM_Core_Smarty
::singleton();
207 $invoiceElements = CRM_Contribute_Form_Task_PDF
::getElements($contribIDs, $params, $contactIds);
209 // gives the status id when contribution status is 'Refunded'
210 $contributionStatusID = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
211 $refundedStatusId = CRM_Utils_Array
::key('Refunded', $contributionStatusID);
212 $cancelledStatusId = CRM_Utils_Array
::key('Cancelled', $contributionStatusID);
213 $pendingStatusId = CRM_Utils_Array
::key('Pending', $contributionStatusID);
215 foreach ($invoiceElements['details'] as $contribID => $detail) {
216 $input = $ids = $objects = [];
217 if (in_array($detail['contact'], $invoiceElements['excludeContactIds'])) {
221 $input['component'] = $detail['component'];
223 $ids['contact'] = $detail['contact'];
224 $ids['contribution'] = $contribID;
225 $ids['contributionRecur'] = NULL;
226 $ids['contributionPage'] = NULL;
227 $ids['membership'] = $detail['membership'] ??
NULL;
228 $ids['participant'] = $detail['participant'] ??
NULL;
229 $ids['event'] = $detail['event'] ??
NULL;
231 if (!$invoiceElements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
232 CRM_Core_Error
::statusBounce('Supplied data was not able to be validated');
235 $contribution = &$objects['contribution'];
237 $input['amount'] = $contribution->total_amount
;
238 $input['invoice_id'] = $contribution->invoice_id
;
239 $input['receive_date'] = $contribution->receive_date
;
240 $input['contribution_status_id'] = $contribution->contribution_status_id
;
241 $input['organization_name'] = $contribution->_relatedObjects
['contact']->organization_name
;
243 $objects['contribution']->receive_date
= CRM_Utils_Date
::isoToMysql($objects['contribution']->receive_date
);
245 // Fetch the billing address. getValues should prioritize the billing
246 // address, otherwise will return the primary address.
247 $billingAddress = [];
249 $addressDetails = CRM_Core_BAO_Address
::getValues([
250 'contact_id' => $contribution->contact_id
,
254 if (!empty($addressDetails)) {
255 $billingAddress = array_shift($addressDetails);
258 if ($contribution->contribution_status_id
== $refundedStatusId ||
$contribution->contribution_status_id
== $cancelledStatusId) {
259 $creditNoteId = $contribution->creditnote_id
;
261 if (!$contribution->invoice_number
) {
262 $contribution->invoice_number
= CRM_Contribute_BAO_Contribution
::getInvoiceNumber($contribution->id
);
265 //to obtain due date for PDF invoice
266 $contributionReceiveDate = date('F j,Y', strtotime(date($input['receive_date'])));
267 $invoiceDate = date("F j, Y");
268 $dueDateSetting = Civi
::settings()->get('invoice_due_date');
269 $dueDatePeriodSetting = Civi
::settings()->get('invoice_due_date_period');
270 $dueDate = date('F j, Y', strtotime($contributionReceiveDate . "+" . $dueDateSetting . "" . $dueDatePeriodSetting));
272 $amountPaid = CRM_Core_BAO_FinancialTrxn
::getTotalPayments($contribID, TRUE);
273 $amountDue = ($input['amount'] - $amountPaid);
275 // retrieving the subtotal and sum of same tax_rate
278 $lineItem = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($contribID);
279 foreach ($lineItem as $taxRate) {
280 if (isset($dataArray[(string) $taxRate['tax_rate']])) {
281 $dataArray[(string) $taxRate['tax_rate']] = $dataArray[(string) $taxRate['tax_rate']] + CRM_Utils_Array
::value('tax_amount', $taxRate);
284 $dataArray[(string) $taxRate['tax_rate']] = $taxRate['tax_amount'] ??
NULL;
286 $subTotal +
= CRM_Utils_Array
::value('subTotal', $taxRate);
289 // to email the invoice
292 if ($contribution->_component
== 'event') {
293 $daoName = 'CRM_Event_DAO_Event';
294 $pageId = $contribution->_relatedObjects
['event']->id
;
298 'confirm_from_email',
300 CRM_Core_DAO
::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
301 $values['title'] = $mailDetails[$contribution->_relatedObjects
['event']->id
]['title'] ??
NULL;
302 $values['confirm_from_name'] = $mailDetails[$contribution->_relatedObjects
['event']->id
]['confirm_from_name'] ??
NULL;
303 $values['confirm_from_email'] = $mailDetails[$contribution->_relatedObjects
['event']->id
]['confirm_from_email'] ??
NULL;
305 $title = $mailDetails[$contribution->_relatedObjects
['event']->id
]['title'] ??
NULL;
307 elseif ($contribution->_component
== 'contribute') {
308 $daoName = 'CRM_Contribute_DAO_ContributionPage';
309 $pageId = $contribution->contribution_page_id
;
313 'receipt_from_email',
317 CRM_Core_DAO
::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
319 $values['title'] = CRM_Utils_Array
::value('title', CRM_Utils_Array
::value($contribution->contribution_page_id
, $mailDetails));
320 $values['receipt_from_name'] = CRM_Utils_Array
::value('receipt_from_name', CRM_Utils_Array
::value($contribution->contribution_page_id
, $mailDetails));
321 $values['receipt_from_email'] = CRM_Utils_Array
::value('receipt_from_email', CRM_Utils_Array
::value($contribution->contribution_page_id
, $mailDetails));
322 $values['cc_receipt'] = CRM_Utils_Array
::value('cc_receipt', CRM_Utils_Array
::value($contribution->contribution_page_id
, $mailDetails));
323 $values['bcc_receipt'] = CRM_Utils_Array
::value('bcc_receipt', CRM_Utils_Array
::value($contribution->contribution_page_id
, $mailDetails));
325 $title = CRM_Utils_Array
::value('title', CRM_Utils_Array
::value($contribution->contribution_page_id
, $mailDetails));
327 $source = $contribution->source
;
329 $config = CRM_Core_Config
::singleton();
330 if (!isset($params['forPage'])) {
331 $config->doNotAttachPDFReceipt
= 1;
334 // get organization address
335 $domain = CRM_Core_BAO_Domain
::getDomain();
336 $locParams = ['contact_id' => $domain->contact_id
];
337 $locationDefaults = CRM_Core_BAO_Location
::getValues($locParams);
338 if (isset($locationDefaults['address'][1]['state_province_id'])) {
339 $stateProvinceAbbreviationDomain = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($locationDefaults['address'][1]['state_province_id']);
342 $stateProvinceAbbreviationDomain = '';
344 if (isset($locationDefaults['address'][1]['country_id'])) {
345 $countryDomain = CRM_Core_PseudoConstant
::country($locationDefaults['address'][1]['country_id']);
351 $invoiceNotes = Civi
::settings()->get('invoice_notes') ??
NULL;
353 // parameters to be assign for template
356 'component' => $input['component'],
357 'id' => $contribution->id
,
359 'invoice_number' => $contribution->invoice_number
,
360 'invoice_id' => $contribution->invoice_id
,
361 'resourceBase' => $config->userFrameworkResourceURL
,
362 'defaultCurrency' => $config->defaultCurrency
,
363 'amount' => $contribution->total_amount
,
364 'amountDue' => $amountDue,
365 'amountPaid' => $amountPaid,
366 'invoice_date' => $invoiceDate,
367 'dueDate' => $dueDate,
368 'notes' => $invoiceNotes,
369 'display_name' => $contribution->_relatedObjects
['contact']->display_name
,
370 'lineItem' => $lineItem,
371 'dataArray' => $dataArray,
372 'refundedStatusId' => $refundedStatusId,
373 'pendingStatusId' => $pendingStatusId,
374 'cancelledStatusId' => $cancelledStatusId,
375 'contribution_status_id' => $contribution->contribution_status_id
,
376 'contributionStatusName' => CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution->contribution_status_id
),
377 'subTotal' => $subTotal,
378 'street_address' => $billingAddress['street_address'] ??
NULL,
379 'supplemental_address_1' => $billingAddress['supplemental_address_1'] ??
NULL,
380 'supplemental_address_2' => $billingAddress['supplemental_address_2'] ??
NULL,
381 'supplemental_address_3' => $billingAddress['supplemental_address_3'] ??
NULL,
382 'city' => $billingAddress['city'] ??
NULL,
383 'postal_code' => $billingAddress['postal_code'] ??
NULL,
384 'state_province' => $billingAddress['state_province'] ??
NULL,
385 'state_province_abbreviation' => $billingAddress['state_province_abbreviation'] ??
NULL,
386 // Kept for backwards compatibility
387 'stateProvinceAbbreviation' => $billingAddress['state_province_abbreviation'] ??
NULL,
388 'country' => $billingAddress['country'] ??
NULL,
389 'is_pay_later' => $contribution->is_pay_later
,
390 'organization_name' => $contribution->_relatedObjects
['contact']->organization_name
,
391 'domain_organization' => $domain->name
,
392 'domain_street_address' => CRM_Utils_Array
::value('street_address', CRM_Utils_Array
::value('1', $locationDefaults['address'])),
393 'domain_supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1', CRM_Utils_Array
::value('1', $locationDefaults['address'])),
394 'domain_supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2', CRM_Utils_Array
::value('1', $locationDefaults['address'])),
395 'domain_supplemental_address_3' => CRM_Utils_Array
::value('supplemental_address_3', CRM_Utils_Array
::value('1', $locationDefaults['address'])),
396 'domain_city' => CRM_Utils_Array
::value('city', CRM_Utils_Array
::value('1', $locationDefaults['address'])),
397 'domain_postal_code' => CRM_Utils_Array
::value('postal_code', CRM_Utils_Array
::value('1', $locationDefaults['address'])),
398 'domain_state' => $stateProvinceAbbreviationDomain,
399 'domain_country' => $countryDomain,
400 'domain_email' => CRM_Utils_Array
::value('email', CRM_Utils_Array
::value('1', $locationDefaults['email'])),
401 'domain_phone' => CRM_Utils_Array
::value('phone', CRM_Utils_Array
::value('1', $locationDefaults['phone'])),
404 if (isset($creditNoteId)) {
405 $tplParams['creditnote_id'] = $creditNoteId;
408 $pdfFileName = $contribution->invoice_number
. ".pdf";
409 $sendTemplateParams = [
410 'groupName' => 'msg_tpl_workflow_contribution',
411 'valueName' => 'contribution_invoice_receipt',
412 'contactId' => $contribution->contact_id
,
413 'tplParams' => $tplParams,
414 'PDFFilename' => $pdfFileName,
417 // from email address
418 $fromEmailAddress = $params['from_email_address'] ??
NULL;
420 // condition to check for download PDF Invoice or email Invoice
421 if ($invoiceElements['createPdf']) {
422 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
423 if (isset($params['forPage'])) {
428 'subject' => $subject,
433 $messageInvoice[] = $mail['html'];
436 $messageInvoice[] = nl2br($mail['body']);
440 elseif ($contribution->_component
== 'contribute') {
441 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contribution->contact_id
);
443 $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]);
444 $sendTemplateParams['from'] = $fromEmailAddress;
445 $sendTemplateParams['toEmail'] = $email;
446 $sendTemplateParams['cc'] = $values['cc_receipt'] ??
NULL;
447 $sendTemplateParams['bcc'] = $values['bcc_receipt'] ??
NULL;
449 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
450 // functions call for adding activity with attachment
451 $fileName = self
::putFile($html, $pdfFileName);
452 self
::addActivities($subject, $contribution->contact_id
, $fileName, $params, $contribution->id
);
454 elseif ($contribution->_component
== 'event') {
455 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contribution->contact_id
);
457 $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]);
458 $sendTemplateParams['from'] = $fromEmailAddress;
459 $sendTemplateParams['toEmail'] = $email;
460 $sendTemplateParams['cc'] = $values['cc_confirm'] ??
NULL;
461 $sendTemplateParams['bcc'] = $values['bcc_confirm'] ??
NULL;
463 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
464 // functions call for adding activity with attachment
465 $fileName = self
::putFile($html, $pdfFileName);
466 self
::addActivities($subject, $contribution->contact_id
, $fileName, $params, $contribution->id
);
468 $invoiceTemplate->clearTemplateVars();
471 if ($invoiceElements['createPdf']) {
472 if (isset($params['forPage'])) {
476 CRM_Utils_PDF_Utils
::html2pdf($messageInvoice, $pdfFileName, FALSE, [
481 // functions call for adding activity with attachment
482 $fileName = self
::putFile($html, $pdfFileName);
483 self
::addActivities($subject, $contactIds, $fileName, $params);
485 CRM_Utils_System
::civiExit();
489 if ($invoiceElements['suppressedEmails']) {
490 $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).', [1 => $invoiceElements['suppressedEmails']]);
491 $msgTitle = ts('Email Error');
495 $status = ts('Your mail has been sent.');
496 $msgTitle = ts('Sent');
497 $msgType = 'success';
499 CRM_Core_Session
::setStatus($status, $msgTitle, $msgType);
504 * Add activity for Email Invoice and the PDF Invoice.
506 * @param string $subject
508 * @param array $contactIds
510 * @param string $fileName
511 * Gives the location with name of the file.
512 * @param array $params
514 * @param int $contributionId
518 public static function addActivities($subject, $contactIds, $fileName, $params, $contributionId = NULL) {
519 $session = CRM_Core_Session
::singleton();
520 $userID = $session->get('userID');
521 $config = CRM_Core_Config
::singleton();
522 $config->doNotAttachPDFReceipt
= 1;
524 if (!empty($params['output']) && $params['output'] == 'pdf_invoice') {
525 $activityType = 'Downloaded Invoice';
528 $activityType = 'Emailed Invoice';
532 'subject' => $subject,
533 'source_contact_id' => $userID,
534 'target_contact_id' => $contactIds,
535 'activity_type_id' => $activityType,
536 'activity_date_time' => date('YmdHis'),
539 'type' => 'application/pdf',
540 'location' => $fileName,
541 'upload_date' => date('YmdHis'),
544 if ($contributionId) {
545 $activityParams['source_record_id'] = $contributionId;
547 civicrm_api3('Activity', 'create', $activityParams);
551 * Create the Invoice file in upload folder for attachment.
553 * @param string $html
554 * Content for pdf in html format.
556 * @param string $name
559 * Name of file which is in pdf format
561 public static function putFile($html, $name = 'Invoice.pdf') {
562 $options = new Options();
563 $options->set('isRemoteEnabled', TRUE);
565 $doc = new DOMPDF($options);
566 $doc->load_html($html);
568 $html = $doc->output();
569 $config = CRM_Core_Config
::singleton();
570 $fileName = $config->uploadDir
. $name;
571 file_put_contents($fileName, $html);
576 * Callback to perform action on Print Invoice button.
578 public static function getPrintPDF() {
579 $contributionId = CRM_Utils_Request
::retrieve('id', 'Positive', CRM_Core_DAO
::$_nullObject, FALSE);
580 $contributionIDs = [$contributionId];
581 $contactId = CRM_Utils_Request
::retrieve('cid', 'Positive', CRM_Core_DAO
::$_nullObject, FALSE);
582 $params = ['output' => 'pdf_invoice'];
583 CRM_Contribute_Form_Task_Invoice
::printPDF($contributionIDs, $params, $contactId);