- Jobs get a budget / quoted amount (cents, VAT 0). Only users who may see quotes see and edit it on the job form and page, and only they see its changes in the history. Others' saves keep the stored amount: the server decides whether the amount is set, not the browser. - Jobs record when they were done: set when a job first reaches a status that locks its hours, kept through the billing statuses, cleared when management opens the lock. The billing export will use it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
127 lines
5.4 KiB
PHP
127 lines
5.4 KiB
PHP
<?php
|
|
|
|
use App\Modules\Jobs\Domain\Entities\CustomerJob;
|
|
use App\Modules\Jobs\Domain\Enums\JobKind;
|
|
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
|
use App\Modules\Jobs\Domain\Enums\Priority;
|
|
use App\Modules\Jobs\Domain\Exceptions\DueBeforeStart;
|
|
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
|
|
use App\Modules\Jobs\Domain\Exceptions\InvalidJobLink;
|
|
use App\Modules\Jobs\Domain\Exceptions\InvalidTag;
|
|
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
|
|
use App\Modules\Jobs\Domain\Exceptions\ServiceUnitRequired;
|
|
use App\Modules\Jobs\Domain\ValueObjects\EstimatedHours;
|
|
use App\Modules\Jobs\Domain\ValueObjects\JobDetails;
|
|
use App\Modules\Jobs\Domain\ValueObjects\JobLink;
|
|
use App\Modules\Jobs\Domain\ValueObjects\JobName;
|
|
use App\Modules\Jobs\Domain\ValueObjects\JobNumber;
|
|
use App\Modules\Jobs\Domain\ValueObjects\Tag;
|
|
use App\Modules\Shared\Domain\ValueObjects\Money;
|
|
|
|
/**
|
|
* @param array<string, mixed> $overrides
|
|
*/
|
|
function jobDetails(array $overrides = []): JobDetails
|
|
{
|
|
return new JobDetails(...array_merge([
|
|
'name' => new JobName('Messuvideo'),
|
|
'description' => null,
|
|
'customerId' => 1,
|
|
'serviceUnitIds' => [1],
|
|
'startsOn' => null,
|
|
'dueOn' => null,
|
|
'plannedFrom' => null,
|
|
'plannedTo' => null,
|
|
'kind' => JobKind::OneOff,
|
|
'status' => JobStatus::Tentative,
|
|
'priority' => Priority::Normal,
|
|
'paymentBasis' => null,
|
|
'estimatedHours' => null,
|
|
'customerReference' => null,
|
|
'instructions' => null,
|
|
'responsibleUserId' => 10,
|
|
'contactUserId' => null,
|
|
'customerContactId' => null,
|
|
'billerUserId' => null,
|
|
'billingUnitId' => null,
|
|
'memberUserIds' => [],
|
|
'tags' => [],
|
|
'links' => [],
|
|
], $overrides));
|
|
}
|
|
|
|
it('formats and parses job numbers', function () {
|
|
expect((new JobNumber(2026, 42))->format())->toBe('2026-0042')
|
|
->and(JobNumber::fromString('2027-0001')->sequence)->toBe(1);
|
|
});
|
|
|
|
it('accepts estimated hours in quarters only', function () {
|
|
expect(EstimatedHours::fromString('7,25')->format())->toBe('7.25')
|
|
->and(EstimatedHours::fromString('8')->format())->toBe('8')
|
|
->and(fn () => EstimatedHours::fromString('1.1'))->toThrow(InvalidEstimatedHours::class)
|
|
->and(fn () => new EstimatedHours(-1))->toThrow(InvalidEstimatedHours::class);
|
|
});
|
|
|
|
it('accepts only http and https links with a title', function () {
|
|
expect((new JobLink(' Briiffi ', 'https://drive.example.test/x'))->title)->toBe('Briiffi')
|
|
->and(fn () => new JobLink('Tiedosto', 'javascript:alert(1)'))->toThrow(InvalidJobLink::class)
|
|
->and(fn () => new JobLink('', 'https://example.test'))->toThrow(InvalidJobLink::class)
|
|
->and(fn () => new Tag(str_repeat('a', 51)))->toThrow(InvalidTag::class);
|
|
});
|
|
|
|
it('checks the dates and service units of a job', function (array $overrides, string $exception) {
|
|
expect(fn () => jobDetails($overrides))->toThrow($exception);
|
|
})->with([
|
|
'due before start' => [['startsOn' => new DateTimeImmutable('2026-05-10'), 'dueOn' => new DateTimeImmutable('2026-05-09')], DueBeforeStart::class],
|
|
'plan ends before start' => [['plannedFrom' => new DateTimeImmutable('2026-05-10'), 'plannedTo' => new DateTimeImmutable('2026-05-01')], PlanEndsBeforeStart::class],
|
|
'no service unit' => [['serviceUnitIds' => []], ServiceUnitRequired::class],
|
|
]);
|
|
|
|
it('removes duplicate tags ignoring case, and sorts ids', function () {
|
|
$details = jobDetails(['tags' => [new Tag('Messut'), new Tag('messut'), new Tag('video')], 'memberUserIds' => [3, 1, 3]]);
|
|
|
|
expect(array_map(fn (Tag $tag) => $tag->value, $details->tags))->toBe(['Messut', 'video'])
|
|
->and($details->memberUserIds)->toBe([1, 3])
|
|
->and($details->involves(3))->toBeTrue()
|
|
->and($details->involves(99))->toBeFalse();
|
|
});
|
|
|
|
it('returns only the fields that changed', function () {
|
|
$job = CustomerJob::open(new JobNumber(2026, 1), jobDetails(), 10);
|
|
|
|
$changes = $job->changeDetails(jobDetails(['priority' => Priority::Urgent, 'tags' => [new Tag('messut')]]));
|
|
|
|
expect(array_map(fn ($change) => [$change->field, $change->oldValue, $change->newValue], $changes))->toBe([
|
|
['priority', 'normal', 'urgent'],
|
|
['tags', null, 'messut'],
|
|
])
|
|
->and($job->changeStatus(JobStatus::Tentative))->toBe([])
|
|
->and(JobStatus::kanbanStatuses())->not->toContain(JobStatus::Archived);
|
|
});
|
|
|
|
it('compares the quoted amount for the history', function () {
|
|
$job = CustomerJob::open(new JobNumber(2026, 1), jobDetails(), 10);
|
|
|
|
$changes = $job->changeDetails(jobDetails(['quotedAmount' => Money::fromCents(450000)]));
|
|
|
|
expect(array_map(fn ($change) => [$change->field, $change->oldValue, $change->newValue], $changes))->toBe([['quoted_amount', null, '450000']]);
|
|
});
|
|
|
|
it('records the completion time while the job is locked and clears it when opened', function () {
|
|
$job = CustomerJob::open(new JobNumber(2026, 1), jobDetails(), 10);
|
|
$done = new DateTimeImmutable('2026-11-20 10:00');
|
|
|
|
$job->recordCompletion($done);
|
|
expect($job->completedAt())->toBeNull();
|
|
|
|
$job->changeStatus(JobStatus::Done);
|
|
$job->recordCompletion($done);
|
|
$job->changeStatus(JobStatus::Billed);
|
|
$job->recordCompletion(new DateTimeImmutable('2026-12-01'));
|
|
expect($job->completedAt())->toEqual($done);
|
|
|
|
$job->changeStatus(JobStatus::InProduction);
|
|
$job->recordCompletion(new DateTimeImmutable('2026-12-02'));
|
|
expect($job->completedAt())->toBeNull();
|
|
});
|