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