Skip to content

Commit

Permalink
A new approach to deal with $defaultcostcenter. Working (#86)
Browse files Browse the repository at this point in the history
- If item(s) do not have costcenter defined, chain of costcenters will be: "nocostcenter" -> default costcenter (if set)
- If item(s) have costcenter defined, chain of costcenters will be: "nocostcenter" -> given costcenter
  • Loading branch information
semteacher committed Nov 6, 2024
1 parent 87e6e19 commit aa81afe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
8 changes: 6 additions & 2 deletions classes/local/cartstore.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public function add_item(cartitem $item) {
$data['items'][$cacheitemkey] = $itemdata;
$data['expirationtime'] = $expirationtime;

$data['costcenter'] = !empty($item->costcenter) ? $item->costcenter : ($data['costcenter'] ?? '');
// Use ot item ot default costcenter.
$defaultcostcenter = get_config('local_shopping_cart', 'defaultcostcenterforcredits');
$data['costcenter'] = !empty($item->costcenter) ? $item->costcenter : ($defaultcostcenter ?? '');

// When we add the first item, we need to reset credit...
// ... because we can only use the one from the correct cost center.
Expand Down Expand Up @@ -601,14 +603,16 @@ public function same_costcenter(string $currentcostcenter) {
*/
public function get_costcenter(): string {
$costcenterincart = '';
$defaultcostcenter = get_config('local_shopping_cart', 'defaultcostcenterforcredits');

$items = $this->get_items();
foreach ($items as $itemincart) {
if ($itemincart['area'] == 'bookingfee' || $itemincart['area'] == 'rebookingcredit') {
// We only need to check for "real" items, booking fee does not apply.
continue;
} else {
$costcenterincart = $itemincart['costcenter'] ?? '';
// Use ot item ot default costcenter.
$costcenterincart = !empty($itemincart['costcenter']) ? $itemincart['costcenter'] : ($defaultcostcenter ?? '');
break;
}
}
Expand Down
15 changes: 11 additions & 4 deletions classes/shopping_cart_credits.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,18 @@ public static function get_balance(int $userid, string $costcenter = '', $withem
];
$additionalsql = " COALESCE(NULLIF(costcenter, ''), '') = :costcenter ";
$params['costcenter'] = $costcenter;
$defaultcostcenter = get_config('local_shopping_cart', 'defaultcostcenterforcredits');

$defaultcostcentersql = '';

if ($withempty) {
$defaultcostcentersql = " OR COALESCE(NULLIF(costcenter, ''), '') = '' ";
} else {
$defaultcostcentersql = '';
// Get balance for costcenter without name ("no costcenter").
$defaultcostcentersql .= " OR COALESCE(NULLIF(costcenter, ''), '') = '' ";
// Inclide balance for default costcenter if costcenter not provided explicitly.
$defaultcostcenter = get_config('local_shopping_cart', 'defaultcostcenterforcredits');
if (!empty($defaultcostcenter) && $defaultcostcenter == $costcenter) {
$defaultcostcentersql = " OR COALESCE(NULLIF(costcenter, ''), '') = :defaultcostcenter ";
$params['defaultcostcenter'] = $defaultcostcenter;
}
}

$sql = 'SELECT SUM(balance) AS balance, MAX(currency) as currency
Expand Down

0 comments on commit aa81afe

Please sign in to comment.