CRM-19427 - set deductible amount at price field option level
[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
1a1a2f4f 326 * Array of fields belonging to the price set used for particular event
414c1420
TO
327 * @param array $values
328 * Reference to the values array(.
16b10e64 329 * this is
6a488035 330 * lineItem array)
1a1a2f4f 331 * @param string $amount_override
6a488035 332 */
1a1a2f4f 333 public static function format($fid, $params, $fields, &$values, $amount_override = NULL) {
6a488035
TO
334 if (empty($params["price_{$fid}"])) {
335 return;
336 }
337
6a488035
TO
338 //lets first check in fun parameter,
339 //since user might modified w/ hooks.
340 $options = array();
341 if (array_key_exists('options', $fields)) {
342 $options = $fields['options'];
343 }
344 else {
9da8dc8c 345 CRM_Price_BAO_PriceFieldValue::getValues($fid, $options, 'weight', TRUE);
6a488035
TO
346 }
347 $fieldTitle = CRM_Utils_Array::value('label', $fields);
348 if (!$fieldTitle) {
9da8dc8c 349 $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'label');
6a488035
TO
350 }
351
352 foreach ($params["price_{$fid}"] as $oid => $qty) {
1a1a2f4f 353 $price = $amount_override === NULL ? $options[$oid]['amount'] : $amount_override;
6a488035
TO
354
355 // lets clean the price in case it is not yet cleant
356 // CRM-10974
357 $price = CRM_Utils_Rule::cleanMoney($price);
358
359 $participantsPerField = CRM_Utils_Array::value('count', $options[$oid], 0);
360
361 $values[$oid] = array(
362 'price_field_id' => $fid,
363 'price_field_value_id' => $oid,
364 'label' => CRM_Utils_Array::value('label', $options[$oid]),
365 'field_title' => $fieldTitle,
366 'description' => CRM_Utils_Array::value('description', $options[$oid]),
367 'qty' => $qty,
368 'unit_price' => $price,
369 'line_total' => $qty * $price,
370 'participant_count' => $qty * $participantsPerField,
371 'max_value' => CRM_Utils_Array::value('max_value', $options[$oid]),
372 'membership_type_id' => CRM_Utils_Array::value('membership_type_id', $options[$oid]),
373 'membership_num_terms' => CRM_Utils_Array::value('membership_num_terms', $options[$oid]),
374 'auto_renew' => CRM_Utils_Array::value('auto_renew', $options[$oid]),
375 'html_type' => $fields['html_type'],
bf45dbe8 376 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $options[$oid]),
5a18a545 377 'tax_amount' => CRM_Utils_Array::value('tax_amount', $options[$oid]),
5afce5ad 378 'non_deductible_amount' => CRM_Utils_Array::value('non_deductible_amount', $options[$oid]),
6a488035 379 );
cdeb4bdf 380
e03317f1 381 if ($values[$oid]['membership_type_id'] && empty($values[$oid]['auto_renew'])) {
421dfaa6 382 $values[$oid]['auto_renew'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $values[$oid]['membership_type_id'], 'auto_renew');
6a488035
TO
383 }
384 }
385 }
386
387 /**
388 * Delete line items for given entity.
389 *
390 * @param int $entityId
391 * @param int $entityTable
392 *
77b97be7 393 * @return bool
6a488035 394 */
421dfaa6 395 public static function deleteLineItems($entityId, $entityTable) {
6a488035 396 if (!$entityId || !$entityTable) {
9330406e 397 return FALSE;
6a488035
TO
398 }
399
400 if ($entityId && !is_array($entityId)) {
401 $entityId = array($entityId);
402 }
403
de1c25e1 404 $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'";
6a488035 405 $dao = CRM_Core_DAO::executeQuery($query);
9330406e 406 return TRUE;
6a488035
TO
407 }
408
409 /**
100fef9d 410 * Process price set and line items.
dd244018 411 *
100fef9d 412 * @param int $entityId
414c1420
TO
413 * @param array $lineItem
414 * Line item array.
6a488035 415 * @param object $contributionDetails
414c1420
TO
416 * @param string $entityTable
417 * Entity table.
6a488035 418 *
dd244018
EM
419 * @param bool $update
420 *
6a488035 421 * @return void
6a488035 422 */
00be9182 423 public static function processPriceSet($entityId, $lineItem, $contributionDetails = NULL, $entityTable = 'civicrm_contribution', $update = FALSE) {
6a488035
TO
424 if (!$entityId || !is_array($lineItem)
425 || CRM_Utils_system::isNull($lineItem)
426 ) {
427 return;
428 }
421dfaa6 429
8cf6bd83 430 foreach ($lineItem as $priceSetId => &$values) {
6a488035
TO
431 if (!$priceSetId) {
432 continue;
433 }
434
8cf6bd83 435 foreach ($values as &$line) {
6a488035 436 $line['entity_table'] = $entityTable;
34a100a7
EM
437 if (empty($line['entity_id'])) {
438 $line['entity_id'] = $entityId;
439 }
22e263ad 440 if (!empty($line['membership_type_id'])) {
79ebec7b 441 $line['entity_table'] = 'civicrm_membership';
8aa7457a 442 }
79ebec7b
PN
443 if (!empty($contributionDetails->id)) {
444 $line['contribution_id'] = $contributionDetails->id;
445 if ($line['entity_table'] == 'civicrm_contribution') {
446 $line['entity_id'] = $contributionDetails->id;
7d3f62f6 447 }
75d83bd0 448 // CRM-19094: entity_table is set to civicrm_membership then ensure
449 // the entityId is set to membership ID not contribution by default
41ef4d67 450 elseif ($line['entity_table'] == 'civicrm_membership' && !empty($line['entity_id']) && $line['entity_id'] == $contributionDetails->id) {
75d83bd0 451 $membershipId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', 'contribution_id', $line['entity_id'], 'membership_id');
452 $line['entity_id'] = $membershipId ? $membershipId : $line['entity_id'];
453 }
a7886853 454 }
c206647d 455
6a488035
TO
456 // if financial type is not set and if price field value is NOT NULL
457 // get financial type id of price field value
8cc574cf 458 if (!empty($line['price_field_value_id']) && empty($line['financial_type_id'])) {
9da8dc8c 459 $line['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $line['price_field_value_id'], 'financial_type_id');
6a488035
TO
460 }
461 $lineItems = CRM_Price_BAO_LineItem::create($line);
462 if (!$update && $contributionDetails) {
8cf6bd83
PN
463 $financialItem = CRM_Financial_BAO_FinancialItem::add($lineItems, $contributionDetails);
464 $line['financial_item_id'] = $financialItem->id;
43c8d1dd 465 if (!empty($line['tax_amount'])) {
0b7bd9dd 466 CRM_Financial_BAO_FinancialItem::add($lineItems, $contributionDetails, TRUE);
467 }
6a488035
TO
468 }
469 }
470 }
8cf6bd83
PN
471 if (!$update && $contributionDetails) {
472 CRM_Core_BAO_FinancialTrxn::createDeferredTrxn($lineItem, $contributionDetails);
473 }
421dfaa6 474 }
6a488035 475
ffd93213 476 /**
100fef9d 477 * @param int $entityId
ffd93213
EM
478 * @param string $entityTable
479 * @param $amount
100fef9d 480 * @param array $otherParams
ffd93213 481 */
6a488035 482 public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) {
ba1dcfda 483 if (!$entityId || CRM_Utils_System::isNull($amount)) {
6a488035 484 return;
ba1dcfda 485 }
6a488035
TO
486
487 $from = " civicrm_line_item li
488 LEFT JOIN civicrm_price_field pf ON pf.id = li.price_field_id
489 LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id ";
490
421dfaa6 491 $set = " li.unit_price = %3,
6a488035
TO
492 li.line_total = %3 ";
493
421dfaa6 494 $where = " li.entity_id = %1 AND
6a488035
TO
495 li.entity_table = %2 ";
496
497 $params = array(
498 1 => array($entityId, 'Integer'),
499 2 => array($entityTable, 'String'),
500 3 => array($amount, 'Float'),
501 );
502
503 if ($entityTable == 'civicrm_contribution') {
504 $entityName = 'default_contribution_amount';
505 $where .= " AND ps.name = %4 ";
421dfaa6 506 $params[4] = array($entityName, 'String');
507 }
6a488035
TO
508 elseif ($entityTable == 'civicrm_participant') {
509 $from .= "
510 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = ps.id
511 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = pf.id and cpfv.label = %4 ";
512 $set .= " ,li.label = %4,
513 li.price_field_value_id = cpfv.id ";
514 $where .= " AND cpse.entity_table = 'civicrm_event' AND cpse.entity_id = %5 ";
ba1dcfda 515 $amount = empty($amount) ? 0 : $amount;
6a488035
TO
516 $params += array(
517 4 => array($otherParams['fee_label'], 'String'),
518 5 => array($otherParams['event_id'], 'String'),
519 );
520 }
521
421dfaa6 522 $query = "
6a488035
TO
523 UPDATE $from
524 SET $set
421dfaa6 525 WHERE $where
6a488035
TO
526 ";
527
528 CRM_Core_DAO::executeQuery($query, $params);
529 }
530
ba1dcfda 531 /**
100fef9d 532 * Build line items array.
ea3ddccf 533 *
414c1420
TO
534 * @param array $params
535 * Form values.
6a488035 536 *
414c1420
TO
537 * @param string $entityId
538 * Entity id.
6a488035 539 *
414c1420
TO
540 * @param string $entityTable
541 * Entity Table.
6a488035 542 *
ea3ddccf 543 * @param bool $isRelatedID
6a488035 544 */
00be9182 545 public static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution', $isRelatedID = FALSE) {
421dfaa6 546
6a488035 547 if (!$entityId) {
82cc6775
PN
548 $priceSetDetails = CRM_Price_BAO_PriceSet::getDefaultPriceSet($entityTable);
549 $totalAmount = CRM_Utils_Array::value('total_amount', $params);
550 $financialType = CRM_Utils_Array::value('financial_type_id', $params);
6a488035 551 foreach ($priceSetDetails as $values) {
c206647d 552 if ($entityTable == 'membership') {
82cc6775
PN
553 if ($isRelatedID != $values['membership_type_id']) {
554 continue;
555 }
556 if (!$totalAmount) {
557 $totalAmount = $values['amount'];
558 }
559 $financialType = $values['financial_type_id'];
560 }
6a488035
TO
561 $params['line_item'][$values['setID']][$values['priceFieldID']] = array(
562 'price_field_id' => $values['priceFieldID'],
563 'price_field_value_id' => $values['priceFieldValueID'],
564 'label' => $values['label'],
565 'qty' => 1,
82cc6775
PN
566 'unit_price' => $totalAmount,
567 'line_total' => $totalAmount,
568 'financial_type_id' => $financialType,
21dfd5f5 569 'membership_type_id' => $values['membership_type_id'],
6a488035 570 );
82cc6775 571 break;
6a488035 572 }
421dfaa6 573 }
6a488035
TO
574 else {
575 $setID = NULL;
464bb009 576 $totalEntityId = count($entityId);
2a131e0c
PN
577 if ($entityTable == 'contribution') {
578 $isRelatedID = TRUE;
579 }
464bb009 580 foreach ($entityId as $id) {
79ebec7b 581 $lineItems = CRM_Price_BAO_LineItem::getLineItems($id, $entityTable, NULL, TRUE, $isRelatedID);
464bb009 582 foreach ($lineItems as $key => $values) {
32cb2feb 583 if (!$setID && $values['price_field_id']) {
9da8dc8c 584 $setID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $values['price_field_id'], 'price_set_id');
585 $params['is_quick_config'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'is_quick_config');
464bb009 586 }
a7488080 587 if (!empty($params['is_quick_config']) && array_key_exists('total_amount', $params)
353ffa53
TO
588 && $totalEntityId == 1
589 ) {
464bb009
PN
590 $values['line_total'] = $values['unit_price'] = $params['total_amount'];
591 }
592 $values['id'] = $key;
593 $params['line_item'][$setID][$key] = $values;
6a488035 594 }
6a488035
TO
595 }
596 }
597 }
9849720e
RK
598
599 /**
fe482240 600 * Calculate tax rate in percentage.
2826a42e 601 *
5a4f6742 602 * @param array $lineItemId
414c1420 603 * An assoc array of lineItem.
2826a42e 604 *
79d7553f 605 * @return int|void
72b3a70c 606 * tax rate
9849720e
RK
607 */
608 public static function calculateTaxRate($lineItemId) {
b98ca468 609 if ($lineItemId['unit_price'] == 0 || $lineItemId['qty'] == 0) {
79d7553f 610 return FALSE;
5b22467a 611 }
9849720e 612 if ($lineItemId['html_type'] == 'Text') {
00400e19 613 $tax = round($lineItemId['tax_amount'] / ($lineItemId['unit_price'] * $lineItemId['qty']) * 100, 2);
9849720e
RK
614 }
615 else {
00400e19 616 $tax = round(($lineItemId['tax_amount'] / $lineItemId['unit_price']) * 100, 2);
9849720e
RK
617 }
618 return $tax;
619 }
96025800 620
6a488035 621}