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