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