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_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
extends CRM_Upgrade_Snapshot_V4p2_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_Upgrade_Snapshot_V4p2_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_Upgrade_Snapshot_V4p2_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_Upgrade_Snapshot_V4p2_Price_BAO_LineItem object
85 static function retrieve(&$params, &$defaults) {
86 $lineItem = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem();
87 $lineItem->copyValues($params);
88 if ($lineItem->find(TRUE)) {
89 CRM_Core_DAO
::storeValues($lineItem, $defaults);
96 * Given a participant id/contribution id,
97 * return contribution/fee line items
99 * @param $entityId int participant/contribution id
100 * @param $entity string participant/contribution.
102 * @param null $isQuick
104 * @return array of line items
106 static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL) {
107 $selectClause = $whereClause = $fromClause = NULL;
115 pf.label as field_title,
117 pfv.membership_type_id,
119 li.participant_count,
120 li.price_field_value_id,
124 FROM civicrm_%2 as %2
125 LEFT JOIN civicrm_line_item li ON ( li.entity_id = %2.id AND li.entity_table = 'civicrm_%2')
126 LEFT JOIN civicrm_price_field_value pfv ON ( pfv.id = li.price_field_value_id )
127 LEFT JOIN civicrm_price_field pf ON (pf.id = li.price_field_id )";
132 $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id ";
133 $whereClause .= " and cps.is_quick_config = 0";
135 $lineItems = array();
137 if (!$entityId ||
!$entity ||
!$fromClause) {
142 1 => array($entityId, 'Integer'),
143 2 => array($entity, 'Text'),
146 $dao = CRM_Core_DAO
::executeQuery("$selectClause $fromClause $whereClause", $params);
147 while ($dao->fetch()) {
151 $lineItems[$dao->id
] = array(
153 'label' => $dao->label
,
154 'unit_price' => $dao->unit_price
,
155 'line_total' => $dao->line_total
,
156 'price_field_id' => $dao->price_field_id
,
157 'participant_count' => $dao->participant_count
,
158 'price_field_value_id' => $dao->price_field_value_id
,
159 'field_title' => $dao->field_title
,
160 'html_type' => $dao->html_type
,
161 'description' => $dao->description
,
162 'entity_id' => $entityId,
163 'membership_type_id' => $dao->membership_type_id
,
170 * This method will create the lineItem array required for
171 * processAmount method
173 * @param int $fid price set field id
174 * @param array $params reference to form values
175 * @param array $fields reference to array of fields belonging
176 * to the price set used for particular event
177 * @param array $values reference to the values array(
184 static function format($fid, &$params, &$fields, &$values) {
185 if (empty($params["price_{$fid}"])) {
189 $optionIDs = implode(',', array_keys($params["price_{$fid}"]));
191 //lets first check in fun parameter,
192 //since user might modified w/ hooks.
194 if (array_key_exists('options', $fields)) {
195 $options = $fields['options'];
198 CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue
::getValues($fid, $options, 'weight', TRUE);
200 $fieldTitle = CRM_Utils_Array
::value('label', $fields);
202 $fieldTitle = CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $fid, 'label');
205 foreach ($params["price_{$fid}"] as $oid => $qty) {
206 $price = $options[$oid]['amount'];
208 // lets clean the price in case it is not yet cleaned
210 $price = CRM_Utils_Rule
::cleanMoney($price);
212 $participantsPerField = CRM_Utils_Array
::value('count', $options[$oid], 0);
214 $values[$oid] = array(
215 'price_field_id' => $fid,
216 'price_field_value_id' => $oid,
217 'label' => CRM_Utils_Array
::value('label', $options[$oid]),
218 'field_title' => $fieldTitle,
219 'description' => CRM_Utils_Array
::value('description', $options[$oid]),
221 'unit_price' => $price,
222 'line_total' => $qty * $price,
223 'participant_count' => $qty * $participantsPerField,
224 'max_value' => CRM_Utils_Array
::value('max_value', $options[$oid]),
225 'membership_type_id' => CRM_Utils_Array
::value('membership_type_id', $options[$oid]),
226 'auto_renew' => CRM_Utils_Array
::value('auto_renew', $options[$oid]),
227 'html_type' => $fields['html_type'],
233 * Delete line items for given entity.
235 * @param int $entityId
236 * @param int $entityTable
242 public static function deleteLineItems($entityId, $entityTable) {
244 if (!$entityId ||
!$entityTable) {
248 if ($entityId && !is_array($entityId)) {
249 $entityId = array($entityId);
252 $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'";
253 $dao = CRM_Core_DAO
::executeQuery($query);
259 * @param string $entityTable
261 * @param null $otherParams
263 public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) {
264 if (!$entityId || CRM_Utils_System
::isNull($amount))
267 $from = " civicrm_line_item li
268 LEFT JOIN civicrm_price_field pf ON pf.id = li.price_field_id
269 LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id ";
271 $set = " li.unit_price = %3,
272 li.line_total = %3 ";
274 $where = " li.entity_id = %1 AND
275 li.entity_table = %2 ";
278 1 => array($entityId, 'Integer'),
279 2 => array($entityTable, 'String'),
280 3 => array($amount, 'Float'),
283 if ($entityTable == 'civicrm_contribution') {
284 $entityName = 'default_contribution_amount';
285 $where .= " AND ps.name = %4 ";
286 $params[4] = array($entityName, 'String');
287 } elseif ($entityTable == 'civicrm_participant') {
289 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = ps.id
290 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = pf.id and cpfv.label = %4 ";
291 $set .= " ,li.label = %4,
292 li.price_field_value_id = cpfv.id ";
293 $where .= " AND cpse.entity_table = 'civicrm_event' AND cpse.entity_id = %5 ";
294 $amount = empty($amount) ?
0: $amount;
296 4 => array($otherParams['fee_label'], 'String'),
297 5 => array($otherParams['event_id'], 'String'),
307 CRM_Core_DAO
::executeQuery($query, $params);