Files
InvoiceShelf/app/Models/Item.php
mchev 3259173066 Laravel 11 (#84)
* Convert string references to `::class`

PHP 5.5.9 adds the new static `class` property which provides the fully qualified class name. This is preferred over using strings for class names since the `class` property references are checked by PHP.

* Use Faker methods

Accessing Faker properties was deprecated in Faker 1.14.

* Convert route options to fluent methods

Laravel 8 adopts the tuple syntax for controller actions. Since the old options array is incompatible with this syntax, Shift converted them to use modern, fluent methods.

* Adopt class based routes

* Remove default `app` files

* Shift core files

* Streamline config files

* Set new `ENV` variables

* Default new `bootstrap/app.php`

* Re-register HTTP middleware

* Consolidate service providers

* Re-register service providers

* Re-register routes

* Re-register scheduled commands

* Bump Composer dependencies

* Use `<env>` tags for configuration

`<env>` tags have a lower precedence than system environment variables making it easier to overwrite PHPUnit configuration values in additional environments, such a CI.

Review this blog post for more details on configuration precedence when testing Laravel: https://jasonmccreary.me/articles/laravel-testing-configuration-precedence/

* Adopt anonymous migrations

* Rename `password_resets` table

* Convert `$casts` property to method

* Adopt Laravel type hints

* Mark base controller as `abstract`

* Remove `CreatesApplication` testing trait

* Shift cleanup

* Fix shift first issues

* Updating Rules for laravel 11, sanctum config and pint

* Fix Carbon issue on dashboard

* Temporary fix for tests while migration is issue fixed on laravel side

* Carbon needs numerical values, not strings

* Minimum php version

* Fix domain installation step not fetching the correct company_id

* Fix Role Policy wasn't properly registered

---------
2024-06-05 11:33:52 +02:00

180 lines
4.6 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;
use Illuminate\Support\Facades\Auth;
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(\App\Models\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)->format($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);
}
public static function createItem($request)
{
$data = $request->validated();
$data['company_id'] = $request->header('company');
$data['creator_id'] = Auth::id();
$company_currency = CompanySetting::getSetting('currency', $request->header('company'));
$data['currency_id'] = $company_currency;
$item = self::create($data);
if ($request->has('taxes')) {
foreach ($request->taxes as $tax) {
$item->tax_per_item = true;
$item->save();
$tax['company_id'] = $request->header('company');
$item->taxes()->create($tax);
}
}
$item = self::with('taxes')->find($item->id);
return $item;
}
public function updateItem($request)
{
$this->update($request->validated());
$this->taxes()->delete();
if ($request->has('taxes')) {
foreach ($request->taxes as $tax) {
$this->tax_per_item = true;
$this->save();
$tax['company_id'] = $request->header('company');
$this->taxes()->create($tax);
}
}
return Item::with('taxes')->find($this->id);
}
}