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