Skip to content

Commit

Permalink
Fix bug for debit
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 committed Oct 2, 2024
1 parent 586497e commit 9b80584
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
15 changes: 9 additions & 6 deletions classes/util/balance_op.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,21 @@ protected function cut_from_main($amount) {
$nonrefundable = $this->details['mainnonrefund'];
$free = $this->details['mainfree'] ?? 0;
if ($refundable >= $amount) {
$refundable = $refundable - $amount;
$refundable -= $amount;
} else {
$remain = $amount - $refundable;
$refundable = 0;
if ($remain > $nonrefundable) {
$nonrefundable = 0;
$refundable = $nonrefundable - $remain;
$nonrefundable = 0;
} else {
$nonrefundable = $nonrefundable - $remain;
$nonrefundable -= $remain;
}

$newfree = max($free - $remain, 0);
$this->freecut += $free - $newfree;
}

$this->details['mainrefundable'] = $refundable;
$this->details['mainnonrefund'] = $nonrefundable;
$this->details['mainbalance'] = $refundable + $nonrefundable;
Expand Down Expand Up @@ -275,6 +276,7 @@ protected function add_to_main($amount, $refundable, $free = false) {
*/
public function debit($amount, $for = self::USER, $thingid = 0, $desc = '', $neg = false) {
global $DB, $USER;

if ($for == self::USER) {
$charger = !empty($thingid) ? $thingid : $USER->id;
} else {
Expand All @@ -300,6 +302,7 @@ public function debit($amount, $for = self::USER, $thingid = 0, $desc = '', $neg
$response = $wordpress->debit($this->userid, $amount, $desc, $charger);

if (!is_numeric($response)) {
debugging($response);
return false;
}

Expand Down Expand Up @@ -344,8 +347,8 @@ public function debit($amount, $for = self::USER, $thingid = 0, $desc = '', $neg
}

// No debit occurs.
if ($newbalance != $before - $amount) {
debugging("no debit occur");
if ($newbalance != ($before - $amount)) {
throw new \moodle_exception('Error while debiting');
}

$recorddata = [
Expand All @@ -367,7 +370,6 @@ public function debit($amount, $for = self::USER, $thingid = 0, $desc = '', $neg
(new notifications)->transaction_notify($recorddata);

$this->trigger_transaction_event('debit', $charger, $description, false);

return true;
}

Expand Down Expand Up @@ -463,6 +465,7 @@ private function get_debit_description($for, $thingid, $desc) {
*/
public function credit($amount, $by = self::OTHER, $thingid = 0, $desc = '', $refundable = true, $trigger = true) {
global $DB;

if (in_array($by, [self::USER, self::C_TRANSFER, self::C_REFERRAL])) {
$charger = $thingid;
} else {
Expand Down
55 changes: 55 additions & 0 deletions tests/util/balance_op_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,62 @@ private function reset_balance($userid): void {
$op->debit($op->get_valid_balance(), $op::OTHER);
}
}

$balance = new balance($userid);
$this->assertEquals(0, $balance->get_total_balance());
}

/**
* Test if multiple transactions in one call will behave.
* @return void
*/
public function test_multiple_transactions(): void {
global $CFG, $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

$op = new balance_op($user->id);
$op->credit(100, refundable: false);
$op->credit(50, refundable: false);
$op->debit(100);
$op->credit(50, refundable: false);

$op = new balance_op($user->id);
$op->debit(50);
$op->credit(100, refundable: false);
$op->credit(amount: 100, refundable: false);
$op->credit(amount: 30, refundable: false);
$op->debit(20);

$this->assertEquals(260, $op->get_total_balance());
$this->assertEquals(260, $op->get_valid_balance());
$this->assertEquals(260, $op->get_main_balance());

$op = new balance_op($user->id);
$this->assertEquals(260, $op->get_total_balance());
$this->assertEquals(260, $op->get_valid_balance());
$this->assertEquals(260, $op->get_main_balance());

purge_caches();
purge_all_caches();

$op = new balance_op($user->id);
$this->assertEquals(260, $op->get_total_balance());
$this->assertEquals(260, $op->get_valid_balance());
$this->assertEquals(260, $op->get_main_balance());

$op->debit(560, $op::USER, 0, '', true);
$op->debit(100, $op::USER, 0, '', true);
$op->debit(30, $op::USER, 0, '', true);
$op = new balance_op($user->id);
$op->debit(40, $op::USER, 0, '', true);
$op->debit(50, $op::USER, 0, '', true);
$op->debit(60, $op::USER, 0, '', true);

$op = new balance_op($user->id);
$this->assertEquals(-580, $op->get_total_balance());
$this->assertEquals(-580, $op->get_valid_balance());
$this->assertEquals(-580, $op->get_main_balance());
}
}

0 comments on commit 9b80584

Please sign in to comment.