Merge pull request #4771 from pratikshad/CRM-15409
[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 * @access public
55 * @static
56 */
57 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 (reference ) an assoc array of name/value pairs
79 * @param array $defaults (reference ) an assoc array to hold the flattened values
80 *
81 * @return CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem object
82 * @access public
83 * @static
84 */
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);
90 return $lineItem;
91 }
92 return NULL;
93 }
94
95 /**
96 * Given a participant id/contribution id,
97 * return contribution/fee line items
98 *
99 * @param $entityId int participant/contribution id
100 * @param $entity string participant/contribution.
101 *
102 * @param null $isQuick
103 *
104 * @return array of line items
105 */
106 static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL) {
107 $selectClause = $whereClause = $fromClause = NULL;
108
109 $selectClause = "
110 SELECT li.id,
111 li.label,
112 li.qty,
113 li.unit_price,
114 li.line_total,
115 pf.label as field_title,
116 pf.html_type,
117 pfv.membership_type_id,
118 li.price_field_id,
119 li.participant_count,
120 li.price_field_value_id,
121 pfv.description";
122
123 $fromClause = "
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 )";
128 $whereClause = "
129 WHERE %2.id = %1";
130
131 if ($isQuick) {
132 $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id ";
133 $whereClause .= " and cps.is_quick_config = 0";
134 }
135 $lineItems = array();
136
137 if (!$entityId || !$entity || !$fromClause) {
138 return $lineItems;
139 }
140
141 $params = array(
142 1 => array($entityId, 'Integer'),
143 2 => array($entity, 'Text'),
144 );
145
146 $dao = CRM_Core_DAO::executeQuery("$selectClause $fromClause $whereClause", $params);
147 while ($dao->fetch()) {
148 if (!$dao->id) {
149 continue;
150 }
151 $lineItems[$dao->id] = array(
152 'qty' => $dao->qty,
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,
164 );
165 }
166 return $lineItems;
167 }
168
169 /**
170 * This method will create the lineItem array required for
171 * processAmount method
172 *
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(
178 this is
179 * lineItem array)
180 *
181 * @return void
182 * @access static
183 */
184 static function format($fid, &$params, &$fields, &$values) {
185 if (empty($params["price_{$fid}"])) {
186 return;
187 }
188
189 $optionIDs = implode(',', array_keys($params["price_{$fid}"]));
190
191 //lets first check in fun parameter,
192 //since user might modified w/ hooks.
193 $options = array();
194 if (array_key_exists('options', $fields)) {
195 $options = $fields['options'];
196 }
197 else {
198 CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::getValues($fid, $options, 'weight', TRUE);
199 }
200 $fieldTitle = CRM_Utils_Array::value('label', $fields);
201 if (!$fieldTitle) {
202 $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $fid, 'label');
203 }
204
205 foreach ($params["price_{$fid}"] as $oid => $qty) {
206 $price = $options[$oid]['amount'];
207
208 // lets clean the price in case it is not yet cleaned
209 // CRM-10974
210 $price = CRM_Utils_Rule::cleanMoney($price);
211
212 $participantsPerField = CRM_Utils_Array::value('count', $options[$oid], 0);
213
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]),
220 'qty' => $qty,
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'],
228 );
229 }
230 }
231
232 /**
233 * Delete line items for given entity.
234 *
235 * @param int $entityId
236 * @param int $entityTable
237 *
238 * @return bool
239 * @access public
240 * @static
241 */
242 public static function deleteLineItems($entityId, $entityTable) {
243 $result = FALSE;
244 if (!$entityId || !$entityTable) {
245 return $result;
246 }
247
248 if ($entityId && !is_array($entityId)) {
249 $entityId = array($entityId);
250 }
251
252 $query = "DELETE FROM civicrm_line_item where entity_id IN ('" . implode("','", $entityId) . "') AND entity_table = '$entityTable'";
253 $dao = CRM_Core_DAO::executeQuery($query);
254 return $result;
255 }
256
257 /**
258 * @param int $entityId
259 * @param string $entityTable
260 * @param $amount
261 * @param array $otherParams
262 */
263 public static function syncLineItems($entityId, $entityTable = 'civicrm_contribution', $amount, $otherParams = NULL) {
264 if (!$entityId || CRM_Utils_System::isNull($amount))
265 return;
266
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 ";
270
271 $set = " li.unit_price = %3,
272 li.line_total = %3 ";
273
274 $where = " li.entity_id = %1 AND
275 li.entity_table = %2 ";
276
277 $params = array(
278 1 => array($entityId, 'Integer'),
279 2 => array($entityTable, 'String'),
280 3 => array($amount, 'Float'),
281 );
282
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') {
288 $from .= "
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;
295 $params += array(
296 4 => array($otherParams['fee_label'], 'String'),
297 5 => array($otherParams['event_id'], 'String'),
298 );
299 }
300
301 $query = "
302 UPDATE $from
303 SET $set
304 WHERE $where
305 ";
306
307 CRM_Core_DAO::executeQuery($query, $params);
308 }
309 }