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 $id = CRM_Utils_Array
::value('id', $params);
60 CRM_Utils_Hook
::pre('edit', 'LineItem', $id, $params);
63 CRM_Utils_Hook
::pre('create', 'LineItem', $params['entity_id'], $params);
66 // unset entity table and entity id in $params
67 // we never update the entity table and entity id during update mode
69 unset($params['entity_id'], $params['entity_table']);
72 $lineItemBAO = new CRM_Price_BAO_LineItem();
73 $lineItemBAO->copyValues($params);
75 $return = $lineItemBAO->save();
78 CRM_Utils_Hook
::post('edit', 'LineItem', $id, $params);
81 CRM_Utils_Hook
::post('create', 'LineItem', $params['entity_id'], $params);
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.
93 * @param array $params (reference ) an assoc array of name/value pairs
94 * @param array $defaults (reference ) an assoc array to hold the flattened values
96 * @return object CRM_Price_BAO_LineItem object
100 static function retrieve(&$params, &$defaults) {
101 $lineItem = new CRM_Price_BAO_LineItem();
102 $lineItem->copyValues($params);
103 if ($lineItem->find(TRUE)) {
104 CRM_Core_DAO
::storeValues($lineItem, $defaults);
112 * @param $entityTable
114 * @return null|string
116 static function getLineTotal($entityId, $entityTable) {
117 $sqlLineItemTotal = "SELECT SUM(li.line_total + COALESCE(li.tax_amount,0))
118 FROM civicrm_line_item li
119 WHERE li.entity_table = '{$entityTable}'
120 AND li.entity_id = {$entityId}
122 $lineItemTotal = CRM_Core_DAO
::singleValueQuery($sqlLineItemTotal);
123 return $lineItemTotal;
127 * Given a participant id/contribution id,
128 * return contribution/fee line items
130 * @param $entityId int participant/contribution id
131 * @param $entity string participant/contribution.
133 * @param null $isQuick
134 * @param bool $isQtyZero
135 * @param bool $relatedEntity
137 * @return array of line items
139 static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL , $isQtyZero = TRUE, $relatedEntity = FALSE) {
140 $selectClause = $whereClause = $fromClause = NULL;
148 pf.label as field_title,
150 pfv.membership_type_id,
151 pfv.membership_num_terms,
153 li.participant_count,
154 li.price_field_value_id,
155 li.financial_type_id,
159 $condition = "li.entity_id = %2.id AND li.entity_table = 'civicrm_%2'";
160 if ($relatedEntity) {
161 $condition = "li.contribution_id = %2.id ";
165 FROM civicrm_%2 as %2
166 LEFT JOIN civicrm_line_item li ON ({$condition})
167 LEFT JOIN civicrm_price_field_value pfv ON ( pfv.id = li.price_field_value_id )
168 LEFT JOIN civicrm_price_field pf ON (pf.id = li.price_field_id )";
172 $orderByClause = " ORDER BY pf.weight, pfv.weight";
175 $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id ";
176 $whereClause .= " and cps.is_quick_config = 0";
180 $whereClause .= " and li.qty != 0";
183 $lineItems = array();
185 if (!$entityId ||
!$entity ||
!$fromClause) {
190 1 => array($entityId, 'Integer'),
191 2 => array($entity, 'Text'),
194 $getTaxDetails = FALSE;
195 $invoiceSettings = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CONTRIBUTE_PREFERENCES_NAME
,'contribution_invoice_settings');
196 $invoicing = CRM_Utils_Array
::value('invoicing', $invoiceSettings);
197 $dao = CRM_Core_DAO
::executeQuery("$selectClause $fromClause $whereClause $orderByClause", $params);
198 while ($dao->fetch()) {
202 $lineItems[$dao->id
] = array(
204 'label' => $dao->label
,
205 'unit_price' => $dao->unit_price
,
206 'line_total' => $dao->line_total
,
207 'price_field_id' => $dao->price_field_id
,
208 'participant_count' => $dao->participant_count
,
209 'price_field_value_id' => $dao->price_field_value_id
,
210 'field_title' => $dao->field_title
,
211 'html_type' => $dao->html_type
,
212 'description' => $dao->description
,
213 'entity_id' => $entityId,
214 'contribution_id' => $dao->contribution_id
,
215 'financial_type_id' => $dao->financial_type_id
,
216 'membership_type_id' => $dao->membership_type_id
,
217 'membership_num_terms' => $dao->membership_num_terms
,
218 'tax_amount' => $dao->tax_amount
,
220 $lineItems[$dao->id
]['tax_rate'] = CRM_Price_BAO_LineItem
::calculateTaxRate($lineItems[$dao->id
]);
221 $lineItems[$dao->id
]['subTotal'] = $lineItems[$dao->id
]['qty'] * $lineItems[$dao->id
]['unit_price'];
222 if ($lineItems[$dao->id
]['tax_amount'] != '') {
223 $getTaxDetails = TRUE;
227 $taxTerm = CRM_Utils_Array
::value('tax_term', $invoiceSettings);
228 $smarty = CRM_Core_Smarty
::singleton();
229 $smarty->assign('taxTerm', $taxTerm);
230 $smarty->assign('getTaxDetails', $getTaxDetails);
236 * This method will create the lineItem array required for
237 * processAmount method
239 * @param int $fid price set field id
240 * @param array $params referance to form values
241 * @param array $fields referance to array of fields belonging
242 * to the price set used for particular event
243 * @param array $values referance to the values array(
250 static function format($fid, &$params, &$fields, &$values) {
251 if (empty($params["price_{$fid}"])) {
255 $optionIDs = implode(',', array_keys($params["price_{$fid}"]));
257 //lets first check in fun parameter,
258 //since user might modified w/ hooks.
260 if (array_key_exists('options', $fields)) {
261 $options = $fields['options'];
264 CRM_Price_BAO_PriceFieldValue
::getValues($fid, $options, 'weight', TRUE);
266 $fieldTitle = CRM_Utils_Array
::value('label', $fields);
268 $fieldTitle = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'label');
271 foreach ($params["price_{$fid}"] as $oid => $qty) {
272 $price = $options[$oid]['amount'];
274 // lets clean the price in case it is not yet cleant
276 $price = CRM_Utils_Rule
::cleanMoney($price);
278 $participantsPerField = CRM_Utils_Array
::value('count', $options[$oid], 0);
280 $values[$oid] = array(
281 'price_field_id' => $fid,
282 'price_field_value_id' => $oid,
283 'label' => CRM_Utils_Array
::value('label', $options[$oid]),
284 'field_title' => $fieldTitle,
285 'description' => CRM_Utils_Array
::value('description', $options[$oid]),
287 'unit_price' => $price,
288 'line_total' => $qty * $price,
289 'participant_count' => $qty * $participantsPerField,
290 'max_value' => CRM_Utils_Array
::value('max_value', $options[$oid]),
291 'membership_type_id' => CRM_Utils_Array
::value('membership_type_id', $options[$oid]),
292 'membership_num_terms' => CRM_Utils_Array
::value('membership_num_terms', $options[$oid]),
293 'auto_renew' => CRM_Utils_Array
::value('auto_renew', $options[$oid]),
294 'html_type' => $fields['html_type'],
295 'financial_type_id' => CRM_Utils_Array
::value('financial_type_id', $options[$oid]),
296 'tax_amount' => CRM_Utils_Array
::value('tax_amount', $options[$oid]),
299 if ($values[$oid]['membership_type_id'] && empty($values[$oid]['auto_renew'])) {
300 $values[$oid]['auto_renew'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $values[$oid]['membership_type_id'], 'auto_renew');
306 * Delete line items for given entity.
308 * @param int $entityId
309 * @param int $entityTable
315 public static function deleteLineItems($entityId, $entityTable) {
316 if (!$entityId ||
!$entityTable) {
320 if ($entityId && !is_array($entityId)) {
321 $entityId = array($entityId);
324 $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'";
325 $dao = CRM_Core_DAO
::executeQuery($query);
330 * Function to process price set and line items.
333 * @param array $lineItem line item array
334 * @param object $contributionDetails
335 * @param string $entityTable entity table
337 * @param bool $update
339 * @internal param int $contributionId contribution id
340 * @internal param \decimal $initAmount amount
345 static function processPriceSet($entityId, $lineItem, $contributionDetails = NULL, $entityTable = 'civicrm_contribution', $update = FALSE) {
346 if (!$entityId ||
!is_array($lineItem)
347 || CRM_Utils_system
::isNull($lineItem)
352 foreach ($lineItem as $priceSetId => $values) {
357 foreach ($values as $line) {
358 $line['entity_table'] = $entityTable;
359 $line['entity_id'] = $entityId;
360 if(!empty($line['membership_type_id'])) {
361 $entityTable == 'civicrm_membership';
362 $line['contribution_id'] = $contributionDetails->id
;
364 if ($entityTable == 'civicrm_contribution') {
365 $line['contribution_id'] = $entityId;
368 if (!empty($contributionDetails->id
)) {
369 $line['contribution_id'] = $contributionDetails->id
;
372 // if financial type is not set and if price field value is NOT NULL
373 // get financial type id of price field value
374 if (!empty($line['price_field_value_id']) && empty($line['financial_type_id'])) {
375 $line['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $line['price_field_value_id'], 'financial_type_id');
377 $lineItems = CRM_Price_BAO_LineItem
::create($line);
378 if (!$update && $contributionDetails) {
379 CRM_Financial_BAO_FinancialItem
::add($lineItems, $contributionDetails);
380 if (isset($line['tax_amount'])) {
381 CRM_Financial_BAO_FinancialItem
::add($lineItems, $contributionDetails, TRUE);
390 * @param string $entityTable
392 * @param null $otherParams
394 public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) {
395 if (!$entityId || CRM_Utils_System
::isNull($amount))
398 $from = " civicrm_line_item li
399 LEFT JOIN civicrm_price_field pf ON pf.id = li.price_field_id
400 LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id ";
402 $set = " li.unit_price = %3,
403 li.line_total = %3 ";
405 $where = " li.entity_id = %1 AND
406 li.entity_table = %2 ";
409 1 => array($entityId, 'Integer'),
410 2 => array($entityTable, 'String'),
411 3 => array($amount, 'Float'),
414 if ($entityTable == 'civicrm_contribution') {
415 $entityName = 'default_contribution_amount';
416 $where .= " AND ps.name = %4 ";
417 $params[4] = array($entityName, 'String');
419 elseif ($entityTable == 'civicrm_participant') {
421 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = ps.id
422 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = pf.id and cpfv.label = %4 ";
423 $set .= " ,li.label = %4,
424 li.price_field_value_id = cpfv.id ";
425 $where .= " AND cpse.entity_table = 'civicrm_event' AND cpse.entity_id = %5 ";
426 $amount = empty($amount) ?
0: $amount;
428 4 => array($otherParams['fee_label'], 'String'),
429 5 => array($otherParams['event_id'], 'String'),
439 CRM_Core_DAO
::executeQuery($query, $params);
443 * Function to build line items array.
444 * @param array $params form values
446 * @param string $entityId entity id
448 * @param string $entityTable entity Table
454 static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution') {
457 $priceSetDetails = CRM_Price_BAO_PriceSet
::getDefaultPriceSet();
458 foreach ($priceSetDetails as $values) {
459 $params['line_item'][$values['setID']][$values['priceFieldID']] = array(
460 'price_field_id' => $values['priceFieldID'],
461 'price_field_value_id' => $values['priceFieldValueID'],
462 'label' => $values['label'],
464 'unit_price' => $params['total_amount'],
465 'line_total' => $params['total_amount'],
466 'financial_type_id' => $params['financial_type_id']
472 $totalEntityId = count($entityId);
473 foreach ($entityId as $id) {
474 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($id, $entityTable);
475 foreach ($lineItems as $key => $values) {
476 if (!$setID && $values['price_field_id']) {
477 $setID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $values['price_field_id'], 'price_set_id');
478 $params['is_quick_config'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'is_quick_config');
480 if (!empty($params['is_quick_config']) && array_key_exists('total_amount', $params)
481 && $totalEntityId == 1) {
482 $values['line_total'] = $values['unit_price'] = $params['total_amount'];
484 $values['id'] = $key;
485 $params['line_item'][$setID][$key] = $values;
492 * Calculate tax rate in percentage
494 * @param $lineItemId an assoc array of lineItem
501 public static function calculateTaxRate($lineItemId) {
502 if ($lineItemId['html_type'] == 'Text') {
503 $tax = $lineItemId['tax_amount']/($lineItemId['unit_price'] * $lineItemId['qty'])*100;
506 $tax = ($lineItemId['tax_amount']/$lineItemId['unit_price']) * 100;