diff --git a/CRM/Contribute/Form/Task/Invoice.php b/CRM/Contribute/Form/Task/Invoice.php index a4b7963e4fef..94c6433252cb 100644 --- a/CRM/Contribute/Form/Task/Invoice.php +++ b/CRM/Contribute/Form/Task/Invoice.php @@ -214,8 +214,19 @@ public function postProcess() { public static function printPDF($contribIDs, &$params, $contactIds) { // get all the details needed to generate a invoice $messageInvoice = []; + $isCreatePDF = FALSE; + if (!empty($params['output']) && + ($params['output'] === 'pdf_invoice' || $params['output'] === 'pdf_receipt') + ) { + $isCreatePDF = TRUE; + } + $invoiceTemplate = CRM_Core_Smarty::singleton(); - $invoiceElements = CRM_Contribute_Form_Task_PDF::getElements($contribIDs, $params, $contactIds); + $invoiceElements = CRM_Contribute_Form_Task_PDF::getElements($contribIDs, $params, $contactIds, $isCreatePDF); + $elementDetails = $invoiceElements['details']; + $excludedContactIDs = $invoiceElements['excludeContactIds']; + $suppressedEmails = $isCreatePDF ? NULL : $invoiceElements['suppressedEmails']; + unset($invoiceElements); // gives the status id when contribution status is 'Refunded' $contributionStatusID = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); @@ -224,9 +235,9 @@ public static function printPDF($contribIDs, &$params, $contactIds) { $pendingStatusId = CRM_Utils_Array::key('Pending', $contributionStatusID); $pdfFormat = CRM_Core_BAO_PdfFormat::getByName('default_invoice_pdf_format'); - foreach ($invoiceElements['details'] as $contributionID => $detail) { + foreach ($elementDetails as $contributionID => $detail) { $input = $ids = []; - if (in_array($detail['contact'], $invoiceElements['excludeContactIds'])) { + if (in_array($detail['contact'], $excludedContactIDs)) { continue; } @@ -461,7 +472,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) { } // condition to check for download PDF Invoice or email Invoice - if ($invoiceElements['createPdf']) { + if ($isCreatePDF) { [$sent, $subject, $message, $html] = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); if (isset($params['forPage'])) { return $html; @@ -483,7 +494,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) { elseif ($component === 'contribute') { $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id); - $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]); + $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $params['email_comment']]); $sendTemplateParams['from'] = $fromEmailAddress; $sendTemplateParams['toEmail'] = $email; $sendTemplateParams['cc'] = $values['cc_receipt'] ?? NULL; @@ -498,7 +509,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) { elseif ($component == 'event') { $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id); - $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]); + $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $params['email_comment']]); $sendTemplateParams['from'] = $fromEmailAddress; $sendTemplateParams['toEmail'] = $email; $sendTemplateParams['cc'] = $values['cc_confirm'] ?? NULL; @@ -512,7 +523,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) { $invoiceTemplate->clearTemplateVars(); } - if ($invoiceElements['createPdf']) { + if ($isCreatePDF) { if (isset($params['forPage'])) { return $html; } @@ -526,8 +537,8 @@ public static function printPDF($contribIDs, &$params, $contactIds) { } } else { - if ($invoiceElements['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).', [1 => $invoiceElements['suppressedEmails']]); + 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).', [1 => $suppressedEmails]); $msgTitle = ts('Email Error'); $msgType = 'error'; } diff --git a/CRM/Contribute/Form/Task/PDF.php b/CRM/Contribute/Form/Task/PDF.php index d81f5d7deb31..e581b9f3c26e 100644 --- a/CRM/Contribute/Form/Task/PDF.php +++ b/CRM/Contribute/Form/Task/PDF.php @@ -127,12 +127,22 @@ public function postProcess() { $template = CRM_Core_Smarty::singleton(); $params = $this->controller->exportValues($this->_name); - $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds); + $isCreatePDF = FALSE; + if (!empty($params['output']) && + ($params['output'] === 'pdf_invoice' || $params['output'] === 'pdf_receipt') + ) { + $isCreatePDF = TRUE; + } + $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds, $isCreatePDF); + $elementDetails = $elements['details']; + $excludedContactIDs = $elements['excludeContactIds']; + $suppressedEmails = $elements['suppressedEmails']; - foreach ($elements['details'] as $contribID => $detail) { + unset($elements); + foreach ($elementDetails as $contribID => $detail) { $input = $ids = []; - if (in_array($detail['contact'], $elements['excludeContactIds'])) { + if (in_array($detail['contact'], $excludedContactIDs)) { continue; } // @todo - CRM_Contribute_BAO_Contribution::sendMail re-does pretty much everything between here & when we call it. @@ -167,7 +177,7 @@ public function postProcess() { 1 => [$contribution->trxn_id, 'String'], ]); - if (isset($params['from_email_address']) && !$elements['createPdf']) { + if (isset($params['from_email_address']) && !$isCreatePDF) { // If a logged in user from email is used rather than a domain wide from email address // the from_email_address params key will be numerical and we need to convert it to be // in normal from email format @@ -178,7 +188,7 @@ public function postProcess() { $input['receipt_from_name'] = str_replace('"', '', $fromDetails[0]); } - $mail = CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contribID, $elements['createPdf']); + $mail = CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contribID, $isCreatePDF); if (!empty($mail['html'])) { $message[] = $mail['html']; @@ -191,17 +201,17 @@ public function postProcess() { $template->clearTemplateVars(); } - if ($elements['createPdf']) { + if ($isCreatePDF) { CRM_Utils_PDF_Utils::html2pdf($message, 'receipt.pdf', FALSE, - $elements['params']['pdf_format_id'] + $params['pdf_format_id'] ); CRM_Utils_System::civiExit(); } else { - 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).', [1 => $elements['suppressedEmails']]); + 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).', [1 => $suppressedEmails]); $msgTitle = ts('Email Error'); $msgType = 'error'; } @@ -217,39 +227,24 @@ public function postProcess() { /** * Declaration of common variables for Invoice and PDF. * - * * @param array $contribIds * Contribution Id. * @param array $params * Parameter for pdf or email invoices. * @param array $contactIds * Contact Id. + * @param bool $isCreatePDF * * @return array * array of common elements * * @throws \CiviCRM_API3_Exception */ - public static function getElements($contribIds, $params, $contactIds) { + public static function getElements(array $contribIds, array $params, array $contactIds, bool $isCreatePDF): array { $pdfElements = []; - - $pdfElements['contribIDs'] = implode(',', $contribIds); - - $pdfElements['details'] = self::getDetails($pdfElements['contribIDs']); - - $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN(); - - $pdfElements['params'] = $params; - - $pdfElements['createPdf'] = FALSE; - if (!empty($pdfElements['params']['output']) && - ($pdfElements['params']['output'] === 'pdf_invoice' || $pdfElements['params']['output'] === 'pdf_receipt') - ) { - $pdfElements['createPdf'] = TRUE; - } - + $pdfElements['details'] = self::getDetails(implode(',', $contribIds)); $excludeContactIds = []; - if (!$pdfElements['createPdf']) { + if (!$isCreatePDF) { $contactDetails = civicrm_api3('Contact', 'get', [ 'return' => ['email', 'do_not_email', 'is_deceased', 'on_hold'], 'id' => ['IN' => $contactIds],