diff --git a/src/app/Http/Controllers/API/V4/Admin/WalletsController.php b/src/app/Http/Controllers/API/V4/Admin/WalletsController.php --- a/src/app/Http/Controllers/API/V4/Admin/WalletsController.php +++ b/src/app/Http/Controllers/API/V4/Admin/WalletsController.php @@ -91,7 +91,7 @@ 'object_id' => $wallet->id, 'object_type' => Wallet::class, 'type' => $type, - 'amount' => $amount < 0 ? $amount * -1 : $amount, + 'amount' => $amount, 'description' => $request->description ] ); diff --git a/src/app/Http/Controllers/API/V4/WalletsController.php b/src/app/Http/Controllers/API/V4/WalletsController.php --- a/src/app/Http/Controllers/API/V4/WalletsController.php +++ b/src/app/Http/Controllers/API/V4/WalletsController.php @@ -246,25 +246,12 @@ } $result = $result->map(function ($item) use ($isAdmin) { - $amount = $item->amount; - - $negatives = [ - Transaction::WALLET_CHARGEBACK, - Transaction::WALLET_DEBIT, - Transaction::WALLET_PENALTY, - Transaction::WALLET_REFUND, - ]; - - if (in_array($item->type, $negatives)) { - $amount *= -1; - } - $entry = [ 'id' => $item->id, 'createdAt' => $item->created_at->format('Y-m-d H:i'), 'type' => $item->type, 'description' => $item->shortDescription(), - 'amount' => $amount, + 'amount' => $item->amount, 'hasDetails' => !empty($item->cnt), ]; diff --git a/src/app/Providers/PaymentProvider.php b/src/app/Providers/PaymentProvider.php --- a/src/app/Providers/PaymentProvider.php +++ b/src/app/Providers/PaymentProvider.php @@ -185,7 +185,7 @@ 'object_id' => $wallet->id, 'object_type' => Wallet::class, 'type' => $transaction_type, - 'amount' => $refund['amount'], + 'amount' => $refund['amount'] * -1, 'description' => $refund['description'] ?? '', ]); diff --git a/src/app/Transaction.php b/src/app/Transaction.php --- a/src/app/Transaction.php +++ b/src/app/Transaction.php @@ -159,6 +159,8 @@ 'description' => $this->{'description'}, ]; + $amount = $this->amount * ($this->amount < 0 ? -1 : 1); + if ($entitlement = $this->entitlement()) { $wallet = $entitlement->wallet; $cost = $entitlement->cost; @@ -172,7 +174,7 @@ } $result['wallet'] = $wallet->{'description'} ?: 'Default wallet'; - $result['amount'] = $wallet->money($this->amount); + $result['amount'] = $wallet->money($amount); return $result; } diff --git a/src/app/Wallet.php b/src/app/Wallet.php --- a/src/app/Wallet.php +++ b/src/app/Wallet.php @@ -254,7 +254,7 @@ 'object_id' => $this->id, 'object_type' => \App\Wallet::class, 'type' => \App\Transaction::WALLET_DEBIT, - 'amount' => $amount + 'amount' => $amount * -1 ] ); diff --git a/src/database/migrations/2021_02_19_100000_transaction_amount_fix.php b/src/database/migrations/2021_02_19_100000_transaction_amount_fix.php new file mode 100644 --- /dev/null +++ b/src/database/migrations/2021_02_19_100000_transaction_amount_fix.php @@ -0,0 +1,38 @@ +where('type', Transaction::WALLET_PENALTY)->first(); $this->assertSame($post['description'], $transaction->description); - $this->assertSame(4000, $transaction->amount); + $this->assertSame(-4000, $transaction->amount); $this->assertSame($admin->email, $transaction->user_email); } diff --git a/src/tests/Feature/Controller/PaymentsMollieTest.php b/src/tests/Feature/Controller/PaymentsMollieTest.php --- a/src/tests/Feature/Controller/PaymentsMollieTest.php +++ b/src/tests/Feature/Controller/PaymentsMollieTest.php @@ -717,7 +717,7 @@ $transactions = $wallet->transactions()->where('type', Transaction::WALLET_REFUND)->get(); $this->assertCount(1, $transactions); - $this->assertSame(101, $transactions[0]->amount); + $this->assertSame(-101, $transactions[0]->amount); $this->assertSame(Transaction::WALLET_REFUND, $transactions[0]->type); $this->assertSame("refund desc", $transactions[0]->description); @@ -774,7 +774,7 @@ $transactions = $wallet->transactions()->where('type', Transaction::WALLET_CHARGEBACK)->get(); $this->assertCount(1, $transactions); - $this->assertSame(15, $transactions[0]->amount); + $this->assertSame(-15, $transactions[0]->amount); $this->assertSame(Transaction::WALLET_CHARGEBACK, $transactions[0]->type); $this->assertSame('', $transactions[0]->description); diff --git a/src/tests/TestCaseTrait.php b/src/tests/TestCaseTrait.php --- a/src/tests/TestCaseTrait.php +++ b/src/tests/TestCaseTrait.php @@ -80,7 +80,7 @@ 'object_id' => $wallet->id, 'object_type' => \App\Wallet::class, 'type' => Transaction::WALLET_DEBIT, - 'amount' => $debit, + 'amount' => $debit * -1, 'description' => 'Payment', ]); $result[] = $transaction; @@ -107,12 +107,13 @@ // The page size is 10, so we generate so many to have at least two pages $loops = 10; while ($loops-- > 0) { + $type = $types[count($result) % count($types)]; $transaction = Transaction::create([ 'user_email' => 'jeroen.@jeroen.jeroen', 'object_id' => $wallet->id, 'object_type' => \App\Wallet::class, - 'type' => $types[count($result) % count($types)], - 'amount' => 11 * (count($result) + 1), + 'type' => $type, + 'amount' => 11 * (count($result) + 1) * ($type == Transaction::WALLET_PENALTY ? -1 : 1), 'description' => 'TRANS' . $loops, ]); $transaction->created_at = $date->next(Carbon::MONDAY); diff --git a/src/tests/Unit/TransactionTest.php b/src/tests/Unit/TransactionTest.php --- a/src/tests/Unit/TransactionTest.php +++ b/src/tests/Unit/TransactionTest.php @@ -26,7 +26,7 @@ 'object_id' => $wallet->id, 'object_type' => Wallet::class, 'type' => Transaction::WALLET_PENALTY, - 'amount' => 9, + 'amount' => -10, 'description' => "A test penalty" ]); @@ -34,7 +34,7 @@ 'object_id' => $wallet->id, 'object_type' => Wallet::class, 'type' => Transaction::WALLET_DEBIT, - 'amount' => 10 + 'amount' => -9 ]); $transaction = Transaction::create([ @@ -84,10 +84,10 @@ $transactions = Transaction::where('amount', '<', 20)->orderBy('amount')->get(); - $this->assertSame(9, $transactions[0]->amount); + $this->assertSame(-10, $transactions[0]->amount); $this->assertSame(Transaction::WALLET_PENALTY, $transactions[0]->type); $this->assertSame( - "The balance of Default wallet was reduced by 0,09 CHF; A test penalty", + "The balance of Default wallet was reduced by 0,10 CHF; A test penalty", $transactions[0]->toString() ); $this->assertSame( @@ -95,10 +95,10 @@ $transactions[0]->shortDescription() ); - $this->assertSame(10, $transactions[1]->amount); + $this->assertSame(-9, $transactions[1]->amount); $this->assertSame(Transaction::WALLET_DEBIT, $transactions[1]->type); $this->assertSame( - "0,10 CHF was deducted from the balance of Default wallet", + "0,09 CHF was deducted from the balance of Default wallet", $transactions[1]->toString() ); $this->assertSame(