80 lines
1.9 KiB
PHP
80 lines
1.9 KiB
PHP
<?php
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
|
||
class StockSection extends Model
|
||
{
|
||
protected $table = 'stock_section';
|
||
protected $primaryKey = 'section_id';
|
||
public $timestamps = true;
|
||
|
||
protected $fillable = [
|
||
'section_symbol',
|
||
'section_name',
|
||
'position_id',
|
||
'capacity',
|
||
'retrievable'
|
||
];
|
||
|
||
protected $appends = [
|
||
'storage_address',
|
||
];
|
||
|
||
public function position()
|
||
{
|
||
return $this->belongsTo(StockPosition::class, 'position_id', 'position_id');
|
||
}
|
||
|
||
// If you have a StockEntry model, you can set up a many-to-many via the pivot:
|
||
public function entries()
|
||
{
|
||
return $this->belongsToMany(
|
||
StockEntry::class, // your entry model
|
||
'stock_entries2section', // pivot table
|
||
'section_id', // this modelʼs FK on pivot
|
||
'entry_id' // other modelʼs FK on pivot
|
||
)
|
||
->withPivot('count')
|
||
->withTimestamps();
|
||
}
|
||
|
||
public function occupied(): bool
|
||
{
|
||
return $this->entries()->exists();
|
||
}
|
||
|
||
|
||
public function getStorageAddressAttribute(): string
|
||
{
|
||
// calls your existing method, which will
|
||
// loadMissing the relations if needed
|
||
return $this->storageAddress();
|
||
}
|
||
|
||
public function storageAddress(): string
|
||
{
|
||
// eager-load the whole hierarchy if not already
|
||
$this->loadMissing('position.shelf.rack.line.room');
|
||
|
||
|
||
$position = $this->position;
|
||
$shelf = $position->shelf;
|
||
$rack = $shelf->rack;
|
||
$line = $rack->line;
|
||
$room = $line->room;
|
||
|
||
return sprintf(
|
||
'%s-%s-%s-%s-%s-%s',
|
||
$room->room_symbol,
|
||
$line->line_symbol,
|
||
$rack->rack_symbol,
|
||
$shelf->shelf_symbol,
|
||
$position->position_symbol,
|
||
$this->section_symbol
|
||
);
|
||
|
||
}
|
||
|
||
}
|