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
+21 -1
View File
@@ -6,7 +6,9 @@ use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\Machines\Application\UseCases\MachineWorkList;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceItemModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
@@ -30,6 +32,12 @@ beforeEach(function () {
'description' => 'Kaksi kappaletta',
]);
app(MachineWorkList::class)->planTask($this->task->id, $this->machine->id, null);
$counter = MachineCounterModel::query()->create(['machine_id' => $this->machine->id, 'name' => 'Tulosteet', 'unit' => 'piece', 'sort_order' => 1]);
MaintenanceItemModel::query()->create([
'machine_id' => $this->machine->id, 'name' => 'Pään puhdistus', 'machine_counter_id' => $counter->id, 'interval_milli' => 1_000_000,
'period' => 'day', 'period_count' => 1, 'starts_on' => now()->subDays(2)->toDateString(), 'warning_percent' => 90,
]);
});
it('has no accessibility issues on the machine pages', function (string $page, string $device, string $colorScheme) {
@@ -38,6 +46,7 @@ it('has no accessibility issues on the machine pages', function (string $page, s
'show' => route('machines.show', $this->machine->id),
'form' => route('machines.edit', $this->machine->id),
'job' => route('jobs.show', $this->job->id).'#task-'.$this->task->id,
'maintenance' => route('machines.maintenance.create', $this->machine->id),
};
$visit = visit($url);
@@ -47,7 +56,7 @@ it('has no accessibility issues on the machine pages', function (string $page, s
$visit->assertSee($page === 'form' ? __('machines.form.edit_title') : 'Suurkuvatulostin')
->assertNoJavaScriptErrors()
->assertNoAccessibilityIssues(level: 2);
})->with(['index', 'show', 'form', 'job'])->with([
})->with(['index', 'show', 'form', 'job', 'maintenance'])->with([
'desktop, light' => ['desktop', 'light'],
'desktop, dark' => ['desktop', 'dark'],
'mobile, light' => ['mobile', 'light'],
@@ -64,3 +73,14 @@ it('moves a task to another machine with the keyboard from the work list', funct
->assertSee('Tehtävä siirretty koneen Tarraleikkuri työlistalle')
->assertDontSee('Kiiltävä laminointi');
});
it('records maintenance done with the keyboard', function () {
visit(route('machines.show', $this->machine->id))
->assertSee('Huolto myöhässä')
->click(__('machines.maintenance.done'))
->fill('#record-note', 'Puhdistettu')
->click(__('machines.maintenance.confirm_done'))
->assertSee('Huolto Pään puhdistus kirjattu.')
->assertSee('Kunnossa')
->assertNoAccessibilityIssues(level: 2);
});
@@ -0,0 +1,122 @@
<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceItemModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceRecordModel;
use App\Modules\Notifications\Infrastructure\Persistence\Eloquent\NotificationModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->travelTo(now()->setTimezone(config('app.business_timezone'))->setTime(10, 0));
$this->manager = User::factory()->management()->create(['name' => 'Maija Johto']);
$this->employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']);
$print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
$print->update(['uses_stock' => true]);
$this->machine = MachineModel::factory()->inUnits([$print->id])->create(['name' => 'Suurkuvatulostin', 'responsible_user_id' => $this->employee->id]);
$this->prints = MachineCounterModel::query()->create(['machine_id' => $this->machine->id, 'name' => 'Tulosteet', 'unit' => 'piece', 'sort_order' => 1]);
$this->drum = StockItemModel::factory()->create(['name' => 'Vaihtorumpu']);
});
it('adds maintenance items with a counter interval, time intervals or both, and needs one of them', function () {
$this->actingAs($this->manager);
Livewire::test('machines::maintenance-item-form', ['machine' => $this->machine->id])
->set('name', 'Pään puhdistus')
->call('save')
->assertHasErrors('period')
->set('period', 'day')
->call('save')
->assertHasNoErrors();
Livewire::test('machines::maintenance-item-form', ['machine' => $this->machine->id])
->set('name', 'Rummun vaihto')
->set('counterId', (string) $this->prints->id)
->set('interval', '20000')
->set('period', 'month')
->set('periodCount', '6')
->set('stockItemId', (string) $this->drum->id)
->call('save')
->assertHasNoErrors();
expect(MaintenanceItemModel::query()->where('name', 'Rummun vaihto')->sole()->only(['interval_milli', 'period_count', 'stock_quantity_milli']))
->toBe(['interval_milli' => 20_000_000, 'period_count' => 6, 'stock_quantity_milli' => 1_000]);
$this->actingAs($this->employee);
$this->get(route('machines.maintenance.create', $this->machine->id))->assertForbidden();
});
it('shows a maintenance request when due and starts the interval again with a maintenance record that uses the stock item', function () {
$item = MaintenanceItemModel::query()->create([
'machine_id' => $this->machine->id, 'name' => 'Rummun vaihto', 'machine_counter_id' => $this->prints->id, 'interval_milli' => 20_000_000,
'counter_baseline_milli' => 0, 'period' => 'week', 'period_count' => 1, 'starts_on' => now()->toDateString(), 'warning_percent' => 90,
'stock_item_id' => $this->drum->id, 'stock_quantity_milli' => 1_000,
]);
$this->actingAs($this->employee);
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
->assertSee('Kunnossa')
->set('readings.'.$this->prints->id, '19 000')
->call('saveReading', $this->prints->id)
->assertSee('Huolto lähestyy')
->assertSee("jäljellä 1\u{00A0}000 kpl", false);
$this->travel(8)->days();
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
->assertSee('Huolto myöhässä')
->call('startRecord', $item->id)
->assertSet('recordReading', '19000')
->set('recordReading', '19500')
->set('recordNote', 'Rumpu vaihdettu')
->call('record')
->assertSet('message', 'Huolto Rummun vaihto kirjattu.')
->assertSee('Kunnossa')
->assertSee('Rumpu vaihdettu');
$record = MaintenanceRecordModel::query()->sole();
$use = StockMovementModel::query()->sole();
expect($record->counter_reading_milli)->toBe(19_500_000)
->and($use->only(['stock_item_id', 'machine_id', 'maintenance_record_id', 'quantity_milli']))
->toBe(['stock_item_id' => $this->drum->id, 'machine_id' => $this->machine->id, 'maintenance_record_id' => $record->id, 'quantity_milli' => -1_000]);
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])->call('confirmCancel', $record->id)->assertForbidden();
$this->actingAs($this->manager);
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
->call('confirmCancel', $record->id)
->call('cancelRecord')
->assertSee('Peruttu')
->assertSee('Huolto myöhässä');
});
it('notifies the responsible person of a maintenance request once', function () {
MaintenanceItemModel::query()->create([
'machine_id' => $this->machine->id, 'name' => 'Pään puhdistus', 'period' => 'day', 'period_count' => 1,
'starts_on' => now()->subDay()->toDateString(), 'warning_percent' => 90,
]);
$this->artisan('notifications:maintenance-due')->assertSuccessful();
$this->artisan('notifications:maintenance-due')->assertSuccessful();
$notification = NotificationModel::query()->sole();
expect($notification->user_id)->toBe($this->employee->id)
->and($notification->type->value)->toBe('maintenance_due');
$this->actingAs($this->employee);
Livewire::test('notifications::bell')->assertSee('Koneen Suurkuvatulostin huolto Pään puhdistus on myöhässä.');
$this->actingAs($this->manager);
$this->get(route('home'))->assertOk();
Livewire::test('dashboard::management')->assertSee('1 konehuolto myöhässä')->assertSee('Suurkuvatulostin: Pään puhdistus');
});
@@ -0,0 +1,38 @@
<?php
use App\Modules\Machines\Domain\Enums\MaintenanceLevel;
use App\Modules\Machines\Domain\Enums\MaintenancePeriod;
use App\Modules\Machines\Domain\Services\MaintenanceStatus;
$day = fn (string $date): DateTimeImmutable => new DateTimeImmutable($date);
it('counts a counter interval from the reading at the last maintenance', function () use ($day) {
$state = fn (int $reading) => MaintenanceStatus::evaluate(20_000_000, 100_000_000, $reading, null, 1, $day('2026-11-01'), 90, $day('2026-11-26'));
expect($state(110_000_000)->level)->toBe(MaintenanceLevel::Ok)
->and($state(110_000_000)->remainingMilli)->toBe(10_000_000)
->and($state(118_000_000)->level)->toBe(MaintenanceLevel::Approaching)
->and($state(120_000_000)->level)->toBe(MaintenanceLevel::Overdue)
->and($state(121_000_000)->remainingMilli)->toBe(-1_000_000);
});
it('makes a daily, weekly, monthly or yearly maintenance due after its periods', function (MaintenancePeriod $period, int $count, string $since, string $due) use ($day) {
expect(MaintenanceStatus::evaluate(null, 0, null, $period, $count, $day($since), 90, $day($since))->dueOn)->toBe($due);
})->with([
'daily' => [MaintenancePeriod::Day, 1, '2026-11-26', '2026-11-27'],
'every other week' => [MaintenancePeriod::Week, 2, '2026-11-26', '2026-12-10'],
'monthly from the 31st' => [MaintenancePeriod::Month, 1, '2026-01-31', '2026-02-28'],
'yearly from 29 February' => [MaintenancePeriod::Year, 1, '2028-02-29', '2029-02-28'],
]);
it('warns before a time interval but not before daily maintenance, and takes the worse of two intervals', function () use ($day) {
$weekly = fn (string $today) => MaintenanceStatus::evaluate(null, 0, null, MaintenancePeriod::Week, 1, $day('2026-11-16'), 80, $day($today))->level;
$daily = fn (string $today) => MaintenanceStatus::evaluate(null, 0, null, MaintenancePeriod::Day, 1, $day('2026-11-26'), 50, $day($today))->level;
expect($weekly('2026-11-20'))->toBe(MaintenanceLevel::Ok)
->and($weekly('2026-11-22'))->toBe(MaintenanceLevel::Approaching)
->and($weekly('2026-11-23'))->toBe(MaintenanceLevel::Overdue)
->and($daily('2026-11-26'))->toBe(MaintenanceLevel::Ok)
->and($daily('2026-11-27'))->toBe(MaintenanceLevel::Overdue)
->and(MaintenanceStatus::evaluate(1_000, 0, 100, MaintenancePeriod::Month, 1, $day('2026-10-01'), 90, $day('2026-11-02'))->level)->toBe(MaintenanceLevel::Overdue);
});