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