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:
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Reports\Application\DTOs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A job with unbilled hours: approved and waiting for approval in quarter
|
||||||
|
* hours, their value without VAT (null when a fixed price or monthly fee
|
||||||
|
* covers them), whether a work type lacks a rate, the first day worked and
|
||||||
|
* the hours by work type.
|
||||||
|
*/
|
||||||
|
final readonly class UnbilledJob
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param list<UnbilledWorkType> $workTypes
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public ReportJob $job,
|
||||||
|
public int $approvedQuarters,
|
||||||
|
public int $unapprovedQuarters,
|
||||||
|
public ?int $valueCents,
|
||||||
|
public bool $missingRate,
|
||||||
|
public string $oldestDay,
|
||||||
|
public array $workTypes,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Reports\Application\DTOs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unbilled hours by job, oldest work first, with totals and the value of
|
||||||
|
* the hours by the work types' service units (most first).
|
||||||
|
*/
|
||||||
|
final readonly class UnbilledReport
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param list<UnbilledJob> $jobs
|
||||||
|
* @param array<string, int> $valueByServiceUnit
|
||||||
|
* @param array<int, string> $serviceUnits
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public array $jobs,
|
||||||
|
public int $approvedQuarters,
|
||||||
|
public int $unapprovedQuarters,
|
||||||
|
public int $valueCents,
|
||||||
|
public array $valueByServiceUnit,
|
||||||
|
public array $serviceUnits,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Reports\Application\DTOs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A job's unbilled hours on one work type, in quarter hours, and their
|
||||||
|
* value without VAT (all hours × the hourly rate) when the job bills its
|
||||||
|
* hours and the work type has a rate.
|
||||||
|
*/
|
||||||
|
final readonly class UnbilledWorkType
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $name,
|
||||||
|
public string $serviceUnitName,
|
||||||
|
public int $approvedQuarters,
|
||||||
|
public int $unapprovedQuarters,
|
||||||
|
public ?int $rateCents,
|
||||||
|
public ?int $valueCents,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Reports\Application\UseCases;
|
||||||
|
|
||||||
|
use App\Modules\Reports\Application\Contracts\ReportCatalog;
|
||||||
|
use App\Modules\Reports\Application\Contracts\ReportHours;
|
||||||
|
use App\Modules\Reports\Application\Contracts\ReportJobs;
|
||||||
|
use App\Modules\Reports\Application\DTOs\UnbilledEntry;
|
||||||
|
use App\Modules\Reports\Application\DTOs\UnbilledJob;
|
||||||
|
use App\Modules\Reports\Application\DTOs\UnbilledReport;
|
||||||
|
use App\Modules\Reports\Application\DTOs\UnbilledWorkType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unbilled hours (spec 13): the hours worked on customers' billable jobs
|
||||||
|
* up to a day that no billing material has taken yet, approved or not.
|
||||||
|
* Their value is the hours at the work types' hourly rates for jobs billed
|
||||||
|
* by their hours; a fixed price or monthly fee already covers the hours of
|
||||||
|
* other jobs. Internal and not billable jobs are left out.
|
||||||
|
*/
|
||||||
|
final readonly class UnbilledHoursReport
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private ReportHours $hours,
|
||||||
|
private ReportJobs $jobs,
|
||||||
|
private ReportCatalog $catalog,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function execute(string $day, ?int $serviceUnitId = null): UnbilledReport
|
||||||
|
{
|
||||||
|
$entries = $this->hours->unbilledUntil($day);
|
||||||
|
$jobs = $this->jobs->infoOf(array_values(array_unique(array_map(fn (UnbilledEntry $entry): int => $entry->jobId, $entries))));
|
||||||
|
$workTypes = $this->catalog->workTypes();
|
||||||
|
|
||||||
|
$byJob = [];
|
||||||
|
foreach ($entries as $entry) {
|
||||||
|
$job = $jobs[$entry->jobId] ?? null;
|
||||||
|
|
||||||
|
if ($job === null || ! $job->isBillable) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($serviceUnitId !== null && ! in_array($serviceUnitId, $job->serviceUnitIds, true)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$byJob[$job->id][] = $entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows = [];
|
||||||
|
$valueByServiceUnit = [];
|
||||||
|
|
||||||
|
foreach ($byJob as $jobId => $jobEntries) {
|
||||||
|
$job = $jobs[$jobId];
|
||||||
|
$lines = [];
|
||||||
|
$missingRate = false;
|
||||||
|
|
||||||
|
foreach ($jobEntries as $entry) {
|
||||||
|
$workType = $workTypes[$entry->workTypeId] ?? null;
|
||||||
|
$rate = $workType?->rateCents;
|
||||||
|
$value = null;
|
||||||
|
|
||||||
|
if ($job->billsHours) {
|
||||||
|
if ($rate === null) {
|
||||||
|
$missingRate = true;
|
||||||
|
} else {
|
||||||
|
// Rounded half up to a cent, as on invoice lines.
|
||||||
|
$value = intdiv(($entry->approvedQuarters + $entry->unapprovedQuarters) * $rate * 2 + 4, 8);
|
||||||
|
$unit = $workType->serviceUnitName;
|
||||||
|
$valueByServiceUnit[$unit] = ($valueByServiceUnit[$unit] ?? 0) + $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$lines[] = new UnbilledWorkType($workType->name ?? '', $workType->serviceUnitName ?? '', $entry->approvedQuarters, $entry->unapprovedQuarters, $rate, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows[] = new UnbilledJob(
|
||||||
|
$job,
|
||||||
|
array_sum(array_map(fn (UnbilledEntry $entry): int => $entry->approvedQuarters, $jobEntries)),
|
||||||
|
array_sum(array_map(fn (UnbilledEntry $entry): int => $entry->unapprovedQuarters, $jobEntries)),
|
||||||
|
$job->billsHours ? array_sum(array_map(fn (UnbilledWorkType $line): int => $line->valueCents ?? 0, $lines)) : null,
|
||||||
|
$missingRate,
|
||||||
|
min(array_map(fn (UnbilledEntry $entry): string => $entry->oldestDay, $jobEntries)),
|
||||||
|
$lines,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
usort($rows, fn (UnbilledJob $a, UnbilledJob $b): int => [$a->oldestDay, $a->job->number] <=> [$b->oldestDay, $b->job->number]);
|
||||||
|
arsort($valueByServiceUnit);
|
||||||
|
|
||||||
|
return new UnbilledReport(
|
||||||
|
$rows,
|
||||||
|
array_sum(array_map(fn (UnbilledJob $row): int => $row->approvedQuarters, $rows)),
|
||||||
|
array_sum(array_map(fn (UnbilledJob $row): int => $row->unapprovedQuarters, $rows)),
|
||||||
|
array_sum(array_map(fn (UnbilledJob $row): int => $row->valueCents ?? 0, $rows)),
|
||||||
|
$valueByServiceUnit,
|
||||||
|
$this->catalog->serviceUnits(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Reports\Presentation\Csv;
|
||||||
|
|
||||||
|
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
||||||
|
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the report pages' URL filters from a CSV download's query string.
|
||||||
|
*/
|
||||||
|
final class CsvFilters
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @throws InvalidReportPeriod
|
||||||
|
*/
|
||||||
|
public static function period(Request $request): ReportPeriod
|
||||||
|
{
|
||||||
|
return new ReportPeriod(self::text($request, 'alku'), self::text($request, 'loppu'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A single day (Y-m-d).
|
||||||
|
*
|
||||||
|
* @throws InvalidReportPeriod
|
||||||
|
*/
|
||||||
|
public static function day(Request $request, string $key): string
|
||||||
|
{
|
||||||
|
$day = self::text($request, $key);
|
||||||
|
|
||||||
|
return (new ReportPeriod($day, $day))->from;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function id(Request $request, string $key): ?int
|
||||||
|
{
|
||||||
|
$value = self::text($request, $key);
|
||||||
|
|
||||||
|
return ctype_digit($value) ? (int) $value : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function flag(Request $request, string $key): bool
|
||||||
|
{
|
||||||
|
return self::text($request, $key) === '1';
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function text(Request $request, string $key): string
|
||||||
|
{
|
||||||
|
$value = $request->query($key);
|
||||||
|
|
||||||
|
return is_string($value) ? $value : '';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@
|
|||||||
namespace App\Modules\Reports\Presentation\Csv;
|
namespace App\Modules\Reports\Presentation\Csv;
|
||||||
|
|
||||||
use App\Modules\Reports\Application\UseCases\HoursByPerson;
|
use App\Modules\Reports\Application\UseCases\HoursByPerson;
|
||||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
|
||||||
use App\Modules\Reports\Presentation\ReportFormat;
|
use App\Modules\Reports\Presentation\ReportFormat;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
@@ -14,14 +13,21 @@ class HoursCsv implements ReportCsv
|
|||||||
{
|
{
|
||||||
public function __construct(private HoursByPerson $report) {}
|
public function __construct(private HoursByPerson $report) {}
|
||||||
|
|
||||||
|
public function fileName(Request $request): string
|
||||||
|
{
|
||||||
|
$period = CsvFilters::period($request);
|
||||||
|
|
||||||
|
return __('reports.hours.file').'-'.$period->from.'-'.$period->to;
|
||||||
|
}
|
||||||
|
|
||||||
public function headers(): array
|
public function headers(): array
|
||||||
{
|
{
|
||||||
return array_values(__('reports.hours.csv'));
|
return array_values(__('reports.hours.csv'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rows(ReportPeriod $period, Request $request): iterable
|
public function rows(Request $request): iterable
|
||||||
{
|
{
|
||||||
$report = $this->report->execute($period, self::id($request, 'yksikko'), self::id($request, 'asiakas'));
|
$report = $this->report->execute(CsvFilters::period($request), CsvFilters::id($request, 'yksikko'), CsvFilters::id($request, 'asiakas'));
|
||||||
|
|
||||||
foreach ($report->lines as $line) {
|
foreach ($report->lines as $line) {
|
||||||
yield [
|
yield [
|
||||||
@@ -38,11 +44,4 @@ class HoursCsv implements ReportCsv
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function id(Request $request, string $key): ?int
|
|
||||||
{
|
|
||||||
$value = $request->query($key);
|
|
||||||
|
|
||||||
return is_string($value) && ctype_digit($value) ? (int) $value : null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,22 @@
|
|||||||
|
|
||||||
namespace App\Modules\Reports\Presentation\Csv;
|
namespace App\Modules\Reports\Presentation\Csv;
|
||||||
|
|
||||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A report as CSV rows, with the filters of the report page read from the
|
* A report as CSV rows, with the filters of the report page read from the
|
||||||
* request's query string.
|
* request's query string (see CsvFilters).
|
||||||
*/
|
*/
|
||||||
interface ReportCsv
|
interface ReportCsv
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* The file name without the extension, e.g. "tunnit-2026-11-01-2026-11-30".
|
||||||
|
*
|
||||||
|
* @throws InvalidReportPeriod
|
||||||
|
*/
|
||||||
|
public function fileName(Request $request): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return list<string>
|
* @return list<string>
|
||||||
*/
|
*/
|
||||||
@@ -18,6 +25,8 @@ interface ReportCsv
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return iterable<list<string|int>>
|
* @return iterable<list<string|int>>
|
||||||
|
*
|
||||||
|
* @throws InvalidReportPeriod
|
||||||
*/
|
*/
|
||||||
public function rows(ReportPeriod $period, Request $request): iterable;
|
public function rows(Request $request): iterable;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Reports\Presentation\Csv;
|
||||||
|
|
||||||
|
use App\Modules\Reports\Application\UseCases\UnbilledHoursReport;
|
||||||
|
use App\Modules\Reports\Presentation\ReportFormat;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unbilled hours as CSV: a row per job and work type, values in cents.
|
||||||
|
*/
|
||||||
|
class UnbilledCsv implements ReportCsv
|
||||||
|
{
|
||||||
|
public function __construct(private UnbilledHoursReport $report) {}
|
||||||
|
|
||||||
|
public function fileName(Request $request): string
|
||||||
|
{
|
||||||
|
return __('reports.unbilled.file').'-'.CsvFilters::day($request, 'paiva');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function headers(): array
|
||||||
|
{
|
||||||
|
return array_values(__('reports.unbilled.csv'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rows(Request $request): iterable
|
||||||
|
{
|
||||||
|
$report = $this->report->execute(CsvFilters::day($request, 'paiva'), CsvFilters::id($request, 'yksikko'));
|
||||||
|
|
||||||
|
foreach ($report->jobs as $row) {
|
||||||
|
foreach ($row->workTypes as $line) {
|
||||||
|
yield [
|
||||||
|
$row->job->number,
|
||||||
|
$row->job->name,
|
||||||
|
$row->job->customerName,
|
||||||
|
__('jobs.statuses.'.$row->job->status),
|
||||||
|
$row->job->paymentBasis === null ? '' : __('jobs.payment_bases.'.$row->job->paymentBasis),
|
||||||
|
$line->serviceUnitName,
|
||||||
|
$line->name,
|
||||||
|
ReportFormat::csvHours($line->approvedQuarters),
|
||||||
|
ReportFormat::csvHours($line->unapprovedQuarters),
|
||||||
|
$line->rateCents ?? '',
|
||||||
|
$line->valueCents ?? '',
|
||||||
|
$row->oldestDay,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,39 +3,38 @@
|
|||||||
namespace App\Modules\Reports\Presentation\Http\Controllers;
|
namespace App\Modules\Reports\Presentation\Http\Controllers;
|
||||||
|
|
||||||
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
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\HoursCsv;
|
||||||
use App\Modules\Reports\Presentation\Csv\ReportCsv;
|
use App\Modules\Reports\Presentation\Csv\ReportCsv;
|
||||||
|
use App\Modules\Reports\Presentation\Csv\UnbilledCsv;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads a report as a CSV file for Excel, with the filters of its page
|
* 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
|
* in the query string: semicolons, a UTF-8 BOM, amounts in whole cents and
|
||||||
* whole cents and hours with a decimal comma. Text that Excel would read
|
* hours with a decimal comma. Text that Excel would read as a formula gets
|
||||||
* as a formula gets a leading apostrophe, and the file is never cached.
|
* a leading apostrophe, and the file is never cached.
|
||||||
*/
|
*/
|
||||||
class DownloadReport
|
class DownloadReport
|
||||||
{
|
{
|
||||||
/** The reports that have a CSV, by their URL key. */
|
/** 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
|
public function __invoke(string $report, Request $request): StreamedResponse
|
||||||
{
|
{
|
||||||
Gate::authorize('reports.view');
|
Gate::authorize('reports.view');
|
||||||
|
|
||||||
|
$csv = $this->csv($report);
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (InvalidReportPeriod) {
|
||||||
abort(404);
|
abort(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$csv = $this->csv($report);
|
return response()->streamDownload(function () use ($csv, $request): void {
|
||||||
$rows = $csv->rows($period, $request);
|
|
||||||
$headers = $csv->headers();
|
|
||||||
|
|
||||||
return response()->streamDownload(function () use ($headers, $rows): void {
|
|
||||||
$out = fopen('php://output', 'w');
|
$out = fopen('php://output', 'w');
|
||||||
|
|
||||||
if ($out === false) {
|
if ($out === false) {
|
||||||
@@ -43,14 +42,14 @@ class DownloadReport
|
|||||||
}
|
}
|
||||||
|
|
||||||
fwrite($out, "\u{FEFF}");
|
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), ';', '"', '');
|
fputcsv($out, array_map(self::cell(...), $row), ';', '"', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($out);
|
fclose($out);
|
||||||
}, __('reports.csv_file', ['report' => __('reports.'.$report.'.file'), 'from' => $period->from, 'to' => $period->to]), [
|
}, $fileName, [
|
||||||
'Content-Type' => 'text/csv; charset=UTF-8',
|
'Content-Type' => 'text/csv; charset=UTF-8',
|
||||||
'Cache-Control' => 'no-store, private',
|
'Cache-Control' => 'no-store, private',
|
||||||
]);
|
]);
|
||||||
@@ -73,6 +72,7 @@ class DownloadReport
|
|||||||
{
|
{
|
||||||
return match ($report) {
|
return match ($report) {
|
||||||
'hours' => app(HoursCsv::class),
|
'hours' => app(HoursCsv::class),
|
||||||
|
'unbilled' => app(UnbilledCsv::class),
|
||||||
default => abort(404),
|
default => abort(404),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Reports\Presentation\Livewire\Concerns;
|
||||||
|
|
||||||
|
use App\Modules\Reports\Presentation\ReportFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number, money and date formats of the report pages.
|
||||||
|
*/
|
||||||
|
trait FormatsReports
|
||||||
|
{
|
||||||
|
public function money(int $cents): string
|
||||||
|
{
|
||||||
|
return ReportFormat::money($cents);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hours(int $quarters): string
|
||||||
|
{
|
||||||
|
return ReportFormat::hours($quarters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dateLabel(string $day): string
|
||||||
|
{
|
||||||
|
return ReportFormat::date($day);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function percent(float $ratio): string
|
||||||
|
{
|
||||||
|
return ReportFormat::percent($ratio);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,11 +11,13 @@ use Livewire\Attributes\Url;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The period every report page covers, kept in the URL (alku, loppu), its
|
* The period every report page covers, kept in the URL (alku, loppu), its
|
||||||
* quick choices and the shared number formats. Without a valid period in
|
* quick choices and the shared formats. Without a valid period in
|
||||||
* the URL a page starts with the current month.
|
* the URL a page starts with the current month.
|
||||||
*/
|
*/
|
||||||
trait ReportPage
|
trait ReportPage
|
||||||
{
|
{
|
||||||
|
use FormatsReports;
|
||||||
|
|
||||||
#[Url(as: 'alku', except: '')]
|
#[Url(as: 'alku', except: '')]
|
||||||
public string $from = '';
|
public string $from = '';
|
||||||
|
|
||||||
@@ -82,21 +84,6 @@ trait ReportPage
|
|||||||
return $period === null ? '' : ReportFormat::period($period);
|
return $period === null ? '' : ReportFormat::period($period);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function money(int $cents): string
|
|
||||||
{
|
|
||||||
return ReportFormat::money($cents);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hours(int $quarters): string
|
|
||||||
{
|
|
||||||
return ReportFormat::hours($quarters);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function dateLabel(string $day): string
|
|
||||||
{
|
|
||||||
return ReportFormat::date($day);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The CSV of the report with the filters shown.
|
* The CSV of the report with the filters shown.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -0,0 +1,191 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Modules\Reports\Application\DTOs\UnbilledJob;
|
||||||
|
use App\Modules\Reports\Application\DTOs\UnbilledReport;
|
||||||
|
use App\Modules\Reports\Application\UseCases\UnbilledHoursReport;
|
||||||
|
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
||||||
|
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||||
|
use App\Modules\Reports\Presentation\Livewire\Concerns\FormatsReports;
|
||||||
|
use App\Modules\Users\Application\Contracts\Clock;
|
||||||
|
use Livewire\Attributes\Computed;
|
||||||
|
use Livewire\Attributes\Url;
|
||||||
|
use Livewire\Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unbilled hours (spec 13): hours on customers' billable jobs up to a day
|
||||||
|
* that no billing material has taken yet, by job and work type, with
|
||||||
|
* their value at the hourly rates when the job bills its hours.
|
||||||
|
*/
|
||||||
|
new class extends Component
|
||||||
|
{
|
||||||
|
use FormatsReports;
|
||||||
|
|
||||||
|
#[Url(as: 'paiva', except: '')]
|
||||||
|
public string $day = '';
|
||||||
|
|
||||||
|
#[Url(as: 'yksikko', except: '')]
|
||||||
|
public string $serviceUnit = '';
|
||||||
|
|
||||||
|
public function mount(Clock $clock): void
|
||||||
|
{
|
||||||
|
$this->authorize('reports.view');
|
||||||
|
|
||||||
|
if (! $this->dayIsValid()) {
|
||||||
|
$this->day = $clock->today()->format('Y-m-d');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Computed]
|
||||||
|
public function report(): ?UnbilledReport
|
||||||
|
{
|
||||||
|
if (! $this->dayIsValid()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return app(UnbilledHoursReport::class)->execute($this->day, ctype_digit($this->serviceUnit) ? (int) $this->serviceUnit : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updated(): void
|
||||||
|
{
|
||||||
|
$this->authorize('reports.view');
|
||||||
|
unset($this->report);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dayIsValid(): bool
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
new ReportPeriod($this->day, $this->day);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (InvalidReportPeriod) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function csv(): string
|
||||||
|
{
|
||||||
|
return route('reports.csv', array_filter(['report' => 'unbilled', 'paiva' => $this->day, 'yksikko' => $this->serviceUnit]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function valueText(UnbilledJob $row): string
|
||||||
|
{
|
||||||
|
if ($row->valueCents === null) {
|
||||||
|
return __('reports.unbilled.in_price');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->money($row->valueCents).($row->missingRate ? ' '.__('reports.unbilled.missing_rate_short') : '');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(): mixed
|
||||||
|
{
|
||||||
|
return $this->view()->title(__('reports.unbilled.title'));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="mx-auto max-w-6xl space-y-6 p-4 sm:p-6">
|
||||||
|
<div class="space-y-2">
|
||||||
|
<h1 class="text-2xl font-semibold">{{ __('reports.unbilled.title') }}</h1>
|
||||||
|
<p class="max-w-3xl text-sm">{{ __('reports.unbilled.intro') }}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@php($report = $this->report)
|
||||||
|
|
||||||
|
<div class="grid max-w-2xl gap-4 sm:grid-cols-2">
|
||||||
|
<x-input id="report-day" wire:model.live.change="day" type="date" :label="__('reports.unbilled.day')" required />
|
||||||
|
|
||||||
|
@if ($report !== null)
|
||||||
|
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||||
|
<x-select.native wire:model.live="serviceUnit" :label="__('reports.filters.service_unit')" :aria-label="__('reports.filters.service_unit')">
|
||||||
|
<option value="">{{ __('reports.filters.all_service_units') }}</option>
|
||||||
|
@foreach ($report->serviceUnits as $id => $name)
|
||||||
|
<option value="{{ $id }}">{{ $name }}</option>
|
||||||
|
@endforeach
|
||||||
|
</x-select.native>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if ($report === null)
|
||||||
|
<p role="alert" class="flex items-center gap-2 text-sm font-medium text-red-700 dark:text-red-300">
|
||||||
|
<x-icon name="exclamation-triangle" class="size-5" aria-hidden="true" />
|
||||||
|
{{ __('reports.unbilled.invalid_day') }}
|
||||||
|
</p>
|
||||||
|
@else
|
||||||
|
<div class="flex flex-wrap items-center justify-between gap-3">
|
||||||
|
<h2 class="text-xl font-semibold" role="status" aria-live="polite">{{ __('reports.unbilled.until', ['day' => $this->dateLabel($day)]) }}</h2>
|
||||||
|
<x-button :href="$this->csv()" subtle color="black" icon="arrow-down-tray" :text="__('reports.download_csv')" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if ($report->jobs === [])
|
||||||
|
<p>{{ __('reports.unbilled.none') }}</p>
|
||||||
|
@else
|
||||||
|
<div class="overflow-x-auto">
|
||||||
|
<table class="w-full text-sm">
|
||||||
|
<caption class="sr-only">{{ __('reports.unbilled.table', ['day' => $this->dateLabel($day)]) }}</caption>
|
||||||
|
<thead>
|
||||||
|
<tr class="border-b border-zinc-300 text-left dark:border-zinc-600">
|
||||||
|
<th scope="col" class="py-2 pr-3">{{ __('reports.unbilled.job') }}</th>
|
||||||
|
<th scope="col" class="py-2 pr-3">{{ __('reports.unbilled.status') }}</th>
|
||||||
|
<th scope="col" class="py-2 pr-3">{{ __('reports.unbilled.oldest') }}</th>
|
||||||
|
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.unbilled.approved') }}</th>
|
||||||
|
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.unbilled.unapproved') }}</th>
|
||||||
|
<th scope="col" class="py-2 text-right">{{ __('reports.unbilled.value') }}</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
|
||||||
|
@foreach ($report->jobs as $row)
|
||||||
|
<tr wire:key="job-{{ $row->job->id }}" class="align-top">
|
||||||
|
<th scope="row" class="py-2 pr-3 text-left font-normal">
|
||||||
|
<x-link :href="route('jobs.show', $row->job->id)" navigate>{{ $row->job->number }} {{ $row->job->name }}</x-link>
|
||||||
|
<span class="block text-xs text-zinc-600 dark:text-zinc-300">{{ $row->job->customerName }}</span>
|
||||||
|
<details class="mt-1">
|
||||||
|
<summary class="cursor-pointer text-xs font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600">
|
||||||
|
{{ __('reports.unbilled.by_work_type', ['job' => $row->job->number]) }}
|
||||||
|
</summary>
|
||||||
|
<ul class="mt-1 space-y-0.5 text-xs">
|
||||||
|
@foreach ($row->workTypes as $line)
|
||||||
|
<li>
|
||||||
|
{{ __('reports.unbilled.work_type_line', [
|
||||||
|
'name' => $line->name,
|
||||||
|
'approved' => $this->hours($line->approvedQuarters),
|
||||||
|
'unapproved' => $this->hours($line->unapprovedQuarters),
|
||||||
|
]) }}@if ($line->valueCents !== null), {{ $this->money($line->valueCents) }}@elseif ($row->job->billsHours) ({{ __('reports.unbilled.missing_rate') }})@endif
|
||||||
|
</li>
|
||||||
|
@endforeach
|
||||||
|
</ul>
|
||||||
|
</details>
|
||||||
|
</th>
|
||||||
|
<td class="py-2 pr-3">
|
||||||
|
{{ __('jobs.statuses.'.$row->job->status) }}
|
||||||
|
@if ($row->job->paymentBasis !== null)
|
||||||
|
<span class="block text-xs text-zinc-600 dark:text-zinc-300">{{ __('jobs.payment_bases.'.$row->job->paymentBasis) }}</span>
|
||||||
|
@endif
|
||||||
|
</td>
|
||||||
|
<td class="py-2 pr-3 whitespace-nowrap">{{ $this->dateLabel($row->oldestDay) }}</td>
|
||||||
|
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($row->approvedQuarters) }}</td>
|
||||||
|
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($row->unapprovedQuarters) }}</td>
|
||||||
|
<td class="py-2 text-right">{{ $this->valueText($row) }}</td>
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
</tbody>
|
||||||
|
<tfoot>
|
||||||
|
<tr class="border-t-2 border-zinc-400 font-semibold dark:border-zinc-500">
|
||||||
|
<th scope="row" colspan="3" class="py-2 pr-3 text-left">{{ __('reports.total') }}</th>
|
||||||
|
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->approvedQuarters) }}</td>
|
||||||
|
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->unapprovedQuarters) }}</td>
|
||||||
|
<td class="py-2 text-right whitespace-nowrap">{{ $this->money($report->valueCents) }}</td>
|
||||||
|
</tr>
|
||||||
|
</tfoot>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<x-reports.chart
|
||||||
|
:title="__('reports.unbilled.chart')"
|
||||||
|
:summary="implode(', ', array_map(fn ($unit, $cents) => $unit.' '.$this->money($cents), array_keys($report->valueByServiceUnit), $report->valueByServiceUnit))"
|
||||||
|
:labels="array_keys($report->valueByServiceUnit)"
|
||||||
|
:values="array_map(fn ($cents) => $cents / 100, array_values($report->valueByServiceUnit))"
|
||||||
|
:formatter="fn (float $value) => number_format($value, 0, ',', ' ').' €'"
|
||||||
|
/>
|
||||||
|
@endif
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
@@ -8,5 +8,6 @@ Route::middleware(['web', 'auth', 'can:reports.view'])
|
|||||||
->name('reports.')
|
->name('reports.')
|
||||||
->group(function () {
|
->group(function () {
|
||||||
Route::livewire('/hours', 'reports::hours')->name('hours');
|
Route::livewire('/hours', 'reports::hours')->name('hours');
|
||||||
|
Route::livewire('/unbilled', 'reports::unbilled')->name('unbilled');
|
||||||
Route::get('/{report}/csv', DownloadReport::class)->whereIn('report', DownloadReport::REPORTS)->name('csv');
|
Route::get('/{report}/csv', DownloadReport::class)->whereIn('report', DownloadReport::REPORTS)->name('csv');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ final class MainMenu
|
|||||||
'label' => 'layout.group_reports',
|
'label' => 'layout.group_reports',
|
||||||
'items' => [
|
'items' => [
|
||||||
['route' => 'reports.hours', 'pattern' => ['reports.hours'], 'label' => 'layout.report_hours', 'ability' => 'reports.view'],
|
['route' => 'reports.hours', 'pattern' => ['reports.hours'], 'label' => 'layout.report_hours', 'ability' => 'reports.view'],
|
||||||
|
['route' => 'reports.unbilled', 'pattern' => ['reports.unbilled'], 'label' => 'layout.report_unbilled', 'ability' => 'reports.view'],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'maintenance' => [
|
'maintenance' => [
|
||||||
|
|||||||
+37
-1
@@ -5,7 +5,6 @@ return [
|
|||||||
'yes' => 'yes',
|
'yes' => 'yes',
|
||||||
'no' => 'no',
|
'no' => 'no',
|
||||||
'download_csv' => 'Download CSV',
|
'download_csv' => 'Download CSV',
|
||||||
'csv_file' => ':report-:from-:to.csv',
|
|
||||||
'chart_note' => 'The same figures are in the table.',
|
'chart_note' => 'The same figures are in the table.',
|
||||||
'chart_empty' => 'There is nothing to show in the chart.',
|
'chart_empty' => 'There is nothing to show in the chart.',
|
||||||
|
|
||||||
@@ -60,4 +59,41 @@ return [
|
|||||||
'billed' => 'Billed',
|
'billed' => 'Billed',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'unbilled' => [
|
||||||
|
'title' => 'Unbilled hours',
|
||||||
|
'file' => 'unbilled-hours',
|
||||||
|
'intro' => 'Hours on customers’ billable jobs that no billing material has billed yet, approved and not approved. The value is the hours times the work type’s hourly rate (VAT 0) on jobs billed by the hour; a fixed quote or monthly fee covers the hours of other jobs. Internal and not billable jobs are left out.',
|
||||||
|
'day' => 'Worked on or before',
|
||||||
|
'invalid_day' => 'Check the date.',
|
||||||
|
'until' => 'Worked on or before :day',
|
||||||
|
'none' => 'There are no unbilled hours.',
|
||||||
|
'table' => 'Unbilled hours by customer job, worked on or before :day',
|
||||||
|
'job' => 'Customer job',
|
||||||
|
'status' => 'Status and payment basis',
|
||||||
|
'oldest' => 'Oldest entry',
|
||||||
|
'approved' => 'Approved',
|
||||||
|
'unapproved' => 'Not approved',
|
||||||
|
'value' => 'Value (VAT 0)',
|
||||||
|
'in_price' => 'Included in the price',
|
||||||
|
'missing_rate' => 'no hourly rate',
|
||||||
|
'missing_rate_short' => '(partly no hourly rate)',
|
||||||
|
'by_work_type' => 'By work type: :job',
|
||||||
|
'work_type_line' => ':name: approved :approved h, not approved :unapproved h',
|
||||||
|
'chart' => 'Value of unbilled hours by service unit',
|
||||||
|
'csv' => [
|
||||||
|
'job_number' => 'Job number',
|
||||||
|
'job' => 'Customer job',
|
||||||
|
'customer' => 'Customer',
|
||||||
|
'status' => 'Status',
|
||||||
|
'payment_basis' => 'Payment basis',
|
||||||
|
'service_unit' => 'Service unit',
|
||||||
|
'work_type' => 'Work type',
|
||||||
|
'approved' => 'Approved',
|
||||||
|
'unapproved' => 'Not approved',
|
||||||
|
'rate' => 'Hourly rate (cents)',
|
||||||
|
'value' => 'Value (cents)',
|
||||||
|
'oldest' => 'Oldest entry',
|
||||||
|
],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
+37
-1
@@ -5,7 +5,6 @@ return [
|
|||||||
'yes' => 'kyllä',
|
'yes' => 'kyllä',
|
||||||
'no' => 'ei',
|
'no' => 'ei',
|
||||||
'download_csv' => 'Lataa CSV',
|
'download_csv' => 'Lataa CSV',
|
||||||
'csv_file' => ':report-:from-:to.csv',
|
|
||||||
'chart_note' => 'Samat luvut ovat taulukossa.',
|
'chart_note' => 'Samat luvut ovat taulukossa.',
|
||||||
'chart_empty' => 'Kaaviossa ei ole näytettävää.',
|
'chart_empty' => 'Kaaviossa ei ole näytettävää.',
|
||||||
|
|
||||||
@@ -60,4 +59,41 @@ return [
|
|||||||
'billed' => 'Laskutettu',
|
'billed' => 'Laskutettu',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'unbilled' => [
|
||||||
|
'title' => 'Laskuttamattomat tunnit',
|
||||||
|
'file' => 'laskuttamattomat-tunnit',
|
||||||
|
'intro' => 'Asiakkaiden laskutettavien töiden tunnit, joita mikään laskuaineisto ei ole vielä laskuttanut, hyväksytyt ja hyväksymättömät. Arvo on tunnit kertaa työtehtävän tuntihinta (ALV 0) töillä, jotka laskutetaan tunneittain; kiinteän tarjouksen ja kuukausihinnan tunnit sisältyvät hintaan. Sisäiset ja ei laskutettavat työt eivät ole mukana.',
|
||||||
|
'day' => 'Tehty viimeistään',
|
||||||
|
'invalid_day' => 'Tarkista päivämäärä.',
|
||||||
|
'until' => 'Tehty viimeistään :day',
|
||||||
|
'none' => 'Laskuttamattomia tunteja ei ole.',
|
||||||
|
'table' => 'Laskuttamattomat tunnit asiakastöittäin, tehty viimeistään :day',
|
||||||
|
'job' => 'Asiakastyö',
|
||||||
|
'status' => 'Tila ja maksuperuste',
|
||||||
|
'oldest' => 'Vanhin kirjaus',
|
||||||
|
'approved' => 'Hyväksytty',
|
||||||
|
'unapproved' => 'Hyväksymättä',
|
||||||
|
'value' => 'Arvo (ALV 0)',
|
||||||
|
'in_price' => 'Sisältyy hintaan',
|
||||||
|
'missing_rate' => 'tuntihinta puuttuu',
|
||||||
|
'missing_rate_short' => '(osalta tuntihinta puuttuu)',
|
||||||
|
'by_work_type' => 'Työtehtävittäin: :job',
|
||||||
|
'work_type_line' => ':name: hyväksytty :approved h, hyväksymättä :unapproved h',
|
||||||
|
'chart' => 'Laskuttamattomien tuntien arvo palveluyksiköittäin',
|
||||||
|
'csv' => [
|
||||||
|
'job_number' => 'Työn numero',
|
||||||
|
'job' => 'Asiakastyö',
|
||||||
|
'customer' => 'Asiakas',
|
||||||
|
'status' => 'Tila',
|
||||||
|
'payment_basis' => 'Maksuperuste',
|
||||||
|
'service_unit' => 'Palveluyksikkö',
|
||||||
|
'work_type' => 'Työtehtävä',
|
||||||
|
'approved' => 'Hyväksytty',
|
||||||
|
'unapproved' => 'Hyväksymättä',
|
||||||
|
'rate' => 'Tuntihinta (snt)',
|
||||||
|
'value' => 'Arvo (snt)',
|
||||||
|
'oldest' => 'Vanhin kirjaus',
|
||||||
|
],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||||
|
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
|
||||||
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||||||
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||||
|
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->actingAs(User::factory()->management()->create());
|
||||||
|
$this->editing = WorkTypeModel::factory()->create(['name' => 'Editointi', 'hourly_rate_cents' => 8050]);
|
||||||
|
$this->unpriced = WorkTypeModel::factory()->create(['name' => 'Suunnittelu']);
|
||||||
|
$this->customer = CustomerModel::factory()->company()->create(['name' => 'Asiakas Oy']);
|
||||||
|
});
|
||||||
|
|
||||||
|
function unbilledJob(array $attributes): CustomerJobModel
|
||||||
|
{
|
||||||
|
return CustomerJobModel::factory()->create(array_merge(['customer_id' => test()->customer->id], $attributes));
|
||||||
|
}
|
||||||
|
|
||||||
|
function unbilledHours(CustomerJobModel $job, WorkTypeModel $workType, float $hours, string $day, bool $approved = true): TimeEntryModel
|
||||||
|
{
|
||||||
|
$task = TaskModel::factory()->create(['customer_job_id' => $job->id, 'work_type_id' => $workType->id]);
|
||||||
|
$factory = $approved ? TimeEntryModel::factory()->approved() : TimeEntryModel::factory();
|
||||||
|
|
||||||
|
return $factory->create(['task_id' => $task->id, 'hours' => $hours, 'worked_on' => $day]);
|
||||||
|
}
|
||||||
|
|
||||||
|
it('values unbilled hours of jobs billed by the hour and leaves out billed, internal and not billable work', function () {
|
||||||
|
$hourly = unbilledJob(['name' => 'Tuntityö', 'payment_basis' => 'hourly']);
|
||||||
|
unbilledHours($hourly, $this->editing, 2.5, '2026-11-02');
|
||||||
|
unbilledHours($hourly, $this->editing, 1, '2026-11-20', approved: false);
|
||||||
|
unbilledHours($hourly, $this->unpriced, 1, '2026-11-03');
|
||||||
|
unbilledHours($hourly, $this->editing, 3, '2026-11-26');
|
||||||
|
|
||||||
|
$quote = unbilledJob(['name' => 'Tarjous', 'payment_basis' => 'fixed_quote']);
|
||||||
|
unbilledHours($quote, $this->editing, 4, '2026-10-01');
|
||||||
|
|
||||||
|
$billedExport = DB::table('billing_exports')->insertGetId(['status' => 'created', 'period' => '2026-10', 'created_by' => auth()->id(), 'created_at' => now(), 'updated_at' => now()]);
|
||||||
|
unbilledHours($hourly, $this->editing, 5, '2026-10-15')->update(['billing_export_id' => $billedExport]);
|
||||||
|
unbilledHours(unbilledJob(['payment_basis' => 'not_billable']), $this->editing, 6, '2026-11-02');
|
||||||
|
unbilledHours(CustomerJobModel::factory()->create(['customer_id' => CustomerModel::query()->where('is_internal', true)->value('id')]), $this->editing, 7, '2026-11-02');
|
||||||
|
|
||||||
|
$report = Livewire::withQueryParams(['paiva' => '2026-11-25'])
|
||||||
|
->test('reports::unbilled')
|
||||||
|
->assertOk()
|
||||||
|
->assertSee('Tuntityö')
|
||||||
|
->assertSee(__('reports.unbilled.in_price'))
|
||||||
|
->instance()
|
||||||
|
->report();
|
||||||
|
|
||||||
|
expect($report->jobs)->toHaveCount(2)
|
||||||
|
->and($report->jobs[0]->job->name)->toBe('Tarjous')
|
||||||
|
->and($report->jobs[0]->valueCents)->toBeNull()
|
||||||
|
->and($report->jobs[1]->approvedQuarters)->toBe(14)
|
||||||
|
->and($report->jobs[1]->unapprovedQuarters)->toBe(4)
|
||||||
|
->and($report->jobs[1]->valueCents)->toBe(28175)
|
||||||
|
->and($report->jobs[1]->missingRate)->toBeTrue()
|
||||||
|
->and($report->jobs[1]->oldestDay)->toBe('2026-11-02')
|
||||||
|
->and($report->valueCents)->toBe(28175)
|
||||||
|
->and($report->approvedQuarters)->toBe(30);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('downloads the unbilled hours as CSV', function () {
|
||||||
|
unbilledHours(unbilledJob(['name' => 'Tuntityö', 'payment_basis' => 'hourly']), $this->editing, 2.5, '2026-11-02');
|
||||||
|
|
||||||
|
$response = $this->get(route('reports.csv', ['report' => 'unbilled', 'paiva' => '2026-11-25']))
|
||||||
|
->assertOk()
|
||||||
|
->assertDownload('laskuttamattomat-tunnit-2026-11-25.csv');
|
||||||
|
|
||||||
|
$content = substr($response->streamedContent(), 3);
|
||||||
|
$lines = array_map(fn (string $line): array => str_getcsv($line, ';', '"', ''), array_values(array_filter(explode("\n", $content))));
|
||||||
|
|
||||||
|
expect($lines[0])->toBe(array_values(__('reports.unbilled.csv')))
|
||||||
|
->and(array_slice($lines[1], 1))->toBe(['Tuntityö', 'Asiakas Oy', 'Alustava', 'Tuntityö', $lines[1][5], 'Editointi', '2,5', '0', '8050', '20125', '2026-11-02']);
|
||||||
|
|
||||||
|
$this->get(route('reports.csv', ['report' => 'unbilled', 'paiva' => '2026-02-30']))->assertNotFound();
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user