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