123 lines
6.0 KiB
PHP
123 lines
6.0 KiB
PHP
<?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');
|
|
});
|