Add the unbilled hours report
Hours on customers' billable jobs that no billing material has taken, by job and work type up to a day, valued at the hourly rates for jobs billed by the hour, with a chart by service unit and CSV. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -3,39 +3,38 @@
|
||||
namespace App\Modules\Reports\Presentation\Http\Controllers;
|
||||
|
||||
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
use App\Modules\Reports\Presentation\Csv\HoursCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\ReportCsv;
|
||||
use App\Modules\Reports\Presentation\Csv\UnbilledCsv;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
/**
|
||||
* Downloads a report as a CSV file for Excel, with the filters of its page
|
||||
* (alku, loppu and the report's own): semicolons, a UTF-8 BOM, amounts in
|
||||
* whole cents and hours with a decimal comma. Text that Excel would read
|
||||
* as a formula gets a leading apostrophe, and the file is never cached.
|
||||
* in the query string: semicolons, a UTF-8 BOM, amounts in whole cents and
|
||||
* hours with a decimal comma. Text that Excel would read as a formula gets
|
||||
* a leading apostrophe, and the file is never cached.
|
||||
*/
|
||||
class DownloadReport
|
||||
{
|
||||
/** The reports that have a CSV, by their URL key. */
|
||||
public const array REPORTS = ['hours'];
|
||||
public const array REPORTS = ['hours', 'unbilled'];
|
||||
|
||||
public function __invoke(string $report, Request $request): StreamedResponse
|
||||
{
|
||||
Gate::authorize('reports.view');
|
||||
|
||||
$csv = $this->csv($report);
|
||||
|
||||
try {
|
||||
$period = new ReportPeriod((string) $request->query('alku', ''), (string) $request->query('loppu', ''));
|
||||
// Reading the file name checks the filters before anything is streamed.
|
||||
$fileName = $csv->fileName($request).'.csv';
|
||||
} catch (InvalidReportPeriod) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$csv = $this->csv($report);
|
||||
$rows = $csv->rows($period, $request);
|
||||
$headers = $csv->headers();
|
||||
|
||||
return response()->streamDownload(function () use ($headers, $rows): void {
|
||||
return response()->streamDownload(function () use ($csv, $request): void {
|
||||
$out = fopen('php://output', 'w');
|
||||
|
||||
if ($out === false) {
|
||||
@@ -43,14 +42,14 @@ class DownloadReport
|
||||
}
|
||||
|
||||
fwrite($out, "\u{FEFF}");
|
||||
fputcsv($out, $headers, ';', '"', '');
|
||||
fputcsv($out, $csv->headers(), ';', '"', '');
|
||||
|
||||
foreach ($rows as $row) {
|
||||
foreach ($csv->rows($request) as $row) {
|
||||
fputcsv($out, array_map(self::cell(...), $row), ';', '"', '');
|
||||
}
|
||||
|
||||
fclose($out);
|
||||
}, __('reports.csv_file', ['report' => __('reports.'.$report.'.file'), 'from' => $period->from, 'to' => $period->to]), [
|
||||
}, $fileName, [
|
||||
'Content-Type' => 'text/csv; charset=UTF-8',
|
||||
'Cache-Control' => 'no-store, private',
|
||||
]);
|
||||
@@ -73,6 +72,7 @@ class DownloadReport
|
||||
{
|
||||
return match ($report) {
|
||||
'hours' => app(HoursCsv::class),
|
||||
'unbilled' => app(UnbilledCsv::class),
|
||||
default => abort(404),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user