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