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