Add CC as EntityRef Field, Correct Layout of Email invoice, same as downloaded invoic...
[civicrm-core.git] / CRM / Contribute / Form / Task / Invoice.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 use Civi\Api4\Email;
19 /**
20 * This class provides the functionality to email a group of
21 * contacts.
22 */
23 class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
24 /**
25 * Are we operating in "single mode", i.e. updating the task of only
26 * one specific contribution?
27 *
28 * @var bool
29 */
30 public $_single = FALSE;
31
32 /**
33 * Gives all the statues for conribution.
34 * @var int
35 */
36 public $_contributionStatusId;
37
38 /**
39 * Gives the HTML template of PDF Invoice.
40 * @var string
41 */
42 public $_messageInvoice;
43
44 /**
45 * This variable is used to assign parameters for HTML template of PDF Invoice.
46 * @var string
47 */
48 public $_invoiceTemplate;
49
50 /**
51 * Selected output.
52 * @var string
53 */
54 public $_selectedOutput;
55
56 /**
57 * Build all the data structures needed to build the form.
58 */
59 public function preProcess() {
60 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
61 if ($id) {
62 $this->_contributionIds = [$id];
63 $this->_componentClause = " civicrm_contribution.id IN ( $id ) ";
64 $this->_single = TRUE;
65 $this->assign('totalSelectedContributions', 1);
66
67 // set the redirection after actions
68 $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE);
69 $url = CRM_Utils_System::url('civicrm/contact/view/contribution',
70 "action=view&reset=1&id={$id}&cid={$contactId}&context=contribution&selectedChild=contribute"
71 );
72
73 CRM_Core_Session::singleton()->pushUserContext($url);
74 }
75 else {
76 parent::preProcess();
77 }
78
79 // check that all the contribution ids have status Completed, Pending, Refunded, or Partially Paid.
80 $this->_contributionStatusId = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
81 $status = ['Completed', 'Pending', 'Refunded', 'Partially paid'];
82 $statusId = [];
83 foreach ($this->_contributionStatusId as $key => $value) {
84 if (in_array($value, $status)) {
85 $statusId[] = $key;
86 }
87 }
88 $Id = implode(",", $statusId);
89 $query = "SELECT count(*) FROM civicrm_contribution WHERE contribution_status_id NOT IN ($Id) AND {$this->_componentClause}";
90 $count = CRM_Core_DAO::singleValueQuery($query);
91 if ($count != 0) {
92 CRM_Core_Error::statusBounce(ts('Please select only contributions with Completed, Pending, Refunded, or Partially Paid status.'));
93 }
94
95 // we have all the contribution ids, so now we get the contact ids
96 parent::setContactIDs();
97 $this->assign('single', $this->_single);
98
99 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
100 $urlParams = 'force=1';
101 if (CRM_Utils_Rule::qfKey($qfKey)) {
102 $urlParams .= "&qfKey=$qfKey";
103 }
104
105 $url = CRM_Utils_System::url('civicrm/contribute/search', $urlParams);
106 $breadCrumb = [
107 [
108 'url' => $url,
109 'title' => ts('Search Results'),
110 ],
111 ];
112
113 CRM_Utils_System::appendBreadCrumb($breadCrumb);
114
115 $this->_selectedOutput = CRM_Utils_Request::retrieve('select', 'String', $this);
116 $this->assign('selectedOutput', $this->_selectedOutput);
117
118 CRM_Contact_Form_Task_EmailCommon::preProcessFromAddress($this);
119 if ($this->_selectedOutput == 'email') {
120 CRM_Utils_System::setTitle(ts('Email Invoice'));
121 }
122 else {
123 CRM_Utils_System::setTitle(ts('Print Contribution Invoice'));
124 }
125 }
126
127 /**
128 * Build the form object.
129 */
130 public function buildQuickForm() {
131 $this->preventAjaxSubmit();
132 if (CRM_Core_Permission::check('administer CiviCRM')) {
133 $this->assign('isAdmin', 1);
134 }
135
136 $this->add('select', 'from_email_address', ts('From'), $this->_fromEmails, TRUE);
137 if ($this->_selectedOutput != 'email') {
138 $this->addElement('radio', 'output', NULL, ts('Email Invoice'), 'email_invoice');
139 $this->addElement('radio', 'output', NULL, ts('PDF Invoice'), 'pdf_invoice');
140 $this->addRule('output', ts('Selection required'), 'required');
141 $this->addFormRule(['CRM_Contribute_Form_Task_Invoice', 'formRule']);
142 }
143 else {
144 $this->addRule('from_email_address', ts('From Email Address is required'), 'required');
145 }
146
147 $attributes = ['class' => 'huge'];
148 $this->addEntityRef('cc_id', ts('CC'), [
149 'entity' => 'Email',
150 'multiple' => TRUE,
151 ]);
152 $this->add('text', 'subject', ts('Subject'), $attributes + ['placeholder' => ts('Optional')]);
153 $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.)'), [
154 'rows' => 2,
155 'cols' => 40,
156 ]);
157
158 $this->addButtons([
159 [
160 'type' => 'upload',
161 'name' => $this->_selectedOutput == 'email' ? ts('Send Email') : ts('Process Invoice(s)'),
162 'isDefault' => TRUE,
163 ],
164 [
165 'type' => 'cancel',
166 'name' => ts('Cancel'),
167 ],
168 ]);
169 }
170
171 /**
172 * Global validation rules for the form.
173 *
174 * @param array $values
175 *
176 * @return array
177 * list of errors to be posted back to the form
178 */
179 public static function formRule($values) {
180 $errors = [];
181
182 if ($values['output'] == 'email_invoice' && empty($values['from_email_address'])) {
183 $errors['from_email_address'] = ts("From Email Address is required");
184 }
185
186 return $errors;
187 }
188
189 /**
190 * Process the form after the input has been submitted and validated.
191 */
192 public function postProcess() {
193 $params = $this->controller->exportValues($this->_name);
194 self::printPDF($this->_contributionIds, $params, $this->_contactIds);
195 }
196
197 /**
198 * Process the PDf and email with activity and attachment on click of Print Invoices.
199 *
200 * @param array $contribIDs
201 * Contribution Id.
202 * @param array $params
203 * Associated array of submitted values.
204 * @param array $contactIds
205 * Contact Id.
206 */
207 public static function printPDF($contribIDs, &$params, $contactIds) {
208 // get all the details needed to generate a invoice
209 $messageInvoice = [];
210 $invoiceTemplate = CRM_Core_Smarty::singleton();
211 $invoiceElements = CRM_Contribute_Form_Task_PDF::getElements($contribIDs, $params, $contactIds);
212
213 // gives the status id when contribution status is 'Refunded'
214 $contributionStatusID = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
215 $refundedStatusId = CRM_Utils_Array::key('Refunded', $contributionStatusID);
216 $cancelledStatusId = CRM_Utils_Array::key('Cancelled', $contributionStatusID);
217 $pendingStatusId = CRM_Utils_Array::key('Pending', $contributionStatusID);
218
219 foreach ($invoiceElements['details'] as $contribID => $detail) {
220 $input = $ids = $objects = [];
221 if (in_array($detail['contact'], $invoiceElements['excludeContactIds'])) {
222 continue;
223 }
224
225 $input['component'] = $detail['component'];
226
227 $ids['contact'] = $detail['contact'];
228 $ids['contribution'] = $contribID;
229 $ids['contributionRecur'] = NULL;
230 $ids['contributionPage'] = NULL;
231 $ids['membership'] = $detail['membership'] ?? NULL;
232 $ids['participant'] = $detail['participant'] ?? NULL;
233 $ids['event'] = $detail['event'] ?? NULL;
234
235 if (!$invoiceElements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
236 CRM_Core_Error::statusBounce('Supplied data was not able to be validated');
237 }
238
239 $contribution = &$objects['contribution'];
240
241 $input['amount'] = $contribution->total_amount;
242 $input['invoice_id'] = $contribution->invoice_id;
243 $input['receive_date'] = $contribution->receive_date;
244 $input['contribution_status_id'] = $contribution->contribution_status_id;
245 $input['organization_name'] = $contribution->_relatedObjects['contact']->organization_name;
246
247 $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
248
249 // Fetch the billing address. getValues should prioritize the billing
250 // address, otherwise will return the primary address.
251 $billingAddress = [];
252
253 $addressDetails = CRM_Core_BAO_Address::getValues([
254 'contact_id' => $contribution->contact_id,
255 'is_billing' => 1,
256 ]);
257
258 if (!empty($addressDetails)) {
259 $billingAddress = array_shift($addressDetails);
260 }
261
262 if ($contribution->contribution_status_id == $refundedStatusId || $contribution->contribution_status_id == $cancelledStatusId) {
263 $creditNoteId = $contribution->creditnote_id;
264 }
265 if (!$contribution->invoice_number) {
266 $contribution->invoice_number = CRM_Contribute_BAO_Contribution::getInvoiceNumber($contribution->id);
267 }
268
269 //to obtain due date for PDF invoice
270 $contributionReceiveDate = date('F j,Y', strtotime(date($input['receive_date'])));
271 $invoiceDate = date("F j, Y");
272 $dueDateSetting = Civi::settings()->get('invoice_due_date');
273 $dueDatePeriodSetting = Civi::settings()->get('invoice_due_date_period');
274 $dueDate = date('F j, Y', strtotime($contributionReceiveDate . "+" . $dueDateSetting . "" . $dueDatePeriodSetting));
275
276 $amountPaid = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribID, TRUE);
277 $amountDue = ($input['amount'] - $amountPaid);
278
279 // retrieving the subtotal and sum of same tax_rate
280 $dataArray = [];
281 $subTotal = 0;
282 $lineItem = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribID);
283 foreach ($lineItem as $taxRate) {
284 if (isset($dataArray[(string) $taxRate['tax_rate']])) {
285 $dataArray[(string) $taxRate['tax_rate']] = $dataArray[(string) $taxRate['tax_rate']] + CRM_Utils_Array::value('tax_amount', $taxRate);
286 }
287 else {
288 $dataArray[(string) $taxRate['tax_rate']] = $taxRate['tax_amount'] ?? NULL;
289 }
290 $subTotal += CRM_Utils_Array::value('subTotal', $taxRate);
291 }
292
293 // to email the invoice
294 $mailDetails = [];
295 $values = [];
296 if ($contribution->_component == 'event') {
297 $daoName = 'CRM_Event_DAO_Event';
298 $pageId = $contribution->_relatedObjects['event']->id;
299 $mailElements = [
300 'title',
301 'confirm_from_name',
302 'confirm_from_email',
303 ];
304 CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
305 $values['title'] = $mailDetails[$contribution->_relatedObjects['event']->id]['title'] ?? NULL;
306 $values['confirm_from_name'] = $mailDetails[$contribution->_relatedObjects['event']->id]['confirm_from_name'] ?? NULL;
307 $values['confirm_from_email'] = $mailDetails[$contribution->_relatedObjects['event']->id]['confirm_from_email'] ?? NULL;
308
309 $title = $mailDetails[$contribution->_relatedObjects['event']->id]['title'] ?? NULL;
310 }
311 elseif ($contribution->_component == 'contribute') {
312 $daoName = 'CRM_Contribute_DAO_ContributionPage';
313 $pageId = $contribution->contribution_page_id;
314 $mailElements = [
315 'title',
316 'receipt_from_name',
317 'receipt_from_email',
318 'cc_receipt',
319 'bcc_receipt',
320 ];
321 CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
322
323 $values['title'] = CRM_Utils_Array::value('title', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails));
324 $values['receipt_from_name'] = CRM_Utils_Array::value('receipt_from_name', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails));
325 $values['receipt_from_email'] = CRM_Utils_Array::value('receipt_from_email', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails));
326 $values['cc_receipt'] = CRM_Utils_Array::value('cc_receipt', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails));
327 $values['bcc_receipt'] = CRM_Utils_Array::value('bcc_receipt', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails));
328
329 $title = CRM_Utils_Array::value('title', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails));
330 }
331 $source = $contribution->source;
332
333 $config = CRM_Core_Config::singleton();
334 if (!isset($params['forPage'])) {
335 $config->doNotAttachPDFReceipt = 1;
336 }
337
338 // get organization address
339 $domain = CRM_Core_BAO_Domain::getDomain();
340 $locParams = ['contact_id' => $domain->contact_id];
341 $locationDefaults = CRM_Core_BAO_Location::getValues($locParams);
342 if (isset($locationDefaults['address'][1]['state_province_id'])) {
343 $stateProvinceAbbreviationDomain = CRM_Core_PseudoConstant::stateProvinceAbbreviation($locationDefaults['address'][1]['state_province_id']);
344 }
345 else {
346 $stateProvinceAbbreviationDomain = '';
347 }
348 if (isset($locationDefaults['address'][1]['country_id'])) {
349 $countryDomain = CRM_Core_PseudoConstant::country($locationDefaults['address'][1]['country_id']);
350 }
351 else {
352 $countryDomain = '';
353 }
354
355 $invoiceNotes = Civi::settings()->get('invoice_notes') ?? NULL;
356
357 // parameters to be assign for template
358 $tplParams = [
359 'title' => $title,
360 'component' => $input['component'],
361 'id' => $contribution->id,
362 'source' => $source,
363 'invoice_number' => $contribution->invoice_number,
364 'invoice_id' => $contribution->invoice_id,
365 'resourceBase' => $config->userFrameworkResourceURL,
366 'defaultCurrency' => $config->defaultCurrency,
367 'amount' => $contribution->total_amount,
368 'amountDue' => $amountDue,
369 'amountPaid' => $amountPaid,
370 'invoice_date' => $invoiceDate,
371 'dueDate' => $dueDate,
372 'notes' => $invoiceNotes,
373 'display_name' => $contribution->_relatedObjects['contact']->display_name,
374 'lineItem' => $lineItem,
375 'dataArray' => $dataArray,
376 'refundedStatusId' => $refundedStatusId,
377 'pendingStatusId' => $pendingStatusId,
378 'cancelledStatusId' => $cancelledStatusId,
379 'contribution_status_id' => $contribution->contribution_status_id,
380 'contributionStatusName' => CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution->contribution_status_id),
381 'subTotal' => $subTotal,
382 'street_address' => $billingAddress['street_address'] ?? NULL,
383 'supplemental_address_1' => $billingAddress['supplemental_address_1'] ?? NULL,
384 'supplemental_address_2' => $billingAddress['supplemental_address_2'] ?? NULL,
385 'supplemental_address_3' => $billingAddress['supplemental_address_3'] ?? NULL,
386 'city' => $billingAddress['city'] ?? NULL,
387 'postal_code' => $billingAddress['postal_code'] ?? NULL,
388 'state_province' => $billingAddress['state_province'] ?? NULL,
389 'state_province_abbreviation' => $billingAddress['state_province_abbreviation'] ?? NULL,
390 // Kept for backwards compatibility
391 'stateProvinceAbbreviation' => $billingAddress['state_province_abbreviation'] ?? NULL,
392 'country' => $billingAddress['country'] ?? NULL,
393 'is_pay_later' => $contribution->is_pay_later,
394 'organization_name' => $contribution->_relatedObjects['contact']->organization_name,
395 'domain_organization' => $domain->name,
396 'domain_street_address' => CRM_Utils_Array::value('street_address', CRM_Utils_Array::value('1', $locationDefaults['address'])),
397 'domain_supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', CRM_Utils_Array::value('1', $locationDefaults['address'])),
398 'domain_supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', CRM_Utils_Array::value('1', $locationDefaults['address'])),
399 'domain_supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', CRM_Utils_Array::value('1', $locationDefaults['address'])),
400 'domain_city' => CRM_Utils_Array::value('city', CRM_Utils_Array::value('1', $locationDefaults['address'])),
401 'domain_postal_code' => CRM_Utils_Array::value('postal_code', CRM_Utils_Array::value('1', $locationDefaults['address'])),
402 'domain_state' => $stateProvinceAbbreviationDomain,
403 'domain_country' => $countryDomain,
404 'domain_email' => CRM_Utils_Array::value('email', CRM_Utils_Array::value('1', $locationDefaults['email'])),
405 'domain_phone' => CRM_Utils_Array::value('phone', CRM_Utils_Array::value('1', $locationDefaults['phone'])),
406 ];
407
408 if (isset($creditNoteId)) {
409 $tplParams['creditnote_id'] = $creditNoteId;
410 }
411
412 $pdfFileName = $contribution->invoice_number . ".pdf";
413 $sendTemplateParams = [
414 'groupName' => 'msg_tpl_workflow_contribution',
415 'valueName' => 'contribution_invoice_receipt',
416 'contactId' => $contribution->contact_id,
417 'tplParams' => $tplParams,
418 'PDFFilename' => $pdfFileName,
419 ];
420
421 // from email address
422 $fromEmailAddress = $params['from_email_address'] ?? NULL;
423 if (!empty($params['cc_id'])) {
424 // get contacts and their emails from email id
425 $emailIDs = $params['cc_id'] ? explode(',', $params['cc_id']) : [];
426 $emails = Email::get()
427 ->addWhere('id', 'IN', $emailIDs)
428 ->setCheckPermissions(FALSE)
429 ->setSelect(['contact_id', 'email', 'contact.sort_name', 'contact.display_name'])->execute();
430 $emailStrings = $contactUrlStrings = [];
431 foreach ($emails as $email) {
432 $emailStrings[] = '"' . $email['contact.sort_name'] . '" <' . $email['email'] . '>';
433 // generate the contact url to put in Activity
434 $contactURL = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'force' => 1, 'cid' => $email['contact_id']], TRUE);
435 $contactUrlStrings[] = "<a href='{$contactURL}'>" . $email['contact.display_name'] . '</a>';
436 }
437 $cc_emails = implode(',', $emailStrings);
438 $values['cc_receipt'] = $cc_emails;
439 $ccContactsDetails = implode(',', $contactUrlStrings);
440 // add CC emails as activity details
441 $params['activity_details'] = "\ncc : " . $ccContactsDetails;
442
443 // unset bcc to avoid unknown email come from online page configuration.
444 unset($values['bcc_receipt']);
445 }
446
447 // get subject from UI
448 if (!empty($params['subject'])) {
449 $sendTemplateParams['subject'] = $values['subject'] = $params['subject'];
450 }
451
452 // condition to check for download PDF Invoice or email Invoice
453 if ($invoiceElements['createPdf']) {
454 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
455 if (isset($params['forPage'])) {
456 return $html;
457 }
458 else {
459 $mail = [
460 'subject' => $subject,
461 'body' => $message,
462 'html' => $html,
463 ];
464 if ($mail['html']) {
465 $messageInvoice[] = $mail['html'];
466 }
467 else {
468 $messageInvoice[] = nl2br($mail['body']);
469 }
470 }
471 }
472 elseif ($contribution->_component == 'contribute') {
473 $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id);
474
475 $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]);
476 $sendTemplateParams['from'] = $fromEmailAddress;
477 $sendTemplateParams['toEmail'] = $email;
478 $sendTemplateParams['cc'] = $values['cc_receipt'] ?? NULL;
479 $sendTemplateParams['bcc'] = $values['bcc_receipt'] ?? NULL;
480
481 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
482 // functions call for adding activity with attachment
483 // make sure page layout is same for email and download invoices.
484 $fileName = self::putFile($html, $pdfFileName, [
485 'margin_top' => 10,
486 'margin_left' => 65,
487 'metric' => 'px',
488 ]);
489 self::addActivities($subject, $contribution->contact_id, $fileName, $params, $contribution->id);
490 }
491 elseif ($contribution->_component == 'event') {
492 $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id);
493
494 $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]);
495 $sendTemplateParams['from'] = $fromEmailAddress;
496 $sendTemplateParams['toEmail'] = $email;
497 $sendTemplateParams['cc'] = $values['cc_confirm'] ?? NULL;
498 $sendTemplateParams['bcc'] = $values['bcc_confirm'] ?? NULL;
499
500 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
501 // functions call for adding activity with attachment
502 $fileName = self::putFile($html, $pdfFileName);
503 self::addActivities($subject, $contribution->contact_id, $fileName, $params, $contribution->id);
504 }
505 $invoiceTemplate->clearTemplateVars();
506 }
507
508 if ($invoiceElements['createPdf']) {
509 if (isset($params['forPage'])) {
510 return $html;
511 }
512 else {
513 CRM_Utils_PDF_Utils::html2pdf($messageInvoice, $pdfFileName, FALSE, [
514 'margin_top' => 10,
515 'margin_left' => 65,
516 'metric' => 'px',
517 ]);
518 // functions call for adding activity with attachment
519 $fileName = self::putFile($html, $pdfFileName, [
520 'margin_top' => 10,
521 'margin_left' => 65,
522 'metric' => 'px',
523 ]);
524 self::addActivities($subject, $contactIds, $fileName, $params);
525
526 CRM_Utils_System::civiExit();
527 }
528 }
529 else {
530 if ($invoiceElements['suppressedEmails']) {
531 $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']]);
532 $msgTitle = ts('Email Error');
533 $msgType = 'error';
534 }
535 else {
536 $status = ts('Your mail has been sent.');
537 $msgTitle = ts('Sent');
538 $msgType = 'success';
539 }
540 CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
541 }
542 }
543
544 /**
545 * Add activity for Email Invoice and the PDF Invoice.
546 *
547 * @param string $subject
548 * Activity subject.
549 * @param array $contactIds
550 * Contact Id.
551 * @param string $fileName
552 * Gives the location with name of the file.
553 * @param array $params
554 * For invoices.
555 * @param int $contributionId
556 * Contribution Id.
557 *
558 */
559 public static function addActivities($subject, $contactIds, $fileName, $params, $contributionId = NULL) {
560 $session = CRM_Core_Session::singleton();
561 $userID = $session->get('userID');
562 $config = CRM_Core_Config::singleton();
563 $config->doNotAttachPDFReceipt = 1;
564
565 if (!empty($params['output']) && $params['output'] == 'pdf_invoice') {
566 $activityType = 'Downloaded Invoice';
567 }
568 else {
569 $activityType = 'Emailed Invoice';
570 }
571
572 $activityParams = [
573 'subject' => $subject,
574 'source_contact_id' => $userID,
575 'target_contact_id' => $contactIds,
576 'activity_type_id' => $activityType,
577 'activity_date_time' => date('YmdHis'),
578 'details' => $params['activity_details'] ?? NULL,
579 'attachFile_1' => [
580 'uri' => $fileName,
581 'type' => 'application/pdf',
582 'location' => $fileName,
583 'upload_date' => date('YmdHis'),
584 ],
585 ];
586 if ($contributionId) {
587 $activityParams['source_record_id'] = $contributionId;
588 }
589 civicrm_api3('Activity', 'create', $activityParams);
590 }
591
592 /**
593 * Create the Invoice file in upload folder for attachment.
594 *
595 * @param string $html
596 * Content for pdf in html format.
597 *
598 * @param string $name
599 * @param array $format
600 *
601 * @return string
602 * Name of file which is in pdf format
603 */
604 public static function putFile($html, $name = 'Invoice.pdf', $format = NULL) {
605 return CRM_Utils_Mail::appendPDF($name, $html, $format)['fullPath'] ?? '';
606 }
607
608 /**
609 * Callback to perform action on Print Invoice button.
610 */
611 public static function getPrintPDF() {
612 $contributionId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject, FALSE);
613 $contributionIDs = [$contributionId];
614 $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject, FALSE);
615 $params = ['output' => 'pdf_invoice'];
616 CRM_Contribute_Form_Task_Invoice::printPDF($contributionIDs, $params, $contactId);
617 }
618
619 }