Commit | Line | Data |
---|---|---|
b8df2a80 RK |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
b8df2a80 | 5 | | | |
bc77d7c0 TO |
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 | | |
b8df2a80 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
b8df2a80 | 11 | |
b0295b9a | 12 | use Dompdf\Dompdf; |
6ae4d5d7 SL |
13 | use Dompdf\Options; |
14 | ||
b8df2a80 RK |
15 | /** |
16 | * | |
17 | * @package CRM | |
ca5cec67 | 18 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
b8df2a80 RK |
19 | */ |
20 | ||
21 | /** | |
22 | * This class provides the functionality to email a group of | |
23 | * contacts. | |
24 | */ | |
25 | class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { | |
dce4c3ea | 26 | /** |
b8df2a80 RK |
27 | * Are we operating in "single mode", i.e. updating the task of only |
28 | * one specific contribution? | |
29 | * | |
b67daa72 | 30 | * @var bool |
b8df2a80 RK |
31 | */ |
32 | public $_single = FALSE; | |
33 | ||
3a1261ac | 34 | /** |
fe482240 | 35 | * Gives all the statues for conribution. |
1330f57a | 36 | * @var int |
3a1261ac RK |
37 | */ |
38 | public $_contributionStatusId; | |
39 | ||
40 | /** | |
fe482240 | 41 | * Gives the HTML template of PDF Invoice. |
1330f57a | 42 | * @var string |
3a1261ac RK |
43 | */ |
44 | public $_messageInvoice; | |
45 | ||
46 | /** | |
fe482240 | 47 | * This variable is used to assign parameters for HTML template of PDF Invoice. |
1330f57a | 48 | * @var string |
3a1261ac RK |
49 | */ |
50 | public $_invoiceTemplate; | |
dce4c3ea | 51 | |
830b3e83 | 52 | /** |
fe482240 | 53 | * Selected output. |
1330f57a | 54 | * @var string |
830b3e83 | 55 | */ |
56 | public $_selectedOutput; | |
57 | ||
b8df2a80 | 58 | /** |
fe482240 | 59 | * Build all the data structures needed to build the form. |
dce4c3ea | 60 | */ |
00be9182 | 61 | public function preProcess() { |
b8df2a80 RK |
62 | $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE); |
63 | if ($id) { | |
be2fb01f | 64 | $this->_contributionIds = [$id]; |
b8df2a80 RK |
65 | $this->_componentClause = " civicrm_contribution.id IN ( $id ) "; |
66 | $this->_single = TRUE; | |
67 | $this->assign('totalSelectedContributions', 1); | |
b5c3483d | 68 | |
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" | |
73 | ); | |
74 | ||
75 | CRM_Core_Session::singleton()->pushUserContext($url); | |
b8df2a80 RK |
76 | } |
77 | else { | |
78 | parent::preProcess(); | |
79 | } | |
dce4c3ea | 80 | |
46bee19a | 81 | // check that all the contribution ids have status Completed, Pending, Refunded, or Partially Paid. |
3a1261ac | 82 | $this->_contributionStatusId = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
46bee19a | 83 | $status = ['Completed', 'Pending', 'Refunded', 'Partially paid']; |
be2fb01f | 84 | $statusId = []; |
3a1261ac RK |
85 | foreach ($this->_contributionStatusId as $key => $value) { |
86 | if (in_array($value, $status)) { | |
2826a42e | 87 | $statusId[] = $key; |
3a1261ac RK |
88 | } |
89 | } | |
90 | $Id = implode(",", $statusId); | |
91 | $query = "SELECT count(*) FROM civicrm_contribution WHERE contribution_status_id NOT IN ($Id) AND {$this->_componentClause}"; | |
b8df2a80 RK |
92 | $count = CRM_Core_DAO::singleValueQuery($query); |
93 | if ($count != 0) { | |
46bee19a | 94 | CRM_Core_Error::statusBounce(ts('Please select only contributions with Completed, Pending, Refunded, or Partially Paid status.')); |
b8df2a80 | 95 | } |
dce4c3ea | 96 | |
b8df2a80 RK |
97 | // we have all the contribution ids, so now we get the contact ids |
98 | parent::setContactIDs(); | |
99 | $this->assign('single', $this->_single); | |
dce4c3ea | 100 | |
b8df2a80 RK |
101 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this); |
102 | $urlParams = 'force=1'; | |
103 | if (CRM_Utils_Rule::qfKey($qfKey)) { | |
104 | $urlParams .= "&qfKey=$qfKey"; | |
105 | } | |
dce4c3ea | 106 | |
b8df2a80 | 107 | $url = CRM_Utils_System::url('civicrm/contribute/search', $urlParams); |
be2fb01f CW |
108 | $breadCrumb = [ |
109 | [ | |
dce4c3ea | 110 | 'url' => $url, |
111 | 'title' => ts('Search Results'), | |
be2fb01f CW |
112 | ], |
113 | ]; | |
dce4c3ea | 114 | |
b8df2a80 | 115 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
830b3e83 | 116 | |
117 | $this->_selectedOutput = CRM_Utils_Request::retrieve('select', 'String', $this); | |
118 | $this->assign('selectedOutput', $this->_selectedOutput); | |
119 | ||
beac1417 | 120 | CRM_Contact_Form_Task_EmailCommon::preProcessFromAddress($this); |
830b3e83 | 121 | if ($this->_selectedOutput == 'email') { |
6efffa5d | 122 | CRM_Utils_System::setTitle(ts('Email Invoice')); |
dce4c3ea | 123 | } |
124 | else { | |
6efffa5d PB |
125 | CRM_Utils_System::setTitle(ts('Print Contribution Invoice')); |
126 | } | |
b8df2a80 | 127 | } |
dce4c3ea | 128 | |
b8df2a80 | 129 | /** |
fe482240 | 130 | * Build the form object. |
b8df2a80 RK |
131 | */ |
132 | public function buildQuickForm() { | |
33313a73 | 133 | $this->preventAjaxSubmit(); |
6efffa5d PB |
134 | if (CRM_Core_Permission::check('administer CiviCRM')) { |
135 | $this->assign('isAdmin', 1); | |
136 | } | |
beac1417 MW |
137 | |
138 | $this->add('select', 'from_email_address', ts('From'), $this->_fromEmails, TRUE); | |
830b3e83 | 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'); | |
be2fb01f | 143 | $this->addFormRule(['CRM_Contribute_Form_Task_Invoice', 'formRule']); |
830b3e83 | 144 | } |
145 | else { | |
146 | $this->addRule('from_email_address', ts('From Email Address is required'), 'required'); | |
147 | } | |
148 | ||
be2fb01f | 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.)'), [ |
dce4c3ea | 150 | 'rows' => 2, |
21dfd5f5 | 151 | 'cols' => 40, |
be2fb01f | 152 | ]); |
6efffa5d | 153 | |
be2fb01f | 154 | $this->addButtons([ |
1330f57a SL |
155 | [ |
156 | 'type' => 'upload', | |
157 | 'name' => $this->_selectedOutput == 'email' ? ts('Send Email') : ts('Process Invoice(s)'), | |
158 | 'isDefault' => TRUE, | |
159 | ], | |
160 | [ | |
161 | 'type' => 'cancel', | |
162 | 'name' => ts('Cancel'), | |
163 | ], | |
164 | ]); | |
b8df2a80 | 165 | } |
6f39f13a PB |
166 | |
167 | /** | |
fe482240 | 168 | * Global validation rules for the form. |
6f39f13a | 169 | * |
c490a46a | 170 | * @param array $values |
6f39f13a | 171 | * |
a6c01b45 CW |
172 | * @return array |
173 | * list of errors to be posted back to the form | |
6f39f13a | 174 | */ |
00be9182 | 175 | public static function formRule($values) { |
be2fb01f | 176 | $errors = []; |
830b3e83 | 177 | |
178 | if ($values['output'] == 'email_invoice' && empty($values['from_email_address'])) { | |
179 | $errors['from_email_address'] = ts("From Email Address is required"); | |
6f39f13a | 180 | } |
830b3e83 | 181 | |
6f39f13a | 182 | return $errors; |
b8df2a80 | 183 | } |
dce4c3ea | 184 | |
185 | /** | |
fe482240 | 186 | * Process the form after the input has been submitted and validated. |
b8df2a80 RK |
187 | */ |
188 | public function postProcess() { | |
6efffa5d | 189 | $params = $this->controller->exportValues($this->_name); |
1273d77c | 190 | self::printPDF($this->_contributionIds, $params, $this->_contactIds); |
6efffa5d PB |
191 | } |
192 | ||
dce4c3ea | 193 | /** |
95cdcc0f | 194 | * Process the PDf and email with activity and attachment on click of Print Invoices. |
dce4c3ea | 195 | * |
014c4014 TO |
196 | * @param array $contribIDs |
197 | * Contribution Id. | |
198 | * @param array $params | |
199 | * Associated array of submitted values. | |
200 | * @param array $contactIds | |
201 | * Contact Id. | |
6efffa5d | 202 | */ |
1273d77c | 203 | public static function printPDF($contribIDs, &$params, $contactIds) { |
b8df2a80 | 204 | // get all the details needed to generate a invoice |
be2fb01f | 205 | $messageInvoice = []; |
6efffa5d | 206 | $invoiceTemplate = CRM_Core_Smarty::singleton(); |
6efffa5d | 207 | $invoiceElements = CRM_Contribute_Form_Task_PDF::getElements($contribIDs, $params, $contactIds); |
dce4c3ea | 208 | |
3a1261ac | 209 | // gives the status id when contribution status is 'Refunded' |
6efffa5d PB |
210 | $contributionStatusID = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
211 | $refundedStatusId = CRM_Utils_Array::key('Refunded', $contributionStatusID); | |
41e050b3 | 212 | $cancelledStatusId = CRM_Utils_Array::key('Cancelled', $contributionStatusID); |
2f3e0ab6 | 213 | $pendingStatusId = CRM_Utils_Array::key('Pending', $contributionStatusID); |
f2d5e719 | 214 | |
2826a42e | 215 | foreach ($invoiceElements['details'] as $contribID => $detail) { |
be2fb01f | 216 | $input = $ids = $objects = []; |
3a1261ac | 217 | if (in_array($detail['contact'], $invoiceElements['excludeContactIds'])) { |
b8df2a80 RK |
218 | continue; |
219 | } | |
dce4c3ea | 220 | |
b8df2a80 | 221 | $input['component'] = $detail['component']; |
dce4c3ea | 222 | |
b8df2a80 | 223 | $ids['contact'] = $detail['contact']; |
2826a42e | 224 | $ids['contribution'] = $contribID; |
b8df2a80 RK |
225 | $ids['contributionRecur'] = NULL; |
226 | $ids['contributionPage'] = NULL; | |
9c1bc317 CW |
227 | $ids['membership'] = $detail['membership'] ?? NULL; |
228 | $ids['participant'] = $detail['participant'] ?? NULL; | |
229 | $ids['event'] = $detail['event'] ?? NULL; | |
dce4c3ea | 230 | |
3a1261ac | 231 | if (!$invoiceElements['baseIPN']->validateData($input, $ids, $objects, FALSE)) { |
b8df2a80 RK |
232 | CRM_Core_Error::fatal(); |
233 | } | |
dce4c3ea | 234 | |
353ffa53 | 235 | $contribution = &$objects['contribution']; |
dce4c3ea | 236 | |
b8df2a80 RK |
237 | $input['amount'] = $contribution->total_amount; |
238 | $input['invoice_id'] = $contribution->invoice_id; | |
239 | $input['receive_date'] = $contribution->receive_date; | |
b8df2a80 RK |
240 | $input['contribution_status_id'] = $contribution->contribution_status_id; |
241 | $input['organization_name'] = $contribution->_relatedObjects['contact']->organization_name; | |
dce4c3ea | 242 | |
b8df2a80 | 243 | $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date); |
dce4c3ea | 244 | |
66bc5236 ML |
245 | // Fetch the billing address. getValues should prioritize the billing |
246 | // address, otherwise will return the primary address. | |
be2fb01f | 247 | $billingAddress = []; |
dce4c3ea | 248 | |
66bc5236 ML |
249 | $addressDetails = CRM_Core_BAO_Address::getValues([ |
250 | 'contact_id' => $contribution->contact_id, | |
251 | 'is_billing' => 1, | |
252 | ]); | |
253 | ||
254 | if (!empty($addressDetails)) { | |
255 | $billingAddress = array_shift($addressDetails); | |
6efffa5d | 256 | } |
dce4c3ea | 257 | |
41e050b3 | 258 | if ($contribution->contribution_status_id == $refundedStatusId || $contribution->contribution_status_id == $cancelledStatusId) { |
38c87aa2 | 259 | $creditNoteId = $contribution->creditnote_id; |
b8df2a80 | 260 | } |
b07b172b | 261 | if (!$contribution->invoice_number) { |
262 | $contribution->invoice_number = CRM_Contribute_BAO_Contribution::getInvoiceNumber($contribution->id); | |
12a8f9d7 | 263 | } |
dce4c3ea | 264 | |
b8df2a80 RK |
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"); | |
e3eb0205 SL |
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)); | |
dce4c3ea | 271 | |
449b5e65 | 272 | $amountPaid = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribID, TRUE); |
8f4f19ca | 273 | $amountDue = ($input['amount'] - $amountPaid); |
dce4c3ea | 274 | |
8f4f19ca | 275 | // retrieving the subtotal and sum of same tax_rate |
be2fb01f | 276 | $dataArray = []; |
b8df2a80 | 277 | $subTotal = 0; |
449b5e65 | 278 | $lineItem = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribID); |
fd9e1183 | 279 | foreach ($lineItem as $taxRate) { |
dce4c3ea | 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); | |
b8df2a80 RK |
282 | } |
283 | else { | |
9c1bc317 | 284 | $dataArray[(string) $taxRate['tax_rate']] = $taxRate['tax_amount'] ?? NULL; |
b8df2a80 RK |
285 | } |
286 | $subTotal += CRM_Utils_Array::value('subTotal', $taxRate); | |
287 | } | |
dce4c3ea | 288 | |
b8df2a80 | 289 | // to email the invoice |
be2fb01f CW |
290 | $mailDetails = []; |
291 | $values = []; | |
b8df2a80 RK |
292 | if ($contribution->_component == 'event') { |
293 | $daoName = 'CRM_Event_DAO_Event'; | |
294 | $pageId = $contribution->_relatedObjects['event']->id; | |
be2fb01f | 295 | $mailElements = [ |
dce4c3ea | 296 | 'title', |
297 | 'confirm_from_name', | |
298 | 'confirm_from_email', | |
be2fb01f | 299 | ]; |
b8df2a80 | 300 | CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements); |
9c1bc317 CW |
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; | |
dce4c3ea | 304 | |
9c1bc317 | 305 | $title = $mailDetails[$contribution->_relatedObjects['event']->id]['title'] ?? NULL; |
b8df2a80 RK |
306 | } |
307 | elseif ($contribution->_component == 'contribute') { | |
308 | $daoName = 'CRM_Contribute_DAO_ContributionPage'; | |
309 | $pageId = $contribution->contribution_page_id; | |
be2fb01f | 310 | $mailElements = [ |
dce4c3ea | 311 | 'title', |
312 | 'receipt_from_name', | |
313 | 'receipt_from_email', | |
314 | 'cc_receipt', | |
315 | 'bcc_receipt', | |
be2fb01f | 316 | ]; |
b8df2a80 | 317 | CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements); |
6efffa5d | 318 | |
dce4c3ea | 319 | $values['title'] = CRM_Utils_Array::value('title', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); |
6efffa5d PB |
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)); | |
324 | ||
325 | $title = CRM_Utils_Array::value('title', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); | |
b8df2a80 | 326 | } |
d88a0337 | 327 | $source = $contribution->source; |
dce4c3ea | 328 | |
b8df2a80 | 329 | $config = CRM_Core_Config::singleton(); |
c4d3b8d3 | 330 | if (!isset($params['forPage'])) { |
d88a0337 PD |
331 | $config->doNotAttachPDFReceipt = 1; |
332 | } | |
333 | ||
334 | // get organization address | |
335 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
be2fb01f | 336 | $locParams = ['contact_id' => $domain->contact_id]; |
b69fc6b5 | 337 | $locationDefaults = CRM_Core_BAO_Location::getValues($locParams); |
d88a0337 | 338 | if (isset($locationDefaults['address'][1]['state_province_id'])) { |
dce4c3ea | 339 | $stateProvinceAbbreviationDomain = CRM_Core_PseudoConstant::stateProvinceAbbreviation($locationDefaults['address'][1]['state_province_id']); |
d88a0337 PD |
340 | } |
341 | else { | |
342 | $stateProvinceAbbreviationDomain = ''; | |
343 | } | |
344 | if (isset($locationDefaults['address'][1]['country_id'])) { | |
dce4c3ea | 345 | $countryDomain = CRM_Core_PseudoConstant::country($locationDefaults['address'][1]['country_id']); |
d88a0337 PD |
346 | } |
347 | else { | |
348 | $countryDomain = ''; | |
349 | } | |
6efffa5d | 350 | |
e3eb0205 SL |
351 | $invoiceNotes = Civi::settings()->get('invoice_notes') ?? NULL; |
352 | ||
b8df2a80 | 353 | // parameters to be assign for template |
be2fb01f | 354 | $tplParams = [ |
dce4c3ea | 355 | 'title' => $title, |
356 | 'component' => $input['component'], | |
357 | 'id' => $contribution->id, | |
358 | 'source' => $source, | |
b07b172b | 359 | 'invoice_number' => $contribution->invoice_number, |
9cad3ff4 | 360 | 'invoice_id' => $contribution->invoice_id, |
9f8fe885 | 361 | 'resourceBase' => $config->userFrameworkResourceURL, |
dce4c3ea | 362 | 'defaultCurrency' => $config->defaultCurrency, |
363 | 'amount' => $contribution->total_amount, | |
364 | 'amountDue' => $amountDue, | |
77680511 | 365 | 'amountPaid' => $amountPaid, |
dce4c3ea | 366 | 'invoice_date' => $invoiceDate, |
367 | 'dueDate' => $dueDate, | |
e3eb0205 | 368 | 'notes' => $invoiceNotes, |
dce4c3ea | 369 | 'display_name' => $contribution->_relatedObjects['contact']->display_name, |
370 | 'lineItem' => $lineItem, | |
371 | 'dataArray' => $dataArray, | |
372 | 'refundedStatusId' => $refundedStatusId, | |
2f3e0ab6 | 373 | 'pendingStatusId' => $pendingStatusId, |
41e050b3 | 374 | 'cancelledStatusId' => $cancelledStatusId, |
dce4c3ea | 375 | 'contribution_status_id' => $contribution->contribution_status_id, |
ef425b3b | 376 | 'contributionStatusName' => CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution->contribution_status_id), |
dce4c3ea | 377 | 'subTotal' => $subTotal, |
6b409353 CW |
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, | |
66bc5236 | 386 | // Kept for backwards compatibility |
6b409353 CW |
387 | 'stateProvinceAbbreviation' => $billingAddress['state_province_abbreviation'] ?? NULL, |
388 | 'country' => $billingAddress['country'] ?? NULL, | |
dce4c3ea | 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'])), | |
207f62c6 | 395 | 'domain_supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', CRM_Utils_Array::value('1', $locationDefaults['address'])), |
dce4c3ea | 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'])), | |
be2fb01f | 402 | ]; |
dc0bdd34 | 403 | |
c4d3b8d3 PD |
404 | if (isset($creditNoteId)) { |
405 | $tplParams['creditnote_id'] = $creditNoteId; | |
406 | } | |
d75f2f47 | 407 | |
b07b172b | 408 | $pdfFileName = $contribution->invoice_number . ".pdf"; |
be2fb01f | 409 | $sendTemplateParams = [ |
dce4c3ea | 410 | 'groupName' => 'msg_tpl_workflow_contribution', |
411 | 'valueName' => 'contribution_invoice_receipt', | |
412 | 'contactId' => $contribution->contact_id, | |
413 | 'tplParams' => $tplParams, | |
c119e8ae | 414 | 'PDFFilename' => $pdfFileName, |
be2fb01f | 415 | ]; |
6f39f13a | 416 | |
6efffa5d | 417 | // from email address |
9c1bc317 | 418 | $fromEmailAddress = $params['from_email_address'] ?? NULL; |
511c5767 | 419 | |
b8df2a80 | 420 | // condition to check for download PDF Invoice or email Invoice |
3a1261ac | 421 | if ($invoiceElements['createPdf']) { |
b8df2a80 | 422 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
c4d3b8d3 | 423 | if (isset($params['forPage'])) { |
d88a0337 PD |
424 | return $html; |
425 | } | |
b8df2a80 | 426 | else { |
be2fb01f | 427 | $mail = [ |
d88a0337 | 428 | 'subject' => $subject, |
dce4c3ea | 429 | 'body' => $message, |
d88a0337 | 430 | 'html' => $html, |
be2fb01f | 431 | ]; |
d88a0337 PD |
432 | if ($mail['html']) { |
433 | $messageInvoice[] = $mail['html']; | |
dce4c3ea | 434 | } |
d88a0337 PD |
435 | else { |
436 | $messageInvoice[] = nl2br($mail['body']); | |
437 | } | |
b8df2a80 RK |
438 | } |
439 | } | |
440 | elseif ($contribution->_component == 'contribute') { | |
441 | $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id); | |
dce4c3ea | 442 | |
be2fb01f | 443 | $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]); |
6efffa5d | 444 | $sendTemplateParams['from'] = $fromEmailAddress; |
b8df2a80 | 445 | $sendTemplateParams['toEmail'] = $email; |
9c1bc317 CW |
446 | $sendTemplateParams['cc'] = $values['cc_receipt'] ?? NULL; |
447 | $sendTemplateParams['bcc'] = $values['bcc_receipt'] ?? NULL; | |
dce4c3ea | 448 | |
449 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); | |
6efffa5d | 450 | // functions call for adding activity with attachment |
c119e8ae | 451 | $fileName = self::putFile($html, $pdfFileName); |
3e31220c | 452 | self::addActivities($subject, $contribution->contact_id, $fileName, $params, $contribution->id); |
b8df2a80 RK |
453 | } |
454 | elseif ($contribution->_component == 'event') { | |
455 | $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id); | |
dce4c3ea | 456 | |
be2fb01f | 457 | $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]); |
6efffa5d | 458 | $sendTemplateParams['from'] = $fromEmailAddress; |
b8df2a80 | 459 | $sendTemplateParams['toEmail'] = $email; |
9c1bc317 CW |
460 | $sendTemplateParams['cc'] = $values['cc_confirm'] ?? NULL; |
461 | $sendTemplateParams['bcc'] = $values['bcc_confirm'] ?? NULL; | |
dce4c3ea | 462 | |
463 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); | |
6efffa5d | 464 | // functions call for adding activity with attachment |
c119e8ae | 465 | $fileName = self::putFile($html, $pdfFileName); |
3e31220c | 466 | self::addActivities($subject, $contribution->contact_id, $fileName, $params, $contribution->id); |
b8df2a80 | 467 | } |
6efffa5d | 468 | $invoiceTemplate->clearTemplateVars(); |
b8df2a80 | 469 | } |
dce4c3ea | 470 | |
3a1261ac | 471 | if ($invoiceElements['createPdf']) { |
c4d3b8d3 | 472 | if (isset($params['forPage'])) { |
d88a0337 PD |
473 | return $html; |
474 | } | |
475 | else { | |
be2fb01f | 476 | CRM_Utils_PDF_Utils::html2pdf($messageInvoice, $pdfFileName, FALSE, [ |
dce4c3ea | 477 | 'margin_top' => 10, |
478 | 'margin_left' => 65, | |
21dfd5f5 | 479 | 'metric' => 'px', |
be2fb01f | 480 | ]); |
d88a0337 | 481 | // functions call for adding activity with attachment |
c119e8ae | 482 | $fileName = self::putFile($html, $pdfFileName); |
413796ce | 483 | self::addActivities($subject, $contactIds, $fileName, $params); |
6efffa5d | 484 | |
d88a0337 PD |
485 | CRM_Utils_System::civiExit(); |
486 | } | |
b8df2a80 RK |
487 | } |
488 | else { | |
3a1261ac | 489 | if ($invoiceElements['suppressedEmails']) { |
be2fb01f | 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']]); |
b8df2a80 RK |
491 | $msgTitle = ts('Email Error'); |
492 | $msgType = 'error'; | |
493 | } | |
494 | else { | |
495 | $status = ts('Your mail has been sent.'); | |
496 | $msgTitle = ts('Sent'); | |
497 | $msgType = 'success'; | |
498 | } | |
499 | CRM_Core_Session::setStatus($status, $msgTitle, $msgType); | |
500 | } | |
501 | } | |
6efffa5d | 502 | |
dce4c3ea | 503 | /** |
fe482240 | 504 | * Add activity for Email Invoice and the PDF Invoice. |
6efffa5d | 505 | * |
014c4014 TO |
506 | * @param string $subject |
507 | * Activity subject. | |
508 | * @param array $contactIds | |
509 | * Contact Id. | |
510 | * @param string $fileName | |
511 | * Gives the location with name of the file. | |
512 | * @param array $params | |
513 | * For invoices. | |
3e31220c PN |
514 | * @param int $contributionId |
515 | * Contribution Id. | |
6efffa5d PB |
516 | * |
517 | */ | |
3e31220c | 518 | public static function addActivities($subject, $contactIds, $fileName, $params, $contributionId = NULL) { |
dce4c3ea | 519 | $session = CRM_Core_Session::singleton(); |
520 | $userID = $session->get('userID'); | |
6efffa5d PB |
521 | $config = CRM_Core_Config::singleton(); |
522 | $config->doNotAttachPDFReceipt = 1; | |
413796ce | 523 | |
524 | if (!empty($params['output']) && $params['output'] == 'pdf_invoice') { | |
ac844df6 | 525 | $activityType = 'Downloaded Invoice'; |
dce4c3ea | 526 | } |
92e4c2a5 | 527 | else { |
ac844df6 | 528 | $activityType = 'Emailed Invoice'; |
6efffa5d | 529 | } |
413796ce | 530 | |
be2fb01f | 531 | $activityParams = [ |
6efffa5d PB |
532 | 'subject' => $subject, |
533 | 'source_contact_id' => $userID, | |
534 | 'target_contact_id' => $contactIds, | |
ac844df6 | 535 | 'activity_type_id' => $activityType, |
6efffa5d | 536 | 'activity_date_time' => date('YmdHis'), |
be2fb01f | 537 | 'attachFile_1' => [ |
dce4c3ea | 538 | 'uri' => $fileName, |
539 | 'type' => 'application/pdf', | |
540 | 'location' => $fileName, | |
541 | 'upload_date' => date('YmdHis'), | |
be2fb01f CW |
542 | ], |
543 | ]; | |
3e31220c PN |
544 | if ($contributionId) { |
545 | $activityParams['source_record_id'] = $contributionId; | |
546 | } | |
547 | civicrm_api3('Activity', 'create', $activityParams); | |
6efffa5d | 548 | } |
dce4c3ea | 549 | |
550 | /** | |
fe482240 | 551 | * Create the Invoice file in upload folder for attachment. |
dce4c3ea | 552 | * |
76e7a76c | 553 | * @param string $html |
014c4014 | 554 | * Content for pdf in html format. |
6efffa5d | 555 | * |
ad37ac8e | 556 | * @param string $name |
557 | * | |
03110609 | 558 | * @return string |
76e7a76c | 559 | * Name of file which is in pdf format |
6efffa5d | 560 | */ |
1330f57a | 561 | public static function putFile($html, $name = 'Invoice.pdf') { |
6ae4d5d7 | 562 | $options = new Options(); |
4f31930a | 563 | $options->set('isRemoteEnabled', TRUE); |
6ae4d5d7 | 564 | |
4f31930a | 565 | $doc = new DOMPDF($options); |
6efffa5d PB |
566 | $doc->load_html($html); |
567 | $doc->render(); | |
568 | $html = $doc->output(); | |
569 | $config = CRM_Core_Config::singleton(); | |
c119e8ae | 570 | $fileName = $config->uploadDir . $name; |
dce4c3ea | 571 | file_put_contents($fileName, $html); |
6efffa5d PB |
572 | return $fileName; |
573 | } | |
b5c3483d | 574 | |
575 | /** | |
576 | * Callback to perform action on Print Invoice button. | |
577 | */ | |
00be9182 | 578 | public static function getPrintPDF() { |
b5c3483d | 579 | $contributionId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject, FALSE); |
be2fb01f | 580 | $contributionIDs = [$contributionId]; |
b5c3483d | 581 | $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject, FALSE); |
be2fb01f | 582 | $params = ['output' => 'pdf_invoice']; |
1273d77c | 583 | CRM_Contribute_Form_Task_Invoice::printPDF($contributionIDs, $params, $contactId); |
b5c3483d | 584 | } |
96025800 | 585 | |
b8df2a80 | 586 | } |