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