Feat: Adds multi currency to imports and exports (#89)
* Also adds ability for user to export configurations
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Imports\Sheets;
|
||||
|
||||
use App\Models\BackupImport;
|
||||
use App\Models\Holding;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
use Maatwebsite\Excel\Events\BeforeSheet;
|
||||
|
||||
class ConfigSheet implements SkipsEmptyRows, ToCollection, WithEvents, WithHeadingRow, WithValidation
|
||||
{
|
||||
public function __construct(
|
||||
public BackupImport $backupImport
|
||||
) {}
|
||||
|
||||
public function registerEvents(): array
|
||||
{
|
||||
return [
|
||||
BeforeSheet::class => function (BeforeSheet $event) {
|
||||
DB::commit();
|
||||
$this->backupImport->update([
|
||||
'message' => __('Importing configurations...'),
|
||||
]);
|
||||
DB::beginTransaction();
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public function collection(Collection $configs)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
foreach ($configs as $config) {
|
||||
|
||||
switch ($config['key']) {
|
||||
case 'name':
|
||||
$user->name = $config['value'];
|
||||
$user->save();
|
||||
break;
|
||||
|
||||
case 'locale':
|
||||
$user->setOption('locale', $config['value']);
|
||||
$user->save();
|
||||
break;
|
||||
|
||||
case 'display_currency':
|
||||
$user->setOption('display_currency', $config['value']);
|
||||
$user->save();
|
||||
break;
|
||||
|
||||
case 'reinvest_dividends':
|
||||
|
||||
Holding::myHoldings()->where('id', $config['value'])->update([
|
||||
'reinvest_dividends' => true,
|
||||
]);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'key' => ['required', 'string'],
|
||||
'value' => ['required', 'string'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ class DailyChangesSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
|
||||
BeforeSheet::class => function (BeforeSheet $event) {
|
||||
DB::commit();
|
||||
$this->backupImport->update([
|
||||
'message' => __('Importing daily changes...'),
|
||||
'message' => __('Preparing to import daily changes...'),
|
||||
]);
|
||||
DB::beginTransaction();
|
||||
},
|
||||
@@ -40,19 +40,20 @@ class DailyChangesSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
|
||||
|
||||
public function collection(Collection $dailyChanges)
|
||||
{
|
||||
$dailyChanges->chunk($this->batchSize())->each(function ($chunk) {
|
||||
$totalBatches = count($dailyChanges) / $this->batchSize();
|
||||
|
||||
$dailyChanges->chunk($this->batchSize())->each(function ($chunk, $index) use ($totalBatches) {
|
||||
|
||||
$this->validatePortfolioAccess($chunk);
|
||||
|
||||
$this->backupImport->update([
|
||||
'message' => __('Importing daily changes (Batch :currentBatch of :totalBatches)...', ['currentBatch' => $index + 1, 'totalBatches' => $totalBatches]),
|
||||
]);
|
||||
|
||||
// have to cast to native values
|
||||
$chunk = $chunk->map(function ($dailyChange) {
|
||||
|
||||
return [
|
||||
'total_market_value' => $dailyChange['total_market_value'],
|
||||
'total_cost_basis' => $dailyChange['total_cost_basis'],
|
||||
'total_gain' => $dailyChange['total_gain'],
|
||||
'total_dividends_earned' => $dailyChange['total_dividends_earned'],
|
||||
'realized_gains' => $dailyChange['realized_gains'],
|
||||
'annotation' => $dailyChange['annotation'],
|
||||
'portfolio_id' => $dailyChange['portfolio_id'],
|
||||
'date' => Carbon::parse($dailyChange['date'])->toDateString(),
|
||||
@@ -63,11 +64,6 @@ class DailyChangesSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
|
||||
$chunk->toArray(),
|
||||
['portfolio_id', 'date'],
|
||||
[
|
||||
'total_market_value',
|
||||
'total_cost_basis',
|
||||
'total_gain',
|
||||
'total_dividends_earned',
|
||||
'realized_gains',
|
||||
'annotation',
|
||||
'portfolio_id',
|
||||
'date',
|
||||
@@ -86,11 +82,6 @@ class DailyChangesSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
|
||||
return [
|
||||
'portfolio_id' => ['required', 'uuid'],
|
||||
'date' => ['required', 'date'],
|
||||
'total_market_value' => ['sometimes', 'nullable', 'numeric'],
|
||||
'total_cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'],
|
||||
'total_gain' => ['sometimes', 'nullable', 'numeric'],
|
||||
'total_dividends_earned' => ['sometimes', 'nullable', 'min:0', 'numeric'],
|
||||
'realized_gains' => ['sometimes', 'nullable', 'numeric'],
|
||||
'annotation' => ['sometimes', 'nullable', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ namespace App\Imports\Sheets;
|
||||
|
||||
use App\Imports\ValidatesPortfolioAccess;
|
||||
use App\Models\BackupImport;
|
||||
use App\Models\Currency;
|
||||
use App\Models\CurrencyRate;
|
||||
use App\Models\Holding;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Support\Carbon;
|
||||
@@ -33,7 +35,7 @@ class TransactionsSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
|
||||
BeforeSheet::class => function (BeforeSheet $event) {
|
||||
DB::commit();
|
||||
$this->backupImport->update([
|
||||
'message' => __('Importing transactions...'),
|
||||
'message' => __('Preparing to import transactions...'),
|
||||
]);
|
||||
DB::beginTransaction();
|
||||
},
|
||||
@@ -43,13 +45,37 @@ class TransactionsSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
|
||||
public function collection(Collection $transactions)
|
||||
{
|
||||
|
||||
$transactions->chunk($this->batchSize())->each(function ($chunk) {
|
||||
// if has any transactions not in base currency, need to sync timeseries conversion rates
|
||||
if ($transactions->where('currency', '!=', config('investbrain.base_currency'))->isNotEmpty()) {
|
||||
|
||||
CurrencyRate::timeSeriesRates('', $transactions->min('date'));
|
||||
}
|
||||
|
||||
$totalBatches = count($transactions) / $this->batchSize();
|
||||
|
||||
// chunk transactions
|
||||
$transactions->chunk($this->batchSize())->each(function ($chunk, $index) use ($totalBatches) {
|
||||
|
||||
$this->backupImport->update([
|
||||
'message' => __('Importing transactions (Batch :currentBatch of :totalBatches)...', ['currentBatch' => $index + 1, 'totalBatches' => $totalBatches]),
|
||||
]);
|
||||
|
||||
$this->validatePortfolioAccess($chunk);
|
||||
|
||||
// have to cast to native values
|
||||
$chunk = $chunk->map(function ($transaction) {
|
||||
|
||||
$date = Carbon::parse($transaction['date'])->toDateString();
|
||||
|
||||
// if transaction not in base currency, need to convert
|
||||
if ($transaction['currency'] == config('investbrain.base_currency')) {
|
||||
$cost_basis_base = $transaction['cost_basis'] ?? 0;
|
||||
$sale_price_base = $transaction['sale_price'];
|
||||
} else {
|
||||
$cost_basis_base = Currency::convert($transaction['cost_basis'], $transaction['currency'], date: $date);
|
||||
$sale_price_base = Currency::convert($transaction['sale_price'], $transaction['currency'], date: $date);
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $transaction['transaction_id'] ?? Str::uuid()->toString(),
|
||||
'symbol' => strtoupper($transaction['symbol']),
|
||||
@@ -58,9 +84,11 @@ class TransactionsSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
|
||||
'quantity' => $transaction['quantity'],
|
||||
'cost_basis' => $transaction['cost_basis'] ?? 0,
|
||||
'sale_price' => $transaction['sale_price'],
|
||||
'cost_basis_base' => $cost_basis_base,
|
||||
'sale_price_base' => $sale_price_base,
|
||||
'split' => boolval($transaction['split']) ? 1 : 0,
|
||||
'reinvested_dividend' => boolval($transaction['reinvested_dividend']) ? 1 : 0,
|
||||
'date' => Carbon::parse($transaction['date'])->toDateString(),
|
||||
'date' => $date,
|
||||
];
|
||||
});
|
||||
|
||||
@@ -81,7 +109,7 @@ class TransactionsSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
|
||||
]
|
||||
);
|
||||
|
||||
// stub out related holdings
|
||||
// get unique symbol/portfolio id combination and stub out related holdings
|
||||
$chunk->unique(fn ($item) => $item['symbol'].$item['portfolio_id'])
|
||||
->each(function ($holding) {
|
||||
|
||||
@@ -112,6 +140,7 @@ class TransactionsSheet implements SkipsEmptyRows, ToCollection, WithEvents, Wit
|
||||
'transaction_type' => ['required', 'in:BUY,SELL'],
|
||||
'date' => ['required', 'date'],
|
||||
'quantity' => ['required', 'min:0', 'numeric'],
|
||||
'currency' => ['required', 'string'],
|
||||
'split' => ['sometimes', 'nullable', 'boolean'],
|
||||
'reinvested_dividend' => ['sometimes', 'nullable', 'boolean'],
|
||||
'cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'],
|
||||
|
||||
Reference in New Issue
Block a user