86 lines
1.9 KiB
PHP
86 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Models;
|
|
|
|
use App\Product;
|
|
use App\Variation;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class MissionItem extends Model
|
|
{
|
|
protected $table = 'maintenance_mission_items';
|
|
|
|
protected $fillable = [
|
|
'mission_id',
|
|
'item_type',
|
|
'spare_part_id',
|
|
'product_id',
|
|
'variation_id',
|
|
'tool_id',
|
|
'part_name',
|
|
'part_number',
|
|
'location_id',
|
|
'quantity_issued',
|
|
'quantity_returned',
|
|
'quantity_lost',
|
|
'unit',
|
|
'stock_deducted',
|
|
'status',
|
|
'notes',
|
|
];
|
|
|
|
protected $casts = [
|
|
'quantity_issued' => 'decimal:3',
|
|
'quantity_returned' => 'decimal:3',
|
|
'quantity_lost' => 'decimal:3',
|
|
'stock_deducted' => 'boolean',
|
|
];
|
|
|
|
public function mission()
|
|
{
|
|
return $this->belongsTo(FieldMission::class, 'mission_id');
|
|
}
|
|
|
|
public function sparePart()
|
|
{
|
|
return $this->belongsTo(SparePart::class, 'spare_part_id');
|
|
}
|
|
|
|
public function product()
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function variation()
|
|
{
|
|
return $this->belongsTo(Variation::class);
|
|
}
|
|
|
|
public function tool()
|
|
{
|
|
return $this->belongsTo(MaintenanceTool::class, 'tool_id');
|
|
}
|
|
|
|
public function displayName(): string
|
|
{
|
|
if ($this->item_type === 'tool' && $this->tool) {
|
|
return $this->tool->name;
|
|
}
|
|
if ($this->sparePart) {
|
|
return $this->sparePart->name;
|
|
}
|
|
|
|
return $this->part_name ?: '—';
|
|
}
|
|
|
|
public function quantityConsumed(): float
|
|
{
|
|
return max(0, (float) $this->quantity_issued - (float) $this->quantity_returned - (float) $this->quantity_lost);
|
|
}
|
|
|
|
public function quantityPendingReturn(): float
|
|
{
|
|
return max(0, (float) $this->quantity_issued - (float) $this->quantity_returned - (float) $this->quantity_lost);
|
|
}
|
|
}
|