77 lines
1.9 KiB
PHP
Executable File
77 lines
1.9 KiB
PHP
Executable File
<?php
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Product extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'products';
|
|
|
|
protected $fillable = [
|
|
'default_fornitore_id',
|
|
'type',
|
|
'internal_code',
|
|
'canonical_key',
|
|
'name',
|
|
'brand',
|
|
'model',
|
|
'variant_group',
|
|
'variant_label',
|
|
'color_label',
|
|
'unit_measure',
|
|
'description',
|
|
'track_serials',
|
|
'is_active',
|
|
'meta',
|
|
];
|
|
|
|
protected $casts = [
|
|
'track_serials' => 'boolean',
|
|
'is_active' => 'boolean',
|
|
'meta' => 'array',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::created(function (self $product): void {
|
|
if (! is_string($product->internal_code) || trim($product->internal_code) === '') {
|
|
$product->forceFill([
|
|
'internal_code' => 'ART-' . str_pad((string) $product->id, 6, '0', STR_PAD_LEFT),
|
|
])->saveQuietly();
|
|
}
|
|
});
|
|
}
|
|
|
|
public function fornitore(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Fornitore::class, 'default_fornitore_id');
|
|
}
|
|
|
|
public function identifiers(): HasMany
|
|
{
|
|
return $this->hasMany(ProductIdentifier::class, 'product_id');
|
|
}
|
|
|
|
public function media(): HasMany
|
|
{
|
|
return $this->hasMany(ProductMedia::class, 'product_id');
|
|
}
|
|
|
|
public function serials(): HasMany
|
|
{
|
|
return $this->hasMany(ProductSerial::class, 'product_id');
|
|
}
|
|
|
|
public function offers(): HasMany
|
|
{
|
|
return $this->hasMany(ProductOffer::class, 'product_id');
|
|
}
|
|
}
|