From 1aeccd231fd356992890d9c98c134419123b151c Mon Sep 17 00:00:00 2001 From: Andrii Semenets Date: Sat, 19 Oct 2024 20:16:59 +0300 Subject: [PATCH] remove 'samecostcenter' and 'samecostcenterforcredits' form logic (#86) --- classes/form/modal_creditsmanager.php | 10 ++++------ classes/local/cartstore.php | 14 ++++---------- classes/local/rebookings.php | 8 +++----- classes/output/shoppingcart_history_list.php | 8 +------- classes/shopping_cart.php | 18 ++++++++---------- classes/shopping_cart_credits.php | 17 ++++++----------- 6 files changed, 26 insertions(+), 49 deletions(-) diff --git a/classes/form/modal_creditsmanager.php b/classes/form/modal_creditsmanager.php index 605860e..4386586 100644 --- a/classes/form/modal_creditsmanager.php +++ b/classes/form/modal_creditsmanager.php @@ -87,13 +87,11 @@ public function definition() { $mform->addHelpButton('creditsmanagercredits', 'creditsmanagercredits', 'local_shopping_cart'); $mform->hideIf('creditsmanagercredits', 'creditsmanagermode', 'neq', 1); - if (!empty(get_config('local_shopping_cart', 'samecostcenterforcredits'))) { - $mform->addElement('text', 'creditsmanagercreditscostcenter', + $mform->addElement('text', 'creditsmanagercreditscostcenter', get_string('creditsmanagercreditscostcenter', 'local_shopping_cart')); - $mform->setDefault('creditsmanagercreditscostcenter', ''); - $mform->addHelpButton('creditsmanagercreditscostcenter', 'creditsmanagercreditscostcenter', 'local_shopping_cart'); - $mform->hideIf('creditsmanagercreditscostcenter', 'creditsmanagermode', 'eq', 0); - } + $mform->setDefault('creditsmanagercreditscostcenter', ''); + $mform->addHelpButton('creditsmanagercreditscostcenter', 'creditsmanagercreditscostcenter', 'local_shopping_cart'); + $mform->hideIf('creditsmanagercreditscostcenter', 'creditsmanagermode', 'eq', 0); $paymentmethods = [ 0 => get_string('choose...', 'local_shopping_cart'), diff --git a/classes/local/cartstore.php b/classes/local/cartstore.php index dda4e1c..601cb5e 100644 --- a/classes/local/cartstore.php +++ b/classes/local/cartstore.php @@ -101,16 +101,10 @@ public function add_item(cartitem $item) { // When we add the first item, we need to reset credit... // ... because we can only use the one from the correct cost center. - - if ( - get_config('local_shopping_cart', 'samecostcenterforcredits') - && !empty($data['costcenter']) - ) { - [$credit, $currency] = shopping_cart_credits::get_balance($this->userid, $data['costcenter']); - $data['credit'] = $credit; - $data['remainingcredit'] = $credit; - $data['currency'] = $currency; - } + [$credit, $currency] = shopping_cart_credits::get_balance($this->userid, $data['costcenter']); + $data['credit'] = $credit; + $data['remainingcredit'] = $credit; + $data['currency'] = $currency; $this->set_cache($data); diff --git a/classes/local/rebookings.php b/classes/local/rebookings.php index fe81204..892bf39 100644 --- a/classes/local/rebookings.php +++ b/classes/local/rebookings.php @@ -68,11 +68,9 @@ public static function allow_rebooking(stdClass $item, int $userid = 0) { } $cartstore = cartstore::instance($userid); - if (get_config('local_shopping_cart', 'samecostcenter')) { - $currentcostcenter = $item->costcenter ?? ''; - if (!$cartstore->same_costcenter($currentcostcenter)) { - return false; - } + $currentcostcenter = $item->costcenter ?? ''; + if (!$cartstore->same_costcenter($currentcostcenter)) { + return false; } // If the rebooking period is not empty, we check the rest. diff --git a/classes/output/shoppingcart_history_list.php b/classes/output/shoppingcart_history_list.php index 555792e..fc1c5ae 100644 --- a/classes/output/shoppingcart_history_list.php +++ b/classes/output/shoppingcart_history_list.php @@ -262,13 +262,7 @@ public function __construct(int $userid, int $identifier = 0, $fromledger = fals } } - if (!get_config('local_shopping_cart', 'samecostcenterforcredits')) { - $cartstore = cartstore::instance($userid); - $data = $cartstore->get_data(); - $this->credit = $data['credit']; - } else { - $this->costcentercredits = shopping_cart_credits::get_balance_for_all_costcenters($userid); - } + $this->costcentercredits = shopping_cart_credits::get_balance_for_all_costcenters($userid); } diff --git a/classes/shopping_cart.php b/classes/shopping_cart.php index 76f7ce6..33ef114 100644 --- a/classes/shopping_cart.php +++ b/classes/shopping_cart.php @@ -115,21 +115,19 @@ public static function allow_add_item_to_cart( } if ($area == "option" || $area == "rebookitem") { - // If the setting 'samecostcenter' ist turned on... + // Such as 'samecostcenter' is always enforced... // ... then we do not allow to add items with different cost centers. $providerclass = static::get_service_provider_classname($component); $cartitem = component_class_callback($providerclass, 'allow_add_item_to_cart', [$area, $itemid, $userid]); - if (get_config('local_shopping_cart', 'samecostcenter')) { - $currentcostcenter = $cartitem['costcenter'] ?? ''; - - if (!$cartstore->same_costcenter($currentcostcenter)) { - return [ - 'success' => LOCAL_SHOPPING_CART_CARTPARAM_COSTCENTER, - 'itemname' => $cartitem['itemname'] ?? '', - ]; - } + $currentcostcenter = $cartitem['costcenter'] ?? ''; + if (!$cartstore->same_costcenter($currentcostcenter)) { + return [ + 'success' => LOCAL_SHOPPING_CART_CARTPARAM_COSTCENTER, + 'itemname' => $cartitem['itemname'] ?? '', + ]; } + if (get_config('local_shopping_cart', 'allowchooseaccount')) { $searchdata = [ diff --git a/classes/shopping_cart_credits.php b/classes/shopping_cart_credits.php index 5ccf3dc..ae2ecbd 100644 --- a/classes/shopping_cart_credits.php +++ b/classes/shopping_cart_credits.php @@ -52,7 +52,6 @@ public static function get_balance(int $userid, string $costcenter = '', $withem // Just in case, we do not find it in credits table. $currency = get_config('local_shopping_cart', 'globalcurrency') ?? 'EUR'; - $samecostcenterforcredits = get_config('local_shopping_cart', 'samecostcenterforcredits') ?? 0; $currencies = self::credits_get_used_currencies($userid); if (empty($currencies)) { @@ -68,16 +67,12 @@ public static function get_balance(int $userid, string $costcenter = '', $withem ]; $additionalsql = " COALESCE(NULLIF(costcenter, ''), '') = :costcenter "; $params['costcenter'] = $costcenter; - if (!empty($samecostcenterforcredits)) { - $defaultcostcenter = get_config('local_shopping_cart', 'defaultcostcenterforcredits'); - if ( - $withempty - && (empty($defaultcostcente) || ($defaultcostcenter == $costcenter)) - ) { - $defaultcostcentersql = " OR COALESCE(NULLIF(costcenter, ''), '') = '' "; - } else { - $defaultcostcentersql = ''; - } + $defaultcostcenter = get_config('local_shopping_cart', 'defaultcostcenterforcredits'); + if ( + $withempty + && (empty($defaultcostcente) || ($defaultcostcenter == $costcenter)) + ) { + $defaultcostcentersql = " OR COALESCE(NULLIF(costcenter, ''), '') = '' "; } else { $defaultcostcentersql = ''; }