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