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