MagicMerge - Per-request cache of path/url properties
[civicrm-core.git] / CRM / Core / BAO / Discount.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Core_BAO_Discount extends CRM_Core_DAO_Discount {
36
37 /**
fe482240 38 * Class constructor.
6a488035 39 */
00be9182 40 public function __construct() {
6a488035
TO
41 parent::__construct();
42 }
43
44 /**
fe482240 45 * Delete the discount.
6a488035 46 *
c490a46a
CW
47 * @param int $entityId
48 * @param string $entityTable
6a488035 49 *
5c766a0b 50 * @return bool
6a488035 51 */
2aa397bc 52 public static function del($entityId, $entityTable) {
6a488035 53 // delete all discount records with the selected discounted id
481a74f4 54 $discount = new CRM_Core_DAO_Discount();
353ffa53 55 $discount->entity_id = $entityId;
6a488035
TO
56 $discount->entity_table = $entityTable;
57 if ($discount->delete()) {
58 return TRUE;
59 }
60 return FALSE;
61 }
62
63 /**
64 *
65 * The function extracts all the params it needs to create a
66 * discount object. the params array contains additional unused name/value
67 * pairs
68 *
6a0b768e
TO
69 * @param array $params
70 * (reference) an assoc array of name/value pairs.
6a488035 71 *
a6c01b45
CW
72 * @return object
73 * CRM_Core_DAO_Discount object on success, otherwise null
6a488035 74 */
00be9182 75 public static function add(&$params) {
481a74f4 76 $discount = new CRM_Core_DAO_Discount();
6a488035
TO
77 $discount->copyValues($params);
78 $discount->save();
79 return $discount;
80 }
81
82 /**
83 * Determine whether the given table/id
84 * has discount associated with it
85 *
a1a2a83d 86 * @param int $entityId
6a0b768e
TO
87 * Entity id to be searched.
88 * @param string $entityTable
89 * Entity table to be searched.
6a488035 90 *
a6c01b45
CW
91 * @return array
92 * option group Ids associated with discount
6a488035 93 */
00be9182 94 public static function getOptionGroup($entityId, $entityTable) {
353ffa53 95 $optionGroupIDs = array();
481a74f4 96 $dao = new CRM_Core_DAO_Discount();
353ffa53 97 $dao->entity_id = $entityId;
6a488035
TO
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 /**
fe482240 107 * Determine in which discount set the registration date falls.
6a488035 108 *
6a0b768e
TO
109 * @param int $entityID
110 * Entity id to be searched.
111 * @param string $entityTable
112 * Entity table to be searched.
6a488035 113 *
df8d3074 114 * @return int
a6c01b45 115 * $dao->id discount id of the set which matches
6a488035
TO
116 * the date criteria
117 */
00be9182 118 public static function findSet($entityID, $entityTable) {
c490a46a 119 if (empty($entityID) || empty($entityTable)) {
6a488035
TO
120 // adding this here, to trap errors if values are not sent
121 CRM_Core_Error::fatal();
122 return NULL;
123 }
124
481a74f4 125 $dao = new CRM_Core_DAO_Discount();
353ffa53 126 $dao->entity_id = $entityID;
6a488035
TO
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}