actingAs(User::factory()->management()->create()); $this->editing = WorkTypeModel::factory()->create(['name' => 'Editointi', 'hourly_rate_cents' => 8050]); $this->unpriced = WorkTypeModel::factory()->create(['name' => 'Suunnittelu']); $this->customer = CustomerModel::factory()->company()->create(['name' => 'Asiakas Oy']); }); function unbilledJob(array $attributes): CustomerJobModel { return CustomerJobModel::factory()->create(array_merge(['customer_id' => test()->customer->id], $attributes)); } function unbilledHours(CustomerJobModel $job, WorkTypeModel $workType, float $hours, string $day, bool $approved = true): TimeEntryModel { $task = TaskModel::factory()->create(['customer_job_id' => $job->id, 'work_type_id' => $workType->id]); $factory = $approved ? TimeEntryModel::factory()->approved() : TimeEntryModel::factory(); return $factory->create(['task_id' => $task->id, 'hours' => $hours, 'worked_on' => $day]); } it('values unbilled hours of jobs billed by the hour and leaves out billed, internal and not billable work', function () { $hourly = unbilledJob(['name' => 'Tuntityö', 'payment_basis' => 'hourly']); unbilledHours($hourly, $this->editing, 2.5, '2026-11-02'); unbilledHours($hourly, $this->editing, 1, '2026-11-20', approved: false); unbilledHours($hourly, $this->unpriced, 1, '2026-11-03'); unbilledHours($hourly, $this->editing, 3, '2026-11-26'); $quote = unbilledJob(['name' => 'Tarjous', 'payment_basis' => 'fixed_quote']); unbilledHours($quote, $this->editing, 4, '2026-10-01'); $billedExport = DB::table('billing_exports')->insertGetId(['status' => 'created', 'period' => '2026-10', 'created_by' => auth()->id(), 'created_at' => now(), 'updated_at' => now()]); unbilledHours($hourly, $this->editing, 5, '2026-10-15')->update(['billing_export_id' => $billedExport]); unbilledHours(unbilledJob(['payment_basis' => 'not_billable']), $this->editing, 6, '2026-11-02'); unbilledHours(CustomerJobModel::factory()->create(['customer_id' => CustomerModel::query()->where('is_internal', true)->value('id')]), $this->editing, 7, '2026-11-02'); $report = Livewire::withQueryParams(['paiva' => '2026-11-25']) ->test('reports::unbilled') ->assertOk() ->assertSee('Tuntityö') ->assertSee(__('reports.unbilled.in_price')) ->instance() ->report(); expect($report->jobs)->toHaveCount(2) ->and($report->jobs[0]->job->name)->toBe('Tarjous') ->and($report->jobs[0]->valueCents)->toBeNull() ->and($report->jobs[1]->approvedQuarters)->toBe(14) ->and($report->jobs[1]->unapprovedQuarters)->toBe(4) ->and($report->jobs[1]->valueCents)->toBe(28175) ->and($report->jobs[1]->missingRate)->toBeTrue() ->and($report->jobs[1]->oldestDay)->toBe('2026-11-02') ->and($report->valueCents)->toBe(28175) ->and($report->approvedQuarters)->toBe(30); }); it('downloads the unbilled hours as CSV', function () { unbilledHours(unbilledJob(['name' => 'Tuntityö', 'payment_basis' => 'hourly']), $this->editing, 2.5, '2026-11-02'); $response = $this->get(route('reports.csv', ['report' => 'unbilled', 'paiva' => '2026-11-25'])) ->assertOk() ->assertDownload('laskuttamattomat-tunnit-2026-11-25.csv'); $content = substr($response->streamedContent(), 3); $lines = array_map(fn (string $line): array => str_getcsv($line, ';', '"', ''), array_values(array_filter(explode("\n", $content)))); expect($lines[0])->toBe(array_values(__('reports.unbilled.csv'))) ->and(array_slice($lines[1], 1))->toBe(['Tuntityö', 'Asiakas Oy', 'Alustava', 'Tuntityö', $lines[1][5], 'Editointi', '2,5', '0', '8050', '20125', '2026-11-02']); $this->get(route('reports.csv', ['report' => 'unbilled', 'paiva' => '2026-02-30']))->assertNotFound(); });