Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

mateusjatenee/crypto-tracker

48 stars
2 code files
View mateusjatenee/crypto-tracker on GitHub

app/Services/AccountService.php

Open in GitHub
use 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 GitHub
use 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());
}
//
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.