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);
95 static function getLineTotal($entityId, $entityTable) {
96 $sqlLineItemTotal = "SELECT SUM(li.line_total)
97 FROM civicrm_line_item li
98 WHERE li.entity_table = '{$entityTable}'
99 AND li.entity_id = {$entityId}
101 $lineItemTotal = CRM_Core_DAO
::singleValueQuery($sqlLineItemTotal);
102 return $lineItemTotal;
106 * Given a participant id/contribution id,
107 * return contribution/fee line items
109 * @param $entityId int participant/contribution id
110 * @param $entity string participant/contribution.
112 * @param null $isQuick
113 * @param bool $isQtyZero
115 * @return array of line items
117 static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL , $isQtyZero = TRUE) {
118 $selectClause = $whereClause = $fromClause = NULL;
125 pf.label as field_title,
127 pfv.membership_type_id,
128 pfv.membership_num_terms,
130 li.participant_count,
131 li.price_field_value_id,
132 li.financial_type_id,
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 )";
145 $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id ";
146 $whereClause .= " and cps.is_quick_config = 0";
150 $whereClause .= " and li.qty != 0";
153 $lineItems = array();
155 if (!$entityId ||
!$entity ||
!$fromClause) {
160 1 => array($entityId, 'Integer'),
161 2 => array($entity, 'Text'),
164 $dao = CRM_Core_DAO
::executeQuery("$selectClause $fromClause $whereClause", $params);
165 $getTaxDetails = FALSE;
166 $invoiceSettings = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CONTRIBUTE_PREFERENCES_NAME
,'contribution_invoice_settings');
167 $invoicing = CRM_Utils_Array
::value('invoicing', $invoiceSettings);
168 while ($dao->fetch()) {
172 $lineItems[$dao->id
] = array(
174 'label' => $dao->label
,
175 'unit_price' => $dao->unit_price
,
176 'line_total' => $dao->line_total
,
177 'price_field_id' => $dao->price_field_id
,
178 'participant_count' => $dao->participant_count
,
179 'price_field_value_id' => $dao->price_field_value_id
,
180 'field_title' => $dao->field_title
,
181 'html_type' => $dao->html_type
,
182 'description' => $dao->description
,
183 'entity_id' => $entityId,
184 'financial_type_id' => $dao->financial_type_id
,
185 'membership_type_id' => $dao->membership_type_id
,
186 'membership_num_terms' => $dao->membership_num_terms
,
187 'tax_amount' => $dao->tax_amount
,
189 $lineItems[$dao->id
]['tax_rate'] = CRM_Price_BAO_LineItem
::calculateTaxRate($lineItems[$dao->id
]);
190 $lineItems[$dao->id
]['subTotal'] = $lineItems[$dao->id
]['qty'] * $lineItems[$dao->id
]['unit_price'];
191 if ($lineItems[$dao->id
]['tax_amount'] != '') {
192 $getTaxDetails = TRUE;
196 $taxTerm = CRM_Utils_Array
::value('tax_term', $invoiceSettings);
197 $smarty = CRM_Core_Smarty
::singleton();
198 $smarty->assign('taxTerm', $taxTerm);
199 $smarty->assign('getTaxDetails', $getTaxDetails);
205 * This method will create the lineItem array required for
206 * processAmount method
208 * @param int $fid price set field id
209 * @param array $params referance to form values
210 * @param array $fields referance to array of fields belonging
211 * to the price set used for particular event
212 * @param array $values referance to the values array(
219 static function format($fid, &$params, &$fields, &$values) {
220 if (empty($params["price_{$fid}"])) {
224 $optionIDs = implode(',', array_keys($params["price_{$fid}"]));
226 //lets first check in fun parameter,
227 //since user might modified w/ hooks.
229 if (array_key_exists('options', $fields)) {
230 $options = $fields['options'];
233 CRM_Price_BAO_PriceFieldValue
::getValues($fid, $options, 'weight', TRUE);
235 $fieldTitle = CRM_Utils_Array
::value('label', $fields);
237 $fieldTitle = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'label');
240 foreach ($params["price_{$fid}"] as $oid => $qty) {
241 $price = $options[$oid]['amount'];
243 // lets clean the price in case it is not yet cleant
245 $price = CRM_Utils_Rule
::cleanMoney($price);
247 $participantsPerField = CRM_Utils_Array
::value('count', $options[$oid], 0);
249 $values[$oid] = array(
250 'price_field_id' => $fid,
251 'price_field_value_id' => $oid,
252 'label' => CRM_Utils_Array
::value('label', $options[$oid]),
253 'field_title' => $fieldTitle,
254 'description' => CRM_Utils_Array
::value('description', $options[$oid]),
256 'unit_price' => $price,
257 'line_total' => $qty * $price,
258 'participant_count' => $qty * $participantsPerField,
259 'max_value' => CRM_Utils_Array
::value('max_value', $options[$oid]),
260 'membership_type_id' => CRM_Utils_Array
::value('membership_type_id', $options[$oid]),
261 'membership_num_terms' => CRM_Utils_Array
::value('membership_num_terms', $options[$oid]),
262 'auto_renew' => CRM_Utils_Array
::value('auto_renew', $options[$oid]),
263 'html_type' => $fields['html_type'],
264 'financial_type_id' => CRM_Utils_Array
::value('financial_type_id', $options[$oid]),
267 if ($values[$oid]['membership_type_id'] && !isset($values[$oid]['auto_renew'])) {
268 $values[$oid]['auto_renew'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $values[$oid]['membership_type_id'], 'auto_renew');
274 * Delete line items for given entity.
276 * @param int $entityId
277 * @param int $entityTable
283 public static function deleteLineItems($entityId, $entityTable) {
284 if (!$entityId ||
!$entityTable) {
288 if ($entityId && !is_array($entityId)) {
289 $entityId = array($entityId);
292 $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'";
293 $dao = CRM_Core_DAO
::executeQuery($query);
298 * Function to process price set and line items.
301 * @param array $lineItem line item array
302 * @param object $contributionDetails
303 * @param string $entityTable entity table
305 * @param bool $update
307 * @internal param int $contributionId contribution id
308 * @internal param \decimal $initAmount amount
313 static function processPriceSet($entityId, $lineItem, $contributionDetails = NULL, $entityTable = 'civicrm_contribution', $update = FALSE) {
314 if (!$entityId ||
!is_array($lineItem)
315 || CRM_Utils_system
::isNull($lineItem)
320 foreach ($lineItem as $priceSetId => $values) {
325 foreach ($values as $line) {
326 $line['entity_table'] = $entityTable;
327 $line['entity_id'] = $entityId;
328 // if financial type is not set and if price field value is NOT NULL
329 // get financial type id of price field value
330 if (!empty($line['price_field_value_id']) && empty($line['financial_type_id'])) {
331 $line['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $line['price_field_value_id'], 'financial_type_id');
333 $lineItems = CRM_Price_BAO_LineItem
::create($line);
334 if (!$update && $contributionDetails) {
335 CRM_Financial_BAO_FinancialItem
::add($lineItems, $contributionDetails);
336 if (isset($contributionDetails->tax_trxn_id
) && !empty($contributionDetails->tax_trxn_id
) && isset($line['tax_amount'])) {
337 CRM_Financial_BAO_FinancialItem
::add($lineItems, $contributionDetails, TRUE);
344 public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) {
345 if (!$entityId || CRM_Utils_System
::isNull($amount))
348 $from = " civicrm_line_item li
349 LEFT JOIN civicrm_price_field pf ON pf.id = li.price_field_id
350 LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id ";
352 $set = " li.unit_price = %3,
353 li.line_total = %3 ";
355 $where = " li.entity_id = %1 AND
356 li.entity_table = %2 ";
359 1 => array($entityId, 'Integer'),
360 2 => array($entityTable, 'String'),
361 3 => array($amount, 'Float'),
364 if ($entityTable == 'civicrm_contribution') {
365 $entityName = 'default_contribution_amount';
366 $where .= " AND ps.name = %4 ";
367 $params[4] = array($entityName, 'String');
369 elseif ($entityTable == 'civicrm_participant') {
371 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = ps.id
372 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = pf.id and cpfv.label = %4 ";
373 $set .= " ,li.label = %4,
374 li.price_field_value_id = cpfv.id ";
375 $where .= " AND cpse.entity_table = 'civicrm_event' AND cpse.entity_id = %5 ";
376 $amount = empty($amount) ?
0: $amount;
378 4 => array($otherParams['fee_label'], 'String'),
379 5 => array($otherParams['event_id'], 'String'),
389 CRM_Core_DAO
::executeQuery($query, $params);
393 * Function to build line items array.
394 * @param int $params form values
396 * @param string $entityId entity id
398 * @param string $entityTable entity Table
404 static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution') {
407 $priceSetDetails = CRM_Price_BAO_PriceSet
::getDefaultPriceSet();
408 foreach ($priceSetDetails as $values) {
409 $params['line_item'][$values['setID']][$values['priceFieldID']] = array(
410 'price_field_id' => $values['priceFieldID'],
411 'price_field_value_id' => $values['priceFieldValueID'],
412 'label' => $values['label'],
414 'unit_price' => $params['total_amount'],
415 'line_total' => $params['total_amount'],
416 'financial_type_id' => $params['financial_type_id']
422 $totalEntityId = count($entityId);
423 foreach ($entityId as $id) {
424 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($id, $entityTable);
425 foreach ($lineItems as $key => $values) {
426 if (!$setID && $values['price_field_id']) {
427 $setID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $values['price_field_id'], 'price_set_id');
428 $params['is_quick_config'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'is_quick_config');
430 if (!empty($params['is_quick_config']) && array_key_exists('total_amount', $params)
431 && $totalEntityId == 1) {
432 $values['line_total'] = $values['unit_price'] = $params['total_amount'];
434 $values['id'] = $key;
435 $params['line_item'][$setID][$key] = $values;
442 * Calculate tax rate in percentage
444 * @param $lineItemId an assoc array of lineItem
451 public static function calculateTaxRate($lineItemId) {
452 if ($lineItemId['html_type'] == 'Text') {
453 $tax = (($lineItemId['line_total'] - ($lineItemId['unit_price'] * $lineItemId['qty'])))/($lineItemId['unit_price'] * $lineItemId['qty'])*100;
456 $tax = (($lineItemId['line_total'] - $lineItemId['unit_price'])/$lineItemId['unit_price']) * 100;