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