File: /var/www/ipsremont-demo/app/Models/MovementThroughWarehouses.php
<?php
namespace App\Models;
use App\Helpers\UserHelper;
use App\Models\Repair\Repair;
use App\Models\Service\Service;
use App\Traits\My;
use App\Traits\Sortable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
/**
* @property int $id
* @property string $repair_id
* @property string $part_id
* @property string $amount
* @property string $type
* @property string $service_id
* @property string $created_at
* @property string $updated_at
* @property string $deleted_at
*
* @property Repair $repair
* @property Part $part
* @property Service $service
*/
class MovementThroughWarehouses extends BaseModel
{
use SoftDeletes, Sortable, My;
protected $table = 'movement_through_warehouses';
protected $fillable = [
'part_id',
'repair_id',
'amount',
'type',
'service_id',
];
protected static $labels = [
'part_id' => 'movement.part_id',
'repair_id' => 'movement.repair_id',
'amount' => 'movement.amount',
'type' => 'movement.type',
'created_at' => 'movement.created_at',
];
public static function columns(): array
{
return [
'fields' => [
[
'displayName' => 'movement.repair_id',
'field' => 'repair_id',
'sort' => true,
'sortType' => 'asc',
'class' => 'w-100px',
],
[
'displayName' => 'movement.part_id',
'field' => 'part_id',
'sort' => true,
'sortType' => 'asc',
'class' => 'w-100px',
],
[
'displayName' => 'movement.warehouseAmounts',
'field' => 'amount',
'sort' => true,
'sortType' => 'asc',
'class' => 'w-100px',
],
[
'displayName' => 'movement.amount',
'field' => 'amount',
'sort' => true,
'sortType' => 'asc',
'class' => 'w-100px',
],
[
'displayName' => 'movement.created_at',
'field' => 'created_at',
'sort' => true,
'sortType' => 'asc',
'class' => 'w-100px',
],
],
'sortDefault' => [
[
'field' => 'id',
'sort' => 'desc',
],
],
];
}
public function repair(): BelongsTo
{
return $this->belongsTo(Repair::class);
}
public function part(): BelongsTo
{
return $this->belongsTo(Part::class);
}
public function service(): BelongsTo
{
return $this->belongsTo(Service::class);
}
/**
* Scope a query to only include My items
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeMy($query)
{
if (!Auth::check()) {
return $query;
}
$user = UserHelper::getUser();
if ($user->isService()) {
return $query
->where(['service_id' => $user->getCurrentServiceId()]);
}
if ($user->isManager()) {
abort('403');
}
}
}