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,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class EnsureDailyChangeIsSynced
|
||||
{
|
||||
public function __invoke(Model $model, callable $next)
|
||||
{
|
||||
if (config('app.env') != 'testing') {
|
||||
|
||||
$cacheKey = 'daily_change_synced'.$model->portfolio_id;
|
||||
|
||||
if (
|
||||
! Cache::has($cacheKey)
|
||||
&& $model->date->lessThan(now())
|
||||
&& ($model->date->lessThan($model->portfolio->daily_change()->min('date') ?? now())
|
||||
|| $model->date->lessThan($model->portfolio->transactions()->where('id', '!=', $model->id)->min('date') ?? now())
|
||||
)
|
||||
) {
|
||||
defer(fn () => $model->portfolio->syncDailyChanges());
|
||||
|
||||
Cache::put($cacheKey, now(), now()->addMinutes(5));
|
||||
}
|
||||
}
|
||||
|
||||
return $next($model);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use App\Exports\Sheets\ConfigSheet;
|
||||
use App\Exports\Sheets\DailyChangesSheet;
|
||||
use App\Exports\Sheets\PortfoliosSheet;
|
||||
use App\Exports\Sheets\TransactionsSheet;
|
||||
@@ -24,6 +25,7 @@ class BackupExport implements WithMultipleSheets
|
||||
new PortfoliosSheet($this->empty),
|
||||
new TransactionsSheet($this->empty),
|
||||
new DailyChangesSheet($this->empty),
|
||||
new ConfigSheet($this->empty),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exports\Sheets;
|
||||
|
||||
use App\Models\Holding;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||
|
||||
class ConfigSheet implements FromCollection, WithHeadings, WithTitle
|
||||
{
|
||||
public function __construct(
|
||||
public bool $empty = false
|
||||
) {}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'Key',
|
||||
'Value',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
$configs = collect();
|
||||
|
||||
if ($this->empty) {
|
||||
return $configs;
|
||||
}
|
||||
|
||||
// collect user settings
|
||||
$configs->push([
|
||||
'key' => 'name',
|
||||
'value' => auth()->user()->name,
|
||||
], [
|
||||
'key' => 'locale',
|
||||
'value' => auth()->user()->getLocale(),
|
||||
], [
|
||||
'key' => 'display_currency',
|
||||
'value' => auth()->user()->getCurrency(),
|
||||
]);
|
||||
|
||||
// reinvested holdings
|
||||
Holding::myHoldings()->where('reinvest_dividends', true)->get()->each(function ($holding) use (&$configs) {
|
||||
$configs->push([
|
||||
'key' => 'reinvested_dividends',
|
||||
'value' => $holding->id,
|
||||
]);
|
||||
});
|
||||
|
||||
return $configs;
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return 'Config';
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ class DailyChangesSheet implements FromCollection, WithHeadings, WithTitle
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
return $this->empty ? collect() : DailyChange::myDailyChanges()->get();
|
||||
return $this->empty ? collect() : DailyChange::myDailyChanges()->withDailyPerformance()->get();
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
|
||||
@@ -25,6 +25,7 @@ class TransactionsSheet implements FromCollection, WithHeadings, WithTitle
|
||||
'Quantity',
|
||||
'Cost Basis',
|
||||
'Sale Price',
|
||||
'Currency',
|
||||
'Split',
|
||||
'Reinvested Dividend',
|
||||
'Date',
|
||||
@@ -38,7 +39,30 @@ class TransactionsSheet implements FromCollection, WithHeadings, WithTitle
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
return $this->empty ? collect() : Transaction::myTransactions()->get();
|
||||
if ($this->empty) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
return Transaction::myTransactions()
|
||||
->withMarketData()
|
||||
->get()
|
||||
->map(function ($transaction) {
|
||||
return [
|
||||
'id' => $transaction->id,
|
||||
'symbol' => $transaction->symbol,
|
||||
'portfolio_id' => $transaction->portfolio_id,
|
||||
'transaction_type' => $transaction->transaction_type,
|
||||
'quantity' => $transaction->quantity,
|
||||
'cost_basis' => $transaction->cost_basis,
|
||||
'sale_price' => $transaction->sale_price,
|
||||
'currency' => $transaction->market_data_currency,
|
||||
'split' => $transaction->split,
|
||||
'reinvested_dividend' => $transaction->reinvested_dividend,
|
||||
'date' => $transaction->date,
|
||||
'created_at' => $transaction->created_at,
|
||||
'updated_at' => $transaction->updated_at,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Console\Commands\RefreshDividendData;
|
||||
use App\Console\Commands\RefreshMarketData;
|
||||
use App\Console\Commands\SyncDailyChange;
|
||||
use App\Console\Commands\SyncHoldingData;
|
||||
use App\Imports\Sheets\ConfigSheet;
|
||||
use App\Imports\Sheets\DailyChangesSheet;
|
||||
use App\Imports\Sheets\PortfoliosSheet;
|
||||
use App\Imports\Sheets\TransactionsSheet;
|
||||
@@ -69,6 +70,7 @@ class BackupImport implements WithEvents, WithMultipleSheets
|
||||
'Portfolios' => new PortfoliosSheet($this->backupImportModel),
|
||||
'Transactions' => new TransactionsSheet($this->backupImportModel),
|
||||
'Daily Changes' => new DailyChangesSheet($this->backupImportModel),
|
||||
'Config' => new ConfigSheet($this->backupImportModel),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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'],
|
||||
|
||||
@@ -11,13 +11,13 @@ trait ValidatesPortfolioAccess
|
||||
public function validatePortfolioAccess($collection)
|
||||
{
|
||||
|
||||
$uniquePortfolios = $collection->unique('portfolio_id')->pluck('portfolio_id');
|
||||
$countPortfoliosWithAccess = Portfolio::fullAccess($this->backupImport->user_id)
|
||||
->whereIn('id', $uniquePortfolios)
|
||||
$importingPortfolios = $collection->unique('portfolio_id')->pluck('portfolio_id');
|
||||
$portfoliosWithAccess = Portfolio::fullAccess($this->backupImport->user_id)
|
||||
->whereIn('id', $importingPortfolios)
|
||||
->count();
|
||||
|
||||
if (
|
||||
$countPortfoliosWithAccess < $uniquePortfolios->count()
|
||||
$importingPortfolios->count() > $portfoliosWithAccess
|
||||
) {
|
||||
throw new \Exception(__('You do not have access to that portfolio.'));
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ class DailyChange extends Model
|
||||
]);
|
||||
|
||||
return $query
|
||||
->select(['daily_change.portfolio_id', 'daily_change.date'])
|
||||
->select(['daily_change.date', 'daily_change.portfolio_id'])
|
||||
->leftJoinSub($totalCostBasisSub, 'cost_basis_display', function ($join) {
|
||||
$join->on('daily_change.date', '>=', 'cost_basis_display.date')
|
||||
->whereColumn('daily_change.portfolio_id', '=', 'cost_basis_display.portfolio_id');
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace App\Models;
|
||||
use App\Actions\ConvertToMarketDataCurrency;
|
||||
use App\Actions\CopyToBaseCurrency;
|
||||
use App\Actions\EnsureCostBasisAddedToSale;
|
||||
use App\Actions\EnsureDailyChangeIsSynced;
|
||||
use App\Casts\BaseCurrency;
|
||||
use App\Traits\HasMarketData;
|
||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||
@@ -69,6 +70,12 @@ class Transaction extends Model
|
||||
|
||||
$transaction->syncToHolding();
|
||||
|
||||
$transaction = Pipeline::send($transaction)
|
||||
->through([
|
||||
EnsureDailyChangeIsSynced::class,
|
||||
])
|
||||
->then(fn (Transaction $transaction) => $transaction);
|
||||
|
||||
cache()->forget('portfolio-metrics-'.$transaction->portfolio_id);
|
||||
});
|
||||
|
||||
@@ -104,6 +111,7 @@ class Transaction extends Model
|
||||
{
|
||||
return $query->withAggregate('market_data', 'name')
|
||||
->withAggregate('market_data', 'market_value')
|
||||
->withAggregate('market_data', 'currency')
|
||||
->withAggregate('market_data', 'fifty_two_week_low')
|
||||
->withAggregate('market_data', 'fifty_two_week_high')
|
||||
->withAggregate('market_data', 'updated_at')
|
||||
|
||||
@@ -98,4 +98,14 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
|
||||
return Arr::get($this->options, 'locale') ?? request()->getPreferredLanguage($available_locales) ?? config('app.locale');
|
||||
}
|
||||
|
||||
public function setOption(mixed $key, string $value): self
|
||||
{
|
||||
|
||||
$options = is_array($key) ? $key : [$key => $value];
|
||||
|
||||
$this->user->options = array_merge($this->user->options ?? [], $options);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user