From 9b8058463b3b7f931bec02bf0cbd5bc4dfee2df5 Mon Sep 17 00:00:00 2001 From: Mohammad Farouk Date: Thu, 3 Oct 2024 01:35:11 +0300 Subject: [PATCH] Fix bug for debit --- classes/util/balance_op.php | 15 ++++++---- tests/util/balance_op_test.php | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/classes/util/balance_op.php b/classes/util/balance_op.php index 7b0cee65..1be628d4 100644 --- a/classes/util/balance_op.php +++ b/classes/util/balance_op.php @@ -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; @@ -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 { @@ -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; } @@ -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 = [ @@ -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; } @@ -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 { diff --git a/tests/util/balance_op_test.php b/tests/util/balance_op_test.php index 3058dc2d..9c546cd6 100644 --- a/tests/util/balance_op_test.php +++ b/tests/util/balance_op_test.php @@ -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()); + } }