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