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("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 foreach ($invoiceElements['details'] as $invoiceElements['contribID'] => $detail) {
171 $input = $ids = $objects = array();
173 if (in_array($detail['contact'], $invoiceElements['excludeContactIds'])) {
177 $input['component'] = $detail['component'];
179 $ids['contact'] = $detail['contact'];
180 $ids['contribution'] = $invoiceElements['contribID'];
181 $ids['contributionRecur'] = NULL;
182 $ids['contributionPage'] = NULL;
183 $ids['membership'] = CRM_Utils_Array
::value('membership', $detail);
184 $ids['participant'] = CRM_Utils_Array
::value('participant', $detail);
185 $ids['event'] = CRM_Utils_Array
::value('event', $detail);
187 if (!$invoiceElements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
188 CRM_Core_Error
::fatal();
191 $contribution = &$objects['contribution'];
193 $input['amount'] = $contribution->total_amount
;
194 $input['invoice_id'] = $contribution->invoice_id
;
195 $input['receive_date'] = $contribution->receive_date
;
196 $input['contribution_status_id'] = $contribution->contribution_status_id
;
197 $input['organization_name'] = $contribution->_relatedObjects
['contact']->organization_name
;
199 $objects['contribution']->receive_date
= CRM_Utils_Date
::isoToMysql($objects['contribution']->receive_date
);
201 $addressParams = array('contact_id' => $contribution->contact_id
);
202 $addressDetails = CRM_Core_BAO_Address
::getValues($addressParams);
204 // to get billing address if present
205 $billingAddress = array();
206 foreach ($addressDetails as $key => $address) {
207 if ((isset($address['is_billing']) && $address['is_billing'] == 1) && (isset($address['is_primary']) && $address['is_primary'] == 1) && $address['contact_id'] == $contribution->contact_id
) {
208 $billingAddress[$address['contact_id']] = $address;
211 elseif (($address['is_billing'] == 0 && $address['is_primary'] == 1) ||
(isset($address['is_billing']) && $address['is_billing'] == 1) && $address['contact_id'] == $contribution->contact_id
) {
212 $billingAddress[$address['contact_id']] = $address;
216 $stateProvinceAbbreviation = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($billingAddress[$contribution->contact_id
]['state_province_id']);
218 // getting data from admin page
219 $prefixValue = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CONTRIBUTE_PREFERENCES_NAME
,'contribution_invoice_settings');
221 if ($contribution->contribution_status_id
== $refundedStatusId) {
222 $invoiceId = CRM_Utils_Array
::value('credit_notes_prefix', $prefixValue)."".$contribution->id
;
225 $invoiceId = CRM_Utils_Array
::value('invoice_prefix', $prefixValue)."".$contribution->id
;
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']));
233 if ($input['component'] == 'contribute') {
234 $eid = $invoiceElements['contribID'];
235 $etable = 'contribution';
238 $eid = $contribution->_relatedObjects
['participant']->id
;
239 $etable = 'participant';
242 //TO DO: Need to do changes for partially paid to display amount due on PDF invoice
243 $amountDue = ($input['amount'] - $input['amount']);
245 // retreiving the subtotal and sum of same tax_rate
246 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($eid, $etable);
247 $dataArray = array();
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);
254 $dataArray[$taxRate['tax_rate']] = CRM_Utils_Array
::value('tax_amount', $taxRate);
256 $subTotal +
= CRM_Utils_Array
::value('subTotal', $taxRate);
259 // to email the invoice
260 $mailDetails = array();
262 if ($contribution->_component
== 'event') {
263 $daoName = 'CRM_Event_DAO_Event';
264 $pageId = $contribution->_relatedObjects
['event']->id
;
265 $mailElements = array(
268 'confirm_from_email',
272 CRM_Core_DAO
::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
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
]);
280 $title = CRM_Utils_Array
::value('title', $mailDetails[$contribution->_relatedObjects
['event']->id
]);
282 elseif ($contribution->_component
== 'contribute') {
283 $daoName = 'CRM_Contribute_DAO_ContributionPage';
284 $pageId = $contribution->contribution_page_id
;
285 $mailElements = array(
288 'receipt_from_email',
292 CRM_Core_DAO
::commonRetrieveAll($daoName, 'id', $pageId, $mailDetails, $mailElements);
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
]);
300 $title = CRM_Utils_Array
::value('title', $mailDetails[$contribution->contribution_page_id
]);
303 $config = CRM_Core_Config
::singleton();
304 $config->doNotAttachPDFReceipt
= 1;
306 // parameters to be assign for template
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
,
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',
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);
348 'subject' => $subject,
354 $this->_messageInvoice
[] = $mail['html'];
357 $this->_messageInvoice
[] = nl2br($mail['body']);
360 elseif ($contribution->_component
== 'contribute') {
361 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contribution->contact_id
);
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);
369 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
371 elseif ($contribution->_component
== 'event') {
372 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contribution->contact_id
);
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);
380 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
383 $updateInvoiceId = CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id
, 'invoice_id', $invoiceId);
384 $this->_invoiceTemplate
->clearTemplateVars();
387 if ($invoiceElements['createPdf']) {
388 CRM_Utils_PDF_Utils
::html2pdf($this->_messageInvoice
, 'Invoice.pdf', FALSE);
389 CRM_Utils_System
::civiExit();
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');
398 $status = ts('Your mail has been sent.');
399 $msgTitle = ts('Sent');
400 $msgType = 'success';
402 CRM_Core_Session
::setStatus($status, $msgTitle, $msgType);