Merge pull request #4871 from eileenmcnaughton/CRM-15795
[civicrm-core.git] / CRM / Core / BAO / Discount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Core_BAO_Discount extends CRM_Core_DAO_Discount {
36
37 /**
38 * Class constructor
39 */
40 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Delete the discount
46 *
47 * @param int $entityId
48 * @param string $entityTable
49 *
50 * @return boolean
51 * @static
52 */
53 public static function del($entityId, $entityTable) {
54 // delete all discount records with the selected discounted id
55 $discount = new CRM_Core_DAO_Discount();
56 $discount->entity_id = $entityId;
57 $discount->entity_table = $entityTable;
58 if ($discount->delete()) {
59 return TRUE;
60 }
61 return FALSE;
62 }
63
64 /**
65 *
66 * The function extracts all the params it needs to create a
67 * discount object. the params array contains additional unused name/value
68 * pairs
69 *
70 * @param array $params
71 * (reference) an assoc array of name/value pairs.
72 *
73 * @return object CRM_Core_DAO_Discount object on success, otherwise null
74 * @static
75 */
76 public static function add(&$params) {
77 $discount = new CRM_Core_DAO_Discount();
78 $discount->copyValues($params);
79 $discount->save();
80 return $discount;
81 }
82
83 /**
84 * Determine whether the given table/id
85 * has discount associated with it
86 *
87 * @param int $entityIdEntity id to be searched.
88 * Entity id to be searched.
89 * @param string $entityTable
90 * Entity table to be searched.
91 *
92 * @return array $optionGroupIDs option group Ids associated with discount
93 *
94 */
95 public static function getOptionGroup($entityId, $entityTable) {
96 $optionGroupIDs = array();
97 $dao = new CRM_Core_DAO_Discount();
98 $dao->entity_id = $entityId;
99 $dao->entity_table = $entityTable;
100 $dao->find();
101 while ($dao->fetch()) {
102 $optionGroupIDs[$dao->id] = $dao->price_set_id;
103 }
104 return $optionGroupIDs;
105 }
106
107 /**
108 * Determine in which discount set the registration date falls
109 *
110 * @param int $entityID
111 * Entity id to be searched.
112 * @param string $entityTable
113 * Entity table to be searched.
114 *
115 * @return integer $dao->id discount id of the set which matches
116 * the date criteria
117 */
118 public static function findSet($entityID, $entityTable) {
119 if (empty($entityID) || empty($entityTable)) {
120 // adding this here, to trap errors if values are not sent
121 CRM_Core_Error::fatal();
122 return NULL;
123 }
124
125 $dao = new CRM_Core_DAO_Discount();
126 $dao->entity_id = $entityID;
127 $dao->entity_table = $entityTable;
128 $dao->find();
129
130 while ($dao->fetch()) {
131 $endDate = $dao->end_date;
132 // if end date is not we consider current date as end date
133 if (!$endDate) {
134 $endDate = date('Ymd');
135 }
136 $falls = CRM_Utils_Date::getRange($dao->start_date, $endDate);
137 if ($falls == TRUE) {
138 return $dao->id;
139 }
140 }
141 return FALSE;
142 }
143
144 }