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