-
app/Services/AccountService.php
Open in GitHubuse App\Models\Account; use App\Models\Transaction; use App\Position; use Illuminate\Support\Collection; class AccountService { public function getPositions(Account $account): Collection { return $account->transactions() ->with('asset') ->get() ->groupBy(fn ($transaction) => $transaction->asset->id) ->map(function (Collection $transactions, $assetId) use ($account) { return new Position($account, $transactions->first()->asset, $transactions); }); } }
-
app/Models/Account.php
Open in GitHubuse App\Contracts\HasPositions; use App\Position; use Carbon\Carbon; use App\Models\Asset; use App\Enums\AccountType; use App\Models\Transaction; use App\Enums\TransactionType; use App\Services\AccountService; use App\Models\PositionAggregate; use App\Contracts\Transactionable; use Illuminate\Support\Collection; use App\Enums\PositionAggregateType; use App\Transactions\CashTransaction; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Factories\HasFactory; class Account extends Model implements HasPositions { // public function positions(): Collection { return (new AccountService())->getPositions($this) ->sortByDesc(fn (Position $position) => $position->totalPosition()); } // }