Skip to content

Commit

Permalink
Merge pull request #24317 from eileenmcnaughton/elements_inv
Browse files Browse the repository at this point in the history
[REF] Clarify what is being used from getElements, remove unused
  • Loading branch information
colemanw authored Aug 22, 2022
2 parents 70c4dfa + f436aaf commit 5a90d51
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
29 changes: 20 additions & 9 deletions CRM/Contribute/Form/Task/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -512,7 +523,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
$invoiceTemplate->clearTemplateVars();
}

if ($invoiceElements['createPdf']) {
if ($isCreatePDF) {
if (isset($params['forPage'])) {
return $html;
}
Expand All @@ -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';
}
Expand Down
51 changes: 23 additions & 28 deletions CRM/Contribute/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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'];
Expand All @@ -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';
}
Expand All @@ -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],
Expand Down

0 comments on commit 5a90d51

Please sign in to comment.