Track machine counters and maintenance with counter and time intervals

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:52:49 +02:00
co-authored by Claude Sonnet 5.5
parent aac579257a
commit 3e1c671949
53 changed files with 2636 additions and 6 deletions
@@ -0,0 +1,65 @@
<?php
namespace App\Modules\Machines\Infrastructure\Persistence\Eloquent;
use App\Modules\Machines\Domain\Enums\MaintenancePeriod;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* Persistence model, used only inside the Machines Infrastructure layer.
*
* @property int $id
* @property int $machine_id
* @property string $name
* @property int|null $machine_counter_id
* @property int|null $interval_milli
* @property int $counter_baseline_milli
* @property MaintenancePeriod|null $period
* @property int $period_count
* @property string $starts_on Y-m-d
* @property int $warning_percent
* @property string|null $instructions
* @property int|null $stock_item_id
* @property int|null $stock_quantity_milli
* @property bool $is_active
*/
#[Table('maintenance_items')]
#[Fillable([
'machine_id', 'name', 'machine_counter_id', 'interval_milli', 'counter_baseline_milli', 'period', 'period_count',
'starts_on', 'warning_percent', 'instructions', 'stock_item_id', 'stock_quantity_milli', 'is_active',
])]
class MaintenanceItemModel extends Model
{
/**
* The latest maintenance that was not cancelled.
*
* @return HasOne<MaintenanceRecordModel, $this>
*/
public function lastRecord(): HasOne
{
return $this->hasOne(MaintenanceRecordModel::class, 'maintenance_item_id')
->ofMany(['performed_at' => 'max', 'id' => 'max'], fn ($query) => $query->whereNull('cancelled_at'));
}
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'machine_id' => 'integer',
'machine_counter_id' => 'integer',
'interval_milli' => 'integer',
'counter_baseline_milli' => 'integer',
'period' => MaintenancePeriod::class,
'period_count' => 'integer',
'warning_percent' => 'integer',
'stock_item_id' => 'integer',
'stock_quantity_milli' => 'integer',
'is_active' => 'boolean',
];
}
}