mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 09:14:08 +00:00
Remove createItem/updateItem from Item, createTransaction/ completeTransaction/failedTransaction from Transaction, createCustomField/updateCustomField from CustomField, all business methods from ExchangeRateProvider (CRUD + API checks + URL helpers), and validateCredentials/createDisk/updateDisk/updateDefaultDisks/ setAsDefaultDisk from FileDisk. All logic now lives in their respective service classes.
138 lines
3.3 KiB
PHP
138 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Item extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $appends = [
|
|
'formattedCreatedAt',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'price' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function unit(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Unit::class, 'unit_id');
|
|
}
|
|
|
|
public function company(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class);
|
|
}
|
|
|
|
public function creator(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'creator_id');
|
|
}
|
|
|
|
public function currency(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Currency::class);
|
|
}
|
|
|
|
public function scopeWhereSearch($query, $search)
|
|
{
|
|
return $query->where('items.name', 'LIKE', '%'.$search.'%');
|
|
}
|
|
|
|
public function scopeWherePrice($query, $price)
|
|
{
|
|
return $query->where('items.price', $price);
|
|
}
|
|
|
|
public function scopeWhereUnit($query, $unit_id)
|
|
{
|
|
return $query->where('items.unit_id', $unit_id);
|
|
}
|
|
|
|
public function scopeWhereOrder($query, $orderByField, $orderBy)
|
|
{
|
|
$query->orderBy($orderByField, $orderBy);
|
|
}
|
|
|
|
public function scopeWhereItem($query, $item_id)
|
|
{
|
|
$query->orWhere('id', $item_id);
|
|
}
|
|
|
|
public function scopeApplyFilters($query, array $filters)
|
|
{
|
|
$filters = collect($filters);
|
|
|
|
if ($filters->get('search')) {
|
|
$query->whereSearch($filters->get('search'));
|
|
}
|
|
|
|
if ($filters->get('price')) {
|
|
$query->wherePrice($filters->get('price'));
|
|
}
|
|
|
|
if ($filters->get('unit_id')) {
|
|
$query->whereUnit($filters->get('unit_id'));
|
|
}
|
|
|
|
if ($filters->get('item_id')) {
|
|
$query->whereItem($filters->get('item_id'));
|
|
}
|
|
|
|
if ($filters->get('orderByField') || $filters->get('orderBy')) {
|
|
$field = $filters->get('orderByField') ? $filters->get('orderByField') : 'name';
|
|
$orderBy = $filters->get('orderBy') ? $filters->get('orderBy') : 'asc';
|
|
$query->whereOrder($field, $orderBy);
|
|
}
|
|
}
|
|
|
|
public function scopePaginateData($query, $limit)
|
|
{
|
|
if ($limit == 'all') {
|
|
return $query->get();
|
|
}
|
|
|
|
return $query->paginate($limit);
|
|
}
|
|
|
|
public function getFormattedCreatedAtAttribute($value)
|
|
{
|
|
$dateFormat = CompanySetting::getSetting('carbon_date_format', request()->header('company'));
|
|
|
|
return Carbon::parse($this->created_at)->translatedFormat($dateFormat);
|
|
}
|
|
|
|
public function taxes(): HasMany
|
|
{
|
|
return $this->hasMany(Tax::class)
|
|
->where('invoice_item_id', null)
|
|
->where('estimate_item_id', null);
|
|
}
|
|
|
|
public function scopeWhereCompany($query)
|
|
{
|
|
$query->where('items.company_id', request()->header('company'));
|
|
}
|
|
|
|
public function invoiceItems(): HasMany
|
|
{
|
|
return $this->hasMany(InvoiceItem::class);
|
|
}
|
|
|
|
public function estimateItems(): HasMany
|
|
{
|
|
return $this->hasMany(EstimateItem::class);
|
|
}
|
|
}
|