- Dark theme page background is #5a5e60; text on it is lifted to zinc-200 / primary-100 and TallStackUI's dark labels, hints and checkbox labels are lightened so everything stays at 4.5:1. - Shared HexColor picks white or black text by the higher WCAG contrast; a native-dialog colour picker and a large work dialog component. - Customers get a header colour (customers.header_color), set on the customer page. - New job status Jatkuvat / Kausi (ongoing): open, never late, billed monthly like running jobs. - Kanban model: board columns and views, urgent threshold, per-user board preferences (density, column colours and sort), card positions, tasks of continuing jobs pinned to the board, MoveBoardCard and board queries with logged hours from TimeTracking. Co-Authored-By: Claude Opus 5.5 <[email protected]>
113 lines
4.0 KiB
PHP
113 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Billing\Infrastructure;
|
|
|
|
use App\Modules\Billing\Application\Contracts\BillingJobs;
|
|
use App\Modules\Billing\Application\DTOs\BillableJob;
|
|
use App\Modules\Billing\Domain\Enums\BillingMode;
|
|
use App\Modules\Customers\Application\Contracts\CustomerQueries;
|
|
use App\Modules\Customers\Application\DTOs\CustomerSummary;
|
|
use App\Modules\Customers\Domain\Enums\CustomerType;
|
|
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
|
use App\Modules\Jobs\Application\DTOs\JobBillingInfo;
|
|
use App\Modules\Jobs\Application\UseCases\ChangeJobStatus;
|
|
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
|
use App\Modules\Jobs\Domain\Enums\PaymentBasis;
|
|
|
|
/**
|
|
* Customer jobs for billing through the Jobs module's queries and status
|
|
* change, with the customer's type and identity code from Customers (only
|
|
* whether there is a code, never the code).
|
|
*/
|
|
class JobsForBilling implements BillingJobs
|
|
{
|
|
/** Statuses in which a monthly fee job is billed: running, ongoing or done. */
|
|
private const array MONTHLY_STATUSES = [JobStatus::Order, JobStatus::InProduction, JobStatus::OnHold, JobStatus::Done, JobStatus::Ongoing];
|
|
|
|
public function __construct(
|
|
private CustomerJobQueries $jobs,
|
|
private CustomerQueries $customers,
|
|
private ChangeJobStatus $changeStatus,
|
|
) {}
|
|
|
|
public function candidates(): array
|
|
{
|
|
$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
|
|
{
|
|
$result = [];
|
|
|
|
foreach ($this->toBillable(array_values($this->jobs->billingInfoOf($jobIds))) as $job) {
|
|
$result[$job->id] = $job;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function markInvoiced(int $jobId, int $actorUserId): void
|
|
{
|
|
$this->changeStatus->execute($jobId, JobStatus::Invoiced, $actorUserId);
|
|
}
|
|
|
|
/**
|
|
* @param list<JobBillingInfo> $jobs
|
|
* @return list<BillableJob>
|
|
*/
|
|
private function toBillable(array $jobs): array
|
|
{
|
|
/** @var array<int, CustomerSummary|null> $customers */
|
|
$customers = [];
|
|
|
|
foreach ($jobs as $job) {
|
|
if (! array_key_exists($job->customerId, $customers)) {
|
|
$customers[$job->customerId] = $this->customers->findSummary($job->customerId);
|
|
}
|
|
}
|
|
|
|
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,
|
|
$job->number,
|
|
$job->name,
|
|
$job->customerId,
|
|
$job->customerName,
|
|
$customer?->type === CustomerType::Consumer,
|
|
$customer->isInternal ?? false,
|
|
$customer?->hasPersonalIdentityCode() ?? false,
|
|
$job->billingUnitId,
|
|
$job->billingUnitName,
|
|
$job->customerReference,
|
|
$mode,
|
|
$job->quotedAmountCents,
|
|
$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);
|
|
}
|
|
}
|