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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to email a group of
22 class CRM_Contribute_Form_Task_PDF
extends CRM_Contribute_Form_Task
{
25 * Are we operating in "single mode", i.e. updating the task of only
26 * one specific contribution?
30 public $_single = FALSE;
35 * Build all the data structures needed to build the form.
37 public function preProcess() {
38 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
43 $this->_contributionIds
= [$id];
44 $this->_componentClause
= " civicrm_contribution.id IN ( $id ) ";
45 $this->_single
= TRUE;
46 $this->assign('totalSelectedContributions', 1);
52 // check that all the contribution ids have pending status
55 FROM civicrm_contribution
56 WHERE contribution_status_id != 1
57 AND {$this->_componentClause}";
58 $count = CRM_Core_DAO
::singleValueQuery($query);
60 CRM_Core_Error
::statusBounce("Please select only online contributions with Completed status.");
63 $this->assign('single', $this->_single
);
65 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
66 $urlParams = 'force=1';
67 if (CRM_Utils_Rule
::qfKey($qfKey)) {
68 $urlParams .= "&qfKey=$qfKey";
71 $url = CRM_Utils_System
::url('civicrm/contribute/search', $urlParams);
75 'title' => ts('Search Results'),
78 CRM_Contact_Form_Task_EmailCommon
::preProcessFromAddress($this, FALSE);
79 // we have all the contribution ids, so now we get the contact ids
80 parent
::setContactIDs();
81 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
82 CRM_Utils_System
::setTitle(ts('Print Contribution Receipts'));
86 * Build the form object.
88 public function buildQuickForm() {
90 $this->addElement('radio', 'output', NULL, ts('Email Receipts'), 'email_receipt',
92 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'none';
93 document.getElementById('selectEmailFrom').style.display = 'block';",
96 $this->addElement('radio', 'output', NULL, ts('PDF Receipts'), 'pdf_receipt',
98 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'block';
99 document.getElementById('selectEmailFrom').style.display = 'none';",
102 $this->addRule('output', ts('Selection required'), 'required');
104 $this->add('select', 'pdf_format_id', ts('Page Format'),
105 [0 => ts('- default -')] + CRM_Core_BAO_PdfFormat
::getList(TRUE)
107 $this->add('checkbox', 'receipt_update', ts('Update receipt dates for these contributions'), FALSE);
108 $this->add('checkbox', 'override_privacy', ts('Override privacy setting? (Do not email / Do not mail)'), FALSE);
110 $this->add('select', 'from_email_address', ts('From Email'), $this->_fromEmails
, FALSE);
115 'name' => ts('Process Receipt(s)'),
120 'name' => ts('Cancel'),
126 * Set default values.
128 public function setDefaultValues() {
129 $defaultFormat = CRM_Core_BAO_PdfFormat
::getDefaultValues();
130 return ['pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0];
134 * Process the form after the input has been submitted and validated.
136 public function postProcess() {
137 // get all the details needed to generate a receipt
139 $template = CRM_Core_Smarty
::singleton();
141 $params = $this->controller
->exportValues($this->_name
);
142 $elements = self
::getElements($this->_contributionIds
, $params, $this->_contactIds
);
144 foreach ($elements['details'] as $contribID => $detail) {
145 $input = $ids = $objects = [];
147 if (in_array($detail['contact'], $elements['excludeContactIds'])) {
151 $input['component'] = $detail['component'];
153 $ids['contact'] = $detail['contact'];
154 $ids['contribution'] = $contribID;
155 $ids['contributionRecur'] = NULL;
156 $ids['contributionPage'] = NULL;
157 $ids['membership'] = $detail['membership'] ??
NULL;
158 $ids['participant'] = $detail['participant'] ??
NULL;
159 $ids['event'] = $detail['event'] ??
NULL;
161 if (!$elements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
162 throw new CRM_Core_Exception('invalid data');
165 $contribution = &$objects['contribution'];
167 // set some fake input values so we can reuse IPN code
168 $input['amount'] = $contribution->total_amount
;
169 $input['is_test'] = $contribution->is_test
;
170 $input['fee_amount'] = $contribution->fee_amount
;
171 $input['net_amount'] = $contribution->net_amount
;
172 $input['trxn_id'] = $contribution->trxn_id
;
173 $input['trxn_date'] = $contribution->trxn_date ??
NULL;
174 $input['receipt_update'] = $params['receipt_update'];
175 $input['contribution_status_id'] = $contribution->contribution_status_id
;
176 $input['paymentProcessor'] = empty($contribution->trxn_id
) ?
NULL :
177 CRM_Core_DAO
::singleValueQuery("SELECT payment_processor_id
178 FROM civicrm_financial_trxn
181 1 => [$contribution->trxn_id
, 'String'],
184 // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
185 $objects['contribution']->receive_date
= CRM_Utils_Date
::isoToMysql($objects['contribution']->receive_date
);
187 if (isset($params['from_email_address']) && !$elements['createPdf']) {
188 // If a logged in user from email is used rather than a domain wide from email address
189 // the from_email_address params key will be numerical and we need to convert it to be
190 // in normal from email format
191 $from = CRM_Utils_Mail
::formatFromAddress($params['from_email_address']);
192 // CRM-19129 Allow useres the choice of From Email to send the receipt from.
193 $fromDetails = explode(' <', $from);
194 $input['receipt_from_email'] = substr(trim($fromDetails[1]), 0, -1);
195 $input['receipt_from_name'] = str_replace('"', '', $fromDetails[0]);
198 $mail = CRM_Contribute_BAO_Contribution
::sendMail($input, $ids, $objects['contribution']->id
, $elements['createPdf']);
201 $message[] = $mail['html'];
204 $message[] = nl2br($mail['body']);
207 // reset template values before processing next transactions
208 $template->clearTemplateVars();
211 if ($elements['createPdf']) {
212 CRM_Utils_PDF_Utils
::html2pdf($message,
213 'civicrmContributionReceipt.pdf',
215 $elements['params']['pdf_format_id']
217 CRM_Utils_System
::civiExit();
220 if ($elements['suppressedEmails']) {
221 $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']]);
222 $msgTitle = ts('Email Error');
226 $status = ts('Your mail has been sent.');
227 $msgTitle = ts('Sent');
228 $msgType = 'success';
230 CRM_Core_Session
::setStatus($status, $msgTitle, $msgType);
235 * Declaration of common variables for Invoice and PDF.
238 * @param array $contribIds
240 * @param array $params
241 * Parameter for pdf or email invoices.
242 * @param array $contactIds
246 * array of common elements
249 public static function getElements($contribIds, $params, $contactIds) {
252 $pdfElements['contribIDs'] = implode(',', $contribIds);
254 $pdfElements['details'] = self
::getDetails($pdfElements['contribIDs']);
256 $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();
258 $pdfElements['params'] = $params;
260 $pdfElements['createPdf'] = FALSE;
261 if (!empty($pdfElements['params']['output']) &&
262 ($pdfElements['params']['output'] == "pdf_invoice" ||
$pdfElements['params']['output'] == "pdf_receipt")
264 $pdfElements['createPdf'] = TRUE;
267 $excludeContactIds = [];
268 if (!$pdfElements['createPdf']) {
269 $returnProperties = [
276 list($contactDetails) = CRM_Utils_Token
::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE);
277 $pdfElements['suppressedEmails'] = 0;
278 $suppressedEmails = 0;
279 foreach ($contactDetails as $id => $values) {
280 if (empty($values['email']) ||
281 (empty($params['override_privacy']) && !empty($values['do_not_email']))
282 ||
!empty($values['is_deceased'])
283 ||
!empty($values['on_hold'])
286 $pdfElements['suppressedEmails'] = $suppressedEmails;
287 $excludeContactIds[] = $values['contact_id'];
291 $pdfElements['excludeContactIds'] = $excludeContactIds;
297 * @param string $contributionIDs
301 private static function getDetails($contributionIDs) {
302 if (empty($contributionIDs)) {
306 SELECT c.id as contribution_id,
307 c.contact_id as contact_id ,
308 mp.membership_id as membership_id ,
309 pp.participant_id as participant_id ,
310 p.event_id as event_id
311 FROM civicrm_contribution c
312 LEFT JOIN civicrm_membership_payment mp ON mp.contribution_id = c.id
313 LEFT JOIN civicrm_participant_payment pp ON pp.contribution_id = c.id
314 LEFT JOIN civicrm_participant p ON pp.participant_id = p.id
315 WHERE c.id IN ( $contributionIDs )";
318 $dao = CRM_Core_DAO
::executeQuery($query);
320 while ($dao->fetch()) {
321 $rows[$dao->contribution_id
]['component'] = $dao->participant_id ?
'event' : 'contribute';
322 $rows[$dao->contribution_id
]['contact'] = $dao->contact_id
;
323 if ($dao->membership_id
) {
324 if (!array_key_exists('membership', $rows[$dao->contribution_id
])) {
325 $rows[$dao->contribution_id
]['membership'] = [];
327 $rows[$dao->contribution_id
]['membership'][] = $dao->membership_id
;
329 if ($dao->participant_id
) {
330 $rows[$dao->contribution_id
]['participant'] = $dao->participant_id
;
332 if ($dao->event_id
) {
333 $rows[$dao->contribution_id
]['event'] = $dao->event_id
;