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