Merge pull request #17008 from ivan-compucorp/CPS-70-fix-radio-value
[civicrm-core.git] / CRM / Core / BAO / Discount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19 class CRM_Core_BAO_Discount extends CRM_Core_DAO_Discount {
20
21 /**
22 * Class constructor.
23 */
24 public function __construct() {
25 parent::__construct();
26 }
27
28 /**
29 * Delete the discount.
30 *
31 * @param int $entityId
32 * @param string $entityTable
33 *
34 * @return bool
35 */
36 public static function del($entityId, $entityTable) {
37 // delete all discount records with the selected discounted id
38 $discount = new CRM_Core_DAO_Discount();
39 $discount->entity_id = $entityId;
40 $discount->entity_table = $entityTable;
41 if ($discount->delete()) {
42 return TRUE;
43 }
44 return FALSE;
45 }
46
47 /**
48 *
49 * The function extracts all the params it needs to create a
50 * discount object. the params array contains additional unused name/value
51 * pairs
52 *
53 * @param array $params
54 * (reference) an assoc array of name/value pairs.
55 *
56 * @return object
57 * CRM_Core_DAO_Discount object on success, otherwise null
58 */
59 public static function add(&$params) {
60 $discount = new CRM_Core_DAO_Discount();
61 $discount->copyValues($params);
62 $discount->save();
63 return $discount;
64 }
65
66 /**
67 * Determine whether the given table/id
68 * has discount associated with it
69 *
70 * @param int $entityId
71 * Entity id to be searched.
72 * @param string $entityTable
73 * Entity table to be searched.
74 *
75 * @return array
76 * option group Ids associated with discount
77 */
78 public static function getOptionGroup($entityId, $entityTable) {
79 $optionGroupIDs = [];
80 $dao = new CRM_Core_DAO_Discount();
81 $dao->entity_id = $entityId;
82 $dao->entity_table = $entityTable;
83 $dao->find();
84 while ($dao->fetch()) {
85 $optionGroupIDs[$dao->id] = $dao->price_set_id;
86 }
87 return $optionGroupIDs;
88 }
89
90 /**
91 * Determine in which discount set the registration date falls.
92 *
93 * @param int $entityID
94 * Entity id to be searched.
95 * @param string $entityTable
96 * Entity table to be searched.
97 *
98 * @return int
99 * $dao->id discount id of the set which matches
100 * the date criteria
101 * @throws CRM_Core_Exception
102 */
103 public static function findSet($entityID, $entityTable) {
104 if (empty($entityID) || empty($entityTable)) {
105 // adding this here, to trap errors if values are not sent
106 throw new CRM_Core_Exception('Invalid parameters passed to findSet function');
107 return NULL;
108 }
109
110 $dao = new CRM_Core_DAO_Discount();
111 $dao->entity_id = $entityID;
112 $dao->entity_table = $entityTable;
113 $dao->find();
114
115 while ($dao->fetch()) {
116 $endDate = $dao->end_date;
117 // if end date is not we consider current date as end date
118 if (!$endDate) {
119 $endDate = date('Ymd');
120 }
121 $falls = CRM_Utils_Date::getRange($dao->start_date, $endDate);
122 if ($falls == TRUE) {
123 return $dao->id;
124 }
125 }
126 return FALSE;
127 }
128
129 }