Add the profitability report and the internal hourly cost

Profitability by job and service unit from a period's hours, expenses
and the created billing materials of its months. Hours are costed at one
internal hourly cost, set on the price list maintenance page; a job of
several service units is shared evenly between them.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 08:50:30 +02:00
co-authored by Claude Sonnet 5.5
parent 8079c272d2
commit 8aa62c8e02
31 changed files with 1110 additions and 1 deletions
@@ -0,0 +1,49 @@
<?php
namespace App\Modules\Billing\Infrastructure\Persistence\Repositories;
use App\Modules\Billing\Application\Contracts\BilledRevenueQueries;
use App\Modules\Billing\Application\DTOs\BilledJobRevenue;
use App\Modules\Billing\Domain\Enums\ExportStatus;
use App\Modules\Billing\Domain\Enums\LineKind;
use Illuminate\Support\Facades\DB;
class EloquentBilledRevenueQueries implements BilledRevenueQueries
{
public function byJob(string $fromMonth, string $toMonth): array
{
$rows = DB::table('billing_lines')
->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])
->groupBy('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::FixedPrice, 'net_cents').' as fixed_cents')
->selectRaw(self::sumOf(LineKind::MonthlyFee, 'net_cents').' as monthly_cents')
->selectRaw(self::sumOf(LineKind::Expense, 'net_cents').' as expense_cents')
->selectRaw(self::sumOf(LineKind::Hours, 'quantity_quarters').' as billed_quarters')
->get();
$result = [];
foreach ($rows as $row) {
$jobId = (int) $row->customer_job_id;
$result[$jobId] = new BilledJobRevenue(
$jobId,
(int) $row->hours_cents,
(int) $row->fixed_cents,
(int) $row->monthly_cents,
(int) $row->expense_cents,
(int) $row->billed_quarters,
);
}
return $result;
}
private static function sumOf(LineKind $kind, string $column): string
{
return "SUM(CASE WHEN billing_lines.kind = '{$kind->value}' THEN COALESCE(billing_lines.{$column}, 0) ELSE 0 END)";
}
}