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 | |
b8df2a80 | 304 | if ($input['component'] == 'contribute') { |
77dbdcbc | 305 | $lineItem = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribID); |
dce4c3ea | 306 | } |
b8df2a80 RK |
307 | else { |
308 | $eid = $contribution->_relatedObjects['participant']->id; | |
77dbdcbc | 309 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($eid, 'participant', NULL, TRUE, FALSE, TRUE); |
b8df2a80 | 310 | } |
dce4c3ea | 311 | |
be2fb01f | 312 | $resultPayments = civicrm_api3('Payment', 'get', [ |
1330f57a SL |
313 | 'sequential' => 1, |
314 | 'contribution_id' => $contribID, | |
be2fb01f | 315 | ]); |
8f4f19ca | 316 | $amountPaid = 0; |
77680511 | 317 | foreach ($resultPayments['values'] as $singlePayment) { |
c12b5af4 | 318 | // Only count payments that have been (status =) completed. |
8f4f19ca K |
319 | if ($singlePayment['status_id'] == 1) { |
320 | $amountPaid += $singlePayment['total_amount']; | |
321 | } | |
322 | } | |
323 | $amountDue = ($input['amount'] - $amountPaid); | |
dce4c3ea | 324 | |
8f4f19ca | 325 | // retrieving the subtotal and sum of same tax_rate |
be2fb01f | 326 | $dataArray = []; |
b8df2a80 | 327 | $subTotal = 0; |
fd9e1183 | 328 | foreach ($lineItem as $taxRate) { |
dce4c3ea | 329 | if (isset($dataArray[(string) $taxRate['tax_rate']])) { |
330 | $dataArray[(string) $taxRate['tax_rate']] = $dataArray[(string) $taxRate['tax_rate']] + CRM_Utils_Array::value('tax_amount', $taxRate); | |
b8df2a80 RK |
331 | } |
332 | else { | |
dce4c3ea | 333 | $dataArray[(string) $taxRate['tax_rate']] = CRM_Utils_Array::value('tax_amount', $taxRate); |
b8df2a80 RK |
334 | } |
335 | $subTotal += CRM_Utils_Array::value('subTotal', $taxRate); | |
336 | } | |
dce4c3ea | 337 | |
b8df2a80 | 338 | // to email the invoice |
be2fb01f CW |
339 | $mailDetails = []; |
340 | $values = []; | |
b8df2a80 RK |
341 | if ($contribution->_component == 'event') { |
342 | $daoName = 'CRM_Event_DAO_Event'; | |
343 | $pageId = $contribution->_relatedObjects['event']->id; | |
be2fb01f | 344 | $mailElements = [ |
dce4c3ea | 345 | 'title', |
346 | 'confirm_from_name', | |
347 | 'confirm_from_email', | |
348 | 'cc_confirm', | |
349 | 'bcc_confirm', | |
be2fb01f | 350 | ]; |
b8df2a80 | 351 | CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements); |
b8df2a80 RK |
352 | $values['title'] = CRM_Utils_Array::value('title', $mailDetails[$contribution->_relatedObjects['event']->id]); |
353 | $values['confirm_from_name'] = CRM_Utils_Array::value('confirm_from_name', $mailDetails[$contribution->_relatedObjects['event']->id]); | |
354 | $values['confirm_from_email'] = CRM_Utils_Array::value('confirm_from_email', $mailDetails[$contribution->_relatedObjects['event']->id]); | |
355 | $values['cc_confirm'] = CRM_Utils_Array::value('cc_confirm', $mailDetails[$contribution->_relatedObjects['event']->id]); | |
356 | $values['bcc_confirm'] = CRM_Utils_Array::value('bcc_confirm', $mailDetails[$contribution->_relatedObjects['event']->id]); | |
dce4c3ea | 357 | |
b8df2a80 RK |
358 | $title = CRM_Utils_Array::value('title', $mailDetails[$contribution->_relatedObjects['event']->id]); |
359 | } | |
360 | elseif ($contribution->_component == 'contribute') { | |
361 | $daoName = 'CRM_Contribute_DAO_ContributionPage'; | |
362 | $pageId = $contribution->contribution_page_id; | |
be2fb01f | 363 | $mailElements = [ |
dce4c3ea | 364 | 'title', |
365 | 'receipt_from_name', | |
366 | 'receipt_from_email', | |
367 | 'cc_receipt', | |
368 | 'bcc_receipt', | |
be2fb01f | 369 | ]; |
b8df2a80 | 370 | CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements); |
6efffa5d | 371 | |
dce4c3ea | 372 | $values['title'] = CRM_Utils_Array::value('title', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); |
6efffa5d PB |
373 | $values['receipt_from_name'] = CRM_Utils_Array::value('receipt_from_name', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); |
374 | $values['receipt_from_email'] = CRM_Utils_Array::value('receipt_from_email', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); | |
375 | $values['cc_receipt'] = CRM_Utils_Array::value('cc_receipt', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); | |
376 | $values['bcc_receipt'] = CRM_Utils_Array::value('bcc_receipt', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); | |
377 | ||
378 | $title = CRM_Utils_Array::value('title', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); | |
b8df2a80 | 379 | } |
d88a0337 | 380 | $source = $contribution->source; |
dce4c3ea | 381 | |
b8df2a80 | 382 | $config = CRM_Core_Config::singleton(); |
c4d3b8d3 | 383 | if (!isset($params['forPage'])) { |
d88a0337 PD |
384 | $config->doNotAttachPDFReceipt = 1; |
385 | } | |
386 | ||
387 | // get organization address | |
388 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
be2fb01f | 389 | $locParams = ['contact_id' => $domain->contact_id]; |
b69fc6b5 | 390 | $locationDefaults = CRM_Core_BAO_Location::getValues($locParams); |
d88a0337 | 391 | if (isset($locationDefaults['address'][1]['state_province_id'])) { |
dce4c3ea | 392 | $stateProvinceAbbreviationDomain = CRM_Core_PseudoConstant::stateProvinceAbbreviation($locationDefaults['address'][1]['state_province_id']); |
d88a0337 PD |
393 | } |
394 | else { | |
395 | $stateProvinceAbbreviationDomain = ''; | |
396 | } | |
397 | if (isset($locationDefaults['address'][1]['country_id'])) { | |
dce4c3ea | 398 | $countryDomain = CRM_Core_PseudoConstant::country($locationDefaults['address'][1]['country_id']); |
d88a0337 PD |
399 | } |
400 | else { | |
401 | $countryDomain = ''; | |
402 | } | |
6efffa5d | 403 | |
b8df2a80 | 404 | // parameters to be assign for template |
be2fb01f | 405 | $tplParams = [ |
dce4c3ea | 406 | 'title' => $title, |
407 | 'component' => $input['component'], | |
408 | 'id' => $contribution->id, | |
409 | 'source' => $source, | |
b07b172b | 410 | 'invoice_number' => $contribution->invoice_number, |
9cad3ff4 | 411 | 'invoice_id' => $contribution->invoice_id, |
9f8fe885 | 412 | 'resourceBase' => $config->userFrameworkResourceURL, |
dce4c3ea | 413 | 'defaultCurrency' => $config->defaultCurrency, |
414 | 'amount' => $contribution->total_amount, | |
415 | 'amountDue' => $amountDue, | |
77680511 | 416 | 'amountPaid' => $amountPaid, |
dce4c3ea | 417 | 'invoice_date' => $invoiceDate, |
418 | 'dueDate' => $dueDate, | |
419 | 'notes' => CRM_Utils_Array::value('notes', $prefixValue), | |
420 | 'display_name' => $contribution->_relatedObjects['contact']->display_name, | |
421 | 'lineItem' => $lineItem, | |
422 | 'dataArray' => $dataArray, | |
423 | 'refundedStatusId' => $refundedStatusId, | |
2f3e0ab6 | 424 | 'pendingStatusId' => $pendingStatusId, |
41e050b3 | 425 | 'cancelledStatusId' => $cancelledStatusId, |
dce4c3ea | 426 | 'contribution_status_id' => $contribution->contribution_status_id, |
ef425b3b | 427 | 'contributionStatusName' => CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution->contribution_status_id), |
dce4c3ea | 428 | 'subTotal' => $subTotal, |
429 | 'street_address' => CRM_Utils_Array::value('street_address', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), | |
430 | 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), | |
431 | 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), | |
207f62c6 | 432 | 'supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), |
dce4c3ea | 433 | 'city' => CRM_Utils_Array::value('city', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), |
434 | 'stateProvinceAbbreviation' => $stateProvinceAbbreviation, | |
435 | 'postal_code' => CRM_Utils_Array::value('postal_code', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), | |
436 | 'is_pay_later' => $contribution->is_pay_later, | |
437 | 'organization_name' => $contribution->_relatedObjects['contact']->organization_name, | |
438 | 'domain_organization' => $domain->name, | |
439 | 'domain_street_address' => CRM_Utils_Array::value('street_address', CRM_Utils_Array::value('1', $locationDefaults['address'])), | |
440 | 'domain_supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', CRM_Utils_Array::value('1', $locationDefaults['address'])), | |
441 | 'domain_supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', CRM_Utils_Array::value('1', $locationDefaults['address'])), | |
207f62c6 | 442 | 'domain_supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', CRM_Utils_Array::value('1', $locationDefaults['address'])), |
dce4c3ea | 443 | 'domain_city' => CRM_Utils_Array::value('city', CRM_Utils_Array::value('1', $locationDefaults['address'])), |
444 | 'domain_postal_code' => CRM_Utils_Array::value('postal_code', CRM_Utils_Array::value('1', $locationDefaults['address'])), | |
445 | 'domain_state' => $stateProvinceAbbreviationDomain, | |
446 | 'domain_country' => $countryDomain, | |
447 | 'domain_email' => CRM_Utils_Array::value('email', CRM_Utils_Array::value('1', $locationDefaults['email'])), | |
448 | 'domain_phone' => CRM_Utils_Array::value('phone', CRM_Utils_Array::value('1', $locationDefaults['phone'])), | |
be2fb01f | 449 | ]; |
dc0bdd34 | 450 | |
c4d3b8d3 PD |
451 | if (isset($creditNoteId)) { |
452 | $tplParams['creditnote_id'] = $creditNoteId; | |
453 | } | |
d75f2f47 | 454 | |
b07b172b | 455 | $pdfFileName = $contribution->invoice_number . ".pdf"; |
be2fb01f | 456 | $sendTemplateParams = [ |
dce4c3ea | 457 | 'groupName' => 'msg_tpl_workflow_contribution', |
458 | 'valueName' => 'contribution_invoice_receipt', | |
459 | 'contactId' => $contribution->contact_id, | |
460 | 'tplParams' => $tplParams, | |
c119e8ae | 461 | 'PDFFilename' => $pdfFileName, |
be2fb01f | 462 | ]; |
6f39f13a | 463 | |
6efffa5d | 464 | // from email address |
511c5767 BS |
465 | $fromEmailAddress = CRM_Utils_Array::value('from_email_address', $params); |
466 | ||
b8df2a80 | 467 | // condition to check for download PDF Invoice or email Invoice |
3a1261ac | 468 | if ($invoiceElements['createPdf']) { |
b8df2a80 | 469 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
c4d3b8d3 | 470 | if (isset($params['forPage'])) { |
d88a0337 PD |
471 | return $html; |
472 | } | |
b8df2a80 | 473 | else { |
be2fb01f | 474 | $mail = [ |
d88a0337 | 475 | 'subject' => $subject, |
dce4c3ea | 476 | 'body' => $message, |
d88a0337 | 477 | 'html' => $html, |
be2fb01f | 478 | ]; |
d88a0337 PD |
479 | if ($mail['html']) { |
480 | $messageInvoice[] = $mail['html']; | |
dce4c3ea | 481 | } |
d88a0337 PD |
482 | else { |
483 | $messageInvoice[] = nl2br($mail['body']); | |
484 | } | |
b8df2a80 RK |
485 | } |
486 | } | |
487 | elseif ($contribution->_component == 'contribute') { | |
488 | $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id); | |
dce4c3ea | 489 | |
be2fb01f | 490 | $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]); |
6efffa5d | 491 | $sendTemplateParams['from'] = $fromEmailAddress; |
b8df2a80 RK |
492 | $sendTemplateParams['toEmail'] = $email; |
493 | $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_receipt', $values); | |
494 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $values); | |
dce4c3ea | 495 | |
496 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); | |
6efffa5d | 497 | // functions call for adding activity with attachment |
c119e8ae | 498 | $fileName = self::putFile($html, $pdfFileName); |
6efffa5d | 499 | self::addActivities($subject, $contribution->contact_id, $fileName, $params); |
b8df2a80 RK |
500 | } |
501 | elseif ($contribution->_component == 'event') { | |
502 | $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id); | |
dce4c3ea | 503 | |
be2fb01f | 504 | $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $invoiceElements['params']['email_comment']]); |
6efffa5d | 505 | $sendTemplateParams['from'] = $fromEmailAddress; |
b8df2a80 RK |
506 | $sendTemplateParams['toEmail'] = $email; |
507 | $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_confirm', $values); | |
508 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_confirm', $values); | |
dce4c3ea | 509 | |
510 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); | |
6efffa5d | 511 | // functions call for adding activity with attachment |
c119e8ae | 512 | $fileName = self::putFile($html, $pdfFileName); |
6efffa5d | 513 | self::addActivities($subject, $contribution->contact_id, $fileName, $params); |
b8df2a80 | 514 | } |
6efffa5d | 515 | $invoiceTemplate->clearTemplateVars(); |
b8df2a80 | 516 | } |
dce4c3ea | 517 | |
3a1261ac | 518 | if ($invoiceElements['createPdf']) { |
c4d3b8d3 | 519 | if (isset($params['forPage'])) { |
d88a0337 PD |
520 | return $html; |
521 | } | |
522 | else { | |
be2fb01f | 523 | CRM_Utils_PDF_Utils::html2pdf($messageInvoice, $pdfFileName, FALSE, [ |
dce4c3ea | 524 | 'margin_top' => 10, |
525 | 'margin_left' => 65, | |
21dfd5f5 | 526 | 'metric' => 'px', |
be2fb01f | 527 | ]); |
d88a0337 | 528 | // functions call for adding activity with attachment |
c119e8ae | 529 | $fileName = self::putFile($html, $pdfFileName); |
413796ce | 530 | self::addActivities($subject, $contactIds, $fileName, $params); |
6efffa5d | 531 | |
d88a0337 PD |
532 | CRM_Utils_System::civiExit(); |
533 | } | |
b8df2a80 RK |
534 | } |
535 | else { | |
3a1261ac | 536 | if ($invoiceElements['suppressedEmails']) { |
be2fb01f | 537 | $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 |
538 | $msgTitle = ts('Email Error'); |
539 | $msgType = 'error'; | |
540 | } | |
541 | else { | |
542 | $status = ts('Your mail has been sent.'); | |
543 | $msgTitle = ts('Sent'); | |
544 | $msgType = 'success'; | |
545 | } | |
546 | CRM_Core_Session::setStatus($status, $msgTitle, $msgType); | |
547 | } | |
548 | } | |
6efffa5d | 549 | |
dce4c3ea | 550 | /** |
fe482240 | 551 | * Add activity for Email Invoice and the PDF Invoice. |
6efffa5d | 552 | * |
014c4014 TO |
553 | * @param string $subject |
554 | * Activity subject. | |
555 | * @param array $contactIds | |
556 | * Contact Id. | |
557 | * @param string $fileName | |
558 | * Gives the location with name of the file. | |
559 | * @param array $params | |
560 | * For invoices. | |
6efffa5d PB |
561 | * |
562 | */ | |
1330f57a | 563 | public static function addActivities($subject, $contactIds, $fileName, $params) { |
dce4c3ea | 564 | $session = CRM_Core_Session::singleton(); |
565 | $userID = $session->get('userID'); | |
6efffa5d PB |
566 | $config = CRM_Core_Config::singleton(); |
567 | $config->doNotAttachPDFReceipt = 1; | |
413796ce | 568 | |
569 | if (!empty($params['output']) && $params['output'] == 'pdf_invoice') { | |
ba133d87 PN |
570 | $activityTypeID = CRM_Core_PseudoConstant::getKey( |
571 | 'CRM_Activity_DAO_Activity', | |
572 | 'activity_type_id', | |
573 | 'Downloaded Invoice' | |
6efffa5d | 574 | ); |
dce4c3ea | 575 | } |
92e4c2a5 | 576 | else { |
ba133d87 PN |
577 | $activityTypeID = CRM_Core_PseudoConstant::getKey( |
578 | 'CRM_Activity_DAO_Activity', | |
579 | 'activity_type_id', | |
580 | 'Emailed Invoice' | |
6efffa5d PB |
581 | ); |
582 | } | |
413796ce | 583 | |
be2fb01f | 584 | $activityParams = [ |
6efffa5d PB |
585 | 'subject' => $subject, |
586 | 'source_contact_id' => $userID, | |
587 | 'target_contact_id' => $contactIds, | |
588 | 'activity_type_id' => $activityTypeID, | |
589 | 'activity_date_time' => date('YmdHis'), | |
be2fb01f | 590 | 'attachFile_1' => [ |
dce4c3ea | 591 | 'uri' => $fileName, |
592 | 'type' => 'application/pdf', | |
593 | 'location' => $fileName, | |
594 | 'upload_date' => date('YmdHis'), | |
be2fb01f CW |
595 | ], |
596 | ]; | |
dce4c3ea | 597 | CRM_Activity_BAO_Activity::create($activityParams); |
6efffa5d | 598 | } |
dce4c3ea | 599 | |
600 | /** | |
fe482240 | 601 | * Create the Invoice file in upload folder for attachment. |
dce4c3ea | 602 | * |
76e7a76c | 603 | * @param string $html |
014c4014 | 604 | * Content for pdf in html format. |
6efffa5d | 605 | * |
ad37ac8e | 606 | * @param string $name |
607 | * | |
03110609 | 608 | * @return string |
76e7a76c | 609 | * Name of file which is in pdf format |
6efffa5d | 610 | */ |
1330f57a | 611 | public static function putFile($html, $name = 'Invoice.pdf') { |
6ae4d5d7 | 612 | $options = new Options(); |
4f31930a | 613 | $options->set('isRemoteEnabled', TRUE); |
6ae4d5d7 | 614 | |
4f31930a | 615 | $doc = new DOMPDF($options); |
6efffa5d PB |
616 | $doc->load_html($html); |
617 | $doc->render(); | |
618 | $html = $doc->output(); | |
619 | $config = CRM_Core_Config::singleton(); | |
c119e8ae | 620 | $fileName = $config->uploadDir . $name; |
dce4c3ea | 621 | file_put_contents($fileName, $html); |
6efffa5d PB |
622 | return $fileName; |
623 | } | |
b5c3483d | 624 | |
625 | /** | |
626 | * Callback to perform action on Print Invoice button. | |
627 | */ | |
00be9182 | 628 | public static function getPrintPDF() { |
b5c3483d | 629 | $contributionId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject, FALSE); |
be2fb01f | 630 | $contributionIDs = [$contributionId]; |
b5c3483d | 631 | $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject, FALSE); |
be2fb01f | 632 | $params = ['output' => 'pdf_invoice']; |
1273d77c | 633 | CRM_Contribute_Form_Task_Invoice::printPDF($contributionIDs, $params, $contactId); |
b5c3483d | 634 | } |
96025800 | 635 | |
b8df2a80 | 636 | } |