Convert amounts in ProcessRecurrings job if currency differs
MODIFY
app/Http/Controllers/RecurringController.php
app/Http/Controllers/RecurringController.php
@@ -56,7 +56,8 @@ class RecurringController extends Controller
$request->input('end', null),
$request->input('tag', null),
$request->input('description'),
- Helper::rawNumberToInteger($request->input('amount'))
+ Helper::rawNumberToInteger($request->input('amount')),
+ $request->currency_id
);
ProcessRecurrings::dispatch();

MODIFY
app/Jobs/ProcessRecurrings.php
app/Jobs/ProcessRecurrings.php
namespace App\Jobs;
+use App\Repositories\ConversionRateRepository;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
@@ -21,6 +22,7 @@ class ProcessRecurrings implements ShouldQueue
use SerializesModels;
private $recurringRepository;
+ private $conversionRateRepository;
private $earningRepository;
private $spendingRepository;
@@ -31,10 +33,12 @@ class ProcessRecurrings implements ShouldQueue
public function handle(
RecurringRepository $recurringRepository,
+ ConversionRateRepository $conversionRateRepository,
EarningRepository $earningRepository,
SpendingRepository $spendingRepository
) {
$this->recurringRepository = $recurringRepository;
+ $this->conversionRateRepository = $conversionRateRepository;
$this->earningRepository = $earningRepository;
$this->spendingRepository = $spendingRepository;
@@ -55,6 +59,17 @@ class ProcessRecurrings implements ShouldQueue
throw new Exception('Unknown type "' . $recurring->type . '" for recurring');
}
+ // If necessary, convert amount based on conversion rates
+ $amount = $recurring->amount;
+
+ if ($recurring->currency_id !== $recurring->space->currency_id) {
+ $amount = $this->conversionRateRepository->convert(
+ $recurring->currency_id,
+ $recurring->space->currency_id,
+ $amount
+ );
+ }
+
// Determine date on which transaction should occur
$occurancesDates = [];
@@ -126,7 +141,7 @@ class ProcessRecurrings implements ShouldQueue
$recurring->id,
$occuranceDate,
$recurring->description,
- $recurring->amount
+ $amount
);
}
@@ -138,7 +153,7 @@ class ProcessRecurrings implements ShouldQueue
$recurring->tag_id,
$occuranceDate,
$recurring->description,
- $recurring->amount
+ $amount
);
}

MODIFY
app/Repositories/RecurringRepository.php
app/Repositories/RecurringRepository.php
@@ -20,7 +20,8 @@ class RecurringRepository
'end' => 'nullable|date|date_format:Y-m-d',
'tag' => 'nullable|exists:tags,id', // TODO CHECK IF TAG BELONGS TO USER
'description' => 'required|max:255',
- 'amount' => 'required|regex:/^\d*(\.\d{2})?$/'
+ 'amount' => 'required|regex:/^\d*(\.\d{2})?$/',
+ 'currency_id' => 'required|exists:currencies,id'
];
}
@@ -163,7 +164,8 @@ class RecurringRepository
?string $endDate,
?int $tagId,
string $description,
- string $amount
+ string $amount,
+ int $currencyId
): Recurring {
if ($type !== 'earning' && $type !== 'spending') {
throw new Exception('Unknown type "' . $type . '"');
@@ -182,7 +184,8 @@ class RecurringRepository
'ends_on' => $endDate,
'tag_id' => $tagId,
'description' => $description,
- 'amount' => $amount
+ 'amount' => $amount,
+ 'currency_id' => $currencyId
]);
}

MODIFY
resources/assets/js/components/TransactionWizard.vue
resources/assets/js/components/TransactionWizard.vue
day: this.date.slice(-2),
start: this.date,
description: this.description,
- amount: this.amount
+ amount: this.amount,
+ currency_id: this.selectedCurrencyId
}
if (this.recurringEnd == 'fixed') {

MODIFY
tests/Feature/RecurringTest.php
tests/Feature/RecurringTest.php
@@ -18,7 +18,8 @@ class RecurringTest extends TestCase
$space = factory(Space::class)->create();
$recurring = factory(Recurring::class)->create([
- 'space_id' => $space->id
+ 'space_id' => $space->id,
+ 'currency_id' => $space->currency_id
]);
$this->actingAs($user);
@@ -30,18 +31,19 @@ class RecurringTest extends TestCase
public function testSuccessfulRecurringCreation(): void
{
+ $user = factory(User::class)->create();
+ $space = factory(Space::class)->create();
+
$requestData = [
'type' => 'spending',
'interval' => 'monthly',
'day' => 1,
'start' => date('Y-m-d'),
'description' => 'Test',
- 'amount' => 123
+ 'amount' => 123,
+ 'currency_id' => $space->currency_id
];
- $user = factory(User::class)->create();
- $space = factory(Space::class)->create();
-
$response = $this->actingAs($user)
->withSession(['space' => $space])
->postJson('/recurrings', $requestData);

MODIFY
tests/Unit/Repositories/RecurringRepositoryTest.php
tests/Unit/Repositories/RecurringRepositoryTest.php
@@ -26,11 +26,11 @@ class RecurringRepositoryTest extends TestCase
public function testCreation(): void
{
- $spaceId = 1;
+ $space = Space::find(1);
$tagId = 1;
$recurring = $this->recurringRepository->create(
- $spaceId,
+ $space->id,
'spending',
'monthly',
3,
@@ -38,7 +38,8 @@ class RecurringRepositoryTest extends TestCase
null,
$tagId,
'Foo',
- 10000
+ 10000,
+ $space->currency_id
);
$this->assertNotNull($recurring);

Convert amounts in ProcessRecurrings job if currency differs