Show a monthly fee job's billed periods on a page of its own
The new billing::job-months page lists the months that created billing materials have billed for a monthly fee job, with links to the materials and the total, and the months still to bill up to the current month, counted the same way as a new material counts them. The job page links to it next to the payment basis for those who manage billing. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\Contracts;
|
||||
|
||||
use App\Modules\Billing\Application\DTOs\BilledMonth;
|
||||
use App\Modules\Billing\Application\DTOs\ExportSummary;
|
||||
|
||||
/**
|
||||
@@ -17,4 +18,11 @@ interface BillingQueries
|
||||
* @return list<ExportSummary>
|
||||
*/
|
||||
public function exports(): array;
|
||||
|
||||
/**
|
||||
* The monthly fee months of a job billed on created materials, oldest first.
|
||||
*
|
||||
* @return list<BilledMonth>
|
||||
*/
|
||||
public function billedMonthsOf(int $jobId): array;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\DTOs;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* One month of a monthly fee job billed on a created billing material.
|
||||
*/
|
||||
final readonly class BilledMonth
|
||||
{
|
||||
/**
|
||||
* @param string $month Y-m.
|
||||
*/
|
||||
public function __construct(
|
||||
public string $month,
|
||||
public int $exportId,
|
||||
public int $exportNumber,
|
||||
public ?DateTimeImmutable $finalizedAt,
|
||||
public int $netCents,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\DTOs;
|
||||
|
||||
/**
|
||||
* The billing periods of a monthly fee job: the months billed on created
|
||||
* materials and the months still to bill up to the current month.
|
||||
*/
|
||||
final readonly class MonthlyBillingView
|
||||
{
|
||||
/**
|
||||
* @param list<BilledMonth> $billed Oldest first.
|
||||
* @param list<string> $unbilled Y-m, oldest first, up to the current month.
|
||||
* @param string|null $billingEndsOn Y-m-d: the job's last day or completion day, after which nothing is billed.
|
||||
*/
|
||||
public function __construct(
|
||||
public BillableJob $job,
|
||||
public array $billed,
|
||||
public array $unbilled,
|
||||
public string $currentMonth,
|
||||
public ?string $billingEndsOn,
|
||||
) {}
|
||||
|
||||
public function billedNetCents(): int
|
||||
{
|
||||
return array_sum(array_map(fn (BilledMonth $month): int => $month->netCents, $this->billed));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\Services;
|
||||
|
||||
use App\Modules\Billing\Application\Contracts\BillingJobs;
|
||||
use App\Modules\Billing\Application\Contracts\BillingQueries;
|
||||
use App\Modules\Billing\Application\DTOs\BilledMonth;
|
||||
use App\Modules\Billing\Application\DTOs\MonthlyBillingView;
|
||||
use App\Modules\Billing\Domain\Enums\BillingMode;
|
||||
use App\Modules\Billing\Domain\ValueObjects\BillingMonths;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
|
||||
/**
|
||||
* The billing periods of a monthly fee job (spec 5.5): which months the
|
||||
* created billing materials have billed, and which months from the job's
|
||||
* start up to the current month are still to bill, the same way a new
|
||||
* material counts them.
|
||||
*/
|
||||
final class MonthlyBillingViews
|
||||
{
|
||||
public function __construct(
|
||||
private BillingJobs $jobs,
|
||||
private BillingQueries $queries,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Null when the job does not exist or is not billed by a monthly fee.
|
||||
*/
|
||||
public function forJob(int $jobId): ?MonthlyBillingView
|
||||
{
|
||||
$job = $this->jobs->find([$jobId])[$jobId] ?? null;
|
||||
|
||||
if ($job === null || $job->mode !== BillingMode::MonthlyFee) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$billed = $this->queries->billedMonthsOf($jobId);
|
||||
$currentMonth = $this->clock->today()->format('Y-m');
|
||||
$unbilled = $job->billingStartsOn === ''
|
||||
? []
|
||||
: BillingMonths::unbilled($job->billingStartsOn, $job->billingEndsOn, $currentMonth, array_map(fn (BilledMonth $month): string => $month->month, $billed));
|
||||
|
||||
return new MonthlyBillingView($job, $billed, $unbilled, $currentMonth, $job->billingEndsOn);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user