Merge pull request #23742 from eileenmcnaughton/import_remove
[civicrm-core.git] / CRM / Core / BAO / Discount.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Core_BAO_Discount extends CRM_Core_DAO_Discount {
18
6a488035 19 /**
fe482240 20 * Delete the discount.
6a488035 21 *
c490a46a
CW
22 * @param int $entityId
23 * @param string $entityTable
6a488035 24 *
5c766a0b 25 * @return bool
6a488035 26 */
2aa397bc 27 public static function del($entityId, $entityTable) {
6a488035 28 // delete all discount records with the selected discounted id
481a74f4 29 $discount = new CRM_Core_DAO_Discount();
353ffa53 30 $discount->entity_id = $entityId;
6a488035
TO
31 $discount->entity_table = $entityTable;
32 if ($discount->delete()) {
33 return TRUE;
34 }
35 return FALSE;
36 }
37
38 /**
39 *
40 * The function extracts all the params it needs to create a
41 * discount object. the params array contains additional unused name/value
42 * pairs
43 *
6a0b768e
TO
44 * @param array $params
45 * (reference) an assoc array of name/value pairs.
6a488035 46 *
a6c01b45
CW
47 * @return object
48 * CRM_Core_DAO_Discount object on success, otherwise null
6a488035 49 */
00be9182 50 public static function add(&$params) {
481a74f4 51 $discount = new CRM_Core_DAO_Discount();
6a488035
TO
52 $discount->copyValues($params);
53 $discount->save();
54 return $discount;
55 }
56
57 /**
58 * Determine whether the given table/id
59 * has discount associated with it
60 *
a1a2a83d 61 * @param int $entityId
6a0b768e
TO
62 * Entity id to be searched.
63 * @param string $entityTable
64 * Entity table to be searched.
6a488035 65 *
a6c01b45
CW
66 * @return array
67 * option group Ids associated with discount
6a488035 68 */
00be9182 69 public static function getOptionGroup($entityId, $entityTable) {
be2fb01f 70 $optionGroupIDs = [];
481a74f4 71 $dao = new CRM_Core_DAO_Discount();
353ffa53 72 $dao->entity_id = $entityId;
6a488035
TO
73 $dao->entity_table = $entityTable;
74 $dao->find();
75 while ($dao->fetch()) {
76 $optionGroupIDs[$dao->id] = $dao->price_set_id;
77 }
78 return $optionGroupIDs;
79 }
80
81 /**
fe482240 82 * Determine in which discount set the registration date falls.
6a488035 83 *
6a0b768e
TO
84 * @param int $entityID
85 * Entity id to be searched.
86 * @param string $entityTable
87 * Entity table to be searched.
6a488035 88 *
df8d3074 89 * @return int
a6c01b45 90 * $dao->id discount id of the set which matches
6a488035 91 * the date criteria
ac15829d 92 * @throws CRM_Core_Exception
6a488035 93 */
00be9182 94 public static function findSet($entityID, $entityTable) {
c490a46a 95 if (empty($entityID) || empty($entityTable)) {
6a488035 96 // adding this here, to trap errors if values are not sent
ac15829d 97 throw new CRM_Core_Exception('Invalid parameters passed to findSet function');
6a488035
TO
98 return NULL;
99 }
100
481a74f4 101 $dao = new CRM_Core_DAO_Discount();
353ffa53 102 $dao->entity_id = $entityID;
6a488035
TO
103 $dao->entity_table = $entityTable;
104 $dao->find();
105
106 while ($dao->fetch()) {
107 $endDate = $dao->end_date;
108 // if end date is not we consider current date as end date
109 if (!$endDate) {
110 $endDate = date('Ymd');
111 }
112 $falls = CRM_Utils_Date::getRange($dao->start_date, $endDate);
113 if ($falls == TRUE) {
114 return $dao->id;
115 }
116 }
117 return FALSE;
118 }
119
120}