Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | * | |
51 | * @param array $params (reference) an assoc array of name/value pairs | |
52 | * | |
53 | * @return object CRM_Price_DAO_LineItem object | |
54 | * @access public | |
55 | * @static | |
56 | */ | |
57 | static function create(&$params) { | |
58 | //create mode only as we don't support editing line items | |
59 | ||
60 | CRM_Utils_Hook::pre('create', 'LineItem', $params['entity_id'], $params); | |
421dfaa6 | 61 | |
6a488035 TO |
62 | $lineItemBAO = new CRM_Price_BAO_LineItem(); |
63 | $lineItemBAO->copyValues($params); | |
64 | ||
65 | $return = $lineItemBAO->save(); | |
66 | ||
67 | CRM_Utils_Hook::post('create', 'LineItem', $params['entity_id'], $params); | |
68 | ||
69 | return $return; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Takes a bunch of params that are needed to match certain criteria and | |
74 | * retrieves the relevant objects. Typically, the valid params are only | |
75 | * price_field_id. This is the inverse function of create. It also | |
76 | * stores all of the retrieved values in the default array. | |
77 | * | |
78 | * @param array $params (reference ) an assoc array of name/value pairs | |
79 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
80 | * | |
81 | * @return object CRM_Price_BAO_LineItem object | |
82 | * @access public | |
83 | * @static | |
84 | */ | |
85 | static function retrieve(&$params, &$defaults) { | |
86 | $lineItem = new CRM_Price_BAO_LineItem(); | |
87 | $lineItem->copyValues($params); | |
88 | if ($lineItem->find(TRUE)) { | |
89 | CRM_Core_DAO::storeValues($lineItem, $defaults); | |
90 | return $lineItem; | |
91 | } | |
92 | return NULL; | |
93 | } | |
94 | ||
bc2eeabb PJ |
95 | static function getLineTotal($entityId, $entityTable) { |
96 | $sqlLineItemTotal = "SELECT SUM(li.line_total) | |
97 | FROM civicrm_line_item li | |
ae53df5f PJ |
98 | WHERE li.entity_table = '{$entityTable}' |
99 | AND li.entity_id = {$entityId} | |
100 | "; | |
bc2eeabb PJ |
101 | $lineItemTotal = CRM_Core_DAO::singleValueQuery($sqlLineItemTotal); |
102 | return $lineItemTotal; | |
103 | } | |
104 | ||
6a488035 TO |
105 | /** |
106 | * Given a participant id/contribution id, | |
107 | * return contribution/fee line items | |
108 | * | |
109 | * @param $entityId int participant/contribution id | |
110 | * @param $entity string participant/contribution. | |
111 | * | |
dd244018 EM |
112 | * @param null $isQuick |
113 | * @param bool $isQtyZero | |
114 | * | |
6a488035 TO |
115 | * @return array of line items |
116 | */ | |
d5397f2f | 117 | static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL , $isQtyZero = TRUE) { |
6a488035 TO |
118 | $selectClause = $whereClause = $fromClause = NULL; |
119 | $selectClause = " | |
120 | SELECT li.id, | |
121 | li.label, | |
122 | li.qty, | |
123 | li.unit_price, | |
124 | li.line_total, | |
125 | pf.label as field_title, | |
126 | pf.html_type, | |
127 | pfv.membership_type_id, | |
9c09f5b7 | 128 | pfv.membership_num_terms, |
6a488035 TO |
129 | li.price_field_id, |
130 | li.participant_count, | |
131 | li.price_field_value_id, | |
bf45dbe8 | 132 | li.financial_type_id, |
9849720e | 133 | li.tax_amount, |
6a488035 TO |
134 | pfv.description"; |
135 | ||
136 | $fromClause = " | |
137 | FROM civicrm_%2 as %2 | |
138 | LEFT JOIN civicrm_line_item li ON ( li.entity_id = %2.id AND li.entity_table = 'civicrm_%2') | |
139 | LEFT JOIN civicrm_price_field_value pfv ON ( pfv.id = li.price_field_value_id ) | |
140 | LEFT JOIN civicrm_price_field pf ON (pf.id = li.price_field_id )"; | |
141 | $whereClause = " | |
142 | WHERE %2.id = %1"; | |
143 | ||
144 | if ($isQuick) { | |
145 | $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id "; | |
146 | $whereClause .= " and cps.is_quick_config = 0"; | |
147 | } | |
d5397f2f PJ |
148 | |
149 | if (!$isQtyZero) { | |
150 | $whereClause .= " and li.qty != 0"; | |
151 | } | |
152 | ||
6a488035 TO |
153 | $lineItems = array(); |
154 | ||
155 | if (!$entityId || !$entity || !$fromClause) { | |
156 | return $lineItems; | |
157 | } | |
158 | ||
159 | $params = array( | |
160 | 1 => array($entityId, 'Integer'), | |
161 | 2 => array($entity, 'Text'), | |
162 | ); | |
163 | ||
164 | $dao = CRM_Core_DAO::executeQuery("$selectClause $fromClause $whereClause", $params); | |
165 | while ($dao->fetch()) { | |
166 | if (!$dao->id) { | |
167 | continue; | |
168 | } | |
169 | $lineItems[$dao->id] = array( | |
170 | 'qty' => $dao->qty, | |
171 | 'label' => $dao->label, | |
172 | 'unit_price' => $dao->unit_price, | |
173 | 'line_total' => $dao->line_total, | |
174 | 'price_field_id' => $dao->price_field_id, | |
175 | 'participant_count' => $dao->participant_count, | |
176 | 'price_field_value_id' => $dao->price_field_value_id, | |
177 | 'field_title' => $dao->field_title, | |
178 | 'html_type' => $dao->html_type, | |
179 | 'description' => $dao->description, | |
180 | 'entity_id' => $entityId, | |
bf45dbe8 | 181 | 'financial_type_id' => $dao->financial_type_id, |
6a488035 | 182 | 'membership_type_id' => $dao->membership_type_id, |
9c09f5b7 | 183 | 'membership_num_terms' => $dao->membership_num_terms, |
9849720e | 184 | 'tax_amount' => $dao->tax_amount, |
6a488035 | 185 | ); |
9849720e RK |
186 | $lineItems[$dao->id]['tax_rate'] = CRM_Price_BAO_LineItem::calculateTaxRate($lineItems[$dao->id]); |
187 | $lineItems[$dao->id]['subTotal'] = $lineItems[$dao->id]['qty'] * $lineItems[$dao->id]['unit_price']; | |
6a488035 TO |
188 | } |
189 | return $lineItems; | |
190 | } | |
191 | ||
192 | /** | |
193 | * This method will create the lineItem array required for | |
194 | * processAmount method | |
195 | * | |
196 | * @param int $fid price set field id | |
197 | * @param array $params referance to form values | |
198 | * @param array $fields referance to array of fields belonging | |
199 | * to the price set used for particular event | |
200 | * @param array $values referance to the values array( | |
201 | this is | |
202 | * lineItem array) | |
203 | * | |
204 | * @return void | |
205 | * @access static | |
206 | */ | |
207 | static function format($fid, &$params, &$fields, &$values) { | |
208 | if (empty($params["price_{$fid}"])) { | |
209 | return; | |
210 | } | |
211 | ||
212 | $optionIDs = implode(',', array_keys($params["price_{$fid}"])); | |
213 | ||
214 | //lets first check in fun parameter, | |
215 | //since user might modified w/ hooks. | |
216 | $options = array(); | |
217 | if (array_key_exists('options', $fields)) { | |
218 | $options = $fields['options']; | |
219 | } | |
220 | else { | |
9da8dc8c | 221 | CRM_Price_BAO_PriceFieldValue::getValues($fid, $options, 'weight', TRUE); |
6a488035 TO |
222 | } |
223 | $fieldTitle = CRM_Utils_Array::value('label', $fields); | |
224 | if (!$fieldTitle) { | |
9da8dc8c | 225 | $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'label'); |
6a488035 TO |
226 | } |
227 | ||
228 | foreach ($params["price_{$fid}"] as $oid => $qty) { | |
229 | $price = $options[$oid]['amount']; | |
230 | ||
231 | // lets clean the price in case it is not yet cleant | |
232 | // CRM-10974 | |
233 | $price = CRM_Utils_Rule::cleanMoney($price); | |
234 | ||
235 | $participantsPerField = CRM_Utils_Array::value('count', $options[$oid], 0); | |
236 | ||
237 | $values[$oid] = array( | |
238 | 'price_field_id' => $fid, | |
239 | 'price_field_value_id' => $oid, | |
240 | 'label' => CRM_Utils_Array::value('label', $options[$oid]), | |
241 | 'field_title' => $fieldTitle, | |
242 | 'description' => CRM_Utils_Array::value('description', $options[$oid]), | |
243 | 'qty' => $qty, | |
244 | 'unit_price' => $price, | |
245 | 'line_total' => $qty * $price, | |
246 | 'participant_count' => $qty * $participantsPerField, | |
247 | 'max_value' => CRM_Utils_Array::value('max_value', $options[$oid]), | |
248 | 'membership_type_id' => CRM_Utils_Array::value('membership_type_id', $options[$oid]), | |
249 | 'membership_num_terms' => CRM_Utils_Array::value('membership_num_terms', $options[$oid]), | |
250 | 'auto_renew' => CRM_Utils_Array::value('auto_renew', $options[$oid]), | |
251 | 'html_type' => $fields['html_type'], | |
bf45dbe8 | 252 | 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $options[$oid]), |
6a488035 | 253 | ); |
cdeb4bdf | 254 | |
6a488035 | 255 | if ($values[$oid]['membership_type_id'] && !isset($values[$oid]['auto_renew'])) { |
421dfaa6 | 256 | $values[$oid]['auto_renew'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $values[$oid]['membership_type_id'], 'auto_renew'); |
6a488035 TO |
257 | } |
258 | } | |
259 | } | |
260 | ||
261 | /** | |
262 | * Delete line items for given entity. | |
263 | * | |
264 | * @param int $entityId | |
265 | * @param int $entityTable | |
266 | * | |
77b97be7 | 267 | * @return bool |
6a488035 TO |
268 | * @access public |
269 | * @static | |
270 | */ | |
421dfaa6 | 271 | public static function deleteLineItems($entityId, $entityTable) { |
6a488035 | 272 | if (!$entityId || !$entityTable) { |
9330406e | 273 | return FALSE; |
6a488035 TO |
274 | } |
275 | ||
276 | if ($entityId && !is_array($entityId)) { | |
277 | $entityId = array($entityId); | |
278 | } | |
279 | ||
de1c25e1 | 280 | $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'"; |
6a488035 | 281 | $dao = CRM_Core_DAO::executeQuery($query); |
9330406e | 282 | return TRUE; |
6a488035 TO |
283 | } |
284 | ||
285 | /** | |
286 | * Function to process price set and line items. | |
dd244018 EM |
287 | * |
288 | * @param $entityId | |
6a488035 TO |
289 | * @param array $lineItem line item array |
290 | * @param object $contributionDetails | |
6a488035 TO |
291 | * @param string $entityTable entity table |
292 | * | |
dd244018 EM |
293 | * @param bool $update |
294 | * | |
295 | * @internal param int $contributionId contribution id | |
296 | * @internal param \decimal $initAmount amount | |
6a488035 TO |
297 | * @access public |
298 | * @return void | |
299 | * @static | |
300 | */ | |
301 | static function processPriceSet($entityId, $lineItem, $contributionDetails = NULL, $entityTable = 'civicrm_contribution', $update = FALSE) { | |
302 | if (!$entityId || !is_array($lineItem) | |
303 | || CRM_Utils_system::isNull($lineItem) | |
304 | ) { | |
305 | return; | |
306 | } | |
421dfaa6 | 307 | |
6a488035 TO |
308 | foreach ($lineItem as $priceSetId => $values) { |
309 | if (!$priceSetId) { | |
310 | continue; | |
311 | } | |
312 | ||
313 | foreach ($values as $line) { | |
314 | $line['entity_table'] = $entityTable; | |
315 | $line['entity_id'] = $entityId; | |
316 | // if financial type is not set and if price field value is NOT NULL | |
317 | // get financial type id of price field value | |
8cc574cf | 318 | if (!empty($line['price_field_value_id']) && empty($line['financial_type_id'])) { |
9da8dc8c | 319 | $line['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $line['price_field_value_id'], 'financial_type_id'); |
6a488035 TO |
320 | } |
321 | $lineItems = CRM_Price_BAO_LineItem::create($line); | |
322 | if (!$update && $contributionDetails) { | |
323 | CRM_Financial_BAO_FinancialItem::add($lineItems, $contributionDetails); | |
324 | } | |
325 | } | |
326 | } | |
421dfaa6 | 327 | } |
6a488035 TO |
328 | |
329 | public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) { | |
330 | if (!$entityId || CRM_Utils_System::isNull($amount)) | |
331 | return; | |
332 | ||
333 | $from = " civicrm_line_item li | |
334 | LEFT JOIN civicrm_price_field pf ON pf.id = li.price_field_id | |
335 | LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id "; | |
336 | ||
421dfaa6 | 337 | $set = " li.unit_price = %3, |
6a488035 TO |
338 | li.line_total = %3 "; |
339 | ||
421dfaa6 | 340 | $where = " li.entity_id = %1 AND |
6a488035 TO |
341 | li.entity_table = %2 "; |
342 | ||
343 | $params = array( | |
344 | 1 => array($entityId, 'Integer'), | |
345 | 2 => array($entityTable, 'String'), | |
346 | 3 => array($amount, 'Float'), | |
347 | ); | |
348 | ||
349 | if ($entityTable == 'civicrm_contribution') { | |
350 | $entityName = 'default_contribution_amount'; | |
351 | $where .= " AND ps.name = %4 "; | |
421dfaa6 | 352 | $params[4] = array($entityName, 'String'); |
353 | } | |
6a488035 TO |
354 | elseif ($entityTable == 'civicrm_participant') { |
355 | $from .= " | |
356 | LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = ps.id | |
357 | LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = pf.id and cpfv.label = %4 "; | |
358 | $set .= " ,li.label = %4, | |
359 | li.price_field_value_id = cpfv.id "; | |
360 | $where .= " AND cpse.entity_table = 'civicrm_event' AND cpse.entity_id = %5 "; | |
361 | $amount = empty($amount) ? 0: $amount; | |
362 | $params += array( | |
363 | 4 => array($otherParams['fee_label'], 'String'), | |
364 | 5 => array($otherParams['event_id'], 'String'), | |
365 | ); | |
366 | } | |
367 | ||
421dfaa6 | 368 | $query = " |
6a488035 TO |
369 | UPDATE $from |
370 | SET $set | |
421dfaa6 | 371 | WHERE $where |
6a488035 TO |
372 | "; |
373 | ||
374 | CRM_Core_DAO::executeQuery($query, $params); | |
375 | } | |
376 | ||
377 | /** | |
378 | * Function to build line items array. | |
379 | * @param int $params form values | |
380 | * | |
381 | * @param string $entityId entity id | |
382 | * | |
383 | * @param string $entityTable entity Table | |
384 | * | |
385 | * @access public | |
386 | * @return void | |
387 | * @static | |
388 | */ | |
389 | static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution') { | |
421dfaa6 | 390 | |
6a488035 | 391 | if (!$entityId) { |
9da8dc8c | 392 | $priceSetDetails = CRM_Price_BAO_PriceSet::getDefaultPriceSet(); |
6a488035 TO |
393 | foreach ($priceSetDetails as $values) { |
394 | $params['line_item'][$values['setID']][$values['priceFieldID']] = array( | |
395 | 'price_field_id' => $values['priceFieldID'], | |
396 | 'price_field_value_id' => $values['priceFieldValueID'], | |
397 | 'label' => $values['label'], | |
398 | 'qty' => 1, | |
399 | 'unit_price' => $params['total_amount'], | |
400 | 'line_total' => $params['total_amount'], | |
401 | 'financial_type_id' => $params['financial_type_id'] | |
402 | ); | |
403 | } | |
421dfaa6 | 404 | } |
6a488035 TO |
405 | else { |
406 | $setID = NULL; | |
464bb009 PN |
407 | $totalEntityId = count($entityId); |
408 | foreach ($entityId as $id) { | |
409 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($id, $entityTable); | |
410 | foreach ($lineItems as $key => $values) { | |
32cb2feb | 411 | if (!$setID && $values['price_field_id']) { |
9da8dc8c | 412 | $setID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $values['price_field_id'], 'price_set_id'); |
413 | $params['is_quick_config'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'is_quick_config'); | |
464bb009 | 414 | } |
a7488080 | 415 | if (!empty($params['is_quick_config']) && array_key_exists('total_amount', $params) |
464bb009 PN |
416 | && $totalEntityId == 1) { |
417 | $values['line_total'] = $values['unit_price'] = $params['total_amount']; | |
418 | } | |
419 | $values['id'] = $key; | |
420 | $params['line_item'][$setID][$key] = $values; | |
6a488035 | 421 | } |
6a488035 TO |
422 | } |
423 | } | |
424 | } | |
9849720e RK |
425 | |
426 | /** | |
2826a42e RK |
427 | * Calculate tax rate in percentage |
428 | * | |
429 | * @param $lineItemId an assoc array of lineItem | |
430 | * | |
431 | * @return tax rate | |
432 | * | |
433 | * @access public | |
434 | * @static | |
9849720e RK |
435 | */ |
436 | public static function calculateTaxRate($lineItemId) { | |
437 | if ($lineItemId['html_type'] == 'Text') { | |
438 | $tax = (($lineItemId['line_total'] - ($lineItemId['unit_price'] * $lineItemId['qty'])))/($lineItemId['unit_price'] * $lineItemId['qty'])*100; | |
439 | } | |
440 | else { | |
441 | $tax = (($lineItemId['line_total'] - $lineItemId['unit_price'])/$lineItemId['unit_price']) * 100; | |
442 | } | |
443 | return $tax; | |
444 | } | |
6a488035 | 445 | } |