Merge pull request #23232 from braders/nodefaults-tab-links
[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 */
17 class CRM_Core_BAO_Discount extends CRM_Core_DAO_Discount {
18
19 /**
20 * Delete the discount.
21 *
22 * @param int $entityId
23 * @param string $entityTable
24 *
25 * @return bool
26 */
27 public static function del($entityId, $entityTable) {
28 // delete all discount records with the selected discounted id
29 $discount = new CRM_Core_DAO_Discount();
30 $discount->entity_id = $entityId;
31 $discount->entity_table = $entityTable;
32 if ($discount->delete()) {
33 return TRUE;
34 }
35 return FALSE;
36 }
37
38 /**
39 *
40 * The function extracts all the params it needs to create a
41 * discount object. the params array contains additional unused name/value
42 * pairs
43 *
44 * @param array $params
45 * (reference) an assoc array of name/value pairs.
46 *
47 * @return object
48 * CRM_Core_DAO_Discount object on success, otherwise null
49 */
50 public static function add(&$params) {
51 $discount = new CRM_Core_DAO_Discount();
52 $discount->copyValues($params);
53 $discount->save();
54 return $discount;
55 }
56
57 /**
58 * Determine whether the given table/id
59 * has discount associated with it
60 *
61 * @param int $entityId
62 * Entity id to be searched.
63 * @param string $entityTable
64 * Entity table to be searched.
65 *
66 * @return array
67 * option group Ids associated with discount
68 */
69 public static function getOptionGroup($entityId, $entityTable) {
70 $optionGroupIDs = [];
71 $dao = new CRM_Core_DAO_Discount();
72 $dao->entity_id = $entityId;
73 $dao->entity_table = $entityTable;
74 $dao->find();
75 while ($dao->fetch()) {
76 $optionGroupIDs[$dao->id] = $dao->price_set_id;
77 }
78 return $optionGroupIDs;
79 }
80
81 /**
82 * Determine in which discount set the registration date falls.
83 *
84 * @param int $entityID
85 * Entity id to be searched.
86 * @param string $entityTable
87 * Entity table to be searched.
88 *
89 * @return int
90 * $dao->id discount id of the set which matches
91 * the date criteria
92 * @throws CRM_Core_Exception
93 */
94 public static function findSet($entityID, $entityTable) {
95 if (empty($entityID) || empty($entityTable)) {
96 // adding this here, to trap errors if values are not sent
97 throw new CRM_Core_Exception('Invalid parameters passed to findSet function');
98 return NULL;
99 }
100
101 $dao = new CRM_Core_DAO_Discount();
102 $dao->entity_id = $entityID;
103 $dao->entity_table = $entityTable;
104 $dao->find();
105
106 while ($dao->fetch()) {
107 $endDate = $dao->end_date;
108 // if end date is not we consider current date as end date
109 if (!$endDate) {
110 $endDate = date('Ymd');
111 }
112 $falls = CRM_Utils_Date::getRange($dao->start_date, $endDate);
113 if ($falls == TRUE) {
114 return $dao->id;
115 }
116 }
117 return FALSE;
118 }
119
120 }