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