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