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