Also convert earnings' amounts if different currency is selected
MODIFY
app/Http/Controllers/EarningController.php
app/Http/Controllers/EarningController.php
@@ -5,16 +5,21 @@ namespace App\Http\Controllers;
use App\Helper;
use Illuminate\Http\Request;
use App\Models\Earning;
+use App\Repositories\ConversionRateRepository;
use App\Repositories\EarningRepository;
use Auth;
class EarningController extends Controller
{
private $earningRepository;
+ private $conversionRateRepository;
- public function __construct(EarningRepository $earningRepository)
- {
+ public function __construct(
+ EarningRepository $earningRepository,
+ ConversionRateRepository $conversionRateRepository
+ ) {
$this->earningRepository = $earningRepository;
+ $this->conversionRateRepository = $conversionRateRepository;
}
public function show(Request $request, Earning $earning)
@@ -35,12 +40,23 @@ class EarningController extends Controller
{
$request->validate($this->earningRepository->getValidationRules());
+ $amount = Helper::rawNumberToInteger($request->input('amount'));
+
+ // Convert amount if a different currency was selected
+ if ($request->has('currency_id') && $request->currency_id !== session('space')->currency_id) {
+ $amount = $this->conversionRateRepository->convert(
+ $request->currency_id,
+ session('space')->currency_id,
+ $amount
+ );
+ }
+
$this->earningRepository->create(
session('space')->id,
null,
$request->input('date'),
$request->input('description'),
- Helper::rawNumberToInteger($request->input('amount'))
+ $amount
);
return redirect()->route('dashboard');

MODIFY
app/Repositories/EarningRepository.php
app/Repositories/EarningRepository.php
@@ -12,6 +12,7 @@ class EarningRepository
return [
'date' => 'required|date|date_format:Y-m-d',
'description' => 'required|max:255',
+ 'currency_id' => 'nullable|exists:currencies,id',
'amount' => 'required|regex:/^\d*(\.\d{2})?$/'
];
}

Also convert earnings' amounts if different currency is selected