Add monthly fee billing lines to the billing material
Jobs billed by a fixed monthly fee get a monthly fee field, kept behind the quotes permission like the quoted amount. A billing material now has a billing month: a new draft adds every monthly fee job with something to bill, each on an invoice of its own, and bills one line for every unbilled month from the job's start up to the billing month (or its end or completion), so skipped months are caught up and no month is billed twice. Their expenses are billed alongside, their hours are covered by the fee and marked billed, and the job keeps running. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -21,15 +21,30 @@ use App\Modules\Jobs\Domain\Enums\PaymentBasis;
|
||||
*/
|
||||
class JobsForBilling implements BillingJobs
|
||||
{
|
||||
/** Statuses in which a monthly fee job is billed: running or done. */
|
||||
private const array MONTHLY_STATUSES = [JobStatus::Order, JobStatus::InProduction, JobStatus::OnHold, JobStatus::Done];
|
||||
|
||||
public function __construct(
|
||||
private CustomerJobQueries $jobs,
|
||||
private CustomerQueries $customers,
|
||||
private ChangeJobStatus $changeStatus,
|
||||
) {}
|
||||
|
||||
public function done(): array
|
||||
public function candidates(): array
|
||||
{
|
||||
return $this->toBillable($this->jobs->billingInfoWithStatus(JobStatus::Done));
|
||||
$jobs = [];
|
||||
foreach ([...$this->jobs->billingInfoWithStatus(JobStatus::Done), ...$this->jobs->billingInfoMonthly()] as $job) {
|
||||
$jobs[$job->id] = $job;
|
||||
}
|
||||
|
||||
uasort($jobs, fn (JobBillingInfo $a, JobBillingInfo $b): int => strcmp($a->number, $b->number));
|
||||
|
||||
return $this->toBillable(array_values($jobs));
|
||||
}
|
||||
|
||||
public function monthly(): array
|
||||
{
|
||||
return $this->toBillable($this->jobs->billingInfoMonthly());
|
||||
}
|
||||
|
||||
public function find(array $jobIds): array
|
||||
@@ -65,6 +80,13 @@ class JobsForBilling implements BillingJobs
|
||||
|
||||
return array_map(function (JobBillingInfo $job) use ($customers): BillableJob {
|
||||
$customer = $customers[$job->customerId] ?? null;
|
||||
$mode = match ($job->paymentBasis) {
|
||||
PaymentBasis::FixedQuote => BillingMode::FixedPrice,
|
||||
PaymentBasis::MonthlyFee => BillingMode::MonthlyFee,
|
||||
PaymentBasis::NotBillable => null,
|
||||
default => BillingMode::Hours,
|
||||
};
|
||||
$ends = array_filter([$job->dueOn, $job->completedOn]);
|
||||
|
||||
return new BillableJob(
|
||||
$job->id,
|
||||
@@ -78,13 +100,12 @@ class JobsForBilling implements BillingJobs
|
||||
$job->billingUnitId,
|
||||
$job->billingUnitName,
|
||||
$job->customerReference,
|
||||
match ($job->paymentBasis) {
|
||||
PaymentBasis::FixedQuote => BillingMode::FixedPrice,
|
||||
PaymentBasis::NotBillable, PaymentBasis::MonthlyFee => null,
|
||||
default => BillingMode::Hours,
|
||||
},
|
||||
$mode,
|
||||
$job->quotedAmountCents,
|
||||
$job->status === JobStatus::Done,
|
||||
$mode === BillingMode::MonthlyFee ? in_array($job->status, self::MONTHLY_STATUSES, true) : $job->status === JobStatus::Done,
|
||||
$job->monthlyFeeCents,
|
||||
$job->startsOn ?? $job->createdOn,
|
||||
$ends === [] ? null : min($ends),
|
||||
);
|
||||
}, $jobs);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user