CRM-18661 Fix & add test for financial rows created by repeattransaction
[civicrm-core.git] / CRM / Price / BAO / LineItem.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 *
38 * @package CRM
39 * @author Marshal Newrock <marshal@idealso.com>
40 * $Id$
41 */
42
43/**
44 * Business objects for Line Items generated by monetary transactions
45 */
46class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem {
47
48 /**
49 * Creates a new entry in the database.
50 *
414c1420
TO
51 * @param array $params
52 * (reference) an assoc array of name/value pairs.
6a488035 53 *
16b10e64 54 * @return CRM_Price_DAO_LineItem
6a488035 55 */
00be9182 56 public static function create(&$params) {
22fe049d
PN
57 $id = CRM_Utils_Array::value('id', $params);
58 if ($id) {
59 CRM_Utils_Hook::pre('edit', 'LineItem', $id, $params);
513e5875 60 $op = CRM_Core_Action::UPDATE;
22fe049d
PN
61 }
62 else {
63 CRM_Utils_Hook::pre('create', 'LineItem', $params['entity_id'], $params);
513e5875 64 $op = CRM_Core_Action::ADD;
22fe049d 65 }
c206647d 66
22fe049d
PN
67 // unset entity table and entity id in $params
68 // we never update the entity table and entity id during update mode
69 if ($id) {
70 unset($params['entity_id'], $params['entity_table']);
71 }
93b21909 72 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && CRM_Utils_Array::value('check_permissions', $params)) {
cfba316e
E
73 if (empty($params['financial_type_id'])) {
74 throw new Exception('Mandatory key(s) missing from params array: financial_type_id');
75 }
513e5875
E
76 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types, $op);
77 if (!in_array($params['financial_type_id'], array_keys($types))) {
f8acc368 78 throw new Exception('You do not have permission to create this line item');
513e5875
E
79 }
80 }
421dfaa6 81
6a488035
TO
82 $lineItemBAO = new CRM_Price_BAO_LineItem();
83 $lineItemBAO->copyValues($params);
84
85 $return = $lineItemBAO->save();
43c8d1dd 86 if ($lineItemBAO->entity_table == 'civicrm_membership' && $lineItemBAO->contribution_id && $lineItemBAO->entity_id) {
87 $membershipPaymentParams = array(
88 'membership_id' => $lineItemBAO->entity_id,
89 'contribution_id' => $lineItemBAO->contribution_id,
90 );
91 if (!civicrm_api3('MembershipPayment', 'getcount', $membershipPaymentParams)) {
92 civicrm_api3('MembershipPayment', 'create', $membershipPaymentParams);
93 }
94 }
6a488035 95
22fe049d 96 if ($id) {
3ef5b847 97 CRM_Utils_Hook::post('edit', 'LineItem', $id, $lineItemBAO);
22fe049d
PN
98 }
99 else {
3ef5b847 100 CRM_Utils_Hook::post('create', 'LineItem', $lineItemBAO->id, $lineItemBAO);
22fe049d 101 }
6a488035
TO
102
103 return $return;
104 }
105
106 /**
fe482240
EM
107 * Retrieve DB object based on input parameters.
108 *
109 * It also stores all the retrieved values in the default array.
6a488035 110 *
414c1420
TO
111 * @param array $params
112 * (reference ) an assoc array of name/value pairs.
113 * @param array $defaults
114 * (reference ) an assoc array to hold the flattened values.
6a488035 115 *
16b10e64 116 * @return CRM_Price_BAO_LineItem
6a488035 117 */
00be9182 118 public static function retrieve(&$params, &$defaults) {
6a488035
TO
119 $lineItem = new CRM_Price_BAO_LineItem();
120 $lineItem->copyValues($params);
121 if ($lineItem->find(TRUE)) {
122 CRM_Core_DAO::storeValues($lineItem, $defaults);
123 return $lineItem;
124 }
125 return NULL;
126 }
127
ac5a0d1c
E
128 /**
129 * Modifies $params array for filtering financial types.
130 *
131 * @param array $params
132 * (reference ) an assoc array of name/value pairs.
133 *
134 */
135 public static function getAPILineItemParams(&$params) {
136 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types);
137 if ($types && empty($params['financial_type_id'])) {
138 $params['financial_type_id'] = array('IN' => array_keys($types));
139 }
140 elseif ($types) {
141 if (is_array($params['financial_type_id'])) {
142 $invalidFts = array_diff($params['financial_type_id'], array_keys($types));
143 }
144 elseif (!in_array($params['financial_type_id'], array_keys($types))) {
145 $invalidFts = $params['financial_type_id'];
146 }
147 if ($invalidFts) {
148 $params['financial_type_id'] = array('NOT IN' => $invalidFts);
149 }
150 }
151 else {
152 $params['financial_type_id'] = 0;
153 }
154 }
155
ffd93213 156 /**
100fef9d 157 * @param int $entityId
ffd93213
EM
158 * @param $entityTable
159 *
160 * @return null|string
161 */
00be9182 162 public static function getLineTotal($entityId, $entityTable) {
5a18a545 163 $sqlLineItemTotal = "SELECT SUM(li.line_total + COALESCE(li.tax_amount,0))
bc2eeabb 164FROM civicrm_line_item li
ae53df5f
PJ
165WHERE li.entity_table = '{$entityTable}'
166AND li.entity_id = {$entityId}
167";
bc2eeabb
PJ
168 $lineItemTotal = CRM_Core_DAO::singleValueQuery($sqlLineItemTotal);
169 return $lineItemTotal;
170 }
171
34a100a7 172 /**
fe482240 173 * Wrapper for line item retrieval when contribution ID is known.
100fef9d 174 * @param int $contributionID
34a100a7
EM
175 *
176 * @return array
177 */
00be9182 178 public static function getLineItemsByContributionID($contributionID) {
ba1dcfda 179 return self::getLineItems($contributionID, 'contribution', NULL, TRUE, TRUE, " WHERE contribution_id = " . (int) $contributionID);
34a100a7
EM
180 }
181
6a488035
TO
182 /**
183 * Given a participant id/contribution id,
184 * return contribution/fee line items
185 *
5a4f6742
CW
186 * @param int $entityId
187 * participant/contribution id.
188 * @param string $entity
189 * participant/contribution.
6a488035 190 *
dd244018
EM
191 * @param null $isQuick
192 * @param bool $isQtyZero
0d6f29cd 193 * @param bool $relatedEntity
dd244018 194 *
414c1420
TO
195 * @param string $overrideWhereClause
196 * E.g "WHERE contribution id = 7 " per the getLineItemsByContributionID wrapper.
16b10e64
CW
197 * this function precedes the convenience of the contribution id but since it does quite a bit more than just a db retrieval we need to be able to use it even
198 * when we don't want it's entity-id magix
34a100a7 199 *
111775d3 200 * @param bool $invoice
a6c01b45 201 * @return array
16b10e64 202 * Array of line items
6a488035 203 */
25d0c647 204 public static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL, $isQtyZero = TRUE, $relatedEntity = FALSE, $overrideWhereClause = '', $invoice = FALSE) {
34a100a7 205 $whereClause = $fromClause = NULL;
6a488035
TO
206 $selectClause = "
207 SELECT li.id,
208 li.label,
a7886853 209 li.contribution_id,
6a488035
TO
210 li.qty,
211 li.unit_price,
212 li.line_total,
34a100a7
EM
213 li.entity_table,
214 li.entity_id,
6a488035
TO
215 pf.label as field_title,
216 pf.html_type,
217 pfv.membership_type_id,
9c09f5b7 218 pfv.membership_num_terms,
6a488035
TO
219 li.price_field_id,
220 li.participant_count,
221 li.price_field_value_id,
bf45dbe8 222 li.financial_type_id,
9849720e 223 li.tax_amount,
6a488035
TO
224 pfv.description";
225
0d6f29cd 226 $condition = "li.entity_id = %2.id AND li.entity_table = 'civicrm_%2'";
227 if ($relatedEntity) {
228 $condition = "li.contribution_id = %2.id ";
229 }
230
6a488035
TO
231 $fromClause = "
232 FROM civicrm_%2 as %2
0d6f29cd 233 LEFT JOIN civicrm_line_item li ON ({$condition})
6a488035
TO
234 LEFT JOIN civicrm_price_field_value pfv ON ( pfv.id = li.price_field_value_id )
235 LEFT JOIN civicrm_price_field pf ON (pf.id = li.price_field_id )";
236 $whereClause = "
237 WHERE %2.id = %1";
238
25d0c647
WA
239 // CRM-16250 get additional participant's fee selection details only for invoice PDF (if any)
240 if ($entity == 'participant' && $invoice) {
0977cd6e 241 $additionalParticipantIDs = CRM_Event_BAO_Participant::getAdditionalParticipantIds($entityId);
242 if (!empty($additionalParticipantIDs)) {
36165903 243 $whereClause = "WHERE %2.id IN (%1, " . implode(', ', $additionalParticipantIDs) . ")";
0977cd6e 244 }
245 }
246
e68f41a5
DG
247 $orderByClause = " ORDER BY pf.weight, pfv.weight";
248
6a488035
TO
249 if ($isQuick) {
250 $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id ";
251 $whereClause .= " and cps.is_quick_config = 0";
252 }
d5397f2f
PJ
253
254 if (!$isQtyZero) {
255 $whereClause .= " and li.qty != 0";
256 }
257
6a488035
TO
258 $lineItems = array();
259
260 if (!$entityId || !$entity || !$fromClause) {
261 return $lineItems;
262 }
263
264 $params = array(
265 1 => array($entityId, 'Integer'),
266 2 => array($entity, 'Text'),
267 );
268
2feea3d1 269 $getTaxDetails = FALSE;
aaffa79f 270 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
03b412ae 271 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
34a100a7
EM
272 if ($overrideWhereClause) {
273 $whereClause = $overrideWhereClause;
274 }
275
e68f41a5 276 $dao = CRM_Core_DAO::executeQuery("$selectClause $fromClause $whereClause $orderByClause", $params);
6a488035
TO
277 while ($dao->fetch()) {
278 if (!$dao->id) {
279 continue;
280 }
281 $lineItems[$dao->id] = array(
4dc7af82 282 'qty' => (float) $dao->qty,
6a488035
TO
283 'label' => $dao->label,
284 'unit_price' => $dao->unit_price,
285 'line_total' => $dao->line_total,
286 'price_field_id' => $dao->price_field_id,
287 'participant_count' => $dao->participant_count,
288 'price_field_value_id' => $dao->price_field_value_id,
289 'field_title' => $dao->field_title,
290 'html_type' => $dao->html_type,
291 'description' => $dao->description,
34a100a7
EM
292 // the entity id seems prone to randomness but not sure if it has a reason - so if we are overriding the Where clause we assume
293 // we also JUST WANT TO KNOW the the entity_id in the DB
294 'entity_id' => empty($overrideWhereClause) ? $entityId : $dao->entity_id,
295 'entity_table' => $dao->entity_table,
a7886853 296 'contribution_id' => $dao->contribution_id,
bf45dbe8 297 'financial_type_id' => $dao->financial_type_id,
6a488035 298 'membership_type_id' => $dao->membership_type_id,
9c09f5b7 299 'membership_num_terms' => $dao->membership_num_terms,
9849720e 300 'tax_amount' => $dao->tax_amount,
6a488035 301 );
9849720e
RK
302 $lineItems[$dao->id]['tax_rate'] = CRM_Price_BAO_LineItem::calculateTaxRate($lineItems[$dao->id]);
303 $lineItems[$dao->id]['subTotal'] = $lineItems[$dao->id]['qty'] * $lineItems[$dao->id]['unit_price'];
2feea3d1
PB
304 if ($lineItems[$dao->id]['tax_amount'] != '') {
305 $getTaxDetails = TRUE;
306 }
6a488035 307 }
03b412ae
PB
308 if ($invoicing) {
309 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
310 $smarty = CRM_Core_Smarty::singleton();
311 $smarty->assign('taxTerm', $taxTerm);
312 $smarty->assign('getTaxDetails', $getTaxDetails);
313 }
6a488035
TO
314 return $lineItems;
315 }
316
317 /**
318 * This method will create the lineItem array required for
319 * processAmount method
320 *
414c1420
TO
321 * @param int $fid
322 * Price set field id.
323 * @param array $params
324 * Reference to form values.
325 * @param array $fields
326 * Reference to array of fields belonging.
6a488035 327 * to the price set used for particular event
414c1420
TO
328 * @param array $values
329 * Reference to the values array(.
16b10e64 330 * this is
6a488035
TO
331 * lineItem array)
332 *
333 * @return void
6a488035 334 */
00be9182 335 public static function format($fid, &$params, &$fields, &$values) {
6a488035
TO
336 if (empty($params["price_{$fid}"])) {
337 return;
338 }
339
340 $optionIDs = implode(',', array_keys($params["price_{$fid}"]));
341
342 //lets first check in fun parameter,
343 //since user might modified w/ hooks.
344 $options = array();
345 if (array_key_exists('options', $fields)) {
346 $options = $fields['options'];
347 }
348 else {
9da8dc8c 349 CRM_Price_BAO_PriceFieldValue::getValues($fid, $options, 'weight', TRUE);
6a488035
TO
350 }
351 $fieldTitle = CRM_Utils_Array::value('label', $fields);
352 if (!$fieldTitle) {
9da8dc8c 353 $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'label');
6a488035
TO
354 }
355
356 foreach ($params["price_{$fid}"] as $oid => $qty) {
357 $price = $options[$oid]['amount'];
358
359 // lets clean the price in case it is not yet cleant
360 // CRM-10974
361 $price = CRM_Utils_Rule::cleanMoney($price);
362
363 $participantsPerField = CRM_Utils_Array::value('count', $options[$oid], 0);
364
365 $values[$oid] = array(
366 'price_field_id' => $fid,
367 'price_field_value_id' => $oid,
368 'label' => CRM_Utils_Array::value('label', $options[$oid]),
369 'field_title' => $fieldTitle,
370 'description' => CRM_Utils_Array::value('description', $options[$oid]),
371 'qty' => $qty,
372 'unit_price' => $price,
373 'line_total' => $qty * $price,
374 'participant_count' => $qty * $participantsPerField,
375 'max_value' => CRM_Utils_Array::value('max_value', $options[$oid]),
376 'membership_type_id' => CRM_Utils_Array::value('membership_type_id', $options[$oid]),
377 'membership_num_terms' => CRM_Utils_Array::value('membership_num_terms', $options[$oid]),
378 'auto_renew' => CRM_Utils_Array::value('auto_renew', $options[$oid]),
379 'html_type' => $fields['html_type'],
bf45dbe8 380 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $options[$oid]),
5a18a545 381 'tax_amount' => CRM_Utils_Array::value('tax_amount', $options[$oid]),
6a488035 382 );
cdeb4bdf 383
e03317f1 384 if ($values[$oid]['membership_type_id'] && empty($values[$oid]['auto_renew'])) {
421dfaa6 385 $values[$oid]['auto_renew'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $values[$oid]['membership_type_id'], 'auto_renew');
6a488035
TO
386 }
387 }
388 }
389
390 /**
391 * Delete line items for given entity.
392 *
393 * @param int $entityId
394 * @param int $entityTable
395 *
77b97be7 396 * @return bool
6a488035 397 */
421dfaa6 398 public static function deleteLineItems($entityId, $entityTable) {
6a488035 399 if (!$entityId || !$entityTable) {
9330406e 400 return FALSE;
6a488035
TO
401 }
402
403 if ($entityId && !is_array($entityId)) {
404 $entityId = array($entityId);
405 }
406
de1c25e1 407 $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'";
6a488035 408 $dao = CRM_Core_DAO::executeQuery($query);
9330406e 409 return TRUE;
6a488035
TO
410 }
411
412 /**
100fef9d 413 * Process price set and line items.
dd244018 414 *
100fef9d 415 * @param int $entityId
414c1420
TO
416 * @param array $lineItem
417 * Line item array.
6a488035 418 * @param object $contributionDetails
414c1420
TO
419 * @param string $entityTable
420 * Entity table.
6a488035 421 *
dd244018
EM
422 * @param bool $update
423 *
6a488035 424 * @return void
6a488035 425 */
00be9182 426 public static function processPriceSet($entityId, $lineItem, $contributionDetails = NULL, $entityTable = 'civicrm_contribution', $update = FALSE) {
6a488035
TO
427 if (!$entityId || !is_array($lineItem)
428 || CRM_Utils_system::isNull($lineItem)
429 ) {
430 return;
431 }
421dfaa6 432
6a488035
TO
433 foreach ($lineItem as $priceSetId => $values) {
434 if (!$priceSetId) {
435 continue;
436 }
437
438 foreach ($values as $line) {
439 $line['entity_table'] = $entityTable;
34a100a7
EM
440 if (empty($line['entity_id'])) {
441 $line['entity_id'] = $entityId;
442 }
22e263ad 443 if (!empty($line['membership_type_id'])) {
79ebec7b 444 $line['entity_table'] = 'civicrm_membership';
8aa7457a 445 }
79ebec7b
PN
446 if (!empty($contributionDetails->id)) {
447 $line['contribution_id'] = $contributionDetails->id;
448 if ($line['entity_table'] == 'civicrm_contribution') {
449 $line['entity_id'] = $contributionDetails->id;
7d3f62f6 450 }
a7886853 451 }
c206647d 452
6a488035
TO
453 // if financial type is not set and if price field value is NOT NULL
454 // get financial type id of price field value
8cc574cf 455 if (!empty($line['price_field_value_id']) && empty($line['financial_type_id'])) {
9da8dc8c 456 $line['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $line['price_field_value_id'], 'financial_type_id');
6a488035
TO
457 }
458 $lineItems = CRM_Price_BAO_LineItem::create($line);
459 if (!$update && $contributionDetails) {
460 CRM_Financial_BAO_FinancialItem::add($lineItems, $contributionDetails);
43c8d1dd 461 if (!empty($line['tax_amount'])) {
0b7bd9dd 462 CRM_Financial_BAO_FinancialItem::add($lineItems, $contributionDetails, TRUE);
463 }
6a488035
TO
464 }
465 }
466 }
421dfaa6 467 }
6a488035 468
ffd93213 469 /**
100fef9d 470 * @param int $entityId
ffd93213
EM
471 * @param string $entityTable
472 * @param $amount
100fef9d 473 * @param array $otherParams
ffd93213 474 */
6a488035 475 public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) {
ba1dcfda 476 if (!$entityId || CRM_Utils_System::isNull($amount)) {
6a488035 477 return;
ba1dcfda 478 }
6a488035
TO
479
480 $from = " civicrm_line_item li
481 LEFT JOIN civicrm_price_field pf ON pf.id = li.price_field_id
482 LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id ";
483
421dfaa6 484 $set = " li.unit_price = %3,
6a488035
TO
485 li.line_total = %3 ";
486
421dfaa6 487 $where = " li.entity_id = %1 AND
6a488035
TO
488 li.entity_table = %2 ";
489
490 $params = array(
491 1 => array($entityId, 'Integer'),
492 2 => array($entityTable, 'String'),
493 3 => array($amount, 'Float'),
494 );
495
496 if ($entityTable == 'civicrm_contribution') {
497 $entityName = 'default_contribution_amount';
498 $where .= " AND ps.name = %4 ";
421dfaa6 499 $params[4] = array($entityName, 'String');
500 }
6a488035
TO
501 elseif ($entityTable == 'civicrm_participant') {
502 $from .= "
503 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = ps.id
504 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = pf.id and cpfv.label = %4 ";
505 $set .= " ,li.label = %4,
506 li.price_field_value_id = cpfv.id ";
507 $where .= " AND cpse.entity_table = 'civicrm_event' AND cpse.entity_id = %5 ";
ba1dcfda 508 $amount = empty($amount) ? 0 : $amount;
6a488035
TO
509 $params += array(
510 4 => array($otherParams['fee_label'], 'String'),
511 5 => array($otherParams['event_id'], 'String'),
512 );
513 }
514
421dfaa6 515 $query = "
6a488035
TO
516 UPDATE $from
517 SET $set
421dfaa6 518 WHERE $where
6a488035
TO
519 ";
520
521 CRM_Core_DAO::executeQuery($query, $params);
522 }
523
ba1dcfda 524 /**
100fef9d 525 * Build line items array.
ea3ddccf 526 *
414c1420
TO
527 * @param array $params
528 * Form values.
6a488035 529 *
414c1420
TO
530 * @param string $entityId
531 * Entity id.
6a488035 532 *
414c1420
TO
533 * @param string $entityTable
534 * Entity Table.
6a488035 535 *
ea3ddccf 536 * @param bool $isRelatedID
6a488035 537 */
00be9182 538 public static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution', $isRelatedID = FALSE) {
421dfaa6 539
6a488035 540 if (!$entityId) {
82cc6775
PN
541 $priceSetDetails = CRM_Price_BAO_PriceSet::getDefaultPriceSet($entityTable);
542 $totalAmount = CRM_Utils_Array::value('total_amount', $params);
543 $financialType = CRM_Utils_Array::value('financial_type_id', $params);
6a488035 544 foreach ($priceSetDetails as $values) {
c206647d 545 if ($entityTable == 'membership') {
82cc6775
PN
546 if ($isRelatedID != $values['membership_type_id']) {
547 continue;
548 }
549 if (!$totalAmount) {
550 $totalAmount = $values['amount'];
551 }
552 $financialType = $values['financial_type_id'];
553 }
6a488035
TO
554 $params['line_item'][$values['setID']][$values['priceFieldID']] = array(
555 'price_field_id' => $values['priceFieldID'],
556 'price_field_value_id' => $values['priceFieldValueID'],
557 'label' => $values['label'],
558 'qty' => 1,
82cc6775
PN
559 'unit_price' => $totalAmount,
560 'line_total' => $totalAmount,
561 'financial_type_id' => $financialType,
21dfd5f5 562 'membership_type_id' => $values['membership_type_id'],
6a488035 563 );
82cc6775 564 break;
6a488035 565 }
421dfaa6 566 }
6a488035
TO
567 else {
568 $setID = NULL;
464bb009 569 $totalEntityId = count($entityId);
2a131e0c
PN
570 if ($entityTable == 'contribution') {
571 $isRelatedID = TRUE;
572 }
464bb009 573 foreach ($entityId as $id) {
79ebec7b 574 $lineItems = CRM_Price_BAO_LineItem::getLineItems($id, $entityTable, NULL, TRUE, $isRelatedID);
464bb009 575 foreach ($lineItems as $key => $values) {
32cb2feb 576 if (!$setID && $values['price_field_id']) {
9da8dc8c 577 $setID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $values['price_field_id'], 'price_set_id');
578 $params['is_quick_config'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'is_quick_config');
464bb009 579 }
a7488080 580 if (!empty($params['is_quick_config']) && array_key_exists('total_amount', $params)
353ffa53
TO
581 && $totalEntityId == 1
582 ) {
464bb009
PN
583 $values['line_total'] = $values['unit_price'] = $params['total_amount'];
584 }
585 $values['id'] = $key;
586 $params['line_item'][$setID][$key] = $values;
6a488035 587 }
6a488035
TO
588 }
589 }
590 }
9849720e
RK
591
592 /**
fe482240 593 * Calculate tax rate in percentage.
2826a42e 594 *
5a4f6742 595 * @param array $lineItemId
414c1420 596 * An assoc array of lineItem.
2826a42e 597 *
79d7553f 598 * @return int|void
72b3a70c 599 * tax rate
9849720e
RK
600 */
601 public static function calculateTaxRate($lineItemId) {
b98ca468 602 if ($lineItemId['unit_price'] == 0 || $lineItemId['qty'] == 0) {
79d7553f 603 return FALSE;
5b22467a 604 }
9849720e 605 if ($lineItemId['html_type'] == 'Text') {
00400e19 606 $tax = round($lineItemId['tax_amount'] / ($lineItemId['unit_price'] * $lineItemId['qty']) * 100, 2);
9849720e
RK
607 }
608 else {
00400e19 609 $tax = round(($lineItemId['tax_amount'] / $lineItemId['unit_price']) * 100, 2);
9849720e
RK
610 }
611 return $tax;
612 }
96025800 613
6a488035 614}