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 <[email protected]>
This commit is contained in:
2026-11-25 09:04:43 +02:00
co-authored by Claude Sonnet 5.5
parent 2de3733a3f
commit b13310b815
2 changed files with 18 additions and 1 deletions
@@ -16,7 +16,8 @@ class EloquentBilledRevenueQueries implements BilledRevenueQueries
->join('billing_invoices', 'billing_invoices.id', '=', 'billing_lines.billing_invoice_id') ->join('billing_invoices', 'billing_invoices.id', '=', 'billing_lines.billing_invoice_id')
->join('billing_exports', 'billing_exports.id', '=', 'billing_invoices.billing_export_id') ->join('billing_exports', 'billing_exports.id', '=', 'billing_invoices.billing_export_id')
->where('billing_exports.status', ExportStatus::Created->value) ->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') ->groupBy('billing_lines.customer_job_id')
->select('billing_lines.customer_job_id') ->select('billing_lines.customer_job_id')
->selectRaw(self::sumOf(LineKind::Hours, 'net_cents').' as hours_cents') ->selectRaw(self::sumOf(LineKind::Hours, 'net_cents').' as hours_cents')
@@ -99,6 +99,22 @@ it('reports billing, costs and margins by job and service unit', function () {
->toContain(';-12000'); ->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 () { 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'); profitHours(profitJob(['name' => 'Tuntityö', 'payment_basis' => 'hourly'], [$this->video->id]), 2, '2026-11-03');