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:
+49
@@ -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)";
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Modules\Billing\Infrastructure\Providers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Billing\Application\Contracts\BilledRevenueQueries;
|
||||
use App\Modules\Billing\Application\Contracts\BillingCustomers;
|
||||
use App\Modules\Billing\Application\Contracts\BillingExpenses;
|
||||
use App\Modules\Billing\Application\Contracts\BillingHours;
|
||||
@@ -15,6 +16,7 @@ use App\Modules\Billing\Infrastructure\CustomersForBilling;
|
||||
use App\Modules\Billing\Infrastructure\ExpensesForBilling;
|
||||
use App\Modules\Billing\Infrastructure\HoursForBilling;
|
||||
use App\Modules\Billing\Infrastructure\JobsForBilling;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Repositories\EloquentBilledRevenueQueries;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Repositories\EloquentBillingExportRepository;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Repositories\EloquentBillingQueries;
|
||||
use App\Modules\Billing\Infrastructure\PricesForBilling;
|
||||
@@ -36,6 +38,7 @@ class BillingServiceProvider extends ServiceProvider
|
||||
public array $bindings = [
|
||||
BillingExportRepository::class => EloquentBillingExportRepository::class,
|
||||
BillingQueries::class => EloquentBillingQueries::class,
|
||||
BilledRevenueQueries::class => EloquentBilledRevenueQueries::class,
|
||||
BillingJobs::class => JobsForBilling::class,
|
||||
BillingHours::class => HoursForBilling::class,
|
||||
BillingExpenses::class => ExpensesForBilling::class,
|
||||
|
||||
Reference in New Issue
Block a user