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