mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-01 21:00:58 +00:00
* refactor: stabilize model identities for domain migration * refactor: extract module platform context * refactor: assign models to domain contexts * refactor: extract ai platform context * refactor: extract storage platform context * refactor: extract mail platform context * refactor: extract pdf platform context * refactor: extract operations platform context * refactor: move installation into operations platform * refactor: extract money domain context * refactor: extract taxation domain context * refactor: extract catalog domain context * refactor: extract metadata domain context * refactor: extract reporting domain context * refactor: extract purchases domain context * refactor: extract receivables domain context * refactor: extract accounts domain context * refactor: complete reporting statement boundary * refactor: extract contacts domain context * refactor: extract sales domain context * refactor: remove legacy application layers * fix: migrate legacy bouncer role identities
100 lines
2.6 KiB
PHP
100 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Purchases\Models;
|
|
|
|
use App\Domains\Accounts\Models\Company;
|
|
use App\Domains\Accounts\Models\CompanySetting;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class ExpenseCategory extends Model
|
|
{
|
|
protected $table = 'expense_categories';
|
|
|
|
use HasFactory;
|
|
|
|
protected $fillable = ['name', 'company_id', 'description'];
|
|
|
|
/**
|
|
* The accessors to append to the model's array form.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $appends = ['amount', 'formattedCreatedAt'];
|
|
|
|
public function expenses(): HasMany
|
|
{
|
|
return $this->hasMany(Expense::class);
|
|
}
|
|
|
|
public function company(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class);
|
|
}
|
|
|
|
public function getFormattedCreatedAtAttribute(mixed $value): string
|
|
{
|
|
$dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
|
|
|
|
return Carbon::parse($this->created_at)->format($dateFormat);
|
|
}
|
|
|
|
public function getAmountAttribute(): float
|
|
{
|
|
return $this->expenses()->sum('amount');
|
|
}
|
|
|
|
public function scopeWhereCompany(Builder $query): void
|
|
{
|
|
$query->where('company_id', request()->header('company'));
|
|
}
|
|
|
|
public function scopeWhereCategory(Builder $query, int $category_id): void
|
|
{
|
|
$query->orWhere('id', $category_id);
|
|
}
|
|
|
|
public function scopeWhereSearch(Builder $query, string $search): void
|
|
{
|
|
$query->where('name', 'LIKE', '%'.$search.'%');
|
|
}
|
|
|
|
/**
|
|
* Apply multiple filter conditions including category, company, and search.
|
|
*/
|
|
public function scopeApplyFilters(Builder $query, array $filters): void
|
|
{
|
|
$filters = collect($filters);
|
|
|
|
if ($filters->get('category_id')) {
|
|
$query->whereCategory($filters->get('category_id'));
|
|
}
|
|
|
|
if ($filters->get('company_id')) {
|
|
$query->whereCompany($filters->get('company_id'));
|
|
}
|
|
|
|
if ($filters->get('search')) {
|
|
$query->whereSearch($filters->get('search'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return LengthAwarePaginator|Collection
|
|
*/
|
|
public function scopePaginateData(Builder $query, string $limit)
|
|
{
|
|
if ($limit == 'all') {
|
|
return $query->get();
|
|
}
|
|
|
|
return $query->paginate($limit);
|
|
}
|
|
}
|