Commit | Line | Data |
---|---|---|
b8df2a80 RK |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
b8df2a80 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 34 | * @copyright CiviCRM LLC (c) 2004-2016 |
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); |
b5c3483d | 226 | $this->printPDF($this->_contributionIds, $params, $this->_contactIds, $this); |
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. | |
238 | * @param CRM_Core_Form $form | |
239 | * Form object. | |
6efffa5d | 240 | */ |
00be9182 | 241 | public static function printPDF($contribIDs, &$params, $contactIds, &$form) { |
b8df2a80 | 242 | // get all the details needed to generate a invoice |
6efffa5d | 243 | $messageInvoice = array(); |
6efffa5d | 244 | $invoiceTemplate = CRM_Core_Smarty::singleton(); |
6efffa5d | 245 | $invoiceElements = CRM_Contribute_Form_Task_PDF::getElements($contribIDs, $params, $contactIds); |
dce4c3ea | 246 | |
3a1261ac | 247 | // gives the status id when contribution status is 'Refunded' |
6efffa5d PB |
248 | $contributionStatusID = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
249 | $refundedStatusId = CRM_Utils_Array::key('Refunded', $contributionStatusID); | |
41e050b3 | 250 | $cancelledStatusId = CRM_Utils_Array::key('Cancelled', $contributionStatusID); |
2f3e0ab6 | 251 | $pendingStatusId = CRM_Utils_Array::key('Pending', $contributionStatusID); |
f2d5e719 RK |
252 | |
253 | // getting data from admin page | |
aaffa79f | 254 | $prefixValue = Civi::settings()->get('contribution_invoice_settings'); |
dce4c3ea | 255 | |
2826a42e | 256 | foreach ($invoiceElements['details'] as $contribID => $detail) { |
b8df2a80 | 257 | $input = $ids = $objects = array(); |
3a1261ac | 258 | if (in_array($detail['contact'], $invoiceElements['excludeContactIds'])) { |
b8df2a80 RK |
259 | continue; |
260 | } | |
dce4c3ea | 261 | |
b8df2a80 | 262 | $input['component'] = $detail['component']; |
dce4c3ea | 263 | |
b8df2a80 | 264 | $ids['contact'] = $detail['contact']; |
2826a42e | 265 | $ids['contribution'] = $contribID; |
b8df2a80 RK |
266 | $ids['contributionRecur'] = NULL; |
267 | $ids['contributionPage'] = NULL; | |
268 | $ids['membership'] = CRM_Utils_Array::value('membership', $detail); | |
269 | $ids['participant'] = CRM_Utils_Array::value('participant', $detail); | |
270 | $ids['event'] = CRM_Utils_Array::value('event', $detail); | |
dce4c3ea | 271 | |
3a1261ac | 272 | if (!$invoiceElements['baseIPN']->validateData($input, $ids, $objects, FALSE)) { |
b8df2a80 RK |
273 | CRM_Core_Error::fatal(); |
274 | } | |
dce4c3ea | 275 | |
353ffa53 | 276 | $contribution = &$objects['contribution']; |
dce4c3ea | 277 | |
b8df2a80 RK |
278 | $input['amount'] = $contribution->total_amount; |
279 | $input['invoice_id'] = $contribution->invoice_id; | |
280 | $input['receive_date'] = $contribution->receive_date; | |
b8df2a80 RK |
281 | $input['contribution_status_id'] = $contribution->contribution_status_id; |
282 | $input['organization_name'] = $contribution->_relatedObjects['contact']->organization_name; | |
dce4c3ea | 283 | |
b8df2a80 | 284 | $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date); |
dce4c3ea | 285 | |
b8df2a80 RK |
286 | $addressParams = array('contact_id' => $contribution->contact_id); |
287 | $addressDetails = CRM_Core_BAO_Address::getValues($addressParams); | |
dce4c3ea | 288 | |
d75f2f47 | 289 | // to get billing address if present |
b8df2a80 RK |
290 | $billingAddress = array(); |
291 | foreach ($addressDetails as $key => $address) { | |
292 | if ((isset($address['is_billing']) && $address['is_billing'] == 1) && (isset($address['is_primary']) && $address['is_primary'] == 1) && $address['contact_id'] == $contribution->contact_id) { | |
293 | $billingAddress[$address['contact_id']] = $address; | |
294 | break; | |
295 | } | |
296 | elseif (($address['is_billing'] == 0 && $address['is_primary'] == 1) || (isset($address['is_billing']) && $address['is_billing'] == 1) && $address['contact_id'] == $contribution->contact_id) { | |
297 | $billingAddress[$address['contact_id']] = $address; | |
298 | } | |
299 | } | |
dce4c3ea | 300 | |
6efffa5d PB |
301 | if (!empty($billingAddress[$contribution->contact_id]['state_province_id'])) { |
302 | $stateProvinceAbbreviation = CRM_Core_PseudoConstant::stateProvinceAbbreviation($billingAddress[$contribution->contact_id]['state_province_id']); | |
303 | } | |
304 | else { | |
305 | $stateProvinceAbbreviation = ''; | |
306 | } | |
dce4c3ea | 307 | |
41e050b3 | 308 | if ($contribution->contribution_status_id == $refundedStatusId || $contribution->contribution_status_id == $cancelledStatusId) { |
7a48a449 | 309 | if (is_null($contribution->creditnote_id)) { |
4add5adb GC |
310 | $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId(); |
311 | CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'creditnote_id', $creditNoteId); | |
7a48a449 GC |
312 | } |
313 | else { | |
314 | $creditNoteId = $contribution->creditnote_id; | |
315 | } | |
b8df2a80 | 316 | } |
933c5f44 | 317 | $invoiceId = CRM_Utils_Array::value('invoice_prefix', $prefixValue) . "" . $contribution->id; |
dce4c3ea | 318 | |
b8df2a80 RK |
319 | //to obtain due date for PDF invoice |
320 | $contributionReceiveDate = date('F j,Y', strtotime(date($input['receive_date']))); | |
321 | $invoiceDate = date("F j, Y"); | |
dce4c3ea | 322 | $dueDate = date('F j ,Y', strtotime($contributionReceiveDate . "+" . $prefixValue['due_date'] . "" . $prefixValue['due_date_period'])); |
323 | ||
b8df2a80 | 324 | if ($input['component'] == 'contribute') { |
77dbdcbc | 325 | $lineItem = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribID); |
dce4c3ea | 326 | } |
b8df2a80 RK |
327 | else { |
328 | $eid = $contribution->_relatedObjects['participant']->id; | |
77dbdcbc | 329 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($eid, 'participant', NULL, TRUE, FALSE, TRUE); |
b8df2a80 | 330 | } |
dce4c3ea | 331 | |
d75f2f47 | 332 | //TO DO: Need to do changes for partially paid to display amount due on PDF invoice |
b8df2a80 | 333 | $amountDue = ($input['amount'] - $input['amount']); |
dce4c3ea | 334 | |
d75f2f47 | 335 | // retreiving the subtotal and sum of same tax_rate |
b8df2a80 RK |
336 | $dataArray = array(); |
337 | $subTotal = 0; | |
338 | foreach ($lineItem as $entity_id => $taxRate) { | |
dce4c3ea | 339 | if (isset($dataArray[(string) $taxRate['tax_rate']])) { |
340 | $dataArray[(string) $taxRate['tax_rate']] = $dataArray[(string) $taxRate['tax_rate']] + CRM_Utils_Array::value('tax_amount', $taxRate); | |
b8df2a80 RK |
341 | } |
342 | else { | |
dce4c3ea | 343 | $dataArray[(string) $taxRate['tax_rate']] = CRM_Utils_Array::value('tax_amount', $taxRate); |
b8df2a80 RK |
344 | } |
345 | $subTotal += CRM_Utils_Array::value('subTotal', $taxRate); | |
346 | } | |
dce4c3ea | 347 | |
b8df2a80 RK |
348 | // to email the invoice |
349 | $mailDetails = array(); | |
350 | $values = array(); | |
351 | if ($contribution->_component == 'event') { | |
352 | $daoName = 'CRM_Event_DAO_Event'; | |
353 | $pageId = $contribution->_relatedObjects['event']->id; | |
354 | $mailElements = array( | |
dce4c3ea | 355 | 'title', |
356 | 'confirm_from_name', | |
357 | 'confirm_from_email', | |
358 | 'cc_confirm', | |
359 | 'bcc_confirm', | |
360 | ); | |
b8df2a80 | 361 | CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements); |
b8df2a80 RK |
362 | $values['title'] = CRM_Utils_Array::value('title', $mailDetails[$contribution->_relatedObjects['event']->id]); |
363 | $values['confirm_from_name'] = CRM_Utils_Array::value('confirm_from_name', $mailDetails[$contribution->_relatedObjects['event']->id]); | |
364 | $values['confirm_from_email'] = CRM_Utils_Array::value('confirm_from_email', $mailDetails[$contribution->_relatedObjects['event']->id]); | |
365 | $values['cc_confirm'] = CRM_Utils_Array::value('cc_confirm', $mailDetails[$contribution->_relatedObjects['event']->id]); | |
366 | $values['bcc_confirm'] = CRM_Utils_Array::value('bcc_confirm', $mailDetails[$contribution->_relatedObjects['event']->id]); | |
dce4c3ea | 367 | |
b8df2a80 RK |
368 | $title = CRM_Utils_Array::value('title', $mailDetails[$contribution->_relatedObjects['event']->id]); |
369 | } | |
370 | elseif ($contribution->_component == 'contribute') { | |
371 | $daoName = 'CRM_Contribute_DAO_ContributionPage'; | |
372 | $pageId = $contribution->contribution_page_id; | |
373 | $mailElements = array( | |
dce4c3ea | 374 | 'title', |
375 | 'receipt_from_name', | |
376 | 'receipt_from_email', | |
377 | 'cc_receipt', | |
378 | 'bcc_receipt', | |
379 | ); | |
b8df2a80 | 380 | CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements); |
6efffa5d | 381 | |
dce4c3ea | 382 | $values['title'] = CRM_Utils_Array::value('title', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); |
6efffa5d PB |
383 | $values['receipt_from_name'] = CRM_Utils_Array::value('receipt_from_name', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); |
384 | $values['receipt_from_email'] = CRM_Utils_Array::value('receipt_from_email', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); | |
385 | $values['cc_receipt'] = CRM_Utils_Array::value('cc_receipt', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); | |
386 | $values['bcc_receipt'] = CRM_Utils_Array::value('bcc_receipt', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); | |
387 | ||
388 | $title = CRM_Utils_Array::value('title', CRM_Utils_Array::value($contribution->contribution_page_id, $mailDetails)); | |
b8df2a80 | 389 | } |
d88a0337 | 390 | $source = $contribution->source; |
dce4c3ea | 391 | |
b8df2a80 | 392 | $config = CRM_Core_Config::singleton(); |
c4d3b8d3 | 393 | if (!isset($params['forPage'])) { |
d88a0337 PD |
394 | $config->doNotAttachPDFReceipt = 1; |
395 | } | |
396 | ||
397 | // get organization address | |
398 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
48fcab8e | 399 | $locParams = array('contact_id' => $domain->contact_id); |
b69fc6b5 | 400 | $locationDefaults = CRM_Core_BAO_Location::getValues($locParams); |
d88a0337 | 401 | if (isset($locationDefaults['address'][1]['state_province_id'])) { |
dce4c3ea | 402 | $stateProvinceAbbreviationDomain = CRM_Core_PseudoConstant::stateProvinceAbbreviation($locationDefaults['address'][1]['state_province_id']); |
d88a0337 PD |
403 | } |
404 | else { | |
405 | $stateProvinceAbbreviationDomain = ''; | |
406 | } | |
407 | if (isset($locationDefaults['address'][1]['country_id'])) { | |
dce4c3ea | 408 | $countryDomain = CRM_Core_PseudoConstant::country($locationDefaults['address'][1]['country_id']); |
d88a0337 PD |
409 | } |
410 | else { | |
411 | $countryDomain = ''; | |
412 | } | |
6efffa5d | 413 | |
b8df2a80 RK |
414 | // parameters to be assign for template |
415 | $tplParams = array( | |
dce4c3ea | 416 | 'title' => $title, |
417 | 'component' => $input['component'], | |
418 | 'id' => $contribution->id, | |
419 | 'source' => $source, | |
420 | 'invoice_id' => $invoiceId, | |
9f8fe885 | 421 | 'resourceBase' => $config->userFrameworkResourceURL, |
dce4c3ea | 422 | 'defaultCurrency' => $config->defaultCurrency, |
423 | 'amount' => $contribution->total_amount, | |
424 | 'amountDue' => $amountDue, | |
425 | 'invoice_date' => $invoiceDate, | |
426 | 'dueDate' => $dueDate, | |
427 | 'notes' => CRM_Utils_Array::value('notes', $prefixValue), | |
428 | 'display_name' => $contribution->_relatedObjects['contact']->display_name, | |
429 | 'lineItem' => $lineItem, | |
430 | 'dataArray' => $dataArray, | |
431 | 'refundedStatusId' => $refundedStatusId, | |
2f3e0ab6 | 432 | 'pendingStatusId' => $pendingStatusId, |
41e050b3 | 433 | 'cancelledStatusId' => $cancelledStatusId, |
dce4c3ea | 434 | 'contribution_status_id' => $contribution->contribution_status_id, |
435 | 'subTotal' => $subTotal, | |
436 | 'street_address' => CRM_Utils_Array::value('street_address', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), | |
437 | 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), | |
438 | 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), | |
439 | 'city' => CRM_Utils_Array::value('city', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), | |
440 | 'stateProvinceAbbreviation' => $stateProvinceAbbreviation, | |
441 | 'postal_code' => CRM_Utils_Array::value('postal_code', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), | |
442 | 'is_pay_later' => $contribution->is_pay_later, | |
443 | 'organization_name' => $contribution->_relatedObjects['contact']->organization_name, | |
444 | 'domain_organization' => $domain->name, | |
445 | 'domain_street_address' => CRM_Utils_Array::value('street_address', CRM_Utils_Array::value('1', $locationDefaults['address'])), | |
446 | 'domain_supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', CRM_Utils_Array::value('1', $locationDefaults['address'])), | |
447 | 'domain_supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', CRM_Utils_Array::value('1', $locationDefaults['address'])), | |
448 | 'domain_city' => CRM_Utils_Array::value('city', CRM_Utils_Array::value('1', $locationDefaults['address'])), | |
449 | 'domain_postal_code' => CRM_Utils_Array::value('postal_code', CRM_Utils_Array::value('1', $locationDefaults['address'])), | |
450 | 'domain_state' => $stateProvinceAbbreviationDomain, | |
451 | 'domain_country' => $countryDomain, | |
452 | 'domain_email' => CRM_Utils_Array::value('email', CRM_Utils_Array::value('1', $locationDefaults['email'])), | |
453 | 'domain_phone' => CRM_Utils_Array::value('phone', CRM_Utils_Array::value('1', $locationDefaults['phone'])), | |
454 | ); | |
dc0bdd34 | 455 | |
c4d3b8d3 PD |
456 | if (isset($creditNoteId)) { |
457 | $tplParams['creditnote_id'] = $creditNoteId; | |
458 | } | |
d75f2f47 | 459 | |
c119e8ae | 460 | $pdfFileName = "{$invoiceId}.pdf"; |
b8df2a80 | 461 | $sendTemplateParams = array( |
dce4c3ea | 462 | 'groupName' => 'msg_tpl_workflow_contribution', |
463 | 'valueName' => 'contribution_invoice_receipt', | |
464 | 'contactId' => $contribution->contact_id, | |
465 | 'tplParams' => $tplParams, | |
c119e8ae | 466 | 'PDFFilename' => $pdfFileName, |
dce4c3ea | 467 | ); |
6f39f13a | 468 | $session = CRM_Core_Session::singleton(); |
e14cf9e2 | 469 | $contactID = $session->get('userID'); |
3f81ea25 | 470 | //CRM-16319 - we dont store in userID in case the user is doing multiple |
471 | //transactions etc | |
e14cf9e2 | 472 | if (empty($contactID)) { |
473 | $contactID = $session->get('transaction.userID'); | |
474 | } | |
0a6a6d20 C |
475 | // Fix Invoice email doesnot send out when completed payment using Paypal |
476 | if (empty($contactID)) { | |
4d0cca48 | 477 | $contactID = current($contactIds); |
0a6a6d20 | 478 | } |
6f39f13a PB |
479 | $contactEmails = CRM_Core_BAO_Email::allEmails($contactID); |
480 | $emails = array(); | |
481 | $fromDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
dce4c3ea | 482 | $contactID, 'display_name' |
483 | ); | |
6f39f13a PB |
484 | |
485 | foreach ($contactEmails as $emailId => $item) { | |
486 | $email = $item['email']; | |
487 | if ($email) { | |
488 | $emails[$emailId] = '"' . $fromDisplayName . '" <' . $email . '> '; | |
489 | } | |
490 | } | |
491 | $fromEmail = CRM_Utils_Array::crmArrayMerge($emails, CRM_Core_OptionGroup::values('from_email_address')); | |
dce4c3ea | 492 | |
6efffa5d PB |
493 | // from email address |
494 | if (isset($params['from_email_address'])) { | |
6f39f13a | 495 | $fromEmailAddress = CRM_Utils_Array::value($params['from_email_address'], $fromEmail); |
6efffa5d | 496 | } |
b8df2a80 | 497 | // condition to check for download PDF Invoice or email Invoice |
3a1261ac | 498 | if ($invoiceElements['createPdf']) { |
b8df2a80 | 499 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
c4d3b8d3 | 500 | if (isset($params['forPage'])) { |
d88a0337 PD |
501 | return $html; |
502 | } | |
b8df2a80 | 503 | else { |
d88a0337 PD |
504 | $mail = array( |
505 | 'subject' => $subject, | |
dce4c3ea | 506 | 'body' => $message, |
d88a0337 | 507 | 'html' => $html, |
dce4c3ea | 508 | ); |
d88a0337 PD |
509 | if ($mail['html']) { |
510 | $messageInvoice[] = $mail['html']; | |
dce4c3ea | 511 | } |
d88a0337 PD |
512 | else { |
513 | $messageInvoice[] = nl2br($mail['body']); | |
514 | } | |
b8df2a80 RK |
515 | } |
516 | } | |
517 | elseif ($contribution->_component == 'contribute') { | |
518 | $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id); | |
dce4c3ea | 519 | |
520 | $sendTemplateParams['tplParams'] = array_merge($tplParams, array('email_comment' => $invoiceElements['params']['email_comment'])); | |
6efffa5d | 521 | $sendTemplateParams['from'] = $fromEmailAddress; |
b8df2a80 RK |
522 | $sendTemplateParams['toEmail'] = $email; |
523 | $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_receipt', $values); | |
524 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $values); | |
dce4c3ea | 525 | |
526 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); | |
6efffa5d | 527 | // functions call for adding activity with attachment |
c119e8ae | 528 | $pdfFileName = "{$invoiceId}.pdf"; |
529 | $fileName = self::putFile($html, $pdfFileName); | |
6efffa5d | 530 | self::addActivities($subject, $contribution->contact_id, $fileName, $params); |
b8df2a80 RK |
531 | } |
532 | elseif ($contribution->_component == 'event') { | |
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_confirm', $values); | |
539 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_confirm', $values); | |
dce4c3ea | 540 | |
541 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); | |
6efffa5d | 542 | // functions call for adding activity with attachment |
c119e8ae | 543 | $pdfFileName = "{$invoiceId}.pdf"; |
544 | $fileName = self::putFile($html, $pdfFileName); | |
6efffa5d | 545 | self::addActivities($subject, $contribution->contact_id, $fileName, $params); |
b8df2a80 | 546 | } |
dce4c3ea | 547 | |
548 | CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'invoice_id', $invoiceId); | |
6efffa5d | 549 | $invoiceTemplate->clearTemplateVars(); |
b8df2a80 | 550 | } |
dce4c3ea | 551 | |
3a1261ac | 552 | if ($invoiceElements['createPdf']) { |
c4d3b8d3 | 553 | if (isset($params['forPage'])) { |
d88a0337 PD |
554 | return $html; |
555 | } | |
556 | else { | |
c119e8ae | 557 | $pdfFileName = "{$invoiceId}.pdf"; |
558 | CRM_Utils_PDF_Utils::html2pdf($messageInvoice, $pdfFileName, FALSE, array( | |
dce4c3ea | 559 | 'margin_top' => 10, |
560 | 'margin_left' => 65, | |
21dfd5f5 | 561 | 'metric' => 'px', |
dce4c3ea | 562 | )); |
d88a0337 | 563 | // functions call for adding activity with attachment |
c119e8ae | 564 | $fileName = self::putFile($html, $pdfFileName); |
413796ce | 565 | self::addActivities($subject, $contactIds, $fileName, $params); |
6efffa5d | 566 | |
d88a0337 PD |
567 | CRM_Utils_System::civiExit(); |
568 | } | |
b8df2a80 RK |
569 | } |
570 | else { | |
3a1261ac RK |
571 | if ($invoiceElements['suppressedEmails']) { |
572 | $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 |
573 | $msgTitle = ts('Email Error'); |
574 | $msgType = 'error'; | |
575 | } | |
576 | else { | |
577 | $status = ts('Your mail has been sent.'); | |
578 | $msgTitle = ts('Sent'); | |
579 | $msgType = 'success'; | |
580 | } | |
581 | CRM_Core_Session::setStatus($status, $msgTitle, $msgType); | |
582 | } | |
583 | } | |
6efffa5d | 584 | |
dce4c3ea | 585 | /** |
fe482240 | 586 | * Add activity for Email Invoice and the PDF Invoice. |
6efffa5d | 587 | * |
014c4014 TO |
588 | * @param string $subject |
589 | * Activity subject. | |
590 | * @param array $contactIds | |
591 | * Contact Id. | |
592 | * @param string $fileName | |
593 | * Gives the location with name of the file. | |
594 | * @param array $params | |
595 | * For invoices. | |
6efffa5d PB |
596 | * |
597 | */ | |
413796ce | 598 | static public function addActivities($subject, $contactIds, $fileName, $params) { |
dce4c3ea | 599 | $session = CRM_Core_Session::singleton(); |
600 | $userID = $session->get('userID'); | |
6efffa5d PB |
601 | $config = CRM_Core_Config::singleton(); |
602 | $config->doNotAttachPDFReceipt = 1; | |
413796ce | 603 | |
604 | if (!empty($params['output']) && $params['output'] == 'pdf_invoice') { | |
6efffa5d | 605 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', |
413796ce | 606 | 'Downloaded Invoice', |
6efffa5d PB |
607 | 'name' |
608 | ); | |
dce4c3ea | 609 | } |
92e4c2a5 | 610 | else { |
6efffa5d | 611 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', |
413796ce | 612 | 'Emailed Invoice', |
6efffa5d PB |
613 | 'name' |
614 | ); | |
615 | } | |
413796ce | 616 | |
6efffa5d PB |
617 | $activityParams = array( |
618 | 'subject' => $subject, | |
619 | 'source_contact_id' => $userID, | |
620 | 'target_contact_id' => $contactIds, | |
621 | 'activity_type_id' => $activityTypeID, | |
622 | 'activity_date_time' => date('YmdHis'), | |
dce4c3ea | 623 | 'attachFile_1' => array( |
624 | 'uri' => $fileName, | |
625 | 'type' => 'application/pdf', | |
626 | 'location' => $fileName, | |
627 | 'upload_date' => date('YmdHis'), | |
628 | ), | |
6efffa5d | 629 | ); |
dce4c3ea | 630 | CRM_Activity_BAO_Activity::create($activityParams); |
6efffa5d | 631 | } |
dce4c3ea | 632 | |
633 | /** | |
fe482240 | 634 | * Create the Invoice file in upload folder for attachment. |
dce4c3ea | 635 | * |
76e7a76c | 636 | * @param string $html |
014c4014 | 637 | * Content for pdf in html format. |
6efffa5d | 638 | * |
ad37ac8e | 639 | * @param string $name |
640 | * | |
03110609 | 641 | * @return string |
76e7a76c | 642 | * Name of file which is in pdf format |
6efffa5d | 643 | */ |
c119e8ae | 644 | static public function putFile($html, $name = 'Invoice.pdf') { |
6ae4d5d7 | 645 | $options = new Options(); |
4f31930a | 646 | $options->set('isRemoteEnabled', TRUE); |
6ae4d5d7 | 647 | |
4f31930a | 648 | $doc = new DOMPDF($options); |
6efffa5d PB |
649 | $doc->load_html($html); |
650 | $doc->render(); | |
651 | $html = $doc->output(); | |
652 | $config = CRM_Core_Config::singleton(); | |
c119e8ae | 653 | $fileName = $config->uploadDir . $name; |
dce4c3ea | 654 | file_put_contents($fileName, $html); |
6efffa5d PB |
655 | return $fileName; |
656 | } | |
b5c3483d | 657 | |
658 | /** | |
659 | * Callback to perform action on Print Invoice button. | |
660 | */ | |
00be9182 | 661 | public static function getPrintPDF() { |
b5c3483d | 662 | $contributionId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject, FALSE); |
663 | $contributionIDs = array($contributionId); | |
664 | $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject, FALSE); | |
665 | $params = array('output' => 'pdf_invoice'); | |
666 | CRM_Contribute_Form_Task_Invoice::printPDF($contributionIDs, $params, $contactId, CRM_Core_DAO::$_nullObject); | |
667 | } | |
96025800 | 668 | |
b8df2a80 | 669 | } |