Create rawNumberToInteger method in Helper class and replace manual conversions with it
MODIFY
app/Helper.php
app/Helper.php
@@ -7,4 +7,9 @@ class Helper {
{
return number_format($number, 2, '.', '');
}
+
+ public static function rawNumberToInteger(float $rawNumber): int
+ {
+ return (int) round($rawNumber * 100);
+ }
}

MODIFY
app/Http/Controllers/EarningController.php
app/Http/Controllers/EarningController.php
namespace App\Http\Controllers;
+use App\Helper;
use Illuminate\Http\Request;
use App\Models\Earning;
@@ -29,7 +30,7 @@ class EarningController extends Controller {
$earning->space_id = session('space')->id;
$earning->happened_on = $request->input('date');
$earning->description = $request->input('description');
- $earning->amount = (int) ($request->input('amount') * 100);
+ $earning->amount = Helper::rawNumberToInteger($request->input('amount'));
$earning->save();
@@ -50,7 +51,7 @@ class EarningController extends Controller {
$earning->fill([
'happened_on' => $request->input('date'),
'description' => $request->input('description'),
- 'amount' => (int) ($request->input('amount') * 100)
+ 'amount' => Helper::rawNumberToInteger($request->input('amount'))
])->save();
return redirect()->route('transactions.index');

MODIFY
app/Http/Controllers/ImportController.php
app/Http/Controllers/ImportController.php
namespace App\Http\Controllers;
+use App\Helper;
use App\Models\Import;
use App\Models\Spending;
use Illuminate\Http\Request;
@@ -144,7 +145,7 @@ class ImportController extends Controller {
'tag_id' => $row['tag_id'],
'happened_on' => $row['happened_on'],
'description' => $row['description'],
- 'amount' => (int) ($amount * 100)
+ 'amount' => Helper::rawNumberToInteger($amount)
]);
}
}

MODIFY
app/Http/Controllers/RecurringController.php
app/Http/Controllers/RecurringController.php
namespace App\Http\Controllers;
+use App\Helper;
use Illuminate\Http\Request;
use App\Models\Recurring;
@@ -55,7 +56,7 @@ class RecurringController extends Controller {
$recurring->ends_on = $request->input('end');
$recurring->tag_id = $request->input('tag');
$recurring->description = $request->input('description');
- $recurring->amount = (int) ($request->input('amount') * 100);
+ $recurring->amount = Helper::rawNumberToInteger($request->input('amount'));
$recurring->save();
ProcessRecurrings::dispatch();

MODIFY
app/Http/Controllers/SpendingController.php
app/Http/Controllers/SpendingController.php
namespace App\Http\Controllers;
+use App\Helper;
use Illuminate\Http\Request;
use App\Models\Spending;
@@ -30,7 +31,7 @@ class SpendingController extends Controller {
$spending->tag_id = $request->input('tag_id');
$spending->happened_on = $request->input('date');
$spending->description = $request->input('description');
- $spending->amount = (int) ($request->input('amount') * 100);
+ $spending->amount = Helper::rawNumberToInteger($request->input('amount'));
$spending->save();
@@ -60,7 +61,7 @@ class SpendingController extends Controller {
'tag_id' => $request->input('tag_id'),
'happened_on' => $request->input('date'),
'description' => $request->input('description'),
- 'amount' => (int) ($request->input('amount') * 100)
+ 'amount' => Helper::rawNumberToInteger($request->input('amount'))
])->save();
return redirect()->route('transactions.index');

MODIFY
tests/Unit/EarningTest.php
tests/Unit/EarningTest.php
namespace Tests\Unit;
+use App\Helper;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -11,7 +12,7 @@ use App\Models\Earning;
class EarningTest extends TestCase {
public function testFormattedAmount() {
$earning = factory(Earning::class)->make([
- 'amount' => (int) ('39' * 100)
+ 'amount' => Helper::rawNumberToInteger(39)
]);
$this->assertEquals('39.00', $earning->formatted_amount);

MODIFY
tests/Unit/SpaceTest.php
tests/Unit/SpaceTest.php
namespace Tests\Unit;
+use App\Helper;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -16,7 +17,7 @@ class SpaceTest extends TestCase {
factory(Earning::class)->create([
'space_id' => $space->id,
- 'amount' => (int) ('39' * 100),
+ 'amount' => Helper::rawNumberToInteger(39),
'happened_on' => now()
]);
@@ -24,7 +25,7 @@ class SpaceTest extends TestCase {
factory(Spending::class)->create([
'space_id' => $space->id,
- 'amount' => (int) ('12' * 100),
+ 'amount' => Helper::rawNumberToInteger(12),
'happened_on' => now()
]);
@@ -32,7 +33,7 @@ class SpaceTest extends TestCase {
factory(Spending::class)->create([
'space_id' => $space->id,
- 'amount' => (int) ('50' * 100),
+ 'amount' => Helper::rawNumberToInteger(50),
'happened_on' => now()
]);
@@ -41,7 +42,7 @@ class SpaceTest extends TestCase {
factory(Earning::class)->create([
'space_id' => $space->id,
- 'amount' => (int) ('10' * 100),
+ 'amount' => Helper::rawNumberToInteger(10),
'happened_on' => date_create_from_format('Y-m-d', '2018-01-01')
]);
@@ -51,7 +52,7 @@ class SpaceTest extends TestCase {
factory(Earning::class)->create([
'space_id' => 2,
- 'amount' => (int) ('10' * 100),
+ 'amount' => Helper::rawNumberToInteger(10),
'happened_on' => date_create_from_format('Y-m-d', '2020-01-01')
]);

MODIFY
tests/Unit/SpendingTest.php
tests/Unit/SpendingTest.php
namespace Tests\Unit;
+use App\Helper;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -11,7 +12,7 @@ use App\Models\Spending;
class SpendingTest extends TestCase {
public function testFormattedAmount() {
$spending = factory(Spending::class)->make([
- 'amount' => (int) ('92.35' * 100)
+ 'amount' => Helper::rawNumberToInteger(92.35)
]);
$this->assertEquals('92.35', $spending->formatted_amount);

Create rawNumberToInteger method in Helper class and replace manual conversions with it