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, |
6a488035 TO |
133 | pfv.description"; |
134 | ||
135 | $fromClause = " | |
136 | FROM civicrm_%2 as %2 | |
137 | LEFT JOIN civicrm_line_item li ON ( li.entity_id = %2.id AND li.entity_table = 'civicrm_%2') | |
138 | LEFT JOIN civicrm_price_field_value pfv ON ( pfv.id = li.price_field_value_id ) | |
139 | LEFT JOIN civicrm_price_field pf ON (pf.id = li.price_field_id )"; | |
140 | $whereClause = " | |
141 | WHERE %2.id = %1"; | |
142 | ||
143 | if ($isQuick) { | |
144 | $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id "; | |
145 | $whereClause .= " and cps.is_quick_config = 0"; | |
146 | } | |
d5397f2f PJ |
147 | |
148 | if (!$isQtyZero) { | |
149 | $whereClause .= " and li.qty != 0"; | |
150 | } | |
151 | ||
6a488035 TO |
152 | $lineItems = array(); |
153 | ||
154 | if (!$entityId || !$entity || !$fromClause) { | |
155 | return $lineItems; | |
156 | } | |
157 | ||
158 | $params = array( | |
159 | 1 => array($entityId, 'Integer'), | |
160 | 2 => array($entity, 'Text'), | |
161 | ); | |
162 | ||
163 | $dao = CRM_Core_DAO::executeQuery("$selectClause $fromClause $whereClause", $params); | |
164 | while ($dao->fetch()) { | |
165 | if (!$dao->id) { | |
166 | continue; | |
167 | } | |
168 | $lineItems[$dao->id] = array( | |
169 | 'qty' => $dao->qty, | |
170 | 'label' => $dao->label, | |
171 | 'unit_price' => $dao->unit_price, | |
172 | 'line_total' => $dao->line_total, | |
173 | 'price_field_id' => $dao->price_field_id, | |
174 | 'participant_count' => $dao->participant_count, | |
175 | 'price_field_value_id' => $dao->price_field_value_id, | |
176 | 'field_title' => $dao->field_title, | |
177 | 'html_type' => $dao->html_type, | |
178 | 'description' => $dao->description, | |
179 | 'entity_id' => $entityId, | |
bf45dbe8 | 180 | 'financial_type_id' => $dao->financial_type_id, |
6a488035 | 181 | 'membership_type_id' => $dao->membership_type_id, |
9c09f5b7 | 182 | 'membership_num_terms' => $dao->membership_num_terms, |
6a488035 TO |
183 | ); |
184 | } | |
185 | return $lineItems; | |
186 | } | |
187 | ||
188 | /** | |
189 | * This method will create the lineItem array required for | |
190 | * processAmount method | |
191 | * | |
192 | * @param int $fid price set field id | |
193 | * @param array $params referance to form values | |
194 | * @param array $fields referance to array of fields belonging | |
195 | * to the price set used for particular event | |
196 | * @param array $values referance to the values array( | |
197 | this is | |
198 | * lineItem array) | |
199 | * | |
200 | * @return void | |
201 | * @access static | |
202 | */ | |
203 | static function format($fid, &$params, &$fields, &$values) { | |
204 | if (empty($params["price_{$fid}"])) { | |
205 | return; | |
206 | } | |
207 | ||
208 | $optionIDs = implode(',', array_keys($params["price_{$fid}"])); | |
209 | ||
210 | //lets first check in fun parameter, | |
211 | //since user might modified w/ hooks. | |
212 | $options = array(); | |
213 | if (array_key_exists('options', $fields)) { | |
214 | $options = $fields['options']; | |
215 | } | |
216 | else { | |
9da8dc8c | 217 | CRM_Price_BAO_PriceFieldValue::getValues($fid, $options, 'weight', TRUE); |
6a488035 TO |
218 | } |
219 | $fieldTitle = CRM_Utils_Array::value('label', $fields); | |
220 | if (!$fieldTitle) { | |
9da8dc8c | 221 | $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'label'); |
6a488035 TO |
222 | } |
223 | ||
224 | foreach ($params["price_{$fid}"] as $oid => $qty) { | |
225 | $price = $options[$oid]['amount']; | |
226 | ||
227 | // lets clean the price in case it is not yet cleant | |
228 | // CRM-10974 | |
229 | $price = CRM_Utils_Rule::cleanMoney($price); | |
230 | ||
231 | $participantsPerField = CRM_Utils_Array::value('count', $options[$oid], 0); | |
232 | ||
233 | $values[$oid] = array( | |
234 | 'price_field_id' => $fid, | |
235 | 'price_field_value_id' => $oid, | |
236 | 'label' => CRM_Utils_Array::value('label', $options[$oid]), | |
237 | 'field_title' => $fieldTitle, | |
238 | 'description' => CRM_Utils_Array::value('description', $options[$oid]), | |
239 | 'qty' => $qty, | |
240 | 'unit_price' => $price, | |
241 | 'line_total' => $qty * $price, | |
242 | 'participant_count' => $qty * $participantsPerField, | |
243 | 'max_value' => CRM_Utils_Array::value('max_value', $options[$oid]), | |
244 | 'membership_type_id' => CRM_Utils_Array::value('membership_type_id', $options[$oid]), | |
245 | 'membership_num_terms' => CRM_Utils_Array::value('membership_num_terms', $options[$oid]), | |
246 | 'auto_renew' => CRM_Utils_Array::value('auto_renew', $options[$oid]), | |
247 | 'html_type' => $fields['html_type'], | |
bf45dbe8 | 248 | 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $options[$oid]), |
6a488035 | 249 | ); |
cdeb4bdf | 250 | |
6a488035 | 251 | if ($values[$oid]['membership_type_id'] && !isset($values[$oid]['auto_renew'])) { |
421dfaa6 | 252 | $values[$oid]['auto_renew'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $values[$oid]['membership_type_id'], 'auto_renew'); |
6a488035 TO |
253 | } |
254 | } | |
255 | } | |
256 | ||
257 | /** | |
258 | * Delete line items for given entity. | |
259 | * | |
260 | * @param int $entityId | |
261 | * @param int $entityTable | |
262 | * | |
77b97be7 | 263 | * @return bool |
6a488035 TO |
264 | * @access public |
265 | * @static | |
266 | */ | |
421dfaa6 | 267 | public static function deleteLineItems($entityId, $entityTable) { |
6a488035 | 268 | if (!$entityId || !$entityTable) { |
9330406e | 269 | return FALSE; |
6a488035 TO |
270 | } |
271 | ||
272 | if ($entityId && !is_array($entityId)) { | |
273 | $entityId = array($entityId); | |
274 | } | |
275 | ||
de1c25e1 | 276 | $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'"; |
6a488035 | 277 | $dao = CRM_Core_DAO::executeQuery($query); |
9330406e | 278 | return TRUE; |
6a488035 TO |
279 | } |
280 | ||
281 | /** | |
282 | * Function to process price set and line items. | |
dd244018 EM |
283 | * |
284 | * @param $entityId | |
6a488035 TO |
285 | * @param array $lineItem line item array |
286 | * @param object $contributionDetails | |
6a488035 TO |
287 | * @param string $entityTable entity table |
288 | * | |
dd244018 EM |
289 | * @param bool $update |
290 | * | |
291 | * @internal param int $contributionId contribution id | |
292 | * @internal param \decimal $initAmount amount | |
6a488035 TO |
293 | * @access public |
294 | * @return void | |
295 | * @static | |
296 | */ | |
297 | static function processPriceSet($entityId, $lineItem, $contributionDetails = NULL, $entityTable = 'civicrm_contribution', $update = FALSE) { | |
298 | if (!$entityId || !is_array($lineItem) | |
299 | || CRM_Utils_system::isNull($lineItem) | |
300 | ) { | |
301 | return; | |
302 | } | |
421dfaa6 | 303 | |
6a488035 TO |
304 | foreach ($lineItem as $priceSetId => $values) { |
305 | if (!$priceSetId) { | |
306 | continue; | |
307 | } | |
308 | ||
309 | foreach ($values as $line) { | |
310 | $line['entity_table'] = $entityTable; | |
311 | $line['entity_id'] = $entityId; | |
312 | // if financial type is not set and if price field value is NOT NULL | |
313 | // get financial type id of price field value | |
8cc574cf | 314 | if (!empty($line['price_field_value_id']) && empty($line['financial_type_id'])) { |
9da8dc8c | 315 | $line['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $line['price_field_value_id'], 'financial_type_id'); |
6a488035 TO |
316 | } |
317 | $lineItems = CRM_Price_BAO_LineItem::create($line); | |
318 | if (!$update && $contributionDetails) { | |
319 | CRM_Financial_BAO_FinancialItem::add($lineItems, $contributionDetails); | |
320 | } | |
321 | } | |
322 | } | |
421dfaa6 | 323 | } |
6a488035 TO |
324 | |
325 | public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) { | |
326 | if (!$entityId || CRM_Utils_System::isNull($amount)) | |
327 | return; | |
328 | ||
329 | $from = " civicrm_line_item li | |
330 | LEFT JOIN civicrm_price_field pf ON pf.id = li.price_field_id | |
331 | LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id "; | |
332 | ||
421dfaa6 | 333 | $set = " li.unit_price = %3, |
6a488035 TO |
334 | li.line_total = %3 "; |
335 | ||
421dfaa6 | 336 | $where = " li.entity_id = %1 AND |
6a488035 TO |
337 | li.entity_table = %2 "; |
338 | ||
339 | $params = array( | |
340 | 1 => array($entityId, 'Integer'), | |
341 | 2 => array($entityTable, 'String'), | |
342 | 3 => array($amount, 'Float'), | |
343 | ); | |
344 | ||
345 | if ($entityTable == 'civicrm_contribution') { | |
346 | $entityName = 'default_contribution_amount'; | |
347 | $where .= " AND ps.name = %4 "; | |
421dfaa6 | 348 | $params[4] = array($entityName, 'String'); |
349 | } | |
6a488035 TO |
350 | elseif ($entityTable == 'civicrm_participant') { |
351 | $from .= " | |
352 | LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = ps.id | |
353 | LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = pf.id and cpfv.label = %4 "; | |
354 | $set .= " ,li.label = %4, | |
355 | li.price_field_value_id = cpfv.id "; | |
356 | $where .= " AND cpse.entity_table = 'civicrm_event' AND cpse.entity_id = %5 "; | |
357 | $amount = empty($amount) ? 0: $amount; | |
358 | $params += array( | |
359 | 4 => array($otherParams['fee_label'], 'String'), | |
360 | 5 => array($otherParams['event_id'], 'String'), | |
361 | ); | |
362 | } | |
363 | ||
421dfaa6 | 364 | $query = " |
6a488035 TO |
365 | UPDATE $from |
366 | SET $set | |
421dfaa6 | 367 | WHERE $where |
6a488035 TO |
368 | "; |
369 | ||
370 | CRM_Core_DAO::executeQuery($query, $params); | |
371 | } | |
372 | ||
373 | /** | |
374 | * Function to build line items array. | |
375 | * @param int $params form values | |
376 | * | |
377 | * @param string $entityId entity id | |
378 | * | |
379 | * @param string $entityTable entity Table | |
380 | * | |
381 | * @access public | |
382 | * @return void | |
383 | * @static | |
384 | */ | |
385 | static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution') { | |
421dfaa6 | 386 | |
6a488035 | 387 | if (!$entityId) { |
9da8dc8c | 388 | $priceSetDetails = CRM_Price_BAO_PriceSet::getDefaultPriceSet(); |
6a488035 TO |
389 | foreach ($priceSetDetails as $values) { |
390 | $params['line_item'][$values['setID']][$values['priceFieldID']] = array( | |
391 | 'price_field_id' => $values['priceFieldID'], | |
392 | 'price_field_value_id' => $values['priceFieldValueID'], | |
393 | 'label' => $values['label'], | |
394 | 'qty' => 1, | |
395 | 'unit_price' => $params['total_amount'], | |
396 | 'line_total' => $params['total_amount'], | |
397 | 'financial_type_id' => $params['financial_type_id'] | |
398 | ); | |
399 | } | |
421dfaa6 | 400 | } |
6a488035 TO |
401 | else { |
402 | $setID = NULL; | |
464bb009 PN |
403 | $totalEntityId = count($entityId); |
404 | foreach ($entityId as $id) { | |
405 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($id, $entityTable); | |
406 | foreach ($lineItems as $key => $values) { | |
32cb2feb | 407 | if (!$setID && $values['price_field_id']) { |
9da8dc8c | 408 | $setID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $values['price_field_id'], 'price_set_id'); |
409 | $params['is_quick_config'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'is_quick_config'); | |
464bb009 | 410 | } |
a7488080 | 411 | if (!empty($params['is_quick_config']) && array_key_exists('total_amount', $params) |
464bb009 PN |
412 | && $totalEntityId == 1) { |
413 | $values['line_total'] = $values['unit_price'] = $params['total_amount']; | |
414 | } | |
415 | $values['id'] = $key; | |
416 | $params['line_item'][$setID][$key] = $values; | |
6a488035 | 417 | } |
6a488035 TO |
418 | } |
419 | } | |
420 | } | |
421 | } |