Merge pull request #14367 from MegaphoneJon/financial-58
[civicrm-core.git] / CRM / Price / BAO / PriceFieldValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Business objects for managing price fields values.
36 *
37 */
38 class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
39
40 /**
41 * Insert/update a new entry in the database.
42 *
43 * @param array $params
44 *
45 * @param array $ids
46 * Deprecated variable.
47 *
48 * @return CRM_Price_DAO_PriceFieldValue
49 */
50 public static function add(&$params, $ids = []) {
51 $hook = empty($params['id']) ? 'create' : 'edit';
52 CRM_Utils_Hook::pre($hook, 'PriceFieldValue', CRM_Utils_Array::value('id', $params), $params);
53
54 $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
55 $fieldValueBAO->copyValues($params);
56
57 // CRM-16189
58 $priceFieldID = CRM_Utils_Array::value('price_field_id', $params);
59
60 $id = CRM_Utils_Array::value('id', $ids, CRM_Utils_Array::value('id', $params));
61
62 if (!$priceFieldID) {
63 $priceFieldID = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceFieldValue', $id, 'price_field_id');
64 }
65 if (!empty($params['is_default'])) {
66 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
67 $p = [1 => [$params['price_field_id'], 'Integer']];
68 CRM_Core_DAO::executeQuery($query, $p);
69 }
70
71 $fieldValueBAO->save();
72 CRM_Utils_Hook::post($hook, 'PriceFieldValue', $fieldValueBAO->id, $fieldValueBAO);
73
74 // Reset the cached values in this function.
75 CRM_Price_BAO_PriceField::getOptions(CRM_Utils_Array::value('price_field_id', $params), FALSE, TRUE);
76 return $fieldValueBAO;
77 }
78
79 /**
80 * Creates a new entry in the database.
81 *
82 * @param array $params
83 * (reference), array $ids.
84 *
85 * @param $ids
86 *
87 * @return CRM_Price_DAO_PriceFieldValue
88 *
89 * @throws \CRM_Core_Exception
90 */
91 public static function create(&$params, $ids = []) {
92 $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
93 if (!is_array($params) || empty($params)) {
94 return NULL;
95 }
96 if (!$id && empty($params['name'])) {
97 $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242));
98 }
99
100 if ($id && !empty($params['weight'])) {
101 if (isset($params['name'])) {
102 unset($params['name']);
103 }
104
105 $oldWeight = NULL;
106 if ($id) {
107 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
108 }
109 $fieldValues = ['price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0)];
110 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
111 }
112 else {
113 if (!$id) {
114 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
115 if (empty($params['name'])) {
116 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
117 }
118 }
119 }
120
121 $financialType = CRM_Utils_Array::value('financial_type_id', $params, NULL);
122 if (!$financialType && $id) {
123 $financialType = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'financial_type_id', 'id');
124 }
125 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
126 if (!empty($financialType) && !array_key_exists($financialType, $financialTypes) && $params['is_active']) {
127 throw new CRM_Core_Exception("Financial Type for Price Field Option is either disabled or does not exist");
128 }
129 return self::add($params, $ids);
130 }
131
132 /**
133 * Get defaults for new entity.
134 * @return array
135 */
136 public static function getDefaults() {
137 return [
138 'is_active' => 1,
139 'weight' => 1,
140 ];
141
142 }
143
144 /**
145 * Retrieve DB object based on input parameters.
146 *
147 * It also stores all the retrieved values in the default array.
148 *
149 * @param array $params
150 * (reference ) an assoc array.
151 * @param array $defaults
152 * (reference ) an assoc array to hold the flattened values.
153 *
154 * @return CRM_Price_DAO_PriceFieldValue
155 */
156 public static function retrieve(&$params, &$defaults) {
157 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
158 }
159
160 /**
161 * Retrieve all values for given field id.
162 *
163 * @param int $fieldId
164 * Price_field_id.
165 * @param array $values
166 * (reference ) to hold the values.
167 * @param string $orderBy
168 * For order by, default weight.
169 * @param bool|int $isActive is_active, default false
170 * @param bool $admin is this loading it for use on an admin page.
171 *
172 * @return array
173 *
174 */
175 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE, $admin = FALSE) {
176 $sql = "SELECT cs.id FROM civicrm_price_set cs INNER JOIN civicrm_price_field cp ON cp.price_set_id = cs.id
177 WHERE cs.name IN ('default_contribution_amount', 'default_membership_type_amount') AND cp.id = {$fieldId} ";
178 $setId = CRM_Core_DAO::singleValueQuery($sql);
179 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
180 $fieldValueDAO->price_field_id = $fieldId;
181 $addWhere = '';
182 if (!$setId) {
183 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
184 if (!$admin) {
185 $addWhere = "financial_type_id IN (0)";
186 }
187 if (!empty($financialTypes) && !$admin) {
188 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
189 }
190 if (!empty($addWhere)) {
191 $fieldValueDAO->whereAdd($addWhere);
192 }
193 }
194 $fieldValueDAO->orderBy($orderBy, 'label');
195 if ($isActive) {
196 $fieldValueDAO->is_active = 1;
197 }
198 $fieldValueDAO->find();
199 while ($fieldValueDAO->fetch()) {
200 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
201 }
202
203 return $values;
204 }
205
206 /**
207 * Get the price field option label.
208 *
209 * @param int $id
210 * Id of field option.
211 *
212 * @return string
213 * name
214 *
215 */
216 public static function getOptionLabel($id) {
217 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
218 }
219
220 /**
221 * Update the is_active flag in the db.
222 *
223 * @param int $id
224 * Id of the database record.
225 * @param bool $is_active
226 * Value we want to set the is_active field.
227 *
228 * @return bool
229 * true if we found and updated the object, else false
230 */
231 public static function setIsActive($id, $is_active) {
232 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
233 }
234
235 /**
236 * Delete all values of the given field id.
237 *
238 * @param int $fieldId
239 * Price field id.
240 *
241 *
242 */
243 public static function deleteValues($fieldId) {
244 if (!$fieldId) {
245 return;
246 }
247
248 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
249 $fieldValueDAO->price_field_id = $fieldId;
250 $fieldValueDAO->delete();
251 }
252
253 /**
254 * Delete the value.
255 *
256 * @param int $id
257 * Id.
258 *
259 * @return bool
260 *
261 */
262 public static function del($id) {
263 if (!$id) {
264 return FALSE;
265 }
266
267 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
268 $fieldValueDAO->id = $id;
269 return $fieldValueDAO->delete();
270 }
271
272 /**
273 * Update civicrm_price_field_value.financial_type_id
274 * when financial_type_id of contribution_page or event is changed
275 *
276 * @param int $entityId
277 * Id.
278 * @param string $entityTable table.
279 * Entity table.
280 * @param string $financialTypeID type id.
281 * Financial type id.
282 *
283 */
284 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
285 if (!$entityId || !$entityTable || !$financialTypeID) {
286 return;
287 }
288 $params = [
289 1 => [$entityId, 'Integer'],
290 2 => [$entityTable, 'String'],
291 3 => [$financialTypeID, 'Integer'],
292 ];
293 // for event discount
294 $join = $where = '';
295 if ($entityTable == 'civicrm_event') {
296 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
297 $where = ' OR cd.id IS NOT NULL ';
298 }
299 $sql = "UPDATE civicrm_price_set cps
300 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
301 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
302 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
303 {$join}
304 SET cpfv.financial_type_id = CASE
305 WHEN cpfv.membership_type_id IS NOT NULL
306 THEN cpfv.financial_type_id
307 ELSE %3
308 END,
309 cps.financial_type_id = %3
310 WHERE cpse.id IS NOT NULL {$where}";
311
312 CRM_Core_DAO::executeQuery($sql, $params);
313 }
314
315 }