Merge pull request #4875 from civicrm/minor-fix
[civicrm-core.git] / CRM / Upgrade / Snapshot / V4p2 / Price / BAO / LineItem.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
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_Upgrade_Snapshot_V4p2_Price_BAO_LineItem extends CRM_Upgrade_Snapshot_V4p2_Price_DAO_LineItem {
47
48 /**
49 * Creates a new entry in the database.
50 *
51 * @param array $params
52 * (reference) an assoc array of name/value pairs.
53 *
54 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_LineItem object
55 * @static
56 */
57 public 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);
61
62 $lineItemBAO = new CRM_Upgrade_Snapshot_V4p2_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
79 * (reference ) an assoc array of name/value pairs.
80 * @param array $defaults
81 * (reference ) an assoc array to hold the flattened values.
82 *
83 * @return CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem object
84 * @static
85 */
86 public static function retrieve(&$params, &$defaults) {
87 $lineItem = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem();
88 $lineItem->copyValues($params);
89 if ($lineItem->find(TRUE)) {
90 CRM_Core_DAO::storeValues($lineItem, $defaults);
91 return $lineItem;
92 }
93 return NULL;
94 }
95
96 /**
97 * Given a participant id/contribution id,
98 * return contribution/fee line items
99 *
100 * @param int $entityId
101 * participant/contribution id.
102 * @param string $entity
103 * participant/contribution.
104 *
105 * @param null $isQuick
106 *
107 * @return array
108 * of line items
109 */
110 public static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL) {
111 $selectClause = $whereClause = $fromClause = NULL;
112
113 $selectClause = "
114 SELECT li.id,
115 li.label,
116 li.qty,
117 li.unit_price,
118 li.line_total,
119 pf.label as field_title,
120 pf.html_type,
121 pfv.membership_type_id,
122 li.price_field_id,
123 li.participant_count,
124 li.price_field_value_id,
125 pfv.description";
126
127 $fromClause = "
128 FROM civicrm_%2 as %2
129 LEFT JOIN civicrm_line_item li ON ( li.entity_id = %2.id AND li.entity_table = 'civicrm_%2')
130 LEFT JOIN civicrm_price_field_value pfv ON ( pfv.id = li.price_field_value_id )
131 LEFT JOIN civicrm_price_field pf ON (pf.id = li.price_field_id )";
132 $whereClause = "
133 WHERE %2.id = %1";
134
135 if ($isQuick) {
136 $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id ";
137 $whereClause .= " and cps.is_quick_config = 0";
138 }
139 $lineItems = array();
140
141 if (!$entityId || !$entity || !$fromClause) {
142 return $lineItems;
143 }
144
145 $params = array(
146 1 => array($entityId, 'Integer'),
147 2 => array($entity, 'Text'),
148 );
149
150 $dao = CRM_Core_DAO::executeQuery("$selectClause $fromClause $whereClause", $params);
151 while ($dao->fetch()) {
152 if (!$dao->id) {
153 continue;
154 }
155 $lineItems[$dao->id] = array(
156 'qty' => $dao->qty,
157 'label' => $dao->label,
158 'unit_price' => $dao->unit_price,
159 'line_total' => $dao->line_total,
160 'price_field_id' => $dao->price_field_id,
161 'participant_count' => $dao->participant_count,
162 'price_field_value_id' => $dao->price_field_value_id,
163 'field_title' => $dao->field_title,
164 'html_type' => $dao->html_type,
165 'description' => $dao->description,
166 'entity_id' => $entityId,
167 'membership_type_id' => $dao->membership_type_id,
168 );
169 }
170 return $lineItems;
171 }
172
173 /**
174 * This method will create the lineItem array required for
175 * processAmount method
176 *
177 * @param int $fid
178 * Price set field id.
179 * @param array $params
180 * Reference to form values.
181 * @param array $fields
182 * Reference to array of fields belonging.
183 * to the price set used for particular event
184 * @param array $values
185 * Reference to the values array(.
186 * this is
187 * lineItem array)
188 *
189 * @return void
190 */
191 public static function format($fid, &$params, &$fields, &$values) {
192 if (empty($params["price_{$fid}"])) {
193 return;
194 }
195
196 $optionIDs = implode(',', array_keys($params["price_{$fid}"]));
197
198 //lets first check in fun parameter,
199 //since user might modified w/ hooks.
200 $options = array();
201 if (array_key_exists('options', $fields)) {
202 $options = $fields['options'];
203 }
204 else {
205 CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::getValues($fid, $options, 'weight', TRUE);
206 }
207 $fieldTitle = CRM_Utils_Array::value('label', $fields);
208 if (!$fieldTitle) {
209 $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $fid, 'label');
210 }
211
212 foreach ($params["price_{$fid}"] as $oid => $qty) {
213 $price = $options[$oid]['amount'];
214
215 // lets clean the price in case it is not yet cleaned
216 // CRM-10974
217 $price = CRM_Utils_Rule::cleanMoney($price);
218
219 $participantsPerField = CRM_Utils_Array::value('count', $options[$oid], 0);
220
221 $values[$oid] = array(
222 'price_field_id' => $fid,
223 'price_field_value_id' => $oid,
224 'label' => CRM_Utils_Array::value('label', $options[$oid]),
225 'field_title' => $fieldTitle,
226 'description' => CRM_Utils_Array::value('description', $options[$oid]),
227 'qty' => $qty,
228 'unit_price' => $price,
229 'line_total' => $qty * $price,
230 'participant_count' => $qty * $participantsPerField,
231 'max_value' => CRM_Utils_Array::value('max_value', $options[$oid]),
232 'membership_type_id' => CRM_Utils_Array::value('membership_type_id', $options[$oid]),
233 'auto_renew' => CRM_Utils_Array::value('auto_renew', $options[$oid]),
234 'html_type' => $fields['html_type'],
235 );
236 }
237 }
238
239 /**
240 * Delete line items for given entity.
241 *
242 * @param int $entityId
243 * @param int $entityTable
244 *
245 * @return bool
246 * @static
247 */
248 public static function deleteLineItems($entityId, $entityTable) {
249 $result = FALSE;
250 if (!$entityId || !$entityTable) {
251 return $result;
252 }
253
254 if ($entityId && !is_array($entityId)) {
255 $entityId = array($entityId);
256 }
257
258 $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'";
259 $dao = CRM_Core_DAO::executeQuery($query);
260 return $result;
261 }
262
263 /**
264 * @param int $entityId
265 * @param string $entityTable
266 * @param $amount
267 * @param array $otherParams
268 */
269 public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) {
270 if (!$entityId || CRM_Utils_System::isNull($amount)) {
271 return;
272 }
273
274 $from = " civicrm_line_item li
275 LEFT JOIN civicrm_price_field pf ON pf.id = li.price_field_id
276 LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id ";
277
278 $set = " li.unit_price = %3,
279 li.line_total = %3 ";
280
281 $where = " li.entity_id = %1 AND
282 li.entity_table = %2 ";
283
284 $params = array(
285 1 => array($entityId, 'Integer'),
286 2 => array($entityTable, 'String'),
287 3 => array($amount, 'Float'),
288 );
289
290 if ($entityTable == 'civicrm_contribution') {
291 $entityName = 'default_contribution_amount';
292 $where .= " AND ps.name = %4 ";
293 $params[4] = array($entityName, 'String');
294 }
295 elseif ($entityTable == 'civicrm_participant') {
296 $from .= "
297 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = ps.id
298 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = pf.id and cpfv.label = %4 ";
299 $set .= " ,li.label = %4,
300 li.price_field_value_id = cpfv.id ";
301 $where .= " AND cpse.entity_table = 'civicrm_event' AND cpse.entity_id = %5 ";
302 $amount = empty($amount) ? 0 : $amount;
303 $params += array(
304 4 => array($otherParams['fee_label'], 'String'),
305 5 => array($otherParams['event_id'], 'String'),
306 );
307 }
308
309 $query = "
310 UPDATE $from
311 SET $set
312 WHERE $where
313 ";
314
315 CRM_Core_DAO::executeQuery($query, $params);
316 }
317 }