Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Business object for managing price sets | |
38 | * | |
39 | */ | |
9da8dc8c | 40 | class CRM_Price_BAO_PriceSet extends CRM_Price_DAO_PriceSet { |
6a488035 | 41 | |
71ba92c4 | 42 | /** |
100fef9d | 43 | * Static field for default price set details |
71ba92c4 PN |
44 | * |
45 | * @var array | |
46 | * @static | |
47 | */ | |
48 | static $_defaultPriceSet = NULL; | |
49 | ||
6a488035 | 50 | /** |
100fef9d | 51 | * Class constructor |
6a488035 | 52 | */ |
00be9182 | 53 | public function __construct() { |
6a488035 TO |
54 | parent::__construct(); |
55 | } | |
56 | ||
57 | /** | |
100fef9d | 58 | * Takes an associative array and creates a price set object |
6a488035 TO |
59 | * |
60 | * @param array $params (reference) an assoc array of name/value pairs | |
61 | * | |
c490a46a | 62 | * @return CRM_Price_DAO_PriceSet object |
6a488035 TO |
63 | * @static |
64 | */ | |
00be9182 | 65 | public static function create(&$params) { |
e0c1764e EM |
66 | if(empty($params['id']) && empty($params['name'])) { |
67 | $params['name'] = CRM_Utils_String::munge($params['title'], '_', 242); | |
68 | } | |
9da8dc8c | 69 | $priceSetBAO = new CRM_Price_BAO_PriceSet(); |
6a488035 TO |
70 | $priceSetBAO->copyValues($params); |
71 | if (self::eventPriceSetDomainID()) { | |
72 | $priceSetBAO->domain_id = CRM_Core_Config::domainID(); | |
73 | } | |
74 | return $priceSetBAO->save(); | |
75 | } | |
76 | ||
77 | /** | |
c490a46a | 78 | * Fetch object based on array of properties |
6a488035 TO |
79 | * |
80 | * @param array $params (reference ) an assoc array of name/value pairs | |
81 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
82 | * | |
c490a46a | 83 | * @return CRM_Price_DAO_PriceSet object |
6a488035 TO |
84 | * @static |
85 | */ | |
00be9182 | 86 | public static function retrieve(&$params, &$defaults) { |
9da8dc8c | 87 | return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceSet', $params, $defaults); |
6a488035 TO |
88 | } |
89 | ||
90 | /** | |
100fef9d | 91 | * Update the is_active flag in the db |
6a488035 | 92 | * |
6c8f6e67 EM |
93 | * @param int $id id of the database record |
94 | * @param $isActive | |
95 | * | |
96 | * @internal param bool $is_active value we want to set the is_active field | |
6a488035 TO |
97 | * |
98 | * @return Object DAO object on sucess, null otherwise | |
99 | * @static | |
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 | * | |
e0c1764e | 111 | * @return array $defaultPriceSet default price set |
6a488035 | 112 | * |
6a488035 TO |
113 | * @static |
114 | * | |
115 | */ | |
116 | public static function getDefaultPriceSet($entity = 'contribution') { | |
71ba92c4 PN |
117 | if (!empty(self::$_defaultPriceSet[$entity])) { |
118 | return self::$_defaultPriceSet[$entity]; | |
119 | } | |
e0c1764e EM |
120 | $entityName = 'default_contribution_amount'; |
121 | if ($entity == 'membership') { | |
6a488035 TO |
122 | $entityName = 'default_membership_type_amount'; |
123 | } | |
124 | ||
125 | $sql = " | |
82cc6775 | 126 | 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 |
127 | FROM civicrm_price_set ps |
128 | LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id | |
129 | LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id | |
130 | WHERE ps.name = '{$entityName}' | |
131 | "; | |
132 | ||
133 | $dao = CRM_Core_DAO::executeQuery($sql); | |
71ba92c4 | 134 | self::$_defaultPriceSet[$entity] = array(); |
6a488035 | 135 | while ($dao->fetch()) { |
71ba92c4 PN |
136 | self::$_defaultPriceSet[$entity][$dao->priceFieldValueID] = array( |
137 | 'setID' => $dao->setID, | |
138 | 'priceFieldID' => $dao->priceFieldID, | |
139 | 'name' => $dao->name, | |
140 | 'label' => $dao->label, | |
141 | 'priceFieldValueID' => $dao->priceFieldValueID, | |
142 | 'membership_type_id' => $dao->membership_type_id, | |
143 | 'amount' => $dao->amount, | |
144 | 'financial_type_id' => $dao->financial_type_id, | |
145 | ); | |
6a488035 TO |
146 | } |
147 | ||
71ba92c4 | 148 | return self::$_defaultPriceSet[$entity]; |
6a488035 TO |
149 | } |
150 | ||
151 | /** | |
152 | * Get the price set title. | |
153 | * | |
154 | * @param int $id id of price set | |
155 | * | |
156 | * @return string title | |
157 | * | |
6a488035 TO |
158 | * @static |
159 | * | |
160 | */ | |
161 | public static function getTitle($id) { | |
9da8dc8c | 162 | return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $id, 'title'); |
6a488035 TO |
163 | } |
164 | ||
165 | /** | |
166 | * Return a list of all forms which use this price set. | |
167 | * | |
da6b46f4 | 168 | * @param int $id id of price set |
e0c1764e | 169 | * @param bool|string $simpleReturn - get raw data. Possible values: 'entity', 'table' |
6a488035 TO |
170 | * |
171 | * @return array | |
172 | */ | |
173 | public static function &getUsedBy($id, $simpleReturn = FALSE) { | |
174 | $usedBy = $forms = $tables = array(); | |
175 | $queryString = " | |
176 | SELECT entity_table, entity_id | |
177 | FROM civicrm_price_set_entity | |
178 | WHERE price_set_id = %1"; | |
179 | $params = array(1 => array($id, 'Integer')); | |
180 | $crmFormDAO = CRM_Core_DAO::executeQuery($queryString, $params); | |
181 | ||
182 | while ($crmFormDAO->fetch()) { | |
183 | $forms[$crmFormDAO->entity_table][] = $crmFormDAO->entity_id; | |
184 | $tables[] = $crmFormDAO->entity_table; | |
185 | } | |
186 | // Return only tables | |
187 | if ($simpleReturn == 'table') { | |
188 | return $tables; | |
189 | } | |
190 | if (empty($forms)) { | |
191 | $queryString = " | |
192 | SELECT cli.entity_table, cli.entity_id | |
193 | FROM civicrm_line_item cli | |
194 | LEFT JOIN civicrm_price_field cpf ON cli.price_field_id = cpf.id | |
195 | WHERE cpf.price_set_id = %1"; | |
196 | $params = array(1 => array($id, 'Integer')); | |
197 | $crmFormDAO = CRM_Core_DAO::executeQuery($queryString, $params); | |
198 | while ($crmFormDAO->fetch()) { | |
199 | $forms[$crmFormDAO->entity_table][] = $crmFormDAO->entity_id; | |
200 | $tables[] = $crmFormDAO->entity_table; | |
201 | } | |
202 | if (empty($forms)) { | |
203 | return $usedBy; | |
204 | } | |
205 | } | |
206 | // Return only entity data | |
207 | if ($simpleReturn == 'entity') { | |
208 | return $forms; | |
209 | } | |
210 | foreach ($forms as $table => $entities) { | |
211 | switch ($table) { | |
212 | case 'civicrm_event': | |
213 | $ids = implode(',', $entities); | |
c34e4bb4 | 214 | $queryString = "SELECT ce.id as id, ce.title as title, ce.is_public as isPublic, ce.start_date as startDate, ce.end_date as endDate, civicrm_option_value.label as eventType, ce.is_template as isTemplate, ce.template_title as templateTitle |
6a488035 TO |
215 | FROM civicrm_event ce |
216 | LEFT JOIN civicrm_option_value ON | |
217 | ( ce.event_type_id = civicrm_option_value.value ) | |
218 | LEFT JOIN civicrm_option_group ON | |
219 | ( civicrm_option_group.id = civicrm_option_value.option_group_id ) | |
220 | WHERE | |
221 | civicrm_option_group.name = 'event_type' AND | |
6a488035 TO |
222 | ce.id IN ($ids) AND |
223 | ce.is_active = 1;"; | |
224 | $crmDAO = CRM_Core_DAO::executeQuery($queryString); | |
225 | while ($crmDAO->fetch()) { | |
c34e4bb4 PJ |
226 | if ($crmDAO->isTemplate) { |
227 | $usedBy['civicrm_event_template'][$crmDAO->id]['title'] = $crmDAO->templateTitle; | |
228 | $usedBy['civicrm_event_template'][$crmDAO->id]['eventType'] = $crmDAO->eventType; | |
229 | $usedBy['civicrm_event_template'][$crmDAO->id]['isPublic'] = $crmDAO->isPublic; | |
230 | } | |
231 | else { | |
232 | $usedBy[$table][$crmDAO->id]['title'] = $crmDAO->title; | |
233 | $usedBy[$table][$crmDAO->id]['eventType'] = $crmDAO->eventType; | |
234 | $usedBy[$table][$crmDAO->id]['startDate'] = $crmDAO->startDate; | |
235 | $usedBy[$table][$crmDAO->id]['endDate'] = $crmDAO->endDate; | |
236 | $usedBy[$table][$crmDAO->id]['isPublic'] = $crmDAO->isPublic; | |
237 | } | |
6a488035 TO |
238 | } |
239 | break; | |
240 | ||
241 | case 'civicrm_contribution_page': | |
242 | $ids = implode(',', $entities); | |
243 | $queryString = "SELECT cp.id as id, cp.title as title, cp.start_date as startDate, cp.end_date as endDate,ct.name as type | |
244 | FROM civicrm_contribution_page cp, civicrm_financial_type ct | |
cde484fd | 245 | WHERE ct.id = cp.financial_type_id AND |
6a488035 TO |
246 | cp.id IN ($ids) AND |
247 | cp.is_active = 1;"; | |
248 | $crmDAO = CRM_Core_DAO::executeQuery($queryString); | |
249 | while ($crmDAO->fetch()) { | |
250 | $usedBy[$table][$crmDAO->id]['title'] = $crmDAO->title; | |
251 | $usedBy[$table][$crmDAO->id]['type'] = $crmDAO->type; | |
252 | $usedBy[$table][$crmDAO->id]['startDate'] = $crmDAO->startDate; | |
253 | $usedBy[$table][$crmDAO->id]['endDate'] = $crmDAO->endDate; | |
254 | } | |
255 | break; | |
256 | ||
257 | case 'civicrm_contribution': | |
258 | case 'civicrm_membership': | |
259 | case 'civicrm_participant': | |
260 | $usedBy[$table] = 1; | |
261 | break; | |
262 | ||
263 | default: | |
264 | CRM_Core_Error::fatal("$table is not supported in PriceSet::usedBy()"); | |
265 | break; | |
266 | } | |
267 | } | |
268 | ||
269 | return $usedBy; | |
270 | } | |
271 | ||
272 | /** | |
273 | * Delete the price set | |
274 | * | |
275 | * @param int $id Price Set id | |
276 | * | |
277 | * @return boolean false if fields exist for this set, true if the | |
278 | * set could be deleted | |
279 | * | |
6a488035 TO |
280 | * @static |
281 | */ | |
282 | public static function deleteSet($id) { | |
283 | // remove from all inactive forms | |
284 | $usedBy = self::getUsedBy($id); | |
285 | if (isset($usedBy['civicrm_event'])) { | |
286 | foreach ($usedBy['civicrm_event'] as $eventId => $unused) { | |
287 | $eventDAO = new CRM_Event_DAO_Event(); | |
288 | $eventDAO->id = $eventId; | |
289 | $eventDAO->find(); | |
290 | while ($eventDAO->fetch()) { | |
291 | self::removeFrom('civicrm_event', $eventDAO->id); | |
292 | } | |
293 | } | |
294 | } | |
295 | ||
296 | // delete price fields | |
9da8dc8c | 297 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
298 | $priceField->price_set_id = $id; |
299 | $priceField->find(); | |
300 | while ($priceField->fetch()) { | |
301 | // delete options first | |
9da8dc8c | 302 | CRM_Price_BAO_PriceField::deleteField($priceField->id); |
6a488035 TO |
303 | } |
304 | ||
9da8dc8c | 305 | $set = new CRM_Price_DAO_PriceSet(); |
6a488035 TO |
306 | $set->id = $id; |
307 | return $set->delete(); | |
308 | } | |
309 | ||
310 | /** | |
311 | * Link the price set with the specified table and id | |
312 | * | |
313 | * @param string $entityTable | |
314 | * @param integer $entityId | |
315 | * @param integer $priceSetId | |
316 | * | |
317 | * @return bool | |
318 | */ | |
319 | public static function addTo($entityTable, $entityId, $priceSetId) { | |
320 | // verify that the price set exists | |
9da8dc8c | 321 | $dao = new CRM_Price_DAO_PriceSet(); |
6a488035 TO |
322 | $dao->id = $priceSetId; |
323 | if (!$dao->find()) { | |
324 | return FALSE; | |
325 | } | |
326 | unset($dao); | |
327 | ||
9da8dc8c | 328 | $dao = new CRM_Price_DAO_PriceSetEntity(); |
6a488035 TO |
329 | // find if this already exists |
330 | $dao->entity_id = $entityId; | |
331 | $dao->entity_table = $entityTable; | |
332 | $dao->find(TRUE); | |
333 | ||
334 | // add or update price_set_id | |
335 | $dao->price_set_id = $priceSetId; | |
336 | return $dao->save(); | |
337 | } | |
338 | ||
339 | /** | |
340 | * Delete price set for the given entity and id | |
341 | * | |
342 | * @param string $entityTable | |
343 | * @param integer $entityId | |
77b97be7 EM |
344 | * |
345 | * @return mixed | |
6a488035 TO |
346 | */ |
347 | public static function removeFrom($entityTable, $entityId) { | |
9da8dc8c | 348 | $dao = new CRM_Price_DAO_PriceSetEntity(); |
6a488035 TO |
349 | $dao->entity_table = $entityTable; |
350 | $dao->entity_id = $entityId; | |
351 | return $dao->delete(); | |
352 | } | |
353 | ||
354 | /** | |
355 | * Find a price_set_id associatied with the given table, id and usedFor | |
356 | * Used For value for events:1, contribution:2, membership:3 | |
357 | * | |
358 | * @param string $entityTable | |
77b97be7 EM |
359 | * @param int $entityId |
360 | * @param int $usedFor ( price set that extends/used for particular component ) | |
361 | * | |
362 | * @param null $isQuickConfig | |
363 | * @param null $setName | |
6a488035 TO |
364 | * |
365 | * @return integer|false price_set_id, or false if none found | |
366 | */ | |
367 | public static function getFor($entityTable, $entityId, $usedFor = NULL, $isQuickConfig = NULL, &$setName = NULL) { | |
368 | if (!$entityTable || !$entityId) { | |
369 | return FALSE; | |
370 | } | |
371 | ||
372 | $sql = 'SELECT ps.id as price_set_id, ps.name as price_set_name | |
373 | FROM civicrm_price_set ps | |
374 | INNER JOIN civicrm_price_set_entity pse ON ps.id = pse.price_set_id | |
375 | WHERE pse.entity_table = %1 AND pse.entity_id = %2 '; | |
376 | if ($isQuickConfig) { | |
377 | $sql .= ' AND ps.is_quick_config = 0 '; | |
378 | } | |
379 | $params = array(1 => array($entityTable, 'String'), | |
380 | 2 => array($entityId, 'Integer'), | |
381 | ); | |
382 | if ($usedFor) { | |
383 | $sql .= " AND ps.extends LIKE '%%3%' "; | |
384 | $params[3] = array($usedFor, 'Integer'); | |
385 | } | |
386 | ||
387 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
388 | $dao->fetch(); | |
389 | $setName = (isset($dao->price_set_name)) ? $dao->price_set_name : FALSE; | |
390 | return (isset($dao->price_set_id)) ? $dao->price_set_id : FALSE; | |
391 | } | |
392 | ||
393 | /** | |
e0c1764e | 394 | * Find a price_set_id associated with the given option value or field ID |
6a488035 TO |
395 | * |
396 | * @param array $params (reference) an assoc array of name/value pairs | |
397 | * array may contain either option id or | |
398 | * price field id | |
399 | * | |
e0c1764e | 400 | * @return integer|NULL price set id on success, null otherwise |
6a488035 | 401 | * @static |
6a488035 TO |
402 | */ |
403 | public static function getSetId(&$params) { | |
404 | $fid = NULL; | |
405 | ||
406 | if ($oid = CRM_Utils_Array::value('oid', $params)) { | |
9da8dc8c | 407 | $fieldValue = new CRM_Price_DAO_PriceFieldValue(); |
6a488035 TO |
408 | $fieldValue->id = $oid; |
409 | if ($fieldValue->find(TRUE)) { | |
410 | $fid = $fieldValue->price_field_id; | |
411 | } | |
412 | } | |
413 | else { | |
414 | $fid = CRM_Utils_Array::value('fid', $params); | |
415 | } | |
416 | ||
417 | if (isset($fid)) { | |
9da8dc8c | 418 | return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'price_set_id'); |
6a488035 TO |
419 | } |
420 | ||
421 | return NULL; | |
422 | } | |
423 | ||
424 | /** | |
425 | * Return an associative array of all price sets | |
426 | * | |
77b97be7 EM |
427 | * @param bool $withInactive whether or not to include inactive entries |
428 | * @param bool|string $extendComponentName name of the component like 'CiviEvent','CiviContribute' | |
6a488035 TO |
429 | * |
430 | * @return array associative array of id => name | |
431 | */ | |
432 | public static function getAssoc($withInactive = FALSE, $extendComponentName = FALSE) { | |
433 | $query = ' | |
434 | SELECT | |
435 | DISTINCT ( price_set_id ) as id, title | |
436 | FROM | |
437 | civicrm_price_field, | |
438 | civicrm_price_set | |
439 | WHERE | |
440 | civicrm_price_set.id = civicrm_price_field.price_set_id AND is_quick_config = 0 '; | |
441 | ||
442 | if (!$withInactive) { | |
443 | $query .= ' AND civicrm_price_set.is_active = 1 '; | |
444 | } | |
445 | ||
446 | if (self::eventPriceSetDomainID()) { | |
447 | $query .= ' AND civicrm_price_set.domain_id = ' . CRM_Core_Config::domainID(); | |
448 | } | |
449 | ||
450 | $priceSets = array(); | |
451 | ||
452 | if ($extendComponentName) { | |
453 | $componentId = CRM_Core_Component::getComponentID($extendComponentName); | |
454 | if (!$componentId) { | |
455 | return $priceSets; | |
456 | } | |
457 | $query .= " AND civicrm_price_set.extends LIKE '%$componentId%' "; | |
458 | } | |
459 | ||
460 | $dao = CRM_Core_DAO::executeQuery($query); | |
461 | while ($dao->fetch()) { | |
462 | $priceSets[$dao->id] = $dao->title; | |
463 | } | |
464 | return $priceSets; | |
465 | } | |
466 | ||
467 | /** | |
468 | * Get price set details | |
469 | * | |
470 | * An array containing price set details (including price fields) is returned | |
471 | * | |
100fef9d | 472 | * @param int $setID |
2a6da8d7 EM |
473 | * @param bool $required |
474 | * @param bool $validOnly | |
475 | * | |
476 | * @internal param int $setId - price set id whose details are needed | |
6a488035 TO |
477 | * |
478 | * @return array $setTree - array consisting of field details | |
479 | */ | |
480 | public static function getSetDetail($setID, $required = TRUE, $validOnly = FALSE) { | |
481 | // create a new tree | |
482 | $setTree = array(); | |
6a488035 TO |
483 | |
484 | $priceFields = array( | |
485 | 'id', | |
486 | 'name', | |
487 | 'label', | |
488 | 'html_type', | |
489 | 'is_enter_qty', | |
490 | 'help_pre', | |
491 | 'help_post', | |
492 | 'weight', | |
493 | 'is_display_amounts', | |
494 | 'options_per_line', | |
495 | 'is_active', | |
496 | 'active_on', | |
497 | 'expire_on', | |
498 | 'javascript', | |
499 | 'visibility_id', | |
500 | 'is_required', | |
501 | ); | |
502 | if ($required == TRUE) { | |
503 | $priceFields[] = 'is_required'; | |
504 | } | |
505 | ||
506 | // create select | |
507 | $select = 'SELECT ' . implode(',', $priceFields); | |
508 | $from = ' FROM civicrm_price_field'; | |
509 | ||
510 | $params = array(); | |
511 | $params[1] = array($setID, 'Integer'); | |
512 | $where = ' | |
513 | WHERE price_set_id = %1 | |
514 | AND is_active = 1 | |
515 | '; | |
516 | $dateSelect = ''; | |
517 | if ($validOnly) { | |
518 | $currentTime = date('YmdHis'); | |
519 | $dateSelect = " | |
520 | AND ( active_on IS NULL OR active_on <= {$currentTime} ) | |
521 | AND ( expire_on IS NULL OR expire_on >= {$currentTime} ) | |
522 | "; | |
523 | } | |
524 | ||
525 | $orderBy = ' ORDER BY weight'; | |
526 | ||
527 | $sql = $select . $from . $where . $dateSelect . $orderBy; | |
528 | ||
529 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
530 | ||
531 | $visibility = CRM_Core_PseudoConstant::visibility('name'); | |
532 | while ($dao->fetch()) { | |
533 | $fieldID = $dao->id; | |
534 | ||
535 | $setTree[$setID]['fields'][$fieldID] = array(); | |
536 | $setTree[$setID]['fields'][$fieldID]['id'] = $fieldID; | |
537 | ||
538 | foreach ($priceFields as $field) { | |
539 | if ($field == 'id' || is_null($dao->$field)) { | |
540 | continue; | |
541 | } | |
542 | ||
543 | if ($field == 'visibility_id') { | |
544 | $setTree[$setID]['fields'][$fieldID]['visibility'] = $visibility[$dao->$field]; | |
545 | } | |
546 | $setTree[$setID]['fields'][$fieldID][$field] = $dao->$field; | |
547 | } | |
9da8dc8c | 548 | $setTree[$setID]['fields'][$fieldID]['options'] = CRM_Price_BAO_PriceField::getOptions($fieldID, FALSE); |
6a488035 TO |
549 | } |
550 | ||
551 | // also get the pre and post help from this price set | |
552 | $sql = " | |
553 | SELECT extends, financial_type_id, help_pre, help_post, is_quick_config | |
554 | FROM civicrm_price_set | |
555 | WHERE id = %1"; | |
556 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
557 | if ($dao->fetch()) { | |
558 | $setTree[$setID]['extends'] = $dao->extends; | |
157b21d8 | 559 | $setTree[$setID]['financial_type_id'] = $dao->financial_type_id; |
6a488035 TO |
560 | $setTree[$setID]['help_pre'] = $dao->help_pre; |
561 | $setTree[$setID]['help_post'] = $dao->help_post; | |
562 | $setTree[$setID]['is_quick_config'] = $dao->is_quick_config; | |
563 | } | |
564 | return $setTree; | |
565 | } | |
566 | ||
85020e43 EM |
567 | /** |
568 | * Get the Price Field ID. We call this function when more than one being present would represent an error | |
569 | * starting format derived from current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)) | |
570 | * @param array $priceSet | |
571 | * | |
572 | * @throws CRM_Core_Exception | |
573 | * @return int | |
574 | */ | |
00be9182 | 575 | public static function getOnlyPriceFieldID(array $priceSet) { |
85020e43 EM |
576 | if(count($priceSet['fields']) > 1) { |
577 | throw new CRM_Core_Exception(ts('expected only one price field to be in priceset but multiple are present')); | |
578 | } | |
579 | return (int) implode('_', array_keys($priceSet['fields'])); | |
580 | } | |
581 | ||
582 | /** | |
583 | * Get the Price Field Value ID. We call this function when more than one being present would represent an error | |
584 | * current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)) | |
585 | * @param array $priceSet | |
586 | * | |
587 | * @throws CRM_Core_Exception | |
588 | * @return int | |
589 | */ | |
00be9182 | 590 | public static function getOnlyPriceFieldValueID(array $priceSet) { |
85020e43 EM |
591 | $priceFieldID = self::getOnlyPriceFieldID($priceSet); |
592 | if(count($priceSet['fields'][$priceFieldID]['options']) > 1) { | |
593 | throw new CRM_Core_Exception(ts('expected only one price field to be in priceset but multiple are present')); | |
594 | } | |
595 | return (int) implode('_', array_keys($priceSet['fields'][$priceFieldID]['options'])); | |
596 | } | |
597 | ||
598 | ||
ffd93213 | 599 | /** |
e0c1764e | 600 | * @param CRM_Core_Form $form |
100fef9d | 601 | * @param int $id |
ffd93213 EM |
602 | * @param string $entityTable |
603 | * @param bool $validOnly | |
100fef9d | 604 | * @param int $priceSetId |
ffd93213 EM |
605 | * |
606 | * @return bool|false|int|null | |
607 | */ | |
00be9182 | 608 | public static function initSet(&$form, $id, $entityTable = 'civicrm_event', $validOnly = FALSE, $priceSetId = NULL) { |
6a488035 TO |
609 | if (!$priceSetId) { |
610 | $priceSetId = self::getFor($entityTable, $id); | |
611 | } | |
612 | ||
613 | //check if priceset is is_config | |
614 | if (is_numeric($priceSetId)) { | |
9da8dc8c | 615 | if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config') && $form->getVar('_name') != 'Participant') { |
6a488035 TO |
616 | $form->assign('quickConfig', 1); |
617 | } | |
618 | } | |
619 | // get price info | |
620 | if ($priceSetId) { | |
621 | if ($form->_action & CRM_Core_Action::UPDATE) { | |
622 | $entityId = $entity = NULL; | |
623 | ||
624 | switch ($entityTable) { | |
625 | case 'civicrm_event': | |
626 | $entity = 'participant'; | |
627 | if (CRM_Utils_System::getClassName($form) == 'CRM_Event_Form_Participant') { | |
628 | $entityId = $form->_id; | |
629 | } | |
630 | else { | |
631 | $entityId = $form->_participantId; | |
632 | } | |
633 | break; | |
634 | ||
635 | case 'civicrm_contribution_page': | |
636 | case 'civicrm_contribution': | |
637 | $entity = 'contribution'; | |
638 | $entityId = $form->_id; | |
639 | break; | |
640 | } | |
641 | ||
642 | if ($entityId && $entity) { | |
643 | $form->_values['line_items'] = CRM_Price_BAO_LineItem::getLineItems($entityId, $entity); | |
644 | } | |
645 | $required = FALSE; | |
646 | } | |
647 | else { | |
648 | $required = TRUE; | |
649 | } | |
650 | ||
651 | $form->_priceSetId = $priceSetId; | |
652 | $priceSet = self::getSetDetail($priceSetId, $required, $validOnly); | |
653 | $form->_priceSet = CRM_Utils_Array::value($priceSetId, $priceSet); | |
654 | $form->_values['fee'] = CRM_Utils_Array::value('fields', $form->_priceSet); | |
655 | ||
656 | //get the price set fields participant count. | |
657 | if ($entityTable == 'civicrm_event') { | |
658 | //get option count info. | |
659 | $form->_priceSet['optionsCountTotal'] = self::getPricesetCount($priceSetId); | |
660 | if ($form->_priceSet['optionsCountTotal']) { | |
661 | $optionsCountDeails = array(); | |
662 | if (!empty($form->_priceSet['fields'])) { | |
663 | foreach ($form->_priceSet['fields'] as $field) { | |
664 | foreach ($field['options'] as $option) { | |
665 | $count = CRM_Utils_Array::value('count', $option, 0); | |
666 | $optionsCountDeails['fields'][$field['id']]['options'][$option['id']] = $count; | |
667 | } | |
668 | } | |
669 | } | |
670 | $form->_priceSet['optionsCountDetails'] = $optionsCountDeails; | |
671 | } | |
672 | ||
673 | //get option max value info. | |
674 | $optionsMaxValueTotal = 0; | |
675 | $optionsMaxValueDetails = array(); | |
676 | ||
677 | if (!empty($form->_priceSet['fields'])) { | |
678 | foreach ($form->_priceSet['fields'] as $field) { | |
679 | foreach ($field['options'] as $option) { | |
680 | $maxVal = CRM_Utils_Array::value('max_value', $option, 0); | |
681 | $optionsMaxValueDetails['fields'][$field['id']]['options'][$option['id']] = $maxVal; | |
682 | $optionsMaxValueTotal += $maxVal; | |
683 | } | |
684 | } | |
685 | } | |
686 | ||
687 | $form->_priceSet['optionsMaxValueTotal'] = $optionsMaxValueTotal; | |
688 | if ($optionsMaxValueTotal) { | |
689 | $form->_priceSet['optionsMaxValueDetails'] = $optionsMaxValueDetails; | |
690 | } | |
691 | } | |
692 | $form->set('priceSetId', $form->_priceSetId); | |
693 | $form->set('priceSet', $form->_priceSet); | |
694 | ||
695 | return $priceSetId; | |
696 | } | |
697 | return FALSE; | |
698 | } | |
699 | ||
ffd93213 EM |
700 | /** |
701 | * @param $fields | |
c490a46a | 702 | * @param array $params |
ffd93213 EM |
703 | * @param $lineItem |
704 | * @param string $component | |
705 | */ | |
00be9182 | 706 | public static function processAmount(&$fields, &$params, &$lineItem, $component = '') { |
6a488035 | 707 | // using price set |
dc428161 | 708 | $totalPrice = $totalTax = 0; |
6a488035 TO |
709 | $radioLevel = $checkboxLevel = $selectLevel = $textLevel = array(); |
710 | if ($component) { | |
711 | $autoRenew = array(); | |
712 | $autoRenew[0] = $autoRenew[1] = $autoRenew[2] = 0; | |
713 | } | |
714 | foreach ($fields as $id => $field) { | |
a7488080 | 715 | if (empty($params["price_{$id}"]) || |
6a488035 TO |
716 | (empty($params["price_{$id}"]) && $params["price_{$id}"] == NULL) |
717 | ) { | |
718 | // skip if nothing was submitted for this field | |
719 | continue; | |
720 | } | |
721 | ||
722 | switch ($field['html_type']) { | |
723 | case 'Text': | |
be45c8b4 EM |
724 | $firstOption = reset($field['options']); |
725 | $params["price_{$id}"] = array($firstOption['id'] => $params["price_{$id}"]); | |
6a488035 | 726 | CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); |
dc428161 | 727 | if (CRM_Utils_Array::value('tax_rate', $field['options'][key($field['options'])])) { |
728 | $lineItem = self::setLineItem($field, $lineItem, key($field['options'])); | |
729 | $totalTax += $field['options'][key($field['options'])]['tax_amount'] * $lineItem[key($field['options'])]['qty']; | |
730 | } | |
049db839 | 731 | if (CRM_Utils_Array::value('name', $field['options'][key($field['options'])]) == 'contribution_amount') { |
732 | $taxRates = CRM_Core_PseudoConstant::getTaxRates(); | |
733 | if (array_key_exists($params['financial_type_id'], $taxRates)) { | |
734 | $field['options'][key($field['options'])]['tax_rate'] = $taxRates[$params['financial_type_id']]; | |
735 | $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($field['options'][key($field['options'])]['amount'], $field['options'][key($field['options'])]['tax_rate']); | |
cce6ec9f | 736 | $field['options'][key($field['options'])]['tax_amount'] = round($taxAmount['tax_amount'], 2); |
049db839 | 737 | $lineItem = self::setLineItem($field, $lineItem, key($field['options'])); |
738 | $totalTax += $field['options'][key($field['options'])]['tax_amount'] * $lineItem[key($field['options'])]['qty']; | |
739 | } | |
740 | } | |
3b5db8ce | 741 | $totalPrice += $lineItem[$firstOption['id']]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[key($field['options'])]); |
6a488035 TO |
742 | break; |
743 | ||
744 | case 'Radio': | |
745 | //special case if user select -none- | |
746 | if ($params["price_{$id}"] <= 0) { | |
747 | continue; | |
748 | } | |
749 | $params["price_{$id}"] = array($params["price_{$id}"] => 1); | |
750 | $optionValueId = CRM_Utils_Array::key(1, $params["price_{$id}"]); | |
751 | $optionLabel = CRM_Utils_Array::value('label', $field['options'][$optionValueId]); | |
752 | $params['amount_priceset_level_radio'] = array(); | |
753 | $params['amount_priceset_level_radio'][$optionValueId] = $optionLabel; | |
754 | if (isset($radioLevel)) { | |
755 | $radioLevel = array_merge($radioLevel, | |
756 | array_keys($params['amount_priceset_level_radio']) | |
757 | ); | |
758 | } | |
759 | else { | |
760 | $radioLevel = array_keys($params['amount_priceset_level_radio']); | |
761 | } | |
762 | CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); | |
dc428161 | 763 | if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) { |
764 | $lineItem = self::setLineItem($field, $lineItem, $optionValueId); | |
765 | $totalTax += $field['options'][$optionValueId]['tax_amount']; | |
c40e1ff4 | 766 | if (CRM_Utils_Array::value('field_title', $lineItem[$optionValueId]) == 'Membership Amount') { |
767 | $lineItem[$optionValueId]['line_total'] = $lineItem[$optionValueId]['unit_price'] = CRM_Utils_Rule::cleanMoney($lineItem[$optionValueId]['line_total'] - $lineItem[$optionValueId]['tax_amount']); | |
768 | } | |
dc428161 | 769 | } |
c40e1ff4 | 770 | $totalPrice += $lineItem[$optionValueId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionValueId]); |
cde484fd DL |
771 | if ( |
772 | $component && | |
a6c4ca20 | 773 | // auto_renew exists and is empty in some workflows, which php treat as a 0 |
28156120 | 774 | // and hence we explicitly check to see if auto_renew is numeric |
cde484fd DL |
775 | isset($lineItem[$optionValueId]['auto_renew']) && |
776 | is_numeric($lineItem[$optionValueId]['auto_renew']) | |
777 | ) { | |
6a488035 TO |
778 | $autoRenew[$lineItem[$optionValueId]['auto_renew']] += $lineItem[$optionValueId]['line_total']; |
779 | } | |
780 | break; | |
781 | ||
782 | case 'Select': | |
783 | $params["price_{$id}"] = array($params["price_{$id}"] => 1); | |
784 | $optionValueId = CRM_Utils_Array::key(1, $params["price_{$id}"]); | |
785 | $optionLabel = $field['options'][$optionValueId]['label']; | |
786 | $params['amount_priceset_level_select'] = array(); | |
787 | $params['amount_priceset_level_select'][CRM_Utils_Array::key(1, $params["price_{$id}"])] = $optionLabel; | |
788 | if (isset($selectLevel)) { | |
789 | $selectLevel = array_merge($selectLevel, array_keys($params['amount_priceset_level_select'])); | |
790 | } | |
791 | else { | |
792 | $selectLevel = array_keys($params['amount_priceset_level_select']); | |
793 | } | |
794 | CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); | |
dc428161 | 795 | if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) { |
796 | $lineItem = self::setLineItem($field, $lineItem, $optionValueId); | |
797 | $totalTax += $field['options'][$optionValueId]['tax_amount']; | |
798 | } | |
c40e1ff4 | 799 | $totalPrice += $lineItem[$optionValueId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionValueId]); |
cde484fd DL |
800 | if ( |
801 | $component && | |
802 | isset($lineItem[$optionValueId]['auto_renew']) && | |
803 | is_numeric($lineItem[$optionValueId]['auto_renew']) | |
804 | ) { | |
6a488035 TO |
805 | $autoRenew[$lineItem[$optionValueId]['auto_renew']] += $lineItem[$optionValueId]['line_total']; |
806 | } | |
807 | break; | |
808 | ||
809 | case 'CheckBox': | |
810 | $params['amount_priceset_level_checkbox'] = $optionIds = array(); | |
811 | foreach ($params["price_{$id}"] as $optionId => $option) { | |
812 | $optionIds[] = $optionId; | |
813 | $optionLabel = $field['options'][$optionId]['label']; | |
814 | $params['amount_priceset_level_checkbox']["{$field['options'][$optionId]['id']}"] = $optionLabel; | |
815 | if (isset($checkboxLevel)) { | |
816 | $checkboxLevel = array_unique(array_merge( | |
817 | $checkboxLevel, | |
818 | array_keys($params['amount_priceset_level_checkbox']) | |
819 | ) | |
820 | ); | |
821 | } | |
822 | else { | |
823 | $checkboxLevel = array_keys($params['amount_priceset_level_checkbox']); | |
824 | } | |
825 | } | |
826 | CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); | |
827 | foreach ($optionIds as $optionId) { | |
dc428161 | 828 | if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionId])) { |
829 | $lineItem = self::setLineItem($field, $lineItem, $optionId); | |
830 | $totalTax += $field['options'][$optionId]['tax_amount']; | |
831 | } | |
c40e1ff4 | 832 | $totalPrice += $lineItem[$optionId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionId]); |
cde484fd DL |
833 | if ( |
834 | $component && | |
835 | isset($lineItem[$optionId]['auto_renew']) && | |
836 | is_numeric($lineItem[$optionId]['auto_renew']) | |
837 | ) { | |
6a488035 TO |
838 | $autoRenew[$lineItem[$optionId]['auto_renew']] += $lineItem[$optionId]['line_total']; |
839 | } | |
840 | } | |
841 | break; | |
842 | } | |
843 | } | |
844 | ||
845 | $amount_level = array(); | |
846 | $totalParticipant = 0; | |
847 | if (is_array($lineItem)) { | |
848 | foreach ($lineItem as $values) { | |
849 | $totalParticipant += $values['participant_count']; | |
850 | if ($values['html_type'] == 'Text') { | |
851 | $amount_level[] = $values['label'] . ' - ' . $values['qty']; | |
852 | continue; | |
853 | } | |
854 | $amount_level[] = $values['label']; | |
855 | } | |
856 | } | |
857 | ||
858 | $displayParticipantCount = ''; | |
859 | if ($totalParticipant > 0) { | |
860 | $displayParticipantCount = ' Participant Count -' . $totalParticipant; | |
861 | } | |
862 | $params['amount_level'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amount_level) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR; | |
863 | $params['amount'] = $totalPrice; | |
dc428161 | 864 | $params['tax_amount'] = $totalTax; |
6a488035 TO |
865 | if ($component) { |
866 | foreach ($autoRenew as $dontCare => $eachAmount) { | |
867 | if (!$eachAmount) { | |
cde484fd | 868 | unset($autoRenew[$dontCare]); |
6a488035 TO |
869 | } |
870 | } | |
871 | if (count($autoRenew) > 1 ) { | |
872 | $params['autoRenew'] = $autoRenew; | |
873 | } | |
874 | } | |
875 | } | |
876 | ||
877 | /** | |
100fef9d | 878 | * Build the price set form. |
6a488035 | 879 | * |
e0c1764e | 880 | * @param CRM_Core_Form $form |
dd244018 | 881 | * |
355ba699 | 882 | * @return void |
6a488035 | 883 | */ |
00be9182 | 884 | public static function buildPriceSet(&$form) { |
6a488035 | 885 | $priceSetId = $form->get('priceSetId'); |
6a488035 TO |
886 | if (!$priceSetId) { |
887 | return; | |
888 | } | |
889 | ||
890 | $validFieldsOnly = TRUE; | |
891 | $className = CRM_Utils_System::getClassName($form); | |
892 | if (in_array($className, array( | |
893 | 'CRM_Contribute_Form_Contribution', 'CRM_Member_Form_Membership'))) { | |
894 | $validFieldsOnly = FALSE; | |
895 | } | |
896 | ||
897 | $priceSet = self::getSetDetail($priceSetId, TRUE, $validFieldsOnly); | |
898 | $form->_priceSet = CRM_Utils_Array::value($priceSetId, $priceSet); | |
883e4763 | 899 | $validPriceFieldIds = array_keys($form->_priceSet['fields']); |
6a488035 | 900 | $form->_quickConfig = $quickConfig = 0; |
9da8dc8c | 901 | if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config')) { |
6a488035 TO |
902 | $quickConfig = 1; |
903 | } | |
904 | ||
905 | $form->assign('quickConfig', $quickConfig); | |
906 | if ($className == 'CRM_Contribute_Form_Contribution_Main') { | |
907 | $form->_quickConfig = $quickConfig; | |
908 | } | |
909 | $form->assign('priceSet', $form->_priceSet); | |
910 | ||
911 | $component = 'contribution'; | |
912 | if ($className == 'CRM_Member_Form_Membership') { | |
913 | $component = 'membership'; | |
914 | } | |
915 | ||
916 | if ($className == 'CRM_Contribute_Form_Contribution_Main') { | |
917 | $feeBlock = &$form->_values['fee']; | |
918 | if (!empty($form->_useForMember)) { | |
919 | $component = 'membership'; | |
920 | } | |
921 | } | |
922 | else { | |
923 | $feeBlock = &$form->_priceSet['fields']; | |
924 | } | |
925 | ||
926 | // call the hook. | |
927 | CRM_Utils_Hook::buildAmount($component, $form, $feeBlock); | |
928 | ||
c7b3d063 DG |
929 | // CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions |
930 | $adminFieldVisible = false; | |
931 | if (CRM_Core_Permission::check('administer CiviCRM')) { | |
77b97be7 | 932 | $adminFieldVisible = true; |
c7b3d063 | 933 | } |
77b97be7 | 934 | |
883e4763 | 935 | foreach ($feeBlock as $id => $field) { |
6a488035 | 936 | if (CRM_Utils_Array::value('visibility', $field) == 'public' || |
c7b3d063 | 937 | (CRM_Utils_Array::value('visibility', $field) == 'admin' && $adminFieldVisible == true) || |
6a488035 TO |
938 | !$validFieldsOnly |
939 | ) { | |
940 | $options = CRM_Utils_Array::value('options', $field); | |
941 | if ($className == 'CRM_Contribute_Form_Contribution_Main' && $component = 'membership') { | |
cc509891 | 942 | $userid = $form->getVar('_membershipContactID'); |
6a488035 TO |
943 | $checklifetime = self::checkCurrentMembership($options, $userid); |
944 | if ($checklifetime) { | |
945 | $form->assign('ispricelifetime', TRUE); | |
946 | } | |
947 | } | |
883e4763 | 948 | if (!is_array($options) || !in_array($id, $validPriceFieldIds)) { |
6a488035 TO |
949 | continue; |
950 | } | |
9da8dc8c | 951 | CRM_Price_BAO_PriceField::addQuickFormElement($form, |
6a488035 TO |
952 | 'price_' . $field['id'], |
953 | $field['id'], | |
954 | FALSE, | |
955 | CRM_Utils_Array::value('is_required', $field, FALSE), | |
956 | NULL, | |
957 | $options | |
958 | ); | |
959 | } | |
960 | } | |
961 | } | |
962 | ||
963 | /** | |
100fef9d | 964 | * Check the current Membership |
6a488035 TO |
965 | * having end date null. |
966 | */ | |
00be9182 | 967 | public static function checkCurrentMembership(&$options, $userid) { |
6a488035 TO |
968 | if (!$userid || empty($options)) { |
969 | return; | |
970 | } | |
971 | static $_contact_memberships = array(); | |
972 | $checklifetime = FALSE; | |
973 | foreach ($options as $key => $value) { | |
a7488080 | 974 | if (!empty($value['membership_type_id'])) { |
6a488035 TO |
975 | if (!isset($_contact_memberships[$userid][$value['membership_type_id']])) { |
976 | $_contact_memberships[$userid][$value['membership_type_id']] = CRM_Member_BAO_Membership::getContactMembership($userid, $value['membership_type_id'], FALSE); | |
977 | } | |
978 | $currentMembership = $_contact_memberships[$userid][$value['membership_type_id']]; | |
8cc574cf | 979 | if (!empty($currentMembership) && empty($currentMembership['end_date'])) { |
6a488035 TO |
980 | unset($options[$key]); |
981 | $checklifetime = TRUE; | |
982 | } | |
983 | } | |
984 | } | |
985 | if ($checklifetime) { | |
986 | return TRUE; | |
987 | } | |
988 | else { | |
989 | return FALSE; | |
990 | } | |
991 | } | |
992 | ||
993 | /** | |
100fef9d | 994 | * Set daefult the price set fields. |
6a488035 | 995 | * |
c490a46a | 996 | * @param CRM_Core_Form $form |
fd31fa4c EM |
997 | * @param $defaults |
998 | * | |
6a488035 | 999 | * @return array $defaults |
6a488035 | 1000 | */ |
00be9182 | 1001 | public static function setDefaultPriceSet(&$form, &$defaults) { |
6a488035 TO |
1002 | if (!isset($form->_priceSet) || empty($form->_priceSet['fields'])) { |
1003 | return $defaults; | |
1004 | } | |
1005 | ||
1006 | foreach ($form->_priceSet['fields'] as $key => $val) { | |
1007 | foreach ($val['options'] as $keys => $values) { | |
1008 | if ($values['is_default']) { | |
1009 | if ($val['html_type'] == 'CheckBox') { | |
1010 | $defaults["price_{$key}"][$keys] = 1; | |
1011 | } | |
1012 | else { | |
1013 | $defaults["price_{$key}"] = $keys; | |
1014 | } | |
1015 | } | |
1016 | } | |
1017 | } | |
1018 | return $defaults; | |
1019 | } | |
1020 | ||
b2cdd843 EM |
1021 | /** |
1022 | * Supports event create function by setting up required price sets, not tested but expect | |
1023 | * it will work for contribution page | |
1024 | * @param array $params as passed to api/bao create fn | |
1025 | * @param CRM_Core_DAO $entity object for given entity | |
1026 | * @param string $entityName name of entity - e.g event | |
1027 | */ | |
00be9182 | 1028 | public static function setPriceSets(&$params, $entity, $entityName) { |
b2cdd843 EM |
1029 | if(empty($params['price_set_id']) || !is_array($params['price_set_id'])) { |
1030 | return; | |
1031 | } | |
1032 | // CRM-14069 note that we may as well start by assuming more than one. | |
1033 | // currently the form does not pass in as an array & will be skipped | |
1034 | // test is passing in as an array but I feel the api should have a metadata that allows | |
1035 | // transform of single to array - seems good for managing transitions - in which case all api | |
1036 | // calls that set price_set_id will hit this | |
1037 | // e.g in getfields 'price_set_id' => array('blah', 'bao_type' => 'array') - causing | |
1038 | // all separated values, strings, json half-separated values (in participant we hit this) | |
1039 | // to be converted to json @ api layer | |
1040 | $pse = new CRM_Price_DAO_PriceSetEntity(); | |
1041 | $pse->entity_table = 'civicrm_' . $entityName; | |
1042 | $pse->entity_id = $entity->id; | |
1043 | while ($pse->fetch()) { | |
1044 | if(!in_array($pse->price_set_id, $params['price_set_id'])) { | |
1045 | // note an even more aggressive form of this deletion currently happens in event form | |
1046 | // past price sets discounts are made inaccessible by this as the discount_id is set to NULL | |
1047 | // on the participant record | |
1048 | if (CRM_Price_BAO_PriceSet::removeFrom('civicrm_' . $entityName, $entity->id)) { | |
07102c2c | 1049 | CRM_Core_BAO_Discount::del($entity->id,'civicrm_' . $entityName); |
b2cdd843 EM |
1050 | } |
1051 | } | |
1052 | } | |
1053 | foreach ($params['price_set_id'] as $priceSetID) { | |
1054 | CRM_Price_BAO_PriceSet::addTo('civicrm_' . $entityName, $entity->id, $priceSetID); | |
1055 | //@todo - how should we do this - copied from form | |
1056 | //if (CRM_Utils_Array::value('price_field_id', $params)) { | |
1057 | // $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id'); | |
1058 | // CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID, 0); | |
1059 | //} | |
1060 | } | |
1061 | } | |
6a488035 TO |
1062 | /** |
1063 | * Get field ids of a price set | |
1064 | * | |
e0c1764e | 1065 | * @param int $id Price Set id |
6a488035 TO |
1066 | * |
1067 | * @return array of the field ids | |
1068 | * | |
6a488035 TO |
1069 | * @static |
1070 | */ | |
1071 | public static function getFieldIds($id) { | |
9da8dc8c | 1072 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
1073 | $priceField->price_set_id = $id; |
1074 | $priceField->find(); | |
1075 | while ($priceField->fetch()) { | |
1076 | $var[] = $priceField->id; | |
1077 | } | |
1078 | return $var; | |
1079 | } | |
1080 | ||
1081 | /** | |
1082 | * This function is to make a copy of a price set, including | |
1083 | * all the fields | |
1084 | * | |
1085 | * @param int $id the price set id to copy | |
1086 | * | |
1087 | * @return the copy object | |
6a488035 TO |
1088 | * @static |
1089 | */ | |
00be9182 | 1090 | public static function copy($id) { |
6a488035 TO |
1091 | $maxId = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_price_set"); |
1092 | ||
1093 | $title = ts('[Copy id %1]', array(1 => $maxId + 1)); | |
1094 | $fieldsFix = array( | |
1095 | 'suffix' => array('title' => ' ' . $title, | |
1096 | 'name' => '__Copy_id_' . ($maxId + 1) . '_', | |
1097 | ), | |
1098 | ); | |
1099 | ||
9da8dc8c | 1100 | $copy = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceSet', |
6a488035 TO |
1101 | array('id' => $id), |
1102 | NULL, | |
1103 | $fieldsFix | |
1104 | ); | |
1105 | ||
1106 | //copying all the blocks pertaining to the price set | |
9da8dc8c | 1107 | $copyPriceField = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceField', |
6a488035 TO |
1108 | array('price_set_id' => $id), |
1109 | array('price_set_id' => $copy->id) | |
1110 | ); | |
1111 | if (!empty($copyPriceField)) { | |
1112 | $price = array_combine(self::getFieldIds($id), self::getFieldIds($copy->id)); | |
1113 | ||
1114 | //copy option group and values | |
1115 | foreach ($price as $originalId => $copyId) { | |
9da8dc8c | 1116 | CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceFieldValue', |
6a488035 TO |
1117 | array('price_field_id' => $originalId), |
1118 | array('price_field_id' => $copyId) | |
1119 | ); | |
1120 | } | |
1121 | } | |
1122 | $copy->save(); | |
1123 | ||
1124 | CRM_Utils_Hook::copy('Set', $copy); | |
1125 | return $copy; | |
1126 | } | |
1127 | ||
1128 | /** | |
1129 | * This function is to check price set permission | |
1130 | * | |
1131 | * @param int $sid the price set id | |
77b97be7 EM |
1132 | * |
1133 | * @return bool | |
6a488035 | 1134 | */ |
00be9182 | 1135 | public static function checkPermission($sid) { |
44c8822b | 1136 | if ($sid && self::eventPriceSetDomainID()) { |
9da8dc8c | 1137 | $domain_id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $sid, 'domain_id', 'id'); |
6a488035 | 1138 | if (CRM_Core_Config::domainID() != $domain_id) { |
0499b0ad | 1139 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
1140 | } |
1141 | } | |
1142 | return TRUE; | |
1143 | } | |
1144 | ||
1145 | /** | |
1146 | * Get the sum of participant count | |
1147 | * for all fields of given price set. | |
1148 | * | |
1149 | * @param int $sid the price set id | |
1150 | * | |
77b97be7 EM |
1151 | * @param bool $onlyActive |
1152 | * | |
1153 | * @return int|null|string | |
6a488035 TO |
1154 | * @static |
1155 | */ | |
1156 | public static function getPricesetCount($sid, $onlyActive = TRUE) { | |
1157 | $count = 0; | |
1158 | if (!$sid) { | |
1159 | return $count; | |
1160 | } | |
1161 | ||
1162 | $where = NULL; | |
1163 | if ($onlyActive) { | |
1164 | $where = 'AND value.is_active = 1 AND field.is_active = 1'; | |
1165 | } | |
1166 | ||
1167 | static $pricesetFieldCount = array(); | |
1168 | if (!isset($pricesetFieldCount[$sid])) { | |
1169 | $sql = " | |
1170 | SELECT sum(value.count) as totalCount | |
1171 | FROM civicrm_price_field_value value | |
1172 | INNER JOIN civicrm_price_field field ON ( field.id = value.price_field_id ) | |
1173 | INNER JOIN civicrm_price_set pset ON ( pset.id = field.price_set_id ) | |
1174 | WHERE pset.id = %1 | |
1175 | $where"; | |
1176 | ||
1177 | $count = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($sid, 'Positive'))); | |
1178 | $pricesetFieldCount[$sid] = ($count) ? $count : 0; | |
1179 | } | |
1180 | ||
1181 | return $pricesetFieldCount[$sid]; | |
1182 | } | |
1183 | ||
ffd93213 EM |
1184 | /** |
1185 | * @param $ids | |
1186 | * | |
1187 | * @return array | |
1188 | */ | |
6a488035 TO |
1189 | public static function getMembershipCount($ids) { |
1190 | $queryString = " | |
1191 | SELECT count( pfv.id ) AS count, pfv.id AS id | |
1192 | FROM civicrm_price_field_value pfv | |
1193 | INNER JOIN civicrm_membership_type mt ON mt.id = pfv.membership_type_id | |
1194 | WHERE pfv.id IN ( $ids ) | |
1195 | GROUP BY mt.member_of_contact_id"; | |
1196 | ||
1197 | $crmDAO = CRM_Core_DAO::executeQuery($queryString); | |
1198 | $count = array(); | |
1199 | ||
1200 | while ($crmDAO->fetch()) { | |
1201 | $count[$crmDAO->id] = $crmDAO->count; | |
1202 | } | |
1203 | ||
1204 | return $count; | |
1205 | } | |
1206 | ||
1207 | /** | |
100fef9d | 1208 | * Check if auto renew option should be shown |
6a488035 TO |
1209 | * |
1210 | * @param int $priceSetId price set id | |
1211 | * | |
1212 | * @return int $autoRenewOption ( 0:hide, 1:optional 2:required ) | |
1213 | */ | |
1214 | public static function checkAutoRenewForPriceSet($priceSetId) { | |
1215 | // auto-renew option should be visible if membership types associated with all the fields has | |
1216 | // been set for auto-renew option | |
1217 | // Auto renew checkbox should be frozen if for all the membership type auto renew is required | |
1218 | ||
1219 | // get the membership type auto renew option and check if required or optional | |
1220 | $query = 'SELECT mt.auto_renew, mt.duration_interval, mt.duration_unit | |
1221 | FROM civicrm_price_field_value pfv | |
1222 | INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id | |
1223 | INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id | |
1224 | WHERE pf.price_set_id = %1 | |
1225 | AND pf.is_active = 1 | |
1226 | AND pfv.is_active = 1'; | |
1227 | ||
1228 | $params = array(1 => array($priceSetId, 'Integer')); | |
1229 | ||
1230 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1231 | $autoRenewOption = 2; | |
1232 | $interval = $unit = array(); | |
1233 | while ($dao->fetch()) { | |
1234 | if (!$dao->auto_renew) { | |
1235 | $autoRenewOption = 0; | |
1236 | break; | |
1237 | } | |
1238 | if ($dao->auto_renew == 1) { | |
1239 | $autoRenewOption = 1; | |
1240 | } | |
1241 | ||
1242 | $interval[$dao->duration_interval] = $dao->duration_interval; | |
1243 | $unit[$dao->duration_unit] = $dao->duration_unit; | |
1244 | } | |
1245 | ||
1246 | if (count($interval) == 1 && count($unit) == 1 && $autoRenewOption > 0) { | |
1247 | return $autoRenewOption; | |
1248 | } | |
1249 | else { | |
1250 | return 0; | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | /** | |
100fef9d | 1255 | * Retrieve auto renew frequency and interval |
6a488035 TO |
1256 | * |
1257 | * @param int $priceSetId price set id | |
1258 | * | |
1259 | * @return array associate array of frequency interval and unit | |
1260 | * @static | |
6a488035 TO |
1261 | */ |
1262 | public static function getRecurDetails($priceSetId) { | |
1263 | $query = 'SELECT mt.duration_interval, mt.duration_unit | |
1264 | FROM civicrm_price_field_value pfv | |
1265 | INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id | |
1266 | INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id | |
1267 | WHERE pf.price_set_id = %1 LIMIT 1'; | |
1268 | ||
1269 | $params = array(1 => array($priceSetId, 'Integer')); | |
1270 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1271 | $dao->fetch(); | |
1272 | return array($dao->duration_interval, $dao->duration_unit); | |
1273 | } | |
1274 | ||
ffd93213 EM |
1275 | /** |
1276 | * @return object | |
1277 | */ | |
00be9182 | 1278 | public static function eventPriceSetDomainID() { |
6a488035 TO |
1279 | return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, |
1280 | 'event_price_set_domain_id', | |
1281 | NULL, FALSE | |
1282 | ); | |
1283 | } | |
1284 | ||
1285 | /** | |
100fef9d | 1286 | * Update the is_quick_config flag in the db |
6a488035 TO |
1287 | * |
1288 | * @param int $id id of the database record | |
1289 | * @param boolean $isQuickConfig value we want to set the is_quick_config field | |
1290 | * | |
1291 | * @return Object DAO object on sucess, null otherwise | |
1292 | * @static | |
6a488035 | 1293 | */ |
00be9182 | 1294 | public static function setIsQuickConfig($id, $isQuickConfig) { |
9da8dc8c | 1295 | return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceSet', $id, 'is_quick_config', $isQuickConfig); |
6a488035 TO |
1296 | } |
1297 | ||
1298 | /** | |
cde484fd | 1299 | * Check if price set id provides option for |
6a488035 TO |
1300 | * user to select both auto-renew and non-auto-renew memberships |
1301 | * | |
6a488035 TO |
1302 | * @static |
1303 | * | |
1304 | */ | |
1305 | public static function checkMembershipPriceSet($id) { | |
cde484fd | 1306 | $query = |
6a488035 TO |
1307 | " |
1308 | SELECT pfv.id, pfv.price_field_id, pfv.name, pfv.membership_type_id, pf.html_type, mt.auto_renew | |
1309 | FROM civicrm_price_field_value pfv | |
1310 | LEFT JOIN civicrm_price_field pf ON pf.id = pfv.price_field_id | |
1311 | LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id | |
1312 | LEFT JOIN civicrm_membership_type mt ON mt.id = pfv.membership_type_id | |
1313 | WHERE ps.id = %1 | |
1314 | "; | |
1315 | ||
1316 | $params = array(1 => array($id, 'Integer')); | |
1317 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1318 | ||
1319 | $autoRenew = array(); | |
1320 | //FIXME: do a comprehensive check of whether | |
1321 | //2 membership types can be selected | |
1322 | //instead of comparing all of them | |
1323 | while ($dao->fetch()) { | |
cde484fd | 1324 | //temp fix for #CRM-10370 |
6a488035 TO |
1325 | //if its NULL consider it '0' i.e. 'No auto-renew option' |
1326 | $daoAutoRenew = $dao->auto_renew; | |
1327 | if ($daoAutoRenew === NULL) { | |
1328 | $daoAutoRenew = 0; | |
1329 | } | |
1330 | if (!empty($autoRenew) && !in_array($daoAutoRenew, $autoRenew)) { | |
1331 | return true; | |
1332 | } | |
1333 | $autoRenew[] = $daoAutoRenew; | |
1334 | } | |
1335 | return false; | |
1336 | } | |
1337 | ||
e0c1764e | 1338 | /** |
c6914066 PN |
1339 | * Copy priceSet when event/contibution page is copied |
1340 | * | |
c490a46a CW |
1341 | * @param string $baoName BAO name |
1342 | * @param int $id old event/contribution page id | |
1343 | * @param int $newId newly created event/contribution page id | |
c6914066 | 1344 | */ |
00be9182 | 1345 | public static function copyPriceSet($baoName, $id, $newId) { |
9da8dc8c | 1346 | $priceSetId = CRM_Price_BAO_PriceSet::getFor($baoName, $id); |
c6914066 | 1347 | if ($priceSetId) { |
9da8dc8c | 1348 | $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config'); |
c6914066 | 1349 | if($isQuickConfig) { |
9da8dc8c | 1350 | $copyPriceSet = &CRM_Price_BAO_PriceSet::copy($priceSetId); |
1351 | CRM_Price_BAO_PriceSet::addTo($baoName, $newId, $copyPriceSet->id); | |
44c8822b | 1352 | } |
c6914066 | 1353 | else { |
9da8dc8c | 1354 | $copyPriceSet = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceSetEntity', |
c6914066 PN |
1355 | array( |
1356 | 'entity_id' => $id, | |
1357 | 'entity_table' => $baoName, | |
1358 | ), | |
1359 | array('entity_id' => $newId) | |
1360 | ); | |
1361 | } | |
1362 | // copy event discount | |
1363 | if ($baoName == 'civicrm_event') { | |
1364 | $discount = CRM_Core_BAO_Discount::getOptionGroup($id, 'civicrm_event'); | |
1365 | foreach ($discount as $discountId => $setId) { | |
1366 | ||
9da8dc8c | 1367 | $copyPriceSet = &CRM_Price_BAO_PriceSet::copy($setId); |
44c8822b | 1368 | |
28156120 | 1369 | CRM_Core_DAO::copyGeneric( |
c6914066 PN |
1370 | 'CRM_Core_DAO_Discount', |
1371 | array( | |
1372 | 'id' => $discountId, | |
1373 | ), | |
1374 | array( | |
1375 | 'entity_id' => $newId, | |
1376 | 'price_set_id' => $copyPriceSet->id, | |
1377 | ) | |
1378 | ); | |
1379 | } | |
1380 | } | |
1381 | } | |
1382 | } | |
dc428161 | 1383 | |
1384 | /* | |
1385 | * Function to set tax_amount and tax_rate in LineItem | |
1386 | * | |
1387 | * | |
1388 | */ | |
00be9182 | 1389 | public static function setLineItem($field, $lineItem, $optionValueId) { |
dc428161 | 1390 | if ($field['html_type'] == 'Text') { |
1391 | $taxAmount = $field['options'][$optionValueId]['tax_amount'] * $lineItem[$optionValueId]['qty']; | |
1392 | } | |
1393 | else { | |
1394 | $taxAmount = $field['options'][$optionValueId]['tax_amount']; | |
1395 | } | |
1396 | $taxRate = $field['options'][$optionValueId]['tax_rate']; | |
dc428161 | 1397 | $lineItem[$optionValueId]['tax_amount'] = $taxAmount; |
1398 | $lineItem[$optionValueId]['tax_rate'] = $taxRate; | |
1399 | ||
1400 | return $lineItem; | |
1401 | } | |
6a488035 | 1402 | } |