Skip to content

Commit

Permalink
remove 'samecostcenter' and 'samecostcenterforcredits' form logic (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
semteacher committed Nov 6, 2024
1 parent 4ad65b4 commit 1aeccd2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 49 deletions.
10 changes: 4 additions & 6 deletions classes/form/modal_creditsmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
14 changes: 4 additions & 10 deletions classes/local/cartstore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 3 additions & 5 deletions classes/local/rebookings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 1 addition & 7 deletions classes/output/shoppingcart_history_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
18 changes: 8 additions & 10 deletions classes/shopping_cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
17 changes: 6 additions & 11 deletions classes/shopping_cart_credits.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 = '';
}
Expand Down

0 comments on commit 1aeccd2

Please sign in to comment.