File: /var/www/ipsremont-demo/app/Models/Part.php
<?php
namespace App\Models;
use App\Helpers\CurrencyHelper;
use App\Traits\My;
use App\Traits\Sortable;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @property int $id
* @property string $name
* @property string $item
* @property float $price
* @property string $foto
* @property string $external_id
* @property string $external_type
* @property string $deleted_at
* @property string $created_at
* @property string $updated_at
*
* @property DeviceParts $device_parts
* @property SchemaParts $schema_parts
*/
class Part extends BaseModel
{
use SoftDeletes, Sortable, My;
protected $table = 'parts';
public static $imagePath = '/demo-images/';
protected $fillable = [
'name',
'item',
'price',
'foto',
'external_id',
'external_type',
'updated_at',
];
protected static $labels = [
'name' => 'parts.name',
'item' => 'parts.item',
'price' => 'parts.price',
'foto' => 'parts.foto',
'external_id' => 'parts.external_id',
'external_type' => 'parts.external_type',
];
public static function columns(): array
{
return [
'fields' => [
[
'displayName' => 'grid.name',
'field' => 'name',
'sort' => true,
'sortType' => 'asc',
'class' => 'text-left',
],
[
'displayName' => 'parts.external_id',
'field' => 'external_id',
'sort' => true,
'sortType' => 'asc',
'class' => 'w-100px',
],
[
'displayName' => 'grid.price',
'field' => 'price',
'sort' => true,
'sortType' => 'asc',
'class' => 'text-right w-130px',
],
],
'sortDefault' => [
['field' => 'name', 'sort' => 'asc'],
['field' => 'external_id', 'sort' => 'asc'],
],
];
}
public static function columnsAnalog()
{
$columns = self::columns();
if (can([Permission::orders, Permission::actions])) {
$columns['fields']['storage'] = [
"displayName" => 'parts.storage',
"field" => "storage",
"sort" => false,
"sortType" => 'asc',
];
$columns['fields']['actions'] = [
"displayName" => '',
"field" => 'action',
"sort" => false,
'class' => 'w-150px',
];
}
return $columns;
}
public function getFullName()
{
return $this->external_id . ' ' . $this->name;
}
public function getName()
{
return $this->name;
}
public function getImage()
{
return ($this->foto) ? static::$imagePath . $this->foto : '';
}
public function analogs()
{
$normal = PartAnalog::where('part_external_id', $this->external_id)->get()->pluck('analog_external_id')->toArray();
$back = PartAnalog::where('analog_external_id', $this->external_id)->get()->pluck('part_external_id')->toArray();
$all = array_merge($normal, $back);
if (empty($all)) {
return false;
}
return Part::whereIn('external_id', $all)->orderBy('name', 'asc')->get();
}
public function schema_parts(): HasOne
{
return $this->hasOne(SchemaParts::class, 'part_external_id', 'external_id');
}
public function device_parts(): HasOne
{
return $this->hasOne(DeviceParts::class);
}
public function getAmount($cart, $id)
{
return in_array($id, $cart);
}
public function getItemName()
{
$lang = __('parts.' . $this->item);
return ($lang <> 'parts.' . $this->item) ? $lang : $this->item;
}
public function getStoreCount(): int
{
$count = 0;
$partsStorage = PartsStorage::query()->where('part_id', $this->id)->get();
foreach ($partsStorage as $row) {
$count += $row->amount;
}
return $count;
}
public function getPriceConverted($model = null, int $count = 1, bool $formatted = true)
{
if (isset($model) && Order::class == get_class($model) && !$model->isAllowEdit()) {
$date = $model->updated_at;
}
return CurrencyHelper::getPriceConverted($this->price * $count, $date ?? null, $formatted);
}
}