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