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]>
58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Reports\Presentation\Csv;
|
|
|
|
use App\Modules\Reports\Application\UseCases\ProfitabilityByJob;
|
|
use App\Modules\Reports\Presentation\ReportFormat;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* Profitability as CSV: a row per job, amounts in cents (VAT 0); the hour
|
|
* cost and margin are empty when no internal hourly cost is set.
|
|
*/
|
|
class ProfitabilityCsv implements ReportCsv
|
|
{
|
|
public function __construct(private ProfitabilityByJob $report) {}
|
|
|
|
public function fileName(Request $request): string
|
|
{
|
|
$period = CsvFilters::period($request);
|
|
|
|
return __('reports.profitability.file').'-'.$period->from.'-'.$period->to;
|
|
}
|
|
|
|
public function headers(): array
|
|
{
|
|
return array_values(__('reports.profitability.csv'));
|
|
}
|
|
|
|
public function rows(Request $request): iterable
|
|
{
|
|
$report = $this->report->execute(CsvFilters::period($request), CsvFilters::flag($request, 'sisaiset'));
|
|
$serviceUnits = [];
|
|
|
|
foreach ($report->serviceUnits as $unit) {
|
|
$serviceUnits[$unit->serviceUnitId] = $unit->name;
|
|
}
|
|
|
|
foreach ($report->jobs as $row) {
|
|
$profit = $row->profit;
|
|
|
|
yield [
|
|
$row->job->number,
|
|
$row->job->name,
|
|
$row->job->customerName,
|
|
implode(', ', array_map(fn (int $id): string => $serviceUnits[$id] ?? '', $row->job->serviceUnitIds)),
|
|
$row->job->paymentBasis === null ? '' : __('jobs.payment_bases.'.$row->job->paymentBasis),
|
|
ReportFormat::csvHours($profit->quarters),
|
|
ReportFormat::csvHours($profit->billedQuarters),
|
|
$profit->workCents,
|
|
$profit->expenseRevenueCents,
|
|
$profit->expenseCostCents,
|
|
$profit->hourCostCents ?? '',
|
|
$profit->marginCents() ?? '',
|
|
];
|
|
}
|
|
}
|
|
}
|