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