3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
39 * @author Marshal Newrock <marshal@idealso.com>
44 * Business objects for Line Items generated by monetary transactions
46 class CRM_Price_BAO_LineItem
extends CRM_Price_DAO_LineItem
{
49 * Creates a new entry in the database.
51 * @param array $params (reference) an assoc array of name/value pairs
53 * @return object CRM_Price_DAO_LineItem object
57 static function create(&$params) {
58 //create mode only as we don't support editing line items
60 CRM_Utils_Hook
::pre('create', 'LineItem', $params['entity_id'], $params);
62 $lineItemBAO = new CRM_Price_BAO_LineItem();
63 $lineItemBAO->copyValues($params);
65 $return = $lineItemBAO->save();
67 CRM_Utils_Hook
::post('create', 'LineItem', $params['entity_id'], $params);
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.
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
81 * @return object CRM_Price_BAO_LineItem object
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);
101 static function getLineTotal($entityId, $entityTable) {
102 $sqlLineItemTotal = "SELECT SUM(li.line_total)
103 FROM civicrm_line_item li
104 WHERE li.entity_table = '{$entityTable}'
105 AND li.entity_id = {$entityId}
107 $lineItemTotal = CRM_Core_DAO
::singleValueQuery($sqlLineItemTotal);
108 return $lineItemTotal;
112 * Given a participant id/contribution id,
113 * return contribution/fee line items
115 * @param $entityId int participant/contribution id
116 * @param $entity string participant/contribution.
118 * @param null $isQuick
119 * @param bool $isQtyZero
121 * @return array of line items
123 static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL , $isQtyZero = TRUE) {
124 $selectClause = $whereClause = $fromClause = NULL;
132 pf.label as field_title,
134 pfv.membership_type_id,
135 pfv.membership_num_terms,
137 li.participant_count,
138 li.price_field_value_id,
139 li.financial_type_id,
143 FROM civicrm_%2 as %2
144 LEFT JOIN civicrm_line_item li ON (( li.entity_id = %2.id AND li.entity_table = 'civicrm_%2')
145 OR (li.contribution_id = %2.id AND li.entity_table = 'civicrm_membership')
146 OR (li.contribution_id = %2.id AND li.entity_table = 'civicrm_participant'))
147 LEFT JOIN civicrm_price_field_value pfv ON ( pfv.id = li.price_field_value_id )
148 LEFT JOIN civicrm_price_field pf ON (pf.id = li.price_field_id )";
152 $orderByClause = " ORDER BY pf.weight, pfv.weight";
155 $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id ";
156 $whereClause .= " and cps.is_quick_config = 0";
160 $whereClause .= " and li.qty != 0";
163 $lineItems = array();
165 if (!$entityId ||
!$entity ||
!$fromClause) {
170 1 => array($entityId, 'Integer'),
171 2 => array($entity, 'Text'),
174 $dao = CRM_Core_DAO
::executeQuery("$selectClause $fromClause $whereClause $orderByClause", $params);
175 while ($dao->fetch()) {
179 $lineItems[$dao->id
] = array(
181 'label' => $dao->label
,
182 'unit_price' => $dao->unit_price
,
183 'line_total' => $dao->line_total
,
184 'price_field_id' => $dao->price_field_id
,
185 'participant_count' => $dao->participant_count
,
186 'price_field_value_id' => $dao->price_field_value_id
,
187 'field_title' => $dao->field_title
,
188 'html_type' => $dao->html_type
,
189 'description' => $dao->description
,
190 'entity_id' => $entityId,
191 'contribution_id' => $dao->contribution_id
,
192 'financial_type_id' => $dao->financial_type_id
,
193 'membership_type_id' => $dao->membership_type_id
,
194 'membership_num_terms' => $dao->membership_num_terms
,
201 * This method will create the lineItem array required for
202 * processAmount method
204 * @param int $fid price set field id
205 * @param array $params referance to form values
206 * @param array $fields referance to array of fields belonging
207 * to the price set used for particular event
208 * @param array $values referance to the values array(
215 static function format($fid, &$params, &$fields, &$values) {
216 if (empty($params["price_{$fid}"])) {
220 $optionIDs = implode(',', array_keys($params["price_{$fid}"]));
222 //lets first check in fun parameter,
223 //since user might modified w/ hooks.
225 if (array_key_exists('options', $fields)) {
226 $options = $fields['options'];
229 CRM_Price_BAO_PriceFieldValue
::getValues($fid, $options, 'weight', TRUE);
231 $fieldTitle = CRM_Utils_Array
::value('label', $fields);
233 $fieldTitle = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'label');
236 foreach ($params["price_{$fid}"] as $oid => $qty) {
237 $price = $options[$oid]['amount'];
239 // lets clean the price in case it is not yet cleant
241 $price = CRM_Utils_Rule
::cleanMoney($price);
243 $participantsPerField = CRM_Utils_Array
::value('count', $options[$oid], 0);
245 $values[$oid] = array(
246 'price_field_id' => $fid,
247 'price_field_value_id' => $oid,
248 'label' => CRM_Utils_Array
::value('label', $options[$oid]),
249 'field_title' => $fieldTitle,
250 'description' => CRM_Utils_Array
::value('description', $options[$oid]),
252 'unit_price' => $price,
253 'line_total' => $qty * $price,
254 'participant_count' => $qty * $participantsPerField,
255 'max_value' => CRM_Utils_Array
::value('max_value', $options[$oid]),
256 'membership_type_id' => CRM_Utils_Array
::value('membership_type_id', $options[$oid]),
257 'membership_num_terms' => CRM_Utils_Array
::value('membership_num_terms', $options[$oid]),
258 'auto_renew' => CRM_Utils_Array
::value('auto_renew', $options[$oid]),
259 'html_type' => $fields['html_type'],
260 'financial_type_id' => CRM_Utils_Array
::value('financial_type_id', $options[$oid]),
263 if ($values[$oid]['membership_type_id'] && empty($values[$oid]['auto_renew'])) {
264 $values[$oid]['auto_renew'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $values[$oid]['membership_type_id'], 'auto_renew');
270 * Delete line items for given entity.
272 * @param int $entityId
273 * @param int $entityTable
279 public static function deleteLineItems($entityId, $entityTable) {
280 if (!$entityId ||
!$entityTable) {
284 if ($entityId && !is_array($entityId)) {
285 $entityId = array($entityId);
288 $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'";
289 $dao = CRM_Core_DAO
::executeQuery($query);
294 * Function to process price set and line items.
297 * @param array $lineItem line item array
298 * @param object $contributionDetails
299 * @param string $entityTable entity table
301 * @param bool $update
303 * @internal param int $contributionId contribution id
304 * @internal param \decimal $initAmount amount
309 static function processPriceSet($entityId, $lineItem, $contributionDetails = NULL, $entityTable = 'civicrm_contribution', $update = FALSE) {
310 if (!$entityId ||
!is_array($lineItem)
311 || CRM_Utils_system
::isNull($lineItem)
316 foreach ($lineItem as $priceSetId => $values) {
321 foreach ($values as $line) {
322 $line['entity_table'] = $entityTable;
323 $line['entity_id'] = $entityId;
324 if(!empty($line['membership_type_id'])) {
325 $entityTable == 'civicrm_membership';
326 $line['contribution_id'] = $contributionDetails->id
;
328 if ($entityTable == 'civicrm_contribution') {
329 $line['contribution_id'] = $entityId;
332 if (!empty($contributionDetails->id
)) {
333 $line['contribution_id'] = $contributionDetails->id
;
336 // if financial type is not set and if price field value is NOT NULL
337 // get financial type id of price field value
338 if (!empty($line['price_field_value_id']) && empty($line['financial_type_id'])) {
339 $line['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $line['price_field_value_id'], 'financial_type_id');
341 $lineItems = CRM_Price_BAO_LineItem
::create($line);
342 if (!$update && $contributionDetails) {
343 CRM_Financial_BAO_FinancialItem
::add($lineItems, $contributionDetails);
351 * @param string $entityTable
353 * @param null $otherParams
355 public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) {
356 if (!$entityId || CRM_Utils_System
::isNull($amount))
359 $from = " civicrm_line_item li
360 LEFT JOIN civicrm_price_field pf ON pf.id = li.price_field_id
361 LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id ";
363 $set = " li.unit_price = %3,
364 li.line_total = %3 ";
366 $where = " li.entity_id = %1 AND
367 li.entity_table = %2 ";
370 1 => array($entityId, 'Integer'),
371 2 => array($entityTable, 'String'),
372 3 => array($amount, 'Float'),
375 if ($entityTable == 'civicrm_contribution') {
376 $entityName = 'default_contribution_amount';
377 $where .= " AND ps.name = %4 ";
378 $params[4] = array($entityName, 'String');
380 elseif ($entityTable == 'civicrm_participant') {
382 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = ps.id
383 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = pf.id and cpfv.label = %4 ";
384 $set .= " ,li.label = %4,
385 li.price_field_value_id = cpfv.id ";
386 $where .= " AND cpse.entity_table = 'civicrm_event' AND cpse.entity_id = %5 ";
387 $amount = empty($amount) ?
0: $amount;
389 4 => array($otherParams['fee_label'], 'String'),
390 5 => array($otherParams['event_id'], 'String'),
400 CRM_Core_DAO
::executeQuery($query, $params);
404 * Function to build line items array.
405 * @param array $params form values
407 * @param string $entityId entity id
409 * @param string $entityTable entity Table
415 static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution') {
418 $priceSetDetails = CRM_Price_BAO_PriceSet
::getDefaultPriceSet();
419 foreach ($priceSetDetails as $values) {
420 $params['line_item'][$values['setID']][$values['priceFieldID']] = array(
421 'price_field_id' => $values['priceFieldID'],
422 'price_field_value_id' => $values['priceFieldValueID'],
423 'label' => $values['label'],
425 'unit_price' => $params['total_amount'],
426 'line_total' => $params['total_amount'],
427 'financial_type_id' => $params['financial_type_id']
433 $totalEntityId = count($entityId);
434 foreach ($entityId as $id) {
435 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($id, $entityTable);
436 foreach ($lineItems as $key => $values) {
437 if (!$setID && $values['price_field_id']) {
438 $setID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $values['price_field_id'], 'price_set_id');
439 $params['is_quick_config'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'is_quick_config');
441 if (!empty($params['is_quick_config']) && array_key_exists('total_amount', $params)
442 && $totalEntityId == 1) {
443 $values['line_total'] = $values['unit_price'] = $params['total_amount'];
445 $values['id'] = $key;
446 $params['line_item'][$setID][$key] = $values;