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