employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']); $this->actingAs($this->employee); $this->print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole(); $this->print->update(['uses_stock' => true]); $this->installation = WorkTypeModel::query()->where('name', 'asennus')->sole(); $this->printing = WorkTypeModel::query()->where('name', 'painotyö')->sole(); $this->job = CustomerJobModel::factory()->create(['name' => 'Ikkunateippaus', 'status' => 'order']); DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $this->print->id]); $this->task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->installation->id, 'title' => 'Asennus']); $this->transfer = StockItemModel::factory()->create(['name' => 'Siirtotarrapaperi', 'unit' => 'square_metre']); }); function materials(object $test): mixed { return Livewire::test('inventory::task-materials', ['jobId' => $test->job->id, 'taskId' => $test->task->id, 'taskTitle' => 'Asennus']); } it('records material used on a task found by search, and undoes it the same day', function () { materials($this) ->assertSee(__('inventory.task.title')) ->set('search', 'siirto') ->assertSee('Siirtotarrapaperi') ->call('selectItem', $this->transfer->id) ->set('quantity', '2,5') ->call('record') ->assertHasNoErrors() ->assertSet('message', "Kirjattu Siirtotarrapaperi 2,5\u{00A0}m². Saldo nyt -2,5\u{00A0}m²."); $use = StockMovementModel::query()->sole(); expect($use->only(['task_id', 'customer_job_id', 'quantity_milli', 'user_id'])) ->toBe(['task_id' => $this->task->id, 'customer_job_id' => $this->job->id, 'quantity_milli' => -2_500, 'user_id' => $this->employee->id]); materials($this)->call('undo', $use->id)->assertSet('message', "Käyttö peruttu: Siirtotarrapaperi 2,5\u{00A0}m²."); expect((int) StockMovementModel::query()->where('task_id', $this->task->id)->sum('quantity_milli'))->toBe(0); }); it('keeps a task with material from being removed', function () { materials($this)->call('selectItem', $this->transfer->id)->call('record'); $this->actingAs(User::factory()->management()->create()); Livewire::test('jobs::show', ['job' => $this->job->id]) ->call('confirmTaskRemoval', $this->task->id) ->call('removeTask') ->assertSee('Tehtävää Asennus ei poistettu, koska sille on kirjattu tunteja tai käytettyä materiaalia.'); expect(TaskModel::query()->whereKey($this->task->id)->exists())->toBeTrue(); }); it('hides the section in units without stock and shows earlier use read-only', function () { materials($this)->call('selectItem', $this->transfer->id)->call('record'); $this->print->update(['uses_stock' => false]); materials($this) ->assertSee('Siirtotarrapaperi') ->assertDontSee(__('inventory.task.search')) ->call('selectItem', $this->transfer->id) ->assertForbidden(); StockMovementModel::query()->delete(); materials($this)->assertDontSee(__('inventory.task.title')); }); it('suggests the default materials of a workflow step and records them only when confirmed', function () { $templateId = app(AddWorkflowTemplate::class)->execute(new WorkflowTemplateData('Ikkunateippaus', $this->printing->id, [ new WorkflowStepData('Tulostus ja leikkaus'), new WorkflowStepData('Asennus', null, $this->installation->id, [['stockItemId' => $this->transfer->id, 'quantity' => '3']]), ])); app(AddTasksFromWorkflowTemplate::class)->execute($this->job->id, $templateId, $this->employee->id); $this->task = TaskModel::query()->where('title', 'Asennus')->latest('id')->first(); expect(StockMovementModel::query()->count())->toBe(0); $component = materials($this) ->assertSee(__('inventory.task.suggestions')) ->assertSet('suggestionQuantities', fn (array $quantities) => array_values($quantities) === ['3']); $suggestionId = (int) array_key_first($component->get('suggestionQuantities')); $component->set('suggestionQuantities.'.$suggestionId, '3,5') ->call('confirmSuggestion', $suggestionId) ->assertHasNoErrors() ->assertDontSee(__('inventory.task.suggestions')); expect(StockMovementModel::query()->sole()->quantity_milli)->toBe(-3_500) ->and(DB::table('task_material_suggestions')->count())->toBe(0); }); it('edits default materials on the workflow template form', function () { $this->actingAs(User::factory()->management()->create()); Livewire::test('catalog::workflow-template-form') ->set('form.name', 'Teippaus') ->set('form.work_type_id', (string) $this->installation->id) ->set('form.steps.0.title', 'Asennus') ->assertSee(__('catalog.workflows.materials')) ->call('addMaterial', 0) ->set('form.steps.0.materials.0.stock_item_id', (string) $this->transfer->id) ->set('form.steps.0.materials.0.quantity', 'abc') ->call('save') ->assertHasErrors('form.steps.0.materials.0.quantity') ->set('form.steps.0.materials.0.quantity', '1,25') ->call('save') ->assertHasNoErrors(); expect(DB::table('workflow_template_step_materials')->sole()->quantity_milli)->toBe(1_250); $templateId = (int) DB::table('workflow_templates')->where('name', 'Teippaus')->value('id'); Livewire::test('catalog::workflow-template-form', ['template' => $templateId]) ->assertSet('form.steps.0.materials.0.quantity', '1,25'); });