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