76 lines
3.8 KiB
PHP
76 lines
3.8 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
|
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemGroupModel;
|
|
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
|
|
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
|
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
|
use App\Modules\Reports\Application\UseCases\ProfitabilityByJob;
|
|
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Livewire\Livewire;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
beforeEach(function () {
|
|
$this->manager = User::factory()->management()->create();
|
|
$this->actingAs($this->manager);
|
|
ServiceUnitModel::query()->where('name', 'Painotuotanto')->update(['uses_stock' => true]);
|
|
|
|
$this->machine = MachineModel::factory()->create(['name' => 'Suurkuvatulostin']);
|
|
$this->job = CustomerJobModel::factory()->create(['name' => 'Messut', 'number' => '2026-0042']);
|
|
$papers = StockItemGroupModel::factory()->create(['name' => 'Paperit']);
|
|
$this->paper = StockItemModel::factory()->create(['stock_item_group_id' => $papers->id, 'name' => 'Valokuvapaperi', 'unit' => 'sheet', 'reorder_level_milli' => 50_000]);
|
|
|
|
$move = fn (string $kind, int $milli, int $value, string $day, array $extra = []) => StockMovementModel::query()->create([
|
|
'stock_item_id' => $this->paper->id, 'kind' => $kind, 'quantity_milli' => $milli, 'unit_cost_hundredths' => 5_000,
|
|
'value_cents' => $value, 'occurred_on' => $day, 'recorded_at' => now(), 'user_id' => $this->manager->id, ...$extra,
|
|
]);
|
|
|
|
$move('purchase', 100_000, 5_000, '2026-10-15');
|
|
$move('consumption', -20_000, -1_000, '2026-11-05', ['machine_id' => $this->machine->id, 'customer_job_id' => $this->job->id, 'reason' => null]);
|
|
$use = $move('consumption', -10_000, -500, '2026-11-10', ['reason' => 'Koeajo']);
|
|
$move('reversal', 10_000, 500, '2026-11-10', ['reverses_movement_id' => $use->id, 'reason' => 'Koeajo']);
|
|
$move('write_off', -5_000, -250, '2026-11-30', ['reason' => 'Vaurioitunut']);
|
|
});
|
|
|
|
it('reports stock usage by item, machine and job with reversals netted', function () {
|
|
Livewire::test('reports::stock-usage')
|
|
->set('from', '2026-11-01')
|
|
->set('to', '2026-11-30')
|
|
->assertSee('Yhteensä 12,50')
|
|
->assertSee('Valokuvapaperi')
|
|
->assertSee('Suurkuvatulostin')
|
|
->assertSee('2026-0042 Messut');
|
|
|
|
$csv = $this->get(route('reports.csv', ['report' => 'stock-usage', 'alku' => '2026-11-01', 'loppu' => '2026-11-30']))->assertOk()->streamedContent();
|
|
|
|
expect($csv)->toContain('Valokuvapaperi;Paperit;arkki;20;1000;5;250;0;0;1250');
|
|
});
|
|
|
|
it('reports the stock value at the end of a day and the items to order', function () {
|
|
Livewire::test('reports::stock-value')
|
|
->set('day', '2026-11-05')
|
|
->assertSee("Varaston arvo 5.11.2026: 40,00\u{00A0}€", false)
|
|
->set('day', '2026-11-30')
|
|
->assertSee("37,50\u{00A0}€", false)
|
|
->assertDontSee(__('reports.stock_value.to_order'));
|
|
|
|
$this->get(route('reports.csv', ['report' => 'stock-value', 'paiva' => '2026-11-30']))
|
|
->assertOk()
|
|
->assertDownload('varaston-arvo-2026-11-30.csv');
|
|
});
|
|
|
|
it('adds the job\'s material cost to its profitability', function () {
|
|
$report = app(ProfitabilityByJob::class)->execute(new ReportPeriod('2026-11-01', '2026-11-30'), true);
|
|
|
|
expect(collect($report->jobs)->firstWhere('job.id', $this->job->id)->profit->materialCostCents)->toBe(1_000);
|
|
});
|
|
|
|
it('shows stock reports only to management when stock is used', function () {
|
|
$this->actingAs(User::factory()->employee()->create());
|
|
$this->get(route('reports.stock-usage'))->assertForbidden();
|
|
});
|