From b13310b8158c479e15687c12a964cad07bc2a7b2 Mon Sep 17 00:00:00 2001 From: Petri Saarinen Date: Wed, 25 Nov 2026 09:04:43 +0200 Subject: [PATCH] Count billing materials without a billing month in reports Materials created before billing months existed have no period, so the profitability report left out what they billed. Their month is now the month they were created in, as in the export repository. Co-Authored-By: Claude Sonnet 5.5 --- .../EloquentBilledRevenueQueries.php | 3 ++- .../Modules/Reports/ProfitabilityReportTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/Modules/Billing/Infrastructure/Persistence/Repositories/EloquentBilledRevenueQueries.php b/app/Modules/Billing/Infrastructure/Persistence/Repositories/EloquentBilledRevenueQueries.php index 548b217..c6eab3b 100644 --- a/app/Modules/Billing/Infrastructure/Persistence/Repositories/EloquentBilledRevenueQueries.php +++ b/app/Modules/Billing/Infrastructure/Persistence/Repositories/EloquentBilledRevenueQueries.php @@ -16,7 +16,8 @@ class EloquentBilledRevenueQueries implements BilledRevenueQueries ->join('billing_invoices', 'billing_invoices.id', '=', 'billing_lines.billing_invoice_id') ->join('billing_exports', 'billing_exports.id', '=', 'billing_invoices.billing_export_id') ->where('billing_exports.status', ExportStatus::Created->value) - ->whereBetween('billing_exports.period', [$fromMonth, $toMonth]) + // Materials created before billing months existed have none: their month is the month they were created in, as in the export repository. + ->whereRaw('COALESCE(billing_exports.period, SUBSTR(billing_exports.created_at, 1, 7)) BETWEEN ? AND ?', [$fromMonth, $toMonth]) ->groupBy('billing_lines.customer_job_id') ->select('billing_lines.customer_job_id') ->selectRaw(self::sumOf(LineKind::Hours, 'net_cents').' as hours_cents') diff --git a/tests/Feature/Modules/Reports/ProfitabilityReportTest.php b/tests/Feature/Modules/Reports/ProfitabilityReportTest.php index 5ed2651..30a5f9d 100644 --- a/tests/Feature/Modules/Reports/ProfitabilityReportTest.php +++ b/tests/Feature/Modules/Reports/ProfitabilityReportTest.php @@ -99,6 +99,22 @@ it('reports billing, costs and margins by job and service unit', function () { ->toContain(';-12000'); }); +it('counts a fixed price billed by a material without a billing month in the month it was created', function () { + $job = profitJob(['name' => 'Tarra', 'payment_basis' => 'fixed_quote', 'quoted_amount_cents' => 40000, 'status' => JobStatus::Done], [$this->video->id]); + profitHours($job, 1, '2026-11-20'); + + $exportId = app(OpenBillingDraft::class)->execute($this->manager->id); + app(AddJobToDraft::class)->execute($exportId, $job->id); + app(FinalizeBillingExport::class)->execute($exportId, $this->manager->id); + DB::table('billing_exports')->where('id', $exportId)->update(['period' => null, 'created_at' => '2026-11-24 11:03:07']); + + $november = Livewire::withQueryParams(['alku' => '2026-11-01', 'loppu' => '2026-11-30'])->test('reports::profitability')->instance()->report(); + $december = Livewire::withQueryParams(['alku' => '2026-12-01', 'loppu' => '2026-12-31'])->test('reports::profitability')->instance()->report(); + + expect($november->jobs[0]->profit->workCents)->toBe(40000) + ->and($december->jobs)->toBe([]); +}); + it('asks for the internal hourly cost before showing margins', function () { profitHours(profitJob(['name' => 'Tuntityƶ', 'payment_basis' => 'hourly'], [$this->video->id]), 2, '2026-11-03');