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