Skip to content

Commit

Permalink
GH-86 Bugfix: fix costcenter treatment in use_credit function
Browse files Browse the repository at this point in the history
  • Loading branch information
georgmaisser committed Sep 28, 2024
1 parent 6535a3a commit 2beb676
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions classes/shopping_cart_credits.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,10 @@ public static function add_credit(
*
* @param int $userid
* @param array $checkoutdata
* @param bool $fallbackonempty
* @return void
*/
public static function use_credit(int $userid, $checkoutdata) {
public static function use_credit(int $userid, $checkoutdata, $fallbackonempty = true) {

global $DB, $USER;

Expand All @@ -332,9 +333,11 @@ public static function use_credit(int $userid, $checkoutdata) {
$balances = self::get_balance_for_all_costcenters($userid);

foreach ($balances as $balance) {
if (empty($balance['costcenter'])) {
$emptycostcenterbalance = $balance['balance'];
continue;
if ($fallbackonempty) {
if (empty($balance['costcenter'])) {
$emptycostcenterbalance = $balance['balance'];
continue;
}
}
if ($balance['costcenter'] == ($checkoutdata['costcenter'] ?? '')) {
$matchingcostcenterbalance = $balance['balance'];
Expand Down Expand Up @@ -423,7 +426,7 @@ public static function credit_paid_back(
) {
global $USER;

[$balance, $currency] = self::get_balance($userid, $costcenter);
[$balance, $currency] = self::get_balance($userid, $costcenter, false);

$data = [];

Expand All @@ -435,7 +438,7 @@ public static function credit_paid_back(
$data['costcenter'] = $costcenter;
}

self::use_credit($userid, $data);
self::use_credit($userid, $data, false);

// Also record this in the ledger table.
$ledgerrecord = new stdClass();
Expand Down

0 comments on commit 2beb676

Please sign in to comment.