CRM-19417 - distmaker - Rename report file. Tweak edge-case for missing repos.
[civicrm-core.git] / CRM / Price / BAO / PriceSet.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Business object for managing price sets
38 *
39 */
9da8dc8c 40class CRM_Price_BAO_PriceSet extends CRM_Price_DAO_PriceSet {
6a488035 41
71ba92c4 42 /**
fe482240 43 * Static field for default price set details.
71ba92c4
PN
44 *
45 * @var array
71ba92c4
PN
46 */
47 static $_defaultPriceSet = NULL;
48
6a488035 49 /**
fe482240 50 * Class constructor.
6a488035 51 */
00be9182 52 public function __construct() {
6a488035
TO
53 parent::__construct();
54 }
55
56 /**
fe482240 57 * Takes an associative array and creates a price set object.
6a488035 58 *
414c1420
TO
59 * @param array $params
60 * (reference) an assoc array of name/value pairs.
6a488035 61 *
16b10e64 62 * @return CRM_Price_DAO_PriceSet
6a488035 63 */
00be9182 64 public static function create(&$params) {
22e263ad 65 if (empty($params['id']) && empty($params['name'])) {
e0c1764e
EM
66 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 242);
67 }
e68e1c9a
PN
68 $priceSetID = NULL;
69 $validatePriceSet = TRUE;
e10ac060 70 if (!empty($params['extends']) && is_array($params['extends'])) {
e68e1c9a
PN
71 if (!array_key_exists(CRM_Core_Component::getComponentID('CiviEvent'), $params['extends'])
72 || !array_key_exists(CRM_Core_Component::getComponentID('CiviMember'), $params['extends'])
73 ) {
74 $validatePriceSet = FALSE;
75 }
076809e3
JV
76 $params['extends'] = CRM_Utils_Array::implodePadded($params['extends']);
77 }
e68e1c9a
PN
78 else {
79 $priceSetID = CRM_Utils_Array::value('id', $params);
80 }
81 // CRM-16189
f7e2bf47
PN
82 if ($validatePriceSet && !empty($params['financial_type_id'])) {
83 CRM_Financial_BAO_FinancialAccount::validateFinancialType(
84 $params['financial_type_id'],
e68e1c9a
PN
85 $priceSetID
86 );
e68e1c9a 87 }
9da8dc8c 88 $priceSetBAO = new CRM_Price_BAO_PriceSet();
6a488035
TO
89 $priceSetBAO->copyValues($params);
90 if (self::eventPriceSetDomainID()) {
91 $priceSetBAO->domain_id = CRM_Core_Config::domainID();
92 }
93 return $priceSetBAO->save();
94 }
95
96 /**
fe482240 97 * Fetch object based on array of properties.
6a488035 98 *
414c1420
TO
99 * @param array $params
100 * (reference ) an assoc array of name/value pairs.
101 * @param array $defaults
102 * (reference ) an assoc array to hold the flattened values.
6a488035 103 *
16b10e64 104 * @return CRM_Price_DAO_PriceSet
6a488035 105 */
00be9182 106 public static function retrieve(&$params, &$defaults) {
9da8dc8c 107 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceSet', $params, $defaults);
6a488035
TO
108 }
109
110 /**
fe482240 111 * Update the is_active flag in the db.
6a488035 112 *
414c1420
TO
113 * @param int $id
114 * Id of the database record.
6c8f6e67
EM
115 * @param $isActive
116 *
117 * @internal param bool $is_active value we want to set the is_active field
6a488035 118 *
a6c01b45 119 * @return Object
b44e3f84 120 * DAO object on success, null otherwise
6a488035 121 */
00be9182 122 public static function setIsActive($id, $isActive) {
9da8dc8c 123 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceSet', $id, 'is_active', $isActive);
6a488035
TO
124 }
125
126 /**
127 * Calculate the default price set id
128 * assigned to the contribution/membership etc
129 *
130 * @param string $entity
131 *
a6c01b45
CW
132 * @return array
133 * default price set
6a488035 134 *
6a488035
TO
135 */
136 public static function getDefaultPriceSet($entity = 'contribution') {
71ba92c4
PN
137 if (!empty(self::$_defaultPriceSet[$entity])) {
138 return self::$_defaultPriceSet[$entity];
139 }
e0c1764e
EM
140 $entityName = 'default_contribution_amount';
141 if ($entity == 'membership') {
6a488035
TO
142 $entityName = 'default_membership_type_amount';
143 }
144
145 $sql = "
82cc6775 146SELECT ps.id AS setID, pfv.price_field_id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.name, pfv.label, pfv.membership_type_id, pfv.amount, pfv.financial_type_id
6a488035
TO
147FROM civicrm_price_set ps
148LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
149LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
150WHERE ps.name = '{$entityName}'
151";
152
153 $dao = CRM_Core_DAO::executeQuery($sql);
71ba92c4 154 self::$_defaultPriceSet[$entity] = array();
6a488035 155 while ($dao->fetch()) {
71ba92c4 156 self::$_defaultPriceSet[$entity][$dao->priceFieldValueID] = array(
353ffa53
TO
157 'setID' => $dao->setID,
158 'priceFieldID' => $dao->priceFieldID,
159 'name' => $dao->name,
160 'label' => $dao->label,
161 'priceFieldValueID' => $dao->priceFieldValueID,
162 'membership_type_id' => $dao->membership_type_id,
163 'amount' => $dao->amount,
164 'financial_type_id' => $dao->financial_type_id,
71ba92c4 165 );
6a488035
TO
166 }
167
71ba92c4 168 return self::$_defaultPriceSet[$entity];
6a488035
TO
169 }
170
171 /**
172 * Get the price set title.
173 *
414c1420
TO
174 * @param int $id
175 * Id of price set.
6a488035 176 *
a6c01b45
CW
177 * @return string
178 * title
6a488035 179 *
6a488035
TO
180 */
181 public static function getTitle($id) {
9da8dc8c 182 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $id, 'title');
6a488035
TO
183 }
184
185 /**
186 * Return a list of all forms which use this price set.
187 *
414c1420
TO
188 * @param int $id
189 * Id of price set.
e0c1764e 190 * @param bool|string $simpleReturn - get raw data. Possible values: 'entity', 'table'
6a488035
TO
191 *
192 * @return array
193 */
194 public static function &getUsedBy($id, $simpleReturn = FALSE) {
195 $usedBy = $forms = $tables = array();
196 $queryString = "
197SELECT entity_table, entity_id
198FROM civicrm_price_set_entity
199WHERE price_set_id = %1";
200 $params = array(1 => array($id, 'Integer'));
201 $crmFormDAO = CRM_Core_DAO::executeQuery($queryString, $params);
202
203 while ($crmFormDAO->fetch()) {
204 $forms[$crmFormDAO->entity_table][] = $crmFormDAO->entity_id;
205 $tables[] = $crmFormDAO->entity_table;
206 }
207 // Return only tables
208 if ($simpleReturn == 'table') {
209 return $tables;
210 }
211 if (empty($forms)) {
212 $queryString = "
213SELECT cli.entity_table, cli.entity_id
214FROM civicrm_line_item cli
215LEFT JOIN civicrm_price_field cpf ON cli.price_field_id = cpf.id
216WHERE cpf.price_set_id = %1";
217 $params = array(1 => array($id, 'Integer'));
218 $crmFormDAO = CRM_Core_DAO::executeQuery($queryString, $params);
219 while ($crmFormDAO->fetch()) {
220 $forms[$crmFormDAO->entity_table][] = $crmFormDAO->entity_id;
221 $tables[] = $crmFormDAO->entity_table;
222 }
223 if (empty($forms)) {
224 return $usedBy;
225 }
226 }
227 // Return only entity data
228 if ($simpleReturn == 'entity') {
229 return $forms;
230 }
231 foreach ($forms as $table => $entities) {
232 switch ($table) {
233 case 'civicrm_event':
234 $ids = implode(',', $entities);
c34e4bb4 235 $queryString = "SELECT ce.id as id, ce.title as title, ce.is_public as isPublic, ce.start_date as startDate, ce.end_date as endDate, civicrm_option_value.label as eventType, ce.is_template as isTemplate, ce.template_title as templateTitle
6a488035
TO
236FROM civicrm_event ce
237LEFT JOIN civicrm_option_value ON
238 ( ce.event_type_id = civicrm_option_value.value )
239LEFT JOIN civicrm_option_group ON
240 ( civicrm_option_group.id = civicrm_option_value.option_group_id )
241WHERE
242 civicrm_option_group.name = 'event_type' AND
6a488035
TO
243 ce.id IN ($ids) AND
244 ce.is_active = 1;";
245 $crmDAO = CRM_Core_DAO::executeQuery($queryString);
246 while ($crmDAO->fetch()) {
c34e4bb4
PJ
247 if ($crmDAO->isTemplate) {
248 $usedBy['civicrm_event_template'][$crmDAO->id]['title'] = $crmDAO->templateTitle;
249 $usedBy['civicrm_event_template'][$crmDAO->id]['eventType'] = $crmDAO->eventType;
250 $usedBy['civicrm_event_template'][$crmDAO->id]['isPublic'] = $crmDAO->isPublic;
251 }
252 else {
253 $usedBy[$table][$crmDAO->id]['title'] = $crmDAO->title;
254 $usedBy[$table][$crmDAO->id]['eventType'] = $crmDAO->eventType;
255 $usedBy[$table][$crmDAO->id]['startDate'] = $crmDAO->startDate;
256 $usedBy[$table][$crmDAO->id]['endDate'] = $crmDAO->endDate;
257 $usedBy[$table][$crmDAO->id]['isPublic'] = $crmDAO->isPublic;
258 }
6a488035
TO
259 }
260 break;
261
262 case 'civicrm_contribution_page':
263 $ids = implode(',', $entities);
264 $queryString = "SELECT cp.id as id, cp.title as title, cp.start_date as startDate, cp.end_date as endDate,ct.name as type
265FROM civicrm_contribution_page cp, civicrm_financial_type ct
cde484fd 266WHERE ct.id = cp.financial_type_id AND
6a488035
TO
267 cp.id IN ($ids) AND
268 cp.is_active = 1;";
269 $crmDAO = CRM_Core_DAO::executeQuery($queryString);
270 while ($crmDAO->fetch()) {
271 $usedBy[$table][$crmDAO->id]['title'] = $crmDAO->title;
272 $usedBy[$table][$crmDAO->id]['type'] = $crmDAO->type;
273 $usedBy[$table][$crmDAO->id]['startDate'] = $crmDAO->startDate;
274 $usedBy[$table][$crmDAO->id]['endDate'] = $crmDAO->endDate;
275 }
276 break;
277
278 case 'civicrm_contribution':
279 case 'civicrm_membership':
280 case 'civicrm_participant':
281 $usedBy[$table] = 1;
282 break;
283
284 default:
285 CRM_Core_Error::fatal("$table is not supported in PriceSet::usedBy()");
286 break;
287 }
288 }
289
290 return $usedBy;
291 }
292
293 /**
fe482240 294 * Delete the price set.
6a488035 295 *
414c1420
TO
296 * @param int $id
297 * Price Set id.
6a488035 298 *
acb1052e 299 * @return bool
a6c01b45 300 * false if fields exist for this set, true if the
16b10e64 301 * set could be deleted
6a488035 302 *
6a488035
TO
303 */
304 public static function deleteSet($id) {
305 // remove from all inactive forms
306 $usedBy = self::getUsedBy($id);
307 if (isset($usedBy['civicrm_event'])) {
308 foreach ($usedBy['civicrm_event'] as $eventId => $unused) {
309 $eventDAO = new CRM_Event_DAO_Event();
310 $eventDAO->id = $eventId;
311 $eventDAO->find();
312 while ($eventDAO->fetch()) {
313 self::removeFrom('civicrm_event', $eventDAO->id);
314 }
315 }
316 }
317
318 // delete price fields
9da8dc8c 319 $priceField = new CRM_Price_DAO_PriceField();
6a488035
TO
320 $priceField->price_set_id = $id;
321 $priceField->find();
322 while ($priceField->fetch()) {
323 // delete options first
9da8dc8c 324 CRM_Price_BAO_PriceField::deleteField($priceField->id);
6a488035
TO
325 }
326
9da8dc8c 327 $set = new CRM_Price_DAO_PriceSet();
6a488035
TO
328 $set->id = $id;
329 return $set->delete();
330 }
331
332 /**
fe482240 333 * Link the price set with the specified table and id.
6a488035
TO
334 *
335 * @param string $entityTable
414c1420
TO
336 * @param int $entityId
337 * @param int $priceSetId
6a488035
TO
338 *
339 * @return bool
340 */
341 public static function addTo($entityTable, $entityId, $priceSetId) {
342 // verify that the price set exists
9da8dc8c 343 $dao = new CRM_Price_DAO_PriceSet();
6a488035
TO
344 $dao->id = $priceSetId;
345 if (!$dao->find()) {
346 return FALSE;
347 }
348 unset($dao);
349
9da8dc8c 350 $dao = new CRM_Price_DAO_PriceSetEntity();
6a488035
TO
351 // find if this already exists
352 $dao->entity_id = $entityId;
353 $dao->entity_table = $entityTable;
354 $dao->find(TRUE);
355
356 // add or update price_set_id
357 $dao->price_set_id = $priceSetId;
358 return $dao->save();
359 }
360
361 /**
fe482240 362 * Delete price set for the given entity and id.
6a488035
TO
363 *
364 * @param string $entityTable
414c1420 365 * @param int $entityId
77b97be7
EM
366 *
367 * @return mixed
6a488035
TO
368 */
369 public static function removeFrom($entityTable, $entityId) {
353ffa53 370 $dao = new CRM_Price_DAO_PriceSetEntity();
6a488035 371 $dao->entity_table = $entityTable;
353ffa53 372 $dao->entity_id = $entityId;
6a488035
TO
373 return $dao->delete();
374 }
375
376 /**
377 * Find a price_set_id associatied with the given table, id and usedFor
378 * Used For value for events:1, contribution:2, membership:3
379 *
380 * @param string $entityTable
77b97be7 381 * @param int $entityId
414c1420
TO
382 * @param int $usedFor
383 * ( price set that extends/used for particular component ).
77b97be7
EM
384 *
385 * @param null $isQuickConfig
386 * @param null $setName
6a488035 387 *
df8d3074 388 * @return int|false
72b3a70c 389 * price_set_id, or false if none found
6a488035
TO
390 */
391 public static function getFor($entityTable, $entityId, $usedFor = NULL, $isQuickConfig = NULL, &$setName = NULL) {
392 if (!$entityTable || !$entityId) {
393 return FALSE;
394 }
395
396 $sql = 'SELECT ps.id as price_set_id, ps.name as price_set_name
397 FROM civicrm_price_set ps
398 INNER JOIN civicrm_price_set_entity pse ON ps.id = pse.price_set_id
399 WHERE pse.entity_table = %1 AND pse.entity_id = %2 ';
400 if ($isQuickConfig) {
401 $sql .= ' AND ps.is_quick_config = 0 ';
402 }
ba1dcfda 403 $params = array(
353ffa53
TO
404 1 => array($entityTable, 'String'),
405 2 => array($entityId, 'Integer'),
6a488035
TO
406 );
407 if ($usedFor) {
408 $sql .= " AND ps.extends LIKE '%%3%' ";
409 $params[3] = array($usedFor, 'Integer');
410 }
411
412 $dao = CRM_Core_DAO::executeQuery($sql, $params);
413 $dao->fetch();
414 $setName = (isset($dao->price_set_name)) ? $dao->price_set_name : FALSE;
415 return (isset($dao->price_set_id)) ? $dao->price_set_id : FALSE;
416 }
417
418 /**
fe482240 419 * Find a price_set_id associated with the given option value or field ID.
6a488035 420 *
414c1420
TO
421 * @param array $params
422 * (reference) an assoc array of name/value pairs.
6a488035
TO
423 * array may contain either option id or
424 * price field id
425 *
df8d3074 426 * @return int|NULL
72b3a70c 427 * price set id on success, null otherwise
6a488035
TO
428 */
429 public static function getSetId(&$params) {
430 $fid = NULL;
431
432 if ($oid = CRM_Utils_Array::value('oid', $params)) {
9da8dc8c 433 $fieldValue = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
434 $fieldValue->id = $oid;
435 if ($fieldValue->find(TRUE)) {
436 $fid = $fieldValue->price_field_id;
437 }
438 }
439 else {
440 $fid = CRM_Utils_Array::value('fid', $params);
441 }
442
443 if (isset($fid)) {
9da8dc8c 444 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'price_set_id');
6a488035
TO
445 }
446
447 return NULL;
448 }
449
450 /**
fe482240 451 * Return an associative array of all price sets.
6a488035 452 *
414c1420
TO
453 * @param bool $withInactive
454 * Whether or not to include inactive entries.
77b97be7 455 * @param bool|string $extendComponentName name of the component like 'CiviEvent','CiviContribute'
6a3d8d2d 456 * @param string $column name of the column.
6a488035 457 *
a6c01b45
CW
458 * @return array
459 * associative array of id => name
6a488035 460 */
6a3d8d2d
WA
461 public static function getAssoc($withInactive = FALSE, $extendComponentName = FALSE, $column = 'title') {
462 $query = "
6a488035 463 SELECT
75d6d479 464 DISTINCT ( price_set_id ) as id, s.{$column}
6a488035 465 FROM
53df1142
E
466 civicrm_price_set s
467 INNER JOIN civicrm_price_field f ON f.price_set_id = s.id
468 INNER JOIN civicrm_price_field_value v ON v.price_field_id = f.id
6a488035 469 WHERE
75d6d479 470 is_quick_config = 0 ";
6a488035
TO
471
472 if (!$withInactive) {
53df1142 473 $query .= ' AND s.is_active = 1 ';
6a488035
TO
474 }
475
476 if (self::eventPriceSetDomainID()) {
53df1142 477 $query .= ' AND s.domain_id = ' . CRM_Core_Config::domainID();
6a488035
TO
478 }
479
480 $priceSets = array();
481
482 if ($extendComponentName) {
483 $componentId = CRM_Core_Component::getComponentID($extendComponentName);
484 if (!$componentId) {
485 return $priceSets;
486 }
53df1142 487 $query .= " AND s.extends LIKE '%$componentId%' ";
6a488035 488 }
53df1142 489 // Check permissioned financial types
573fd305 490 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialType, CRM_Core_Action::ADD);
53df1142 491 if ($financialType) {
40c655aa 492 $types = implode(',', array_keys($financialType));
53df1142
E
493 $query .= ' AND s.financial_type_id IN (' . $types . ') AND v.financial_type_id IN (' . $types . ') ';
494 }
495 else {
c5c263ca
AH
496 // Do not display any price sets
497 $query .= " AND 0 ";
53df1142
E
498 }
499 $query .= " GROUP BY s.id";
6a488035
TO
500 $dao = CRM_Core_DAO::executeQuery($query);
501 while ($dao->fetch()) {
6a3d8d2d 502 $priceSets[$dao->id] = $dao->$column;
6a488035
TO
503 }
504 return $priceSets;
505 }
506
507 /**
fe482240 508 * Get price set details.
6a488035
TO
509 *
510 * An array containing price set details (including price fields) is returned
511 *
100fef9d 512 * @param int $setID
003ca729 513 * Price Set ID.
2a6da8d7 514 * @param bool $required
003ca729 515 * Appears to have no effect based on reading the code.
2a6da8d7 516 * @param bool $validOnly
003ca729 517 * Should only fields where today's date falls within the valid range be returned?
2a6da8d7 518 *
a6c01b45 519 * @return array
003ca729 520 * Array consisting of field details
6a488035
TO
521 */
522 public static function getSetDetail($setID, $required = TRUE, $validOnly = FALSE) {
523 // create a new tree
524 $setTree = array();
6a488035
TO
525
526 $priceFields = array(
527 'id',
528 'name',
529 'label',
530 'html_type',
531 'is_enter_qty',
532 'help_pre',
533 'help_post',
534 'weight',
535 'is_display_amounts',
536 'options_per_line',
537 'is_active',
538 'active_on',
539 'expire_on',
540 'javascript',
541 'visibility_id',
542 'is_required',
543 );
544 if ($required == TRUE) {
545 $priceFields[] = 'is_required';
546 }
547
548 // create select
549 $select = 'SELECT ' . implode(',', $priceFields);
550 $from = ' FROM civicrm_price_field';
551
353ffa53 552 $params = array();
6a488035 553 $params[1] = array($setID, 'Integer');
353ffa53 554 $where = '
6a488035
TO
555WHERE price_set_id = %1
556AND is_active = 1
557';
558 $dateSelect = '';
559 if ($validOnly) {
560 $currentTime = date('YmdHis');
561 $dateSelect = "
562AND ( active_on IS NULL OR active_on <= {$currentTime} )
563AND ( expire_on IS NULL OR expire_on >= {$currentTime} )
564";
565 }
566
567 $orderBy = ' ORDER BY weight';
568
569 $sql = $select . $from . $where . $dateSelect . $orderBy;
570
571 $dao = CRM_Core_DAO::executeQuery($sql, $params);
572
573 $visibility = CRM_Core_PseudoConstant::visibility('name');
574 while ($dao->fetch()) {
575 $fieldID = $dao->id;
576
577 $setTree[$setID]['fields'][$fieldID] = array();
578 $setTree[$setID]['fields'][$fieldID]['id'] = $fieldID;
579
580 foreach ($priceFields as $field) {
581 if ($field == 'id' || is_null($dao->$field)) {
582 continue;
583 }
584
585 if ($field == 'visibility_id') {
586 $setTree[$setID]['fields'][$fieldID]['visibility'] = $visibility[$dao->$field];
587 }
588 $setTree[$setID]['fields'][$fieldID][$field] = $dao->$field;
589 }
9da8dc8c 590 $setTree[$setID]['fields'][$fieldID]['options'] = CRM_Price_BAO_PriceField::getOptions($fieldID, FALSE);
6a488035
TO
591 }
592
593 // also get the pre and post help from this price set
594 $sql = "
595SELECT extends, financial_type_id, help_pre, help_post, is_quick_config
596FROM civicrm_price_set
597WHERE id = %1";
598 $dao = CRM_Core_DAO::executeQuery($sql, $params);
599 if ($dao->fetch()) {
600 $setTree[$setID]['extends'] = $dao->extends;
157b21d8 601 $setTree[$setID]['financial_type_id'] = $dao->financial_type_id;
6a488035
TO
602 $setTree[$setID]['help_pre'] = $dao->help_pre;
603 $setTree[$setID]['help_post'] = $dao->help_post;
604 $setTree[$setID]['is_quick_config'] = $dao->is_quick_config;
605 }
606 return $setTree;
607 }
608
85020e43 609 /**
003ca729
EM
610 * Get the Price Field ID.
611 *
612 * We call this function when more than one being present would represent an error
85020e43
EM
613 * starting format derived from current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId))
614 * @param array $priceSet
615 *
616 * @throws CRM_Core_Exception
617 * @return int
618 */
00be9182 619 public static function getOnlyPriceFieldID(array $priceSet) {
22e263ad 620 if (count($priceSet['fields']) > 1) {
85020e43
EM
621 throw new CRM_Core_Exception(ts('expected only one price field to be in priceset but multiple are present'));
622 }
623 return (int) implode('_', array_keys($priceSet['fields']));
624 }
625
626 /**
627 * Get the Price Field Value ID. We call this function when more than one being present would represent an error
628 * current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId))
629 * @param array $priceSet
630 *
631 * @throws CRM_Core_Exception
632 * @return int
633 */
00be9182 634 public static function getOnlyPriceFieldValueID(array $priceSet) {
85020e43 635 $priceFieldID = self::getOnlyPriceFieldID($priceSet);
22e263ad 636 if (count($priceSet['fields'][$priceFieldID]['options']) > 1) {
85020e43
EM
637 throw new CRM_Core_Exception(ts('expected only one price field to be in priceset but multiple are present'));
638 }
639 return (int) implode('_', array_keys($priceSet['fields'][$priceFieldID]['options']));
640 }
641
642
ffd93213 643 /**
003ca729
EM
644 * Initiate price set such that various non-BAO things are set on the form.
645 *
646 * This function is not really a BAO function so the location is misleading.
647 *
e0c1764e 648 * @param CRM_Core_Form $form
100fef9d 649 * @param int $id
003ca729 650 * Form entity id.
ffd93213
EM
651 * @param string $entityTable
652 * @param bool $validOnly
100fef9d 653 * @param int $priceSetId
003ca729 654 * Price Set ID
ffd93213
EM
655 *
656 * @return bool|false|int|null
657 */
00be9182 658 public static function initSet(&$form, $id, $entityTable = 'civicrm_event', $validOnly = FALSE, $priceSetId = NULL) {
6a488035
TO
659 if (!$priceSetId) {
660 $priceSetId = self::getFor($entityTable, $id);
661 }
662
663 //check if priceset is is_config
664 if (is_numeric($priceSetId)) {
9da8dc8c 665 if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config') && $form->getVar('_name') != 'Participant') {
6a488035
TO
666 $form->assign('quickConfig', 1);
667 }
668 }
669 // get price info
670 if ($priceSetId) {
671 if ($form->_action & CRM_Core_Action::UPDATE) {
672 $entityId = $entity = NULL;
673
674 switch ($entityTable) {
675 case 'civicrm_event':
676 $entity = 'participant';
677 if (CRM_Utils_System::getClassName($form) == 'CRM_Event_Form_Participant') {
678 $entityId = $form->_id;
679 }
680 else {
681 $entityId = $form->_participantId;
682 }
683 break;
684
685 case 'civicrm_contribution_page':
686 case 'civicrm_contribution':
687 $entity = 'contribution';
688 $entityId = $form->_id;
689 break;
690 }
691
692 if ($entityId && $entity) {
693 $form->_values['line_items'] = CRM_Price_BAO_LineItem::getLineItems($entityId, $entity);
694 }
695 $required = FALSE;
696 }
697 else {
698 $required = TRUE;
699 }
700
701 $form->_priceSetId = $priceSetId;
702 $priceSet = self::getSetDetail($priceSetId, $required, $validOnly);
703 $form->_priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
704 $form->_values['fee'] = CRM_Utils_Array::value('fields', $form->_priceSet);
705
706 //get the price set fields participant count.
707 if ($entityTable == 'civicrm_event') {
708 //get option count info.
709 $form->_priceSet['optionsCountTotal'] = self::getPricesetCount($priceSetId);
710 if ($form->_priceSet['optionsCountTotal']) {
711 $optionsCountDeails = array();
712 if (!empty($form->_priceSet['fields'])) {
713 foreach ($form->_priceSet['fields'] as $field) {
714 foreach ($field['options'] as $option) {
715 $count = CRM_Utils_Array::value('count', $option, 0);
716 $optionsCountDeails['fields'][$field['id']]['options'][$option['id']] = $count;
717 }
718 }
719 }
720 $form->_priceSet['optionsCountDetails'] = $optionsCountDeails;
721 }
722
723 //get option max value info.
724 $optionsMaxValueTotal = 0;
725 $optionsMaxValueDetails = array();
726
727 if (!empty($form->_priceSet['fields'])) {
728 foreach ($form->_priceSet['fields'] as $field) {
729 foreach ($field['options'] as $option) {
730 $maxVal = CRM_Utils_Array::value('max_value', $option, 0);
731 $optionsMaxValueDetails['fields'][$field['id']]['options'][$option['id']] = $maxVal;
732 $optionsMaxValueTotal += $maxVal;
733 }
734 }
735 }
736
737 $form->_priceSet['optionsMaxValueTotal'] = $optionsMaxValueTotal;
738 if ($optionsMaxValueTotal) {
739 $form->_priceSet['optionsMaxValueDetails'] = $optionsMaxValueDetails;
740 }
741 }
742 $form->set('priceSetId', $form->_priceSetId);
743 $form->set('priceSet', $form->_priceSet);
744
745 return $priceSetId;
746 }
747 return FALSE;
748 }
749
ffd93213 750 /**
003ca729
EM
751 * Get line item purchase information.
752 *
753 * This function takes the input parameters and interprets out of it what has been purchased.
754 *
ffd93213 755 * @param $fields
003ca729
EM
756 * This is the output of the function CRM_Price_BAO_PriceSet::getSetDetail($priceSetID, FALSE, FALSE);
757 * And, it would make sense to introduce caching into that function and call it from here rather than
758 * require the $fields array which is passed from pillar to post around the form in order to pass it in here.
c490a46a 759 * @param array $params
003ca729 760 * Params reflecting form input e.g with fields 'price_5' => 7, 'price_8' => array(7, 8)
ffd93213 761 * @param $lineItem
003ca729 762 * Line item array to be altered.
ffd93213 763 * @param string $component
003ca729
EM
764 * This parameter appears to only be relevant to determining whether memberships should be auto-renewed.
765 * (and is effectively a boolean for 'is_membership' which could be calculated from the line items.)
ffd93213 766 */
1a1a2f4f 767 public static function processAmount($fields, &$params, &$lineItem, $component = '', $priceSetID = NULL) {
6a488035 768 // using price set
dc428161 769 $totalPrice = $totalTax = 0;
1a1a2f4f 770 // CRM-18701 Sometimes the amount in the price set is overridden by the amount on the form.
771 // This is notably the case with memberships and we need to put this amount
772 // on the line item rather than the calculated amount.
773 // This seems to only affect radio link items as that is the use case for the 'quick config'
774 // set up (which allows a free form field).
775 $amount_override = NULL;
0be991f0 776
6a488035
TO
777 if ($component) {
778 $autoRenew = array();
779 $autoRenew[0] = $autoRenew[1] = $autoRenew[2] = 0;
780 }
1a1a2f4f 781 if ($priceSetID) {
782 $priceFields = self::filterPriceFieldsFromParams($priceSetID, $params);
783 if (count($priceFields) == 1 && !empty($params['total_amount'])) {
784 $amount_override = $params['total_amount'];
785 }
786 }
6a488035 787 foreach ($fields as $id => $field) {
a7488080 788 if (empty($params["price_{$id}"]) ||
6a488035
TO
789 (empty($params["price_{$id}"]) && $params["price_{$id}"] == NULL)
790 ) {
791 // skip if nothing was submitted for this field
792 continue;
793 }
794
795 switch ($field['html_type']) {
796 case 'Text':
be45c8b4
EM
797 $firstOption = reset($field['options']);
798 $params["price_{$id}"] = array($firstOption['id'] => $params["price_{$id}"]);
6a488035 799 CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
dc428161 800 if (CRM_Utils_Array::value('tax_rate', $field['options'][key($field['options'])])) {
801 $lineItem = self::setLineItem($field, $lineItem, key($field['options']));
802 $totalTax += $field['options'][key($field['options'])]['tax_amount'] * $lineItem[key($field['options'])]['qty'];
803 }
049db839 804 if (CRM_Utils_Array::value('name', $field['options'][key($field['options'])]) == 'contribution_amount') {
805 $taxRates = CRM_Core_PseudoConstant::getTaxRates();
806 if (array_key_exists($params['financial_type_id'], $taxRates)) {
807 $field['options'][key($field['options'])]['tax_rate'] = $taxRates[$params['financial_type_id']];
808 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($field['options'][key($field['options'])]['amount'], $field['options'][key($field['options'])]['tax_rate']);
cce6ec9f 809 $field['options'][key($field['options'])]['tax_amount'] = round($taxAmount['tax_amount'], 2);
049db839 810 $lineItem = self::setLineItem($field, $lineItem, key($field['options']));
811 $totalTax += $field['options'][key($field['options'])]['tax_amount'] * $lineItem[key($field['options'])]['qty'];
812 }
813 }
3b5db8ce 814 $totalPrice += $lineItem[$firstOption['id']]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[key($field['options'])]);
6a488035
TO
815 break;
816
817 case 'Radio':
818 //special case if user select -none-
819 if ($params["price_{$id}"] <= 0) {
820 continue;
821 }
822 $params["price_{$id}"] = array($params["price_{$id}"] => 1);
823 $optionValueId = CRM_Utils_Array::key(1, $params["price_{$id}"]);
0be991f0 824
1a1a2f4f 825 CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem, $amount_override);
dc428161 826 if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) {
827 $lineItem = self::setLineItem($field, $lineItem, $optionValueId);
828 $totalTax += $field['options'][$optionValueId]['tax_amount'];
c40e1ff4 829 if (CRM_Utils_Array::value('field_title', $lineItem[$optionValueId]) == 'Membership Amount') {
830 $lineItem[$optionValueId]['line_total'] = $lineItem[$optionValueId]['unit_price'] = CRM_Utils_Rule::cleanMoney($lineItem[$optionValueId]['line_total'] - $lineItem[$optionValueId]['tax_amount']);
831 }
dc428161 832 }
c40e1ff4 833 $totalPrice += $lineItem[$optionValueId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionValueId]);
cde484fd
DL
834 if (
835 $component &&
a6c4ca20 836 // auto_renew exists and is empty in some workflows, which php treat as a 0
28156120 837 // and hence we explicitly check to see if auto_renew is numeric
cde484fd
DL
838 isset($lineItem[$optionValueId]['auto_renew']) &&
839 is_numeric($lineItem[$optionValueId]['auto_renew'])
840 ) {
6a488035
TO
841 $autoRenew[$lineItem[$optionValueId]['auto_renew']] += $lineItem[$optionValueId]['line_total'];
842 }
843 break;
844
845 case 'Select':
846 $params["price_{$id}"] = array($params["price_{$id}"] => 1);
847 $optionValueId = CRM_Utils_Array::key(1, $params["price_{$id}"]);
0be991f0 848
6a488035 849 CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
dc428161 850 if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) {
851 $lineItem = self::setLineItem($field, $lineItem, $optionValueId);
852 $totalTax += $field['options'][$optionValueId]['tax_amount'];
853 }
c40e1ff4 854 $totalPrice += $lineItem[$optionValueId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionValueId]);
cde484fd
DL
855 if (
856 $component &&
857 isset($lineItem[$optionValueId]['auto_renew']) &&
858 is_numeric($lineItem[$optionValueId]['auto_renew'])
859 ) {
6a488035
TO
860 $autoRenew[$lineItem[$optionValueId]['auto_renew']] += $lineItem[$optionValueId]['line_total'];
861 }
862 break;
863
864 case 'CheckBox':
0be991f0 865
6a488035 866 CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
0be991f0 867 foreach ($params["price_{$id}"] as $optionId => $option) {
dc428161 868 if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionId])) {
869 $lineItem = self::setLineItem($field, $lineItem, $optionId);
870 $totalTax += $field['options'][$optionId]['tax_amount'];
871 }
c40e1ff4 872 $totalPrice += $lineItem[$optionId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionId]);
cde484fd
DL
873 if (
874 $component &&
875 isset($lineItem[$optionId]['auto_renew']) &&
876 is_numeric($lineItem[$optionId]['auto_renew'])
877 ) {
6a488035
TO
878 $autoRenew[$lineItem[$optionId]['auto_renew']] += $lineItem[$optionId]['line_total'];
879 }
880 }
881 break;
882 }
883 }
884
885 $amount_level = array();
886 $totalParticipant = 0;
887 if (is_array($lineItem)) {
888 foreach ($lineItem as $values) {
889 $totalParticipant += $values['participant_count'];
2b308818
DG
890 // This is a bit nasty. The logic of 'quick config' was because price set configuration was
891 // (and still is) too difficult to replace the 'quick config' price set configuration on the contribution
892 // page.
893 //
894 // However, because the quick config concept existed all sorts of logic was hung off it
895 // and function behaviour sometimes depends on whether 'price set' is set - although actually it
896 // is always set at the functional level. In this case we are dealing with the default 'quick config'
897 // price set having a label of 'Contribution Amount' which could wind up creating a 'funny looking' label.
898 // The correct answer is probably for it to have an empty label in the DB - the label is never shown so it is a
899 // place holder.
900 //
901 // But, in the interests of being careful when capacity is low - avoiding the known default value
902 // will get us by.
903 // Crucially a test has been added so a better solution can be implemented later with some comfort.
c039f658 904 // @todo - stop setting amount level in this function & call the getAmountLevel function to retrieve it.
2b308818
DG
905 if ($values['label'] != ts('Contribution Amount')) {
906 $amount_level[] = $values['label'] . ' - ' . (float) $values['qty'];
907 }
6a488035
TO
908 }
909 }
910
911 $displayParticipantCount = '';
912 if ($totalParticipant > 0) {
913 $displayParticipantCount = ' Participant Count -' . $totalParticipant;
914 }
c039f658 915 // @todo - stop setting amount level in this function & call the getAmountLevel function to retrieve it.
c0214b81 916 if (!empty($amount_level)) {
0aa9b939 917 $params['amount_level'] = CRM_Utils_Array::implodePadded($amount_level);
c0214b81 918 if (!empty($displayParticipantCount)) {
d7a867a8 919 $params['amount_level'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amount_level) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR;
c0214b81 920 }
2b308818 921 }
85d3367a 922 $params['amount'] = CRM_Utils_Money::format($totalPrice, NULL, NULL, TRUE);
dc428161 923 $params['tax_amount'] = $totalTax;
6a488035
TO
924 if ($component) {
925 foreach ($autoRenew as $dontCare => $eachAmount) {
926 if (!$eachAmount) {
cde484fd 927 unset($autoRenew[$dontCare]);
6a488035
TO
928 }
929 }
481a74f4 930 if (count($autoRenew) > 1) {
6a488035
TO
931 $params['autoRenew'] = $autoRenew;
932 }
933 }
934 }
935
c039f658 936 /**
937 * Get the text to record for amount level.
938 *
939 * @param array $params
940 * Submitted parameters
941 * - priceSetId is required to be set in the calling function
942 * (we don't e-notice check it to enforce that - all payments DO have a price set - even if it is the
943 * default one & this function asks that be set if it is the case).
944 *
945 * @return string
946 * Text for civicrm_contribution.amount_level field.
947 */
948 public static function getAmountLevelText($params) {
949 $priceSetID = $params['priceSetId'];
950 $priceFieldSelection = self::filterPriceFieldsFromParams($priceSetID, $params);
951 $priceFieldMetadata = self::getCachedPriceSetDetail($priceSetID);
8c632a5d 952 $displayParticipantCount = NULL;
0f8a0349 953
c039f658 954 $amount_level = array();
955 foreach ($priceFieldMetadata['fields'] as $field) {
956 if (!empty($priceFieldSelection[$field['id']])) {
0f8a0349 957 $qtyString = '';
c039f658 958 if ($field['is_enter_qty']) {
959 $qtyString = ' - ' . (float) $params['price_' . $field['id']];
960 }
961 // We deliberately & specifically exclude contribution amount as it has a specific meaning.
962 // ie. it represents the default price field for a contribution. Another approach would be not
963 // to give it a label if we don't want it to show.
964 if ($field['label'] != ts('Contribution Amount')) {
965 $amount_level[] = $field['label'] . $qtyString;
966 }
967 }
968 }
969 return CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amount_level) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR;
970 }
971
972 /**
973 * Get the fields relevant to the price field from the parameters.
974 *
975 * E.g we are looking for price_5 => 7 out of a big array of input parameters.
976 *
977 * @param int $priceSetID
978 * @param array $params
979 *
980 * @return array
981 * Price fields found in the params array
982 */
983 public static function filterPriceFieldsFromParams($priceSetID, $params) {
984 $priceSet = self::getCachedPriceSetDetail($priceSetID);
985 $return = array();
986 foreach ($priceSet['fields'] as $field) {
987 if (!empty($params['price_' . $field['id']])) {
988 $return[$field['id']] = $params['price_' . $field['id']];
989 }
990 }
991 return $return;
992 }
993
994 /**
995 * Wrapper for getSetDetail with caching.
996 *
997 * We seem to be passing this array around in a painful way - presumably to avoid the hit
998 * of loading it - so lets make it callable with caching.
999 *
1000 * Why not just add caching to the other function? We could do - it just seemed a bit unclear the best caching pattern
1001 * & the function was already pretty fugly. Also, I feel like we need to migrate the interaction with price-sets into
1002 * a more granular interaction - ie. retrieve specific data using specific functions on this class & have the form
1003 * think less about the price sets.
1004 *
1005 * @param int $priceSetID
1006 *
1007 * @return array
1008 */
1009 public static function getCachedPriceSetDetail($priceSetID) {
1010 $cacheKey = __CLASS__ . __FUNCTION__ . '_' . $priceSetID;
1011 $cache = CRM_Utils_Cache::singleton();
1012 $values = (array) $cache->get($cacheKey);
1013 if (empty($values)) {
1014 $data = self::getSetDetail($priceSetID);
1015 $values = $data[$priceSetID];
1016 $cache->set($cacheKey, $values);
1017 }
1018 return $values;
1019 }
1020
6a488035 1021 /**
100fef9d 1022 * Build the price set form.
6a488035 1023 *
e0c1764e 1024 * @param CRM_Core_Form $form
dd244018 1025 *
355ba699 1026 * @return void
6a488035 1027 */
00be9182 1028 public static function buildPriceSet(&$form) {
6a488035 1029 $priceSetId = $form->get('priceSetId');
6a488035
TO
1030 if (!$priceSetId) {
1031 return;
1032 }
1033
1034 $validFieldsOnly = TRUE;
1035 $className = CRM_Utils_System::getClassName($form);
1036 if (in_array($className, array(
353ffa53 1037 'CRM_Contribute_Form_Contribution',
acb1052e 1038 'CRM_Member_Form_Membership',
353ffa53 1039 ))) {
6a488035
TO
1040 $validFieldsOnly = FALSE;
1041 }
1042
353ffa53
TO
1043 $priceSet = self::getSetDetail($priceSetId, TRUE, $validFieldsOnly);
1044 $form->_priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
883e4763 1045 $validPriceFieldIds = array_keys($form->_priceSet['fields']);
6a488035 1046 $form->_quickConfig = $quickConfig = 0;
9da8dc8c 1047 if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config')) {
6a488035
TO
1048 $quickConfig = 1;
1049 }
1050
1051 $form->assign('quickConfig', $quickConfig);
1052 if ($className == 'CRM_Contribute_Form_Contribution_Main') {
1053 $form->_quickConfig = $quickConfig;
1054 }
ee7f71d9
AS
1055
1056 // Mark which field should have the auto-renew checkbox, if any. CRM-18305
f4fd225b 1057 if (!empty($form->_membershipTypeValues) && is_array($form->_membershipTypeValues)) {
ee7f71d9
AS
1058 $autoRenewMembershipTypes = array();
1059 foreach ($form->_membershipTypeValues as $membershiptTypeValue) {
1060 if ($membershiptTypeValue['auto_renew']) {
1061 $autoRenewMembershipTypes[] = $membershiptTypeValue['id'];
1062 }
1063 }
1064 foreach ($form->_priceSet['fields'] as &$field) {
1065 if (array_key_exists('options', $field) && is_array($field['options'])) {
1066 foreach ($field['options'] as $option) {
b3460c23 1067 if (!empty($option['membership_type_id'])) {
ee7f71d9
AS
1068 if (in_array($option['membership_type_id'], $autoRenewMembershipTypes)) {
1069 $form->_priceSet['auto_renew_membership_field'] = $field['id'];
1070 // Only one field can offer auto_renew memberships, so break here.
1071 break;
1072 }
1073 }
1074 }
1075 }
1076 }
1077 }
6a488035
TO
1078 $form->assign('priceSet', $form->_priceSet);
1079
1080 $component = 'contribution';
1081 if ($className == 'CRM_Member_Form_Membership') {
1082 $component = 'membership';
1083 }
1084
1085 if ($className == 'CRM_Contribute_Form_Contribution_Main') {
1086 $feeBlock = &$form->_values['fee'];
1087 if (!empty($form->_useForMember)) {
1088 $component = 'membership';
1089 }
1090 }
1091 else {
1092 $feeBlock = &$form->_priceSet['fields'];
1093 }
66af7c48
PN
1094 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
1095 foreach ($feeBlock as $key => $value) {
1096 foreach ($value['options'] as $k => $options) {
1097 if (!CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($options['financial_type_id']))) {
1098 unset($feeBlock[$key]['options'][$k]);
1099 }
1100 }
1101 if (empty($feeBlock[$key]['options'])) {
1102 unset($feeBlock[$key]);
e1a89dfd 1103 }
4a695b27 1104 }
e1a89dfd 1105 }
6a488035
TO
1106 // call the hook.
1107 CRM_Utils_Hook::buildAmount($component, $form, $feeBlock);
1108
c7b3d063 1109 // CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions
ba1dcfda 1110 $adminFieldVisible = FALSE;
c7b3d063 1111 if (CRM_Core_Permission::check('administer CiviCRM')) {
ba1dcfda 1112 $adminFieldVisible = TRUE;
c7b3d063 1113 }
77b97be7 1114
883e4763 1115 foreach ($feeBlock as $id => $field) {
6a488035 1116 if (CRM_Utils_Array::value('visibility', $field) == 'public' ||
ba1dcfda 1117 (CRM_Utils_Array::value('visibility', $field) == 'admin' && $adminFieldVisible == TRUE) ||
6a488035
TO
1118 !$validFieldsOnly
1119 ) {
1120 $options = CRM_Utils_Array::value('options', $field);
1121 if ($className == 'CRM_Contribute_Form_Contribution_Main' && $component = 'membership') {
cc509891 1122 $userid = $form->getVar('_membershipContactID');
6a488035
TO
1123 $checklifetime = self::checkCurrentMembership($options, $userid);
1124 if ($checklifetime) {
1125 $form->assign('ispricelifetime', TRUE);
1126 }
1127 }
883e4763 1128 if (!is_array($options) || !in_array($id, $validPriceFieldIds)) {
6a488035
TO
1129 continue;
1130 }
9da8dc8c 1131 CRM_Price_BAO_PriceField::addQuickFormElement($form,
6a488035
TO
1132 'price_' . $field['id'],
1133 $field['id'],
1134 FALSE,
1135 CRM_Utils_Array::value('is_required', $field, FALSE),
1136 NULL,
1137 $options
1138 );
1139 }
1140 }
1141 }
1142
1143 /**
003ca729
EM
1144 * Check the current Membership having end date null.
1145 *
1146 * @param array $options
1147 * @param int $userid
1148 * Probably actually contact ID.
1149 *
1150 * @return bool
6a488035 1151 */
00be9182 1152 public static function checkCurrentMembership(&$options, $userid) {
6a488035 1153 if (!$userid || empty($options)) {
925157c5 1154 return FALSE;
6a488035
TO
1155 }
1156 static $_contact_memberships = array();
003ca729 1157 $checkLifetime = FALSE;
6a488035 1158 foreach ($options as $key => $value) {
a7488080 1159 if (!empty($value['membership_type_id'])) {
6a488035
TO
1160 if (!isset($_contact_memberships[$userid][$value['membership_type_id']])) {
1161 $_contact_memberships[$userid][$value['membership_type_id']] = CRM_Member_BAO_Membership::getContactMembership($userid, $value['membership_type_id'], FALSE);
1162 }
1163 $currentMembership = $_contact_memberships[$userid][$value['membership_type_id']];
8cc574cf 1164 if (!empty($currentMembership) && empty($currentMembership['end_date'])) {
6a488035 1165 unset($options[$key]);
003ca729 1166 $checkLifetime = TRUE;
6a488035
TO
1167 }
1168 }
1169 }
003ca729 1170 if ($checkLifetime) {
6a488035
TO
1171 return TRUE;
1172 }
1173 else {
1174 return FALSE;
1175 }
1176 }
1177
1178 /**
100fef9d 1179 * Set daefult the price set fields.
6a488035 1180 *
c490a46a 1181 * @param CRM_Core_Form $form
fd31fa4c
EM
1182 * @param $defaults
1183 *
a6c01b45 1184 * @return array
6a488035 1185 */
00be9182 1186 public static function setDefaultPriceSet(&$form, &$defaults) {
6a488035
TO
1187 if (!isset($form->_priceSet) || empty($form->_priceSet['fields'])) {
1188 return $defaults;
1189 }
1190
1191 foreach ($form->_priceSet['fields'] as $key => $val) {
1192 foreach ($val['options'] as $keys => $values) {
cb063506
KJ
1193 // build price field index which is passed via URL
1194 // url format will be appended by "&price_5=11"
1195 $priceFieldName = 'price_' . $values['price_field_id'];
1196 $priceFieldValue = self::getPriceFieldValueFromURL($form, $priceFieldName);
1197 if (!empty($priceFieldValue)) {
034b5cb6 1198 self::setDefaultPriceSetField($priceFieldName, $priceFieldValue, $val['html_type'], $defaults);
cb063506
KJ
1199 // break here to prevent overwriting of default due to 'is_default'
1200 // option configuration. The value sent via URL get's higher priority.
1201 break;
1202 }
1203 elseif ($values['is_default']) {
034b5cb6 1204 self::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $defaults);
6a488035
TO
1205 }
1206 }
1207 }
1208 return $defaults;
1209 }
1210
034b5cb6
KJ
1211 /**
1212 * Get the value of price field if passed via url
1213 *
1214 * @param string $priceFieldName
1215 * @param string $priceFieldValue
1216 * @param string $priceFieldType
1217 * @param array $defaults
1218 *
1219 * @return void
1220 */
1221 public static function setDefaultPriceSetField($priceFieldName, $priceFieldValue, $priceFieldType, &$defaults) {
1222 if ($priceFieldType == 'CheckBox') {
1223 $defaults[$priceFieldName][$priceFieldValue] = 1;
1224 }
1225 else {
1226 $defaults[$priceFieldName] = $priceFieldValue;
1227 }
1228 }
1229
cb063506
KJ
1230 /**
1231 * Get the value of price field if passed via url
1232 *
1233 * @param CRM_Core_Form $form
1234 * @param string $priceFieldName
1235 *
1236 * @return mixed $priceFieldValue
1237 */
1238 public static function getPriceFieldValueFromURL(&$form, $priceFieldName) {
1239 $priceFieldValue = CRM_Utils_Request::retrieve($priceFieldName, 'String', $form, FALSE, NULL, 'GET');
1240 if (!empty($priceFieldValue)) {
1241 return $priceFieldValue;
1242 }
1243 }
1244
b2cdd843
EM
1245 /**
1246 * Supports event create function by setting up required price sets, not tested but expect
1247 * it will work for contribution page
414c1420
TO
1248 * @param array $params
1249 * As passed to api/bao create fn.
1250 * @param CRM_Core_DAO $entity
1251 * Object for given entity.
1252 * @param string $entityName
1253 * Name of entity - e.g event.
b2cdd843 1254 */
00be9182 1255 public static function setPriceSets(&$params, $entity, $entityName) {
22e263ad 1256 if (empty($params['price_set_id']) || !is_array($params['price_set_id'])) {
b2cdd843
EM
1257 return;
1258 }
1259 // CRM-14069 note that we may as well start by assuming more than one.
1260 // currently the form does not pass in as an array & will be skipped
1261 // test is passing in as an array but I feel the api should have a metadata that allows
1262 // transform of single to array - seems good for managing transitions - in which case all api
1263 // calls that set price_set_id will hit this
1264 // e.g in getfields 'price_set_id' => array('blah', 'bao_type' => 'array') - causing
1265 // all separated values, strings, json half-separated values (in participant we hit this)
1266 // to be converted to json @ api layer
1267 $pse = new CRM_Price_DAO_PriceSetEntity();
1268 $pse->entity_table = 'civicrm_' . $entityName;
1269 $pse->entity_id = $entity->id;
1270 while ($pse->fetch()) {
22e263ad 1271 if (!in_array($pse->price_set_id, $params['price_set_id'])) {
b2cdd843
EM
1272 // note an even more aggressive form of this deletion currently happens in event form
1273 // past price sets discounts are made inaccessible by this as the discount_id is set to NULL
1274 // on the participant record
1275 if (CRM_Price_BAO_PriceSet::removeFrom('civicrm_' . $entityName, $entity->id)) {
ba1dcfda 1276 CRM_Core_BAO_Discount::del($entity->id, 'civicrm_' . $entityName);
b2cdd843
EM
1277 }
1278 }
1279 }
1280 foreach ($params['price_set_id'] as $priceSetID) {
1281 CRM_Price_BAO_PriceSet::addTo('civicrm_' . $entityName, $entity->id, $priceSetID);
1282 //@todo - how should we do this - copied from form
b53cbfbc 1283 //if (!empty($params['price_field_id'])) {
b2cdd843
EM
1284 // $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
1285 // CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID, 0);
1286 //}
1287 }
1288 }
353ffa53 1289
6a488035 1290 /**
fe482240 1291 * Get field ids of a price set.
6a488035 1292 *
414c1420
TO
1293 * @param int $id
1294 * Price Set id.
6a488035 1295 *
a6c01b45 1296 * @return array
16b10e64 1297 * Array of the field ids
6a488035 1298 *
6a488035
TO
1299 */
1300 public static function getFieldIds($id) {
9da8dc8c 1301 $priceField = new CRM_Price_DAO_PriceField();
6a488035
TO
1302 $priceField->price_set_id = $id;
1303 $priceField->find();
1304 while ($priceField->fetch()) {
1305 $var[] = $priceField->id;
1306 }
1307 return $var;
1308 }
1309
1310 /**
72b3a70c 1311 * Copy a price set, including all the fields
6a488035 1312 *
414c1420
TO
1313 * @param int $id
1314 * The price set id to copy.
6a488035 1315 *
72b3a70c 1316 * @return CRM_Price_DAO_PriceSet
6a488035 1317 */
00be9182 1318 public static function copy($id) {
6a488035
TO
1319 $maxId = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_price_set");
1320
1321 $title = ts('[Copy id %1]', array(1 => $maxId + 1));
1322 $fieldsFix = array(
ba1dcfda 1323 'suffix' => array(
353ffa53
TO
1324 'title' => ' ' . $title,
1325 'name' => '__Copy_id_' . ($maxId + 1) . '_',
6a488035
TO
1326 ),
1327 );
1328
9da8dc8c 1329 $copy = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceSet',
353ffa53
TO
1330 array('id' => $id),
1331 NULL,
1332 $fieldsFix
6a488035
TO
1333 );
1334
1335 //copying all the blocks pertaining to the price set
9da8dc8c 1336 $copyPriceField = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceField',
353ffa53
TO
1337 array('price_set_id' => $id),
1338 array('price_set_id' => $copy->id)
6a488035
TO
1339 );
1340 if (!empty($copyPriceField)) {
1341 $price = array_combine(self::getFieldIds($id), self::getFieldIds($copy->id));
1342
1343 //copy option group and values
1344 foreach ($price as $originalId => $copyId) {
9da8dc8c 1345 CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceFieldValue',
6a488035
TO
1346 array('price_field_id' => $originalId),
1347 array('price_field_id' => $copyId)
1348 );
1349 }
1350 }
1351 $copy->save();
1352
1353 CRM_Utils_Hook::copy('Set', $copy);
1354 return $copy;
1355 }
1356
1357 /**
fe482240 1358 * check price set permission.
6a488035 1359 *
414c1420
TO
1360 * @param int $sid
1361 * The price set id.
77b97be7
EM
1362 *
1363 * @return bool
6a488035 1364 */
00be9182 1365 public static function checkPermission($sid) {
44c8822b 1366 if ($sid && self::eventPriceSetDomainID()) {
9da8dc8c 1367 $domain_id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $sid, 'domain_id', 'id');
6a488035 1368 if (CRM_Core_Config::domainID() != $domain_id) {
0499b0ad 1369 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
1370 }
1371 }
1372 return TRUE;
1373 }
1374
1375 /**
1376 * Get the sum of participant count
1377 * for all fields of given price set.
1378 *
414c1420
TO
1379 * @param int $sid
1380 * The price set id.
6a488035 1381 *
77b97be7
EM
1382 * @param bool $onlyActive
1383 *
1384 * @return int|null|string
6a488035
TO
1385 */
1386 public static function getPricesetCount($sid, $onlyActive = TRUE) {
1387 $count = 0;
1388 if (!$sid) {
1389 return $count;
1390 }
1391
1392 $where = NULL;
1393 if ($onlyActive) {
1394 $where = 'AND value.is_active = 1 AND field.is_active = 1';
1395 }
1396
1397 static $pricesetFieldCount = array();
1398 if (!isset($pricesetFieldCount[$sid])) {
1399 $sql = "
1400 SELECT sum(value.count) as totalCount
1401 FROM civicrm_price_field_value value
1402INNER JOIN civicrm_price_field field ON ( field.id = value.price_field_id )
1403INNER JOIN civicrm_price_set pset ON ( pset.id = field.price_set_id )
1404 WHERE pset.id = %1
1405 $where";
1406
1407 $count = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($sid, 'Positive')));
1408 $pricesetFieldCount[$sid] = ($count) ? $count : 0;
1409 }
1410
1411 return $pricesetFieldCount[$sid];
1412 }
1413
ffd93213
EM
1414 /**
1415 * @param $ids
1416 *
1417 * @return array
1418 */
6a488035
TO
1419 public static function getMembershipCount($ids) {
1420 $queryString = "
1421SELECT count( pfv.id ) AS count, pfv.id AS id
1422FROM civicrm_price_field_value pfv
1423INNER JOIN civicrm_membership_type mt ON mt.id = pfv.membership_type_id
1424WHERE pfv.id IN ( $ids )
1425GROUP BY mt.member_of_contact_id";
1426
1427 $crmDAO = CRM_Core_DAO::executeQuery($queryString);
1428 $count = array();
1429
1430 while ($crmDAO->fetch()) {
1431 $count[$crmDAO->id] = $crmDAO->count;
1432 }
1433
1434 return $count;
1435 }
1436
1437 /**
fe482240 1438 * Check if auto renew option should be shown.
6a488035 1439 *
8eb1503b 1440 * The auto-renew option should be visible if membership types associated with all the fields has
1441 * been set for auto-renew option.
1442 *
1443 * Auto renew checkbox should be frozen if for all the membership type auto renew is required
1444 *
414c1420
TO
1445 * @param int $priceSetId
1446 * Price set id.
6a488035 1447 *
a6c01b45
CW
1448 * @return int
1449 * $autoRenewOption ( 0:hide, 1:optional 2:required )
6a488035
TO
1450 */
1451 public static function checkAutoRenewForPriceSet($priceSetId) {
8eb1503b 1452 $query = 'SELECT DISTINCT mt.auto_renew, mt.duration_interval, mt.duration_unit,
1453 pf.html_type, pf.id as price_field_id
6a488035
TO
1454 FROM civicrm_price_field_value pfv
1455 INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id
1456 INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id
1457 WHERE pf.price_set_id = %1
1458 AND pf.is_active = 1
8eb1503b 1459 AND pfv.is_active = 1
1460 ORDER BY price_field_id';
6a488035
TO
1461
1462 $params = array(1 => array($priceSetId, 'Integer'));
1463
353ffa53 1464 $dao = CRM_Core_DAO::executeQuery($query, $params);
8eb1503b 1465
88089d21 1466 //CRM-18050: Check count of price set fields which has been set with auto-renew option.
137d1fac
WA
1467 //If price set field is already present with auto-renew option then, it will restrict for adding another price set field with auto-renew option.
1468 if ($dao->N == 0) {
1469 return 0;
1470 }
1471
6a488035 1472 $autoRenewOption = 2;
8eb1503b 1473 $priceFields = array();
6a488035
TO
1474 while ($dao->fetch()) {
1475 if (!$dao->auto_renew) {
8eb1503b 1476 // If any one can't be renewed none can.
1477 return 0;
6a488035
TO
1478 }
1479 if ($dao->auto_renew == 1) {
1480 $autoRenewOption = 1;
1481 }
1482
8eb1503b 1483 if ($dao->html_type == 'Checkbox' && !in_array($dao->duration_interval . $dao->duration_unit, $priceFields[$dao->price_field_id])) {
1484 // Checkbox fields cannot support auto-renew if they have more than one duration configuration
1485 // as more than one can be selected. Radio and select are either-or so they can have more than one duration.
1486 return 0;
1487 }
1488 $priceFields[$dao->price_field_id][] = $dao->duration_interval . $dao->duration_unit;
1489 foreach ($priceFields as $priceFieldID => $durations) {
1490 if ($priceFieldID != $dao->price_field_id && !in_array($dao->duration_interval . $dao->duration_unit, $durations)) {
1491 // Another price field has a duration configuration that differs so we can't offer auto-renew.
1492 return 0;
1493 }
1494 }
6a488035
TO
1495 }
1496
8eb1503b 1497 return $autoRenewOption;
6a488035
TO
1498 }
1499
1500 /**
fe482240 1501 * Retrieve auto renew frequency and interval.
6a488035 1502 *
414c1420
TO
1503 * @param int $priceSetId
1504 * Price set id.
6a488035 1505 *
a6c01b45
CW
1506 * @return array
1507 * associate array of frequency interval and unit
6a488035
TO
1508 */
1509 public static function getRecurDetails($priceSetId) {
1510 $query = 'SELECT mt.duration_interval, mt.duration_unit
1511 FROM civicrm_price_field_value pfv
1512 INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id
1513 INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id
1514 WHERE pf.price_set_id = %1 LIMIT 1';
1515
1516 $params = array(1 => array($priceSetId, 'Integer'));
1517 $dao = CRM_Core_DAO::executeQuery($query, $params);
1518 $dao->fetch();
1519 return array($dao->duration_interval, $dao->duration_unit);
1520 }
1521
ffd93213
EM
1522 /**
1523 * @return object
1524 */
00be9182 1525 public static function eventPriceSetDomainID() {
d356cdeb 1526 return Civi::settings()->get('event_price_set_domain_id');
6a488035
TO
1527 }
1528
1529 /**
fe482240 1530 * Update the is_quick_config flag in the db.
6a488035 1531 *
414c1420
TO
1532 * @param int $id
1533 * Id of the database record.
acb1052e 1534 * @param bool $isQuickConfig we want to set the is_quick_config field.
414c1420 1535 * Value we want to set the is_quick_config field.
6a488035 1536 *
a6c01b45 1537 * @return Object
b44e3f84 1538 * DAO object on success, null otherwise
6a488035 1539 */
00be9182 1540 public static function setIsQuickConfig($id, $isQuickConfig) {
9da8dc8c 1541 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceSet', $id, 'is_quick_config', $isQuickConfig);
6a488035
TO
1542 }
1543
1544 /**
f72e6e2c 1545 * Check if price set id provides option for user to select both auto-renew and non-auto-renew memberships
6a488035 1546 *
003ca729
EM
1547 * @param int $id
1548 *
1549 * @return bool
6a488035 1550 */
f72e6e2c
EM
1551 public static function isMembershipPriceSetContainsMixOfRenewNonRenew($id) {
1552 $membershipTypes = self::getMembershipTypesFromPriceSet($id);
1553 if (!empty($membershipTypes['autorenew']) && !empty($membershipTypes['non_renew'])) {
1554 return TRUE;
6a488035 1555 }
ba1dcfda 1556 return FALSE;
6a488035
TO
1557 }
1558
37b11065
EM
1559 /**
1560 * Get an array of the membership types in a price set.
1561 *
1562 * @param int $id
1563 *
1564 * @return array(
1565 * Membership types in the price set
1566 */
1567 public static function getMembershipTypesFromPriceSet($id) {
1568 $query
1569 = "SELECT pfv.id, pfv.price_field_id, pfv.name, pfv.membership_type_id, pf.html_type, mt.auto_renew
1570FROM civicrm_price_field_value pfv
1571LEFT JOIN civicrm_price_field pf ON pf.id = pfv.price_field_id
1572LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id
1573LEFT JOIN civicrm_membership_type mt ON mt.id = pfv.membership_type_id
1574WHERE ps.id = %1
1575";
1576
1577 $params = array(1 => array($id, 'Integer'));
1578 $dao = CRM_Core_DAO::executeQuery($query, $params);
1579
1580 $membershipTypes = array(
1581 'all' => array(),
1582 'autorenew' => array(),
1583 'autorenew_required' => array(),
1584 'autorenew_optional' => array(),
1585 );
1586 while ($dao->fetch()) {
1587 if (empty($dao->membership_type_id)) {
1588 continue;
1589 }
1590 $membershipTypes['all'][] = $dao->membership_type_id;
1591 if (!empty($dao->auto_renew)) {
1592 $membershipTypes['autorenew'][] = $dao->membership_type_id;
1593 if ($dao->auto_renew == 2) {
1594 $membershipTypes['autorenew_required'][] = $dao->membership_type_id;
1595 }
1596 else {
1597 $membershipTypes['autorenew_optional'][] = $dao->membership_type_id;
1598 }
1599 }
1600 else {
1601 $membershipTypes['non_renew'][] = $dao->membership_type_id;
1602 }
1603 }
1604 return $membershipTypes;
1605 }
1606
e0c1764e 1607 /**
c6914066
PN
1608 * Copy priceSet when event/contibution page is copied
1609 *
414c1420
TO
1610 * @param string $baoName
1611 * BAO name.
1612 * @param int $id
1613 * Old event/contribution page id.
1614 * @param int $newId
1615 * Newly created event/contribution page id.
c6914066 1616 */
00be9182 1617 public static function copyPriceSet($baoName, $id, $newId) {
9da8dc8c 1618 $priceSetId = CRM_Price_BAO_PriceSet::getFor($baoName, $id);
c6914066 1619 if ($priceSetId) {
9da8dc8c 1620 $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
22e263ad 1621 if ($isQuickConfig) {
37326fa1 1622 $copyPriceSet = CRM_Price_BAO_PriceSet::copy($priceSetId);
9da8dc8c 1623 CRM_Price_BAO_PriceSet::addTo($baoName, $newId, $copyPriceSet->id);
44c8822b 1624 }
c6914066 1625 else {
9da8dc8c 1626 $copyPriceSet = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceSetEntity',
c6914066
PN
1627 array(
1628 'entity_id' => $id,
1629 'entity_table' => $baoName,
1630 ),
1631 array('entity_id' => $newId)
1632 );
1633 }
1634 // copy event discount
1635 if ($baoName == 'civicrm_event') {
1636 $discount = CRM_Core_BAO_Discount::getOptionGroup($id, 'civicrm_event');
1637 foreach ($discount as $discountId => $setId) {
1638
9da8dc8c 1639 $copyPriceSet = &CRM_Price_BAO_PriceSet::copy($setId);
44c8822b 1640
28156120 1641 CRM_Core_DAO::copyGeneric(
c6914066
PN
1642 'CRM_Core_DAO_Discount',
1643 array(
1644 'id' => $discountId,
1645 ),
1646 array(
1647 'entity_id' => $newId,
1648 'price_set_id' => $copyPriceSet->id,
1649 )
1650 );
1651 }
1652 }
1653 }
1654 }
dc428161 1655
d424ffde 1656 /**
fe482240 1657 * Function to set tax_amount and tax_rate in LineItem.
d424ffde
CW
1658 *
1659 * @param array $field
1660 * @param array $lineItem
1661 * @param int $optionValueId
1662 *
1663 * @return array
dc428161 1664 */
00be9182 1665 public static function setLineItem($field, $lineItem, $optionValueId) {
dc428161 1666 if ($field['html_type'] == 'Text') {
1667 $taxAmount = $field['options'][$optionValueId]['tax_amount'] * $lineItem[$optionValueId]['qty'];
1668 }
1669 else {
1670 $taxAmount = $field['options'][$optionValueId]['tax_amount'];
1671 }
1672 $taxRate = $field['options'][$optionValueId]['tax_rate'];
dc428161 1673 $lineItem[$optionValueId]['tax_amount'] = $taxAmount;
1674 $lineItem[$optionValueId]['tax_rate'] = $taxRate;
1675
1676 return $lineItem;
1677 }
96025800 1678
362bd1b7 1679 /**
1680 * Get the first price set value IDs from a parameters array.
1681 *
1682 * In practice this is really used when we only expect one to exist.
1683 *
1684 * @param array $params
1685 *
1686 * @return array
1687 * Array of the ids of the price set values.
1688 */
1689 public static function parseFirstPriceSetValueIDFromParams($params) {
1690 $priceSetValueIDs = self::parsePriceSetValueIDsFromParams($params);
1691 return reset($priceSetValueIDs);
1692 }
1693
1694 /**
1695 * Get the price set value IDs from a set of parameters
1696 *
1697 * @param array $params
1698 *
1699 * @return array
1700 * Array of the ids of the price set values.
1701 */
1702 public static function parsePriceSetValueIDsFromParams($params) {
1703 $priceSetParams = self::parsePriceSetArrayFromParams($params);
1704 $priceSetValueIDs = array();
1705 foreach ($priceSetParams as $priceSetParam) {
1706 foreach (array_keys($priceSetParam) as $priceValueID) {
1707 $priceSetValueIDs[] = $priceValueID;
1708 }
1709 }
1710 return $priceSetValueIDs;
1711 }
1712
1713 /**
1714 * Get the price set value IDs from a set of parameters
1715 *
1716 * @param array $params
1717 *
1718 * @return array
1719 * Array of price fields filtered from the params.
1720 */
1721 public static function parsePriceSetArrayFromParams($params) {
1722 $priceSetParams = array();
1723 foreach ($params as $field => $value) {
1724 $parts = explode('_', $field);
1725 if (count($parts) == 2 && $parts[0] == 'price' && is_numeric($parts[1])) {
1726 $priceSetParams[$field] = $value;
1727 }
1728 }
1729 return $priceSetParams;
1730 }
1731
6a488035 1732}