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