Merge pull request #13 from dpradeep/VAT-434
[civicrm-core.git] / CRM / Contribute / Form / Task / Invoice.php
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 {
40 /**
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
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;
68 /**
69 * build all the data structures needed to build the form
70 *
71 * @return void
72 * @access public
73 */
74 function preProcess() {
75 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
76 if ($id) {
77 $this->_contributionIds = array($id);
78 $this->_componentClause = " civicrm_contribution.id IN ( $id ) ";
79 $this->_single = TRUE;
80 $this->assign('totalSelectedContributions', 1);
81 }
82 else {
83 parent::preProcess();
84 }
85
86 // check that all the contribution ids have status Completed, Pending, Refunded.
87 $this->_contributionStatusId = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
88 $status = array('Completed', 'Pending', 'Refunded');
89 $statusId = array();
90 foreach ($this->_contributionStatusId as $key => $value) {
91 if (in_array($value, $status)) {
92 $statusId[] = $key;
93 }
94 }
95 $Id = implode(",", $statusId);
96 $query = "SELECT count(*) FROM civicrm_contribution WHERE contribution_status_id NOT IN ($Id) AND {$this->_componentClause}";
97 $count = CRM_Core_DAO::singleValueQuery($query);
98 if ($count != 0) {
99 CRM_Core_Error::statusBounce(ts('Please select only contributions with Completed, Pending, Refunded status.'));
100 }
101
102 // we have all the contribution ids, so now we get the contact ids
103 parent::setContactIDs();
104 $this->assign('single', $this->_single);
105
106 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
107 $urlParams = 'force=1';
108 if (CRM_Utils_Rule::qfKey($qfKey)) {
109 $urlParams .= "&qfKey=$qfKey";
110 }
111
112 $url = CRM_Utils_System::url('civicrm/contribute/search', $urlParams);
113 $breadCrumb = array(
114 array('url' => $url,
115 'title' => ts('Search Results'),
116 ));
117
118 CRM_Utils_System::appendBreadCrumb($breadCrumb);
119 CRM_Utils_System::setTitle(ts('Print Contribution Invoice'));
120 }
121
122 /**
123 * Build the form
124 *
125 * @access public
126 *
127 * @return void
128 */
129 public function buildQuickForm() {
130 $this->addElement('radio', 'output', NULL, ts('Email Invoice'), 'email_invoice',
131 array('onClick' => "document.getElementById('selectPdfFormat').style.display = 'none';document.getElementById ('comment').style.display = 'block';")
132 );
133 $this->addElement('radio', 'output', NULL, ts('PDF Invoice'), 'pdf_invoice',
134 array('onClick' => "document.getElementById('comment').style.display = 'none';document.getElementById('selectPdfFormat').style.display = 'block';")
135 );
136 $this->addRule('output', ts('Selection required'), 'required');
137 $this->add('textarea', 'email_comment', ts('If you would like to add personal message to email please add it here. (The same messages will sent to all receipients.)'));
138 $this->addButtons(array(
139 array(
140 'type' => 'next',
141 'name' => ts('Process Invoice(s)'),
142 'isDefault' => TRUE,
143 ),
144 array(
145 'type' => 'back',
146 'name' => ts('Cancel'),
147 ),
148 )
149 );
150 }
151
152 /**
153 * process the form after the input has been submitted and validated
154 *
155 * @access public
156 *
157 * @return void
158 */
159 public function postProcess() {
160 // get all the details needed to generate a invoice
161 $this->_messageInvoice = array();
162
163 $this->_invoiceTemplate = CRM_Core_Smarty::singleton();
164
165 $invoiceElements = CRM_Contribute_Form_Task_PDF::getElements();
166
167 // gives the status id when contribution status is 'Refunded'
168 $refundedStatusId = CRM_Utils_Array::key('Refunded', $this->_contributionStatusId);
169
170 // getting data from admin page
171 $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings');
172
173 foreach ($invoiceElements['details'] as $contribID => $detail) {
174 $input = $ids = $objects = array();
175
176 if (in_array($detail['contact'], $invoiceElements['excludeContactIds'])) {
177 continue;
178 }
179
180 $input['component'] = $detail['component'];
181
182 $ids['contact'] = $detail['contact'];
183 $ids['contribution'] = $contribID;
184 $ids['contributionRecur'] = NULL;
185 $ids['contributionPage'] = NULL;
186 $ids['membership'] = CRM_Utils_Array::value('membership', $detail);
187 $ids['participant'] = CRM_Utils_Array::value('participant', $detail);
188 $ids['event'] = CRM_Utils_Array::value('event', $detail);
189
190 if (!$invoiceElements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
191 CRM_Core_Error::fatal();
192 }
193
194 $contribution = &$objects['contribution'];
195
196 $input['amount'] = $contribution->total_amount;
197 $input['invoice_id'] = $contribution->invoice_id;
198 $input['receive_date'] = $contribution->receive_date;
199 $input['contribution_status_id'] = $contribution->contribution_status_id;
200 $input['organization_name'] = $contribution->_relatedObjects['contact']->organization_name;
201
202 $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
203
204 $addressParams = array('contact_id' => $contribution->contact_id);
205 $addressDetails = CRM_Core_BAO_Address::getValues($addressParams);
206
207 // to get billing address if present
208 $billingAddress = array();
209 foreach ($addressDetails as $key => $address) {
210 if ((isset($address['is_billing']) && $address['is_billing'] == 1) && (isset($address['is_primary']) && $address['is_primary'] == 1) && $address['contact_id'] == $contribution->contact_id) {
211 $billingAddress[$address['contact_id']] = $address;
212 break;
213 }
214 elseif (($address['is_billing'] == 0 && $address['is_primary'] == 1) || (isset($address['is_billing']) && $address['is_billing'] == 1) && $address['contact_id'] == $contribution->contact_id) {
215 $billingAddress[$address['contact_id']] = $address;
216 }
217 }
218
219 $stateProvinceAbbreviation = CRM_Core_PseudoConstant::stateProvinceAbbreviation($billingAddress[$contribution->contact_id]['state_province_id']);
220
221 if ($contribution->contribution_status_id == $refundedStatusId) {
222 $invoiceId = CRM_Utils_Array::value('credit_notes_prefix', $prefixValue). "" .$contribution->id;
223 }
224 else {
225 $invoiceId = CRM_Utils_Array::value('invoice_prefix', $prefixValue). "" .$contribution->id;
226 }
227
228 //to obtain due date for PDF invoice
229 $contributionReceiveDate = date('F j,Y', strtotime(date($input['receive_date'])));
230 $invoiceDate = date("F j, Y");
231 $dueDate = date('F j ,Y', strtotime($contributionReceiveDate. "+" .$prefixValue['due_date']. "" .$prefixValue['due_date_period']));
232
233 if ($input['component'] == 'contribute') {
234 $eid = $contribID;
235 $etable = 'contribution';
236 }
237 else {
238 $eid = $contribution->_relatedObjects['participant']->id;
239 $etable = 'participant';
240 }
241
242 //TO DO: Need to do changes for partially paid to display amount due on PDF invoice
243 $amountDue = ($input['amount'] - $input['amount']);
244
245 // retreiving the subtotal and sum of same tax_rate
246 $lineItem = CRM_Price_BAO_LineItem::getLineItems($eid, $etable);
247 $dataArray = array();
248 $subTotal = 0;
249 foreach ($lineItem as $entity_id => $taxRate) {
250 if (isset($dataArray[$taxRate['tax_rate']])) {
251 $dataArray[$taxRate['tax_rate']] = $dataArray[$taxRate['tax_rate']] + CRM_Utils_Array::value('tax_amount', $taxRate);
252 }
253 else {
254 $dataArray[$taxRate['tax_rate']] = CRM_Utils_Array::value('tax_amount', $taxRate);
255 }
256 $subTotal += CRM_Utils_Array::value('subTotal', $taxRate);
257 }
258
259 // to email the invoice
260 $mailDetails = array();
261 $values = array();
262 if ($contribution->_component == 'event') {
263 $daoName = 'CRM_Event_DAO_Event';
264 $pageId = $contribution->_relatedObjects['event']->id;
265 $mailElements = array(
266 'title',
267 'confirm_from_name',
268 'confirm_from_email',
269 'cc_confirm',
270 'bcc_confirm',
271 );
272 CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
273
274 $values['title'] = CRM_Utils_Array::value('title', $mailDetails[$contribution->_relatedObjects['event']->id]);
275 $values['confirm_from_name'] = CRM_Utils_Array::value('confirm_from_name', $mailDetails[$contribution->_relatedObjects['event']->id]);
276 $values['confirm_from_email'] = CRM_Utils_Array::value('confirm_from_email', $mailDetails[$contribution->_relatedObjects['event']->id]);
277 $values['cc_confirm'] = CRM_Utils_Array::value('cc_confirm', $mailDetails[$contribution->_relatedObjects['event']->id]);
278 $values['bcc_confirm'] = CRM_Utils_Array::value('bcc_confirm', $mailDetails[$contribution->_relatedObjects['event']->id]);
279
280 $title = CRM_Utils_Array::value('title', $mailDetails[$contribution->_relatedObjects['event']->id]);
281 }
282 elseif ($contribution->_component == 'contribute') {
283 $daoName = 'CRM_Contribute_DAO_ContributionPage';
284 $pageId = $contribution->contribution_page_id;
285 $mailElements = array(
286 'title',
287 'receipt_from_name',
288 'receipt_from_email',
289 'cc_receipt',
290 'bcc_receipt',
291 );
292 CRM_Core_DAO::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
293
294 $values['title'] = CRM_Utils_Array::value('title',$mailDetails[$contribution->contribution_page_id]);
295 $values['receipt_from_name'] = CRM_Utils_Array::value('receipt_from_name', $mailDetails[$contribution->contribution_page_id]);
296 $values['receipt_from_email'] = CRM_Utils_Array::value('receipt_from_email', $mailDetails[$contribution->contribution_page_id]);
297 $values['cc_receipt'] = CRM_Utils_Array::value('cc_receipt', $mailDetails[$contribution->contribution_page_id]);
298 $values['bcc_receipt'] = CRM_Utils_Array::value('bcc_receipt', $mailDetails[$contribution->contribution_page_id]);
299
300 $title = CRM_Utils_Array::value('title', $mailDetails[$contribution->contribution_page_id]);
301 }
302
303 $config = CRM_Core_Config::singleton();
304 $config->doNotAttachPDFReceipt = 1;
305
306 // parameters to be assign for template
307 $tplParams = array(
308 'title' => $title,
309 'component' => $input['component'],
310 'id' => $contribution->id,
311 'invoice_id' => $invoiceId,
312 'imageUploadURL' => $config->imageUploadURL,
313 'defaultCurrency' => $config->defaultCurrency,
314 'amount' => $contribution->total_amount,
315 'amountDue' => $amountDue,
316 'invoice_date' => $invoiceDate,
317 'dueDate' => $dueDate,
318 'notes' => CRM_Utils_Array::value('notes', $prefixValue),
319 'display_name' => $contribution->_relatedObjects['contact']->display_name,
320 'lineItem' => $lineItem,
321 'dataArray' => $dataArray,
322 'refundedStatusId' => $refundedStatusId,
323 'contribution_status_id' => $contribution->contribution_status_id,
324 'subTotal' => $subTotal,
325 'street_address' => CRM_Utils_Array::value('street_address', $billingAddress[$contribution->contact_id]),
326 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', $billingAddress[$contribution->contact_id]),
327 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', $billingAddress[$contribution->contact_id]),
328 'city' => CRM_Utils_Array::value('city', $billingAddress[$contribution->contact_id]),
329 'stateProvinceAbbreviation' => $stateProvinceAbbreviation,
330 'postal_code' => CRM_Utils_Array::value('postal_code', $billingAddress[$contribution->contact_id]),
331 'is_pay_later' => $contribution->is_pay_later,
332 'organization_name' => $contribution->_relatedObjects['contact']->organization_name,
333 );
334
335 $sendTemplateParams = array(
336 'groupName' => 'msg_tpl_workflow_contribution',
337 'valueName' => 'contribution_invoice_receipt',
338 'contactId' => $contribution->contact_id,
339 'tplParams' => $tplParams,
340 'PDFFilename' => 'Invoice.pdf',
341 );
342
343
344 // condition to check for download PDF Invoice or email Invoice
345 if ($invoiceElements['createPdf']) {
346 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
347 $mail = array(
348 'subject' => $subject,
349 'body' => $message,
350 'html' => $html,
351 );
352
353 if ($mail['html']) {
354 $this->_messageInvoice[] = $mail['html'];
355 }
356 else {
357 $this->_messageInvoice[] = nl2br($mail['body']);
358 }
359 }
360 elseif ($contribution->_component == 'contribute') {
361 $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id);
362
363 $sendTemplateParams['tplParams'] = array_merge($tplParams,array('email_comment' => $invoiceElements['params']['email_comment']));
364 $sendTemplateParams['from'] = CRM_Utils_Array::value('receipt_from_name', $values) . ' <' . $mailDetails[$contribution->contribution_page_id]['receipt_from_email']. '>';
365 $sendTemplateParams['toEmail'] = $email;
366 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_receipt', $values);
367 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $values);
368
369 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
370 }
371 elseif ($contribution->_component == 'event') {
372 $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id);
373
374 $sendTemplateParams['tplParams'] = array_merge($tplParams,array('email_comment' => $invoiceElements['params']['email_comment']));
375 $sendTemplateParams['from'] = CRM_Utils_Array::value('confirm_from_name', $values) . ' <' . $mailDetails[$contribution->_relatedObjects['event']->id]['confirm_from_email']. '>';
376 $sendTemplateParams['toEmail'] = $email;
377 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_confirm', $values);
378 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_confirm', $values);
379
380 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
381 }
382
383 $updateInvoiceId = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'invoice_id', $invoiceId);
384 $this->_invoiceTemplate->clearTemplateVars();
385 }
386
387 if ($invoiceElements['createPdf']) {
388 CRM_Utils_PDF_Utils::html2pdf($this->_messageInvoice, 'Invoice.pdf', FALSE);
389 CRM_Utils_System::civiExit();
390 }
391 else {
392 if ($invoiceElements['suppressedEmails']) {
393 $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']));
394 $msgTitle = ts('Email Error');
395 $msgType = 'error';
396 }
397 else {
398 $status = ts('Your mail has been sent.');
399 $msgTitle = ts('Sent');
400 $msgType = 'success';
401 }
402 CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
403 }
404 }
405 }
406