3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 * This class provides the functionality to email a group of
39 class CRM_Contribute_Form_Task_Invoice
extends CRM_Contribute_Form_Task
{
41 * Are we operating in "single mode", i.e. updating the task of only
42 * one specific contribution?
46 public $_single = FALSE;
49 * gives all the statues for conribution
53 public $_contributionStatusId;
56 * gives the HTML template of PDF Invoice
60 public $_messageInvoice;
63 * This variable is used to assign parameters for HTML template of PDF Invoice
67 public $_invoiceTemplate;
69 * build all the data structures needed to build the form
74 function preProcess() {
75 $id = CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE);
77 $this->_contributionIds
= array($id);
78 $this->_componentClause
= " civicrm_contribution.id IN ( $id ) ";
79 $this->_single
= TRUE;
80 $this->assign('totalSelectedContributions', 1);
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');
90 foreach ($this->_contributionStatusId
as $key => $value) {
91 if (in_array($value, $status)) {
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);
99 CRM_Core_Error
::statusBounce(ts('Please select only contributions with Completed, Pending, Refunded status.'));
102 // we have all the contribution ids, so now we get the contact ids
103 parent
::setContactIDs();
104 $this->assign('single', $this->_single
);
106 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
107 $urlParams = 'force=1';
108 if (CRM_Utils_Rule
::qfKey($qfKey)) {
109 $urlParams .= "&qfKey=$qfKey";
112 $url = CRM_Utils_System
::url('civicrm/contribute/search', $urlParams);
115 'title' => ts('Search Results'),
118 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
119 CRM_Utils_System
::setTitle(ts('Print Contribution Invoice'));
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';")
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';")
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(
141 'name' => ts('Process Invoice(s)'),
146 'name' => ts('Cancel'),
153 * process the form after the input has been submitted and validated
159 public function postProcess() {
160 // get all the details needed to generate a invoice
161 $this->_messageInvoice
= array();
163 $this->_invoiceTemplate
= CRM_Core_Smarty
::singleton();
165 $invoiceElements = CRM_Contribute_Form_Task_PDF
::getElements();
167 // gives the status id when contribution status is 'Refunded'
168 $refundedStatusId = CRM_Utils_Array
::key('Refunded', $this->_contributionStatusId
);
170 // getting data from admin page
171 $prefixValue = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CONTRIBUTE_PREFERENCES_NAME
,'contribution_invoice_settings');
173 foreach ($invoiceElements['details'] as $contribID => $detail) {
174 $input = $ids = $objects = array();
176 if (in_array($detail['contact'], $invoiceElements['excludeContactIds'])) {
180 $input['component'] = $detail['component'];
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);
190 if (!$invoiceElements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
191 CRM_Core_Error
::fatal();
194 $contribution = &$objects['contribution'];
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
;
202 $objects['contribution']->receive_date
= CRM_Utils_Date
::isoToMysql($objects['contribution']->receive_date
);
204 $addressParams = array('contact_id' => $contribution->contact_id
);
205 $addressDetails = CRM_Core_BAO_Address
::getValues($addressParams);
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;
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;
219 $stateProvinceAbbreviation = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($billingAddress[$contribution->contact_id
]['state_province_id']);
221 if ($contribution->contribution_status_id
== $refundedStatusId) {
222 $creditNoteId = CRM_Utils_Array
::value('credit_notes_prefix', $prefixValue) . "" . $contribution->id
;
224 $invoiceId = CRM_Utils_Array
::value('invoice_prefix', $prefixValue) . "" . $contribution->id
;
226 //to obtain due date for PDF invoice
227 $contributionReceiveDate = date('F j,Y', strtotime(date($input['receive_date'])));
228 $invoiceDate = date("F j, Y");
229 $dueDate = date('F j ,Y', strtotime($contributionReceiveDate. "+" .$prefixValue['due_date']. "" .$prefixValue['due_date_period']));
231 if ($input['component'] == 'contribute') {
233 $etable = 'contribution';
236 $eid = $contribution->_relatedObjects
['participant']->id
;
237 $etable = 'participant';
240 //TO DO: Need to do changes for partially paid to display amount due on PDF invoice
241 $amountDue = ($input['amount'] - $input['amount']);
243 // retreiving the subtotal and sum of same tax_rate
244 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($eid, $etable);
245 $dataArray = array();
247 foreach ($lineItem as $entity_id => $taxRate) {
248 if (isset($dataArray[$taxRate['tax_rate']])) {
249 $dataArray[$taxRate['tax_rate']] = $dataArray[$taxRate['tax_rate']] + CRM_Utils_Array
::value('tax_amount', $taxRate);
252 $dataArray[$taxRate['tax_rate']] = CRM_Utils_Array
::value('tax_amount', $taxRate);
254 $subTotal +
= CRM_Utils_Array
::value('subTotal', $taxRate);
257 // to email the invoice
258 $mailDetails = array();
260 if ($contribution->_component
== 'event') {
261 $daoName = 'CRM_Event_DAO_Event';
262 $pageId = $contribution->_relatedObjects
['event']->id
;
263 $mailElements = array(
266 'confirm_from_email',
270 CRM_Core_DAO
::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
272 $values['title'] = CRM_Utils_Array
::value('title', $mailDetails[$contribution->_relatedObjects
['event']->id
]);
273 $values['confirm_from_name'] = CRM_Utils_Array
::value('confirm_from_name', $mailDetails[$contribution->_relatedObjects
['event']->id
]);
274 $values['confirm_from_email'] = CRM_Utils_Array
::value('confirm_from_email', $mailDetails[$contribution->_relatedObjects
['event']->id
]);
275 $values['cc_confirm'] = CRM_Utils_Array
::value('cc_confirm', $mailDetails[$contribution->_relatedObjects
['event']->id
]);
276 $values['bcc_confirm'] = CRM_Utils_Array
::value('bcc_confirm', $mailDetails[$contribution->_relatedObjects
['event']->id
]);
278 $title = CRM_Utils_Array
::value('title', $mailDetails[$contribution->_relatedObjects
['event']->id
]);
280 elseif ($contribution->_component
== 'contribute') {
281 $daoName = 'CRM_Contribute_DAO_ContributionPage';
282 $pageId = $contribution->contribution_page_id
;
283 $mailElements = array(
286 'receipt_from_email',
290 CRM_Core_DAO
::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
292 $values['title'] = CRM_Utils_Array
::value('title',$mailDetails[$contribution->contribution_page_id
]);
293 $values['receipt_from_name'] = CRM_Utils_Array
::value('receipt_from_name', $mailDetails[$contribution->contribution_page_id
]);
294 $values['receipt_from_email'] = CRM_Utils_Array
::value('receipt_from_email', $mailDetails[$contribution->contribution_page_id
]);
295 $values['cc_receipt'] = CRM_Utils_Array
::value('cc_receipt', $mailDetails[$contribution->contribution_page_id
]);
296 $values['bcc_receipt'] = CRM_Utils_Array
::value('bcc_receipt', $mailDetails[$contribution->contribution_page_id
]);
298 $title = CRM_Utils_Array
::value('title', $mailDetails[$contribution->contribution_page_id
]);
301 $config = CRM_Core_Config
::singleton();
302 $config->doNotAttachPDFReceipt
= 1;
304 // parameters to be assign for template
307 'component' => $input['component'],
308 'id' => $contribution->id
,
309 'invoice_id' => $invoiceId,
310 'creditnote_id' => $creditNoteId,
311 'imageUploadURL' => $config->imageUploadURL
,
312 'defaultCurrency' => $config->defaultCurrency
,
313 'amount' => $contribution->total_amount
,
314 'amountDue' => $amountDue,
315 'invoice_date' => $invoiceDate,
316 'dueDate' => $dueDate,
317 'notes' => CRM_Utils_Array
::value('notes', $prefixValue),
318 'display_name' => $contribution->_relatedObjects
['contact']->display_name
,
319 'lineItem' => $lineItem,
320 'dataArray' => $dataArray,
321 'refundedStatusId' => $refundedStatusId,
322 'contribution_status_id' => $contribution->contribution_status_id
,
323 'subTotal' => $subTotal,
324 'street_address' => CRM_Utils_Array
::value('street_address', $billingAddress[$contribution->contact_id
]),
325 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1', $billingAddress[$contribution->contact_id
]),
326 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2', $billingAddress[$contribution->contact_id
]),
327 'city' => CRM_Utils_Array
::value('city', $billingAddress[$contribution->contact_id
]),
328 'stateProvinceAbbreviation' => $stateProvinceAbbreviation,
329 'postal_code' => CRM_Utils_Array
::value('postal_code', $billingAddress[$contribution->contact_id
]),
330 'is_pay_later' => $contribution->is_pay_later
,
331 'organization_name' => $contribution->_relatedObjects
['contact']->organization_name
,
334 $sendTemplateParams = array(
335 'groupName' => 'msg_tpl_workflow_contribution',
336 'valueName' => 'contribution_invoice_receipt',
337 'contactId' => $contribution->contact_id
,
338 'tplParams' => $tplParams,
339 'PDFFilename' => 'Invoice.pdf',
343 // condition to check for download PDF Invoice or email Invoice
344 if ($invoiceElements['createPdf']) {
345 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
347 'subject' => $subject,
353 $this->_messageInvoice
[] = $mail['html'];
356 $this->_messageInvoice
[] = nl2br($mail['body']);
359 elseif ($contribution->_component
== 'contribute') {
360 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contribution->contact_id
);
362 $sendTemplateParams['tplParams'] = array_merge($tplParams,array('email_comment' => $invoiceElements['params']['email_comment']));
363 $sendTemplateParams['from'] = CRM_Utils_Array
::value('receipt_from_name', $values) . ' <' . $mailDetails[$contribution->contribution_page_id
]['receipt_from_email']. '>';
364 $sendTemplateParams['toEmail'] = $email;
365 $sendTemplateParams['cc'] = CRM_Utils_Array
::value('cc_receipt', $values);
366 $sendTemplateParams['bcc'] = CRM_Utils_Array
::value('bcc_receipt', $values);
368 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
370 elseif ($contribution->_component
== 'event') {
371 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contribution->contact_id
);
373 $sendTemplateParams['tplParams'] = array_merge($tplParams,array('email_comment' => $invoiceElements['params']['email_comment']));
374 $sendTemplateParams['from'] = CRM_Utils_Array
::value('confirm_from_name', $values) . ' <' . $mailDetails[$contribution->_relatedObjects
['event']->id
]['confirm_from_email']. '>';
375 $sendTemplateParams['toEmail'] = $email;
376 $sendTemplateParams['cc'] = CRM_Utils_Array
::value('cc_confirm', $values);
377 $sendTemplateParams['bcc'] = CRM_Utils_Array
::value('bcc_confirm', $values);
379 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
382 $updateInvoiceId = CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id
, 'invoice_id', $invoiceId);
383 if ($contribution->contribution_status_id
== $refundedStatusId) {
384 $updateInvoiceId = CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id
, 'creditnote_id', $creditNoteId);
386 $this->_invoiceTemplate
->clearTemplateVars();
389 if ($invoiceElements['createPdf']) {
390 CRM_Utils_PDF_Utils
::html2pdf($this->_messageInvoice
, 'Invoice.pdf', FALSE);
391 CRM_Utils_System
::civiExit();
394 if ($invoiceElements['suppressedEmails']) {
395 $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']));
396 $msgTitle = ts('Email Error');
400 $status = ts('Your mail has been sent.');
401 $msgTitle = ts('Sent');
402 $msgType = 'success';
404 CRM_Core_Session
::setStatus($status, $msgTitle, $msgType);