diff --git a/app/Modules/Jobs/Application/DTOs/CustomerJobData.php b/app/Modules/Jobs/Application/DTOs/CustomerJobData.php index 23ec770..5043ce9 100644 --- a/app/Modules/Jobs/Application/DTOs/CustomerJobData.php +++ b/app/Modules/Jobs/Application/DTOs/CustomerJobData.php @@ -40,5 +40,7 @@ final readonly class CustomerJobData public array $memberUserIds = [], public array $tags = [], public array $links = [], + public ?string $quotedAmount = null, + public bool $setsQuotedAmount = false, ) {} } diff --git a/app/Modules/Jobs/Application/DTOs/CustomerJobDetails.php b/app/Modules/Jobs/Application/DTOs/CustomerJobDetails.php index 7ad0ffc..668c0c3 100644 --- a/app/Modules/Jobs/Application/DTOs/CustomerJobDetails.php +++ b/app/Modules/Jobs/Application/DTOs/CustomerJobDetails.php @@ -48,6 +48,8 @@ final readonly class CustomerJobDetails public array $links, public string $createdAt, public string $createdByName, + public ?int $quotedAmountCents = null, + public ?string $completedAt = null, ) {} /** diff --git a/app/Modules/Jobs/Application/Services/JobDetailsFactory.php b/app/Modules/Jobs/Application/Services/JobDetailsFactory.php index 18a29ad..ebc7410 100644 --- a/app/Modules/Jobs/Application/Services/JobDetailsFactory.php +++ b/app/Modules/Jobs/Application/Services/JobDetailsFactory.php @@ -17,6 +17,8 @@ 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\Tag; +use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount; +use App\Modules\Shared\Domain\ValueObjects\Money; use App\Modules\Users\Application\Contracts\UserAccountQueries; use App\Modules\Users\Domain\Enums\Role; use DateTimeImmutable; @@ -36,7 +38,11 @@ final class JobDetailsFactory ) {} /** + * The quoted amount changes only when the caller may set it + * ($data->setsQuotedAmount); otherwise the job keeps its amount. + * * @throws InvalidJobReference + * @throws InvalidMoneyAmount */ public function build(CustomerJobData $data, ?JobDetails $current = null): JobDetails { @@ -66,6 +72,7 @@ final class JobDetailsFactory memberUserIds: $data->memberUserIds, tags: array_map(fn (string $tag): Tag => new Tag($tag), $data->tags), links: array_map(fn (array $link): JobLink => new JobLink($link['title'], $link['url']), $data->links), + quotedAmount: $data->setsQuotedAmount ? Money::parse((string) $data->quotedAmount) : $current?->quotedAmount, ); } diff --git a/app/Modules/Jobs/Application/UseCases/ChangeJobStatus.php b/app/Modules/Jobs/Application/UseCases/ChangeJobStatus.php index dd83575..bf6daf9 100644 --- a/app/Modules/Jobs/Application/UseCases/ChangeJobStatus.php +++ b/app/Modules/Jobs/Application/UseCases/ChangeJobStatus.php @@ -8,6 +8,7 @@ use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository; use App\Modules\Jobs\Domain\Enums\JobStatus; use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound; use App\Modules\Shared\Application\Contracts\TransactionManager; +use App\Modules\Users\Application\Contracts\Clock; /** * Moves a job to another status (e.g. on the Kanban board) and records it. @@ -17,6 +18,7 @@ final class ChangeJobStatus public function __construct( private CustomerJobRepository $jobs, private TransactionManager $transactions, + private Clock $clock, ) {} /** @@ -27,6 +29,7 @@ final class ChangeJobStatus $this->transactions->run(function () use ($jobId, $status, $actorUserId): void { $job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId); $changes = $job->changeStatus($status); + $job->recordCompletion($this->clock->now()); if ($changes === []) { return; diff --git a/app/Modules/Jobs/Application/UseCases/CreateCustomerJob.php b/app/Modules/Jobs/Application/UseCases/CreateCustomerJob.php index d4f1ceb..e7cf27e 100644 --- a/app/Modules/Jobs/Application/UseCases/CreateCustomerJob.php +++ b/app/Modules/Jobs/Application/UseCases/CreateCustomerJob.php @@ -40,6 +40,7 @@ final class CreateCustomerJob return $this->transactions->run(function () use ($details, $year, $actorUserId): int { $job = CustomerJob::open($this->numbers->next($year), $details, $actorUserId); + $job->recordCompletion($this->clock->now()); $jobId = (int) $this->jobs->save($job)->id(); $this->jobs->recordChanges($jobId, $actorUserId, [new FieldChange('created', null, $job->number()->format())]); diff --git a/app/Modules/Jobs/Application/UseCases/UpdateCustomerJob.php b/app/Modules/Jobs/Application/UseCases/UpdateCustomerJob.php index 35c6453..1f5147c 100644 --- a/app/Modules/Jobs/Application/UseCases/UpdateCustomerJob.php +++ b/app/Modules/Jobs/Application/UseCases/UpdateCustomerJob.php @@ -10,6 +10,7 @@ use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository; use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound; use App\Modules\Jobs\Domain\Exceptions\InvalidJobReference; use App\Modules\Shared\Application\Contracts\TransactionManager; +use App\Modules\Users\Application\Contracts\Clock; /** * Changes a customer job and records each changed field in its history. @@ -21,6 +22,7 @@ final class UpdateCustomerJob private CustomerJobRepository $jobs, private JobDetailsFactory $detailsFactory, private TransactionManager $transactions, + private Clock $clock, ) {} /** @@ -33,6 +35,7 @@ final class UpdateCustomerJob $job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId); $changes = $job->changeDetails($this->detailsFactory->build($data, $job->details())); + $job->recordCompletion($this->clock->now()); if ($changes === []) { return; diff --git a/app/Modules/Jobs/Domain/Entities/CustomerJob.php b/app/Modules/Jobs/Domain/Entities/CustomerJob.php index f63c860..6bb0067 100644 --- a/app/Modules/Jobs/Domain/Entities/CustomerJob.php +++ b/app/Modules/Jobs/Domain/Entities/CustomerJob.php @@ -8,10 +8,13 @@ use App\Modules\Jobs\Domain\Enums\JobStatus; use App\Modules\Jobs\Domain\ValueObjects\FieldChange; use App\Modules\Jobs\Domain\ValueObjects\JobDetails; use App\Modules\Jobs\Domain\ValueObjects\JobNumber; +use DateTimeImmutable; /** * A customer job (asiakastyö): the work done for a customer, which tasks, - * time entries and invoices are attached to. + * time entries and invoices are attached to. It knows when it was done: + * reaching a status that locks hours sets the time, and opening the lock + * clears it. */ final class CustomerJob { @@ -20,6 +23,7 @@ final class CustomerJob private JobNumber $number, private JobDetails $details, private int $createdBy, + private ?DateTimeImmutable $completedAt = null, ) {} public static function open(JobNumber $number, JobDetails $details, int $createdBy): self @@ -27,9 +31,30 @@ final class CustomerJob return new self(null, $number, $details, $createdBy); } - public static function reconstitute(int $id, JobNumber $number, JobDetails $details, int $createdBy): self + public static function reconstitute(int $id, JobNumber $number, JobDetails $details, int $createdBy, ?DateTimeImmutable $completedAt = null): self { - return new self($id, $number, $details, $createdBy); + return new self($id, $number, $details, $createdBy, $completedAt); + } + + /** + * Keeps the completion time in step with the status: set when the job + * first reaches a locking status, kept while it stays locked, cleared + * when the lock is opened. + */ + public function recordCompletion(DateTimeImmutable $now): void + { + if (! $this->details->status->locksTime()) { + $this->completedAt = null; + + return; + } + + $this->completedAt ??= $now; + } + + public function completedAt(): ?DateTimeImmutable + { + return $this->completedAt; } /** diff --git a/app/Modules/Jobs/Domain/ValueObjects/JobDetails.php b/app/Modules/Jobs/Domain/ValueObjects/JobDetails.php index 503f722..ca16d86 100644 --- a/app/Modules/Jobs/Domain/ValueObjects/JobDetails.php +++ b/app/Modules/Jobs/Domain/ValueObjects/JobDetails.php @@ -11,6 +11,7 @@ use App\Modules\Jobs\Domain\Enums\Priority; use App\Modules\Jobs\Domain\Exceptions\DueBeforeStart; use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart; use App\Modules\Jobs\Domain\Exceptions\ServiceUnitRequired; +use App\Modules\Shared\Domain\ValueObjects\Money; use DateTimeImmutable; /** @@ -58,6 +59,7 @@ final readonly class JobDetails array $memberUserIds, array $tags, public array $links, + public ?Money $quotedAmount = null, ) { $serviceUnitIds = array_values(array_unique($serviceUnitIds)); @@ -95,7 +97,7 @@ final readonly class JobDetails $this->kind, $status, $this->priority, $this->paymentBasis, $this->estimatedHours, $this->customerReference, $this->instructions, $this->responsibleUserId, $this->contactUserId, $this->customerContactId, $this->billerUserId, $this->billingUnitId, $this->memberUserIds, - $this->tags, $this->links, + $this->tags, $this->links, $this->quotedAmount, ); } @@ -137,6 +139,7 @@ final readonly class JobDetails 'priority' => $this->priority->value, 'payment_basis' => $this->paymentBasis?->value, 'estimated_hours' => $this->estimatedHours?->format(), + 'quoted_amount' => $this->quotedAmount === null ? null : (string) $this->quotedAmount->cents, 'customer_reference' => $this->customerReference, 'instructions' => $this->instructions, 'responsible_user' => (string) $this->responsibleUserId, diff --git a/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/CustomerJobModel.php b/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/CustomerJobModel.php index 75da550..c8d8569 100644 --- a/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/CustomerJobModel.php +++ b/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/CustomerJobModel.php @@ -41,13 +41,15 @@ use Illuminate\Database\Eloquent\Model; * @property int|null $biller_user_id * @property int|null $billing_unit_id * @property int $created_by + * @property int|null $quoted_amount_cents + * @property CarbonImmutable|null $completed_at * @property CarbonImmutable $created_at */ #[Table('customer_jobs')] #[Fillable([ 'number', 'name', 'description', 'customer_id', 'starts_on', 'due_on', 'planned_from', 'planned_to', - 'kind', 'status', 'priority', 'payment_basis', 'estimated_hours', 'customer_reference', 'instructions', - 'responsible_user_id', 'contact_user_id', 'customer_contact_id', 'biller_user_id', 'billing_unit_id', 'created_by', + 'kind', 'status', 'priority', 'payment_basis', 'estimated_hours', 'quoted_amount_cents', 'customer_reference', 'instructions', + 'responsible_user_id', 'contact_user_id', 'customer_contact_id', 'biller_user_id', 'billing_unit_id', 'created_by', 'completed_at', ])] #[UseFactory(CustomerJobFactory::class)] class CustomerJobModel extends Model @@ -62,6 +64,8 @@ class CustomerJobModel extends Model { return [ 'customer_id' => 'integer', + 'quoted_amount_cents' => 'integer', + 'completed_at' => 'immutable_datetime', 'starts_on' => 'immutable_date', 'due_on' => 'immutable_date', 'planned_from' => 'immutable_date', diff --git a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobQueries.php b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobQueries.php index f107e62..8979a10 100644 --- a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobQueries.php +++ b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobQueries.php @@ -179,6 +179,8 @@ class EloquentCustomerJobQueries implements CustomerJobQueries ->all(), createdAt: $model->created_at->format('Y-m-d H:i'), createdByName: $userNames[$model->created_by] ?? '', + quotedAmountCents: $model->quoted_amount_cents, + completedAt: $model->completed_at?->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'), ); } diff --git a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobRepository.php b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobRepository.php index eccb6d5..9ee9bd1 100644 --- a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobRepository.php +++ b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobRepository.php @@ -11,6 +11,7 @@ use App\Modules\Jobs\Domain\ValueObjects\JobName; use App\Modules\Jobs\Domain\ValueObjects\JobNumber; use App\Modules\Jobs\Domain\ValueObjects\Tag; use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel; +use App\Modules\Shared\Domain\ValueObjects\Money; use DateTimeImmutable; use Illuminate\Support\Facades\DB; @@ -42,6 +43,8 @@ class EloquentCustomerJobRepository implements CustomerJobRepository 'priority' => $details->priority, 'payment_basis' => $details->paymentBasis, 'estimated_hours' => $details->estimatedHours?->value, + 'quoted_amount_cents' => $details->quotedAmount?->cents, + 'completed_at' => $job->completedAt()?->format('Y-m-d H:i:s'), 'customer_reference' => $details->customerReference, 'instructions' => $details->instructions, 'responsible_user_id' => $details->responsibleUserId, @@ -147,8 +150,15 @@ class EloquentCustomerJobRepository implements CustomerJobRepository ->get() ->map(fn ($row): JobLink => new JobLink((string) $row->title, (string) $row->url)) ->all(), + quotedAmount: $model->quoted_amount_cents === null ? null : Money::fromCents($model->quoted_amount_cents), ); - return CustomerJob::reconstitute($model->id, JobNumber::fromString($model->number), $details, $model->created_by); + return CustomerJob::reconstitute( + $model->id, + JobNumber::fromString($model->number), + $details, + $model->created_by, + $model->completed_at === null ? null : DateTimeImmutable::createFromInterface($model->completed_at), + ); } } diff --git a/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php b/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php index 1378b9c..aed4abe 100644 --- a/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php +++ b/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php @@ -18,6 +18,9 @@ use App\Modules\Jobs\Domain\Exceptions\InvalidTag; use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart; use App\Modules\Jobs\Domain\Exceptions\ServiceUnitRequired; use App\Modules\Shared\Domain\Exceptions\DomainException; +use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount; +use App\Modules\Shared\Domain\ValueObjects\Money; +use Illuminate\Support\Facades\Gate; use Illuminate\Validation\Rule; use Livewire\Form; @@ -54,6 +57,11 @@ class JobForm extends Form public string $estimated_hours = ''; + /** + * Only for users who may see quoted amounts; filled and sent only for them. + */ + public string $quoted_amount = ''; + public string $customer_reference = ''; public string $instructions = ''; @@ -92,6 +100,7 @@ class JobForm extends Form $this->priority = $job->priority->value; $this->payment_basis = $job->paymentBasis->value ?? ''; $this->estimated_hours = $job->estimatedHours ?? ''; + $this->quoted_amount = $job->quotedAmountCents === null || ! Gate::allows('money.quotes') ? '' : Money::fromCents($job->quotedAmountCents)->inputValue(); $this->customer_reference = $job->customerReference ?? ''; $this->instructions = $job->instructions ?? ''; $this->responsible_user_id = (string) $job->responsible->id; @@ -135,6 +144,8 @@ class JobForm extends Form memberUserIds: array_map('intval', $this->member_user_ids), tags: $this->tags, links: array_values(array_filter($this->links, fn (array $link): bool => trim($link['title']) !== '' || trim($link['url']) !== '')), + quotedAmount: $text($this->quoted_amount), + setsQuotedAmount: Gate::allows('money.quotes'), ); } @@ -149,6 +160,7 @@ class JobForm extends Form $exception instanceof DueBeforeStart => ['due_on', 'jobs.errors.due_before_start'], $exception instanceof PlanEndsBeforeStart => ['planned_to', 'jobs.errors.plan_ends_before_start'], $exception instanceof InvalidEstimatedHours => ['estimated_hours', 'jobs.errors.invalid_hours'], + $exception instanceof InvalidMoneyAmount => ['quoted_amount', 'jobs.errors.invalid_amount'], $exception instanceof InvalidTag => ['tags', 'jobs.errors.invalid_tag'], $exception instanceof InvalidJobLink => ['links', 'jobs.errors.invalid_link'], $exception instanceof InvalidJobReference => [self::referenceField($exception->field), 'jobs.errors.invalid_reference'], @@ -188,6 +200,7 @@ class JobForm extends Form 'priority' => ['required', Rule::enum(Priority::class)], 'payment_basis' => ['nullable', Rule::enum(PaymentBasis::class)], 'estimated_hours' => ['nullable', 'numeric', 'min:0', 'max:99999'], + 'quoted_amount' => ['nullable', 'string', 'max:20'], 'customer_reference' => ['nullable', 'string', 'max:255'], 'instructions' => ['nullable', 'string', 'max:10000'], 'responsible_user_id' => ['required', 'integer'], @@ -226,6 +239,7 @@ class JobForm extends Form 'estimated_hours.numeric' => __('jobs.errors.invalid_hours'), 'estimated_hours.min' => __('jobs.errors.invalid_hours'), 'estimated_hours.max' => __('jobs.errors.invalid_hours'), + 'quoted_amount.max' => __('jobs.errors.invalid_amount'), 'customer_reference.max' => __('jobs.errors.too_long', ['max' => 255]), 'instructions.max' => __('jobs.errors.too_long', ['max' => 10000]), 'responsible_user_id.required' => __('jobs.errors.responsible_required'), diff --git a/app/Modules/Jobs/Presentation/Livewire/⚡form.blade.php b/app/Modules/Jobs/Presentation/Livewire/⚡form.blade.php index d5ac277..3d33c49 100644 --- a/app/Modules/Jobs/Presentation/Livewire/⚡form.blade.php +++ b/app/Modules/Jobs/Presentation/Livewire/⚡form.blade.php @@ -358,6 +358,16 @@ new class extends Component :label="__('jobs.fields.estimated_hours')" :hint="__('jobs.fields.estimated_hours_description')" /> + + @can('money.quotes') + + @endcan diff --git a/app/Modules/Jobs/Presentation/Livewire/⚡show.blade.php b/app/Modules/Jobs/Presentation/Livewire/⚡show.blade.php index 42e6996..24b316e 100644 --- a/app/Modules/Jobs/Presentation/Livewire/⚡show.blade.php +++ b/app/Modules/Jobs/Presentation/Livewire/⚡show.blade.php @@ -15,6 +15,7 @@ use App\Modules\Jobs\Domain\Exceptions\TaskNotFound; use App\Modules\Users\Application\Contracts\UserAccountQueries; use Carbon\CarbonImmutable; use Illuminate\Support\Facades\Gate; +use App\Modules\Shared\Domain\ValueObjects\Money; use Livewire\Attributes\Computed; use Livewire\Attributes\Locked; use Livewire\Component; @@ -61,7 +62,12 @@ new class extends Component #[Computed] public function changes(): array { - return app(CustomerJobQueries::class)->changes($this->jobId); + $changes = app(CustomerJobQueries::class)->changes($this->jobId); + + // Changes of the quoted amount are shown only to those who may see quotes (spec 5.7). + return Gate::allows('money.quotes') + ? $changes + : array_values(array_filter($changes, fn (JobChangeEntry $change): bool => $change->field !== 'quoted_amount')); } /** @@ -165,6 +171,7 @@ new class extends Component return match ($field) { 'starts_on', 'due_on', 'planned_from', 'planned_to' => $this->formatDate($value), + 'quoted_amount' => Money::fromCents((int) $value)->format(), 'kind' => __('jobs.kinds.'.$value), 'status' => __('jobs.statuses.'.$value), 'priority' => __('jobs.priorities.'.$value), @@ -235,12 +242,22 @@ new class extends Component
{{ __('jobs.fields.estimated_hours') }}
{{ $job->estimatedHours === null ? __('jobs.not_set') : str_replace('.', ',', $job->estimatedHours).' h' }}
+ @can('money.quotes') +
{{ __('jobs.fields.quoted_amount') }}
+
{{ $job->quotedAmountCents === null ? __('jobs.not_set') : Money::fromCents($job->quotedAmountCents)->format().' '.__('jobs.vat_excluded') }}
+ @endcan +
{{ __('jobs.fields.customer_reference') }}
{{ $job->customerReference ?? __('jobs.not_set') }}
{{ __('jobs.fields.tags') }}
{{ $job->tags === [] ? __('jobs.tags.none') : implode(', ', $job->tags) }}
+ @if ($job->completedAt !== null) +
{{ __('jobs.fields.completed_at') }}
+
{{ CarbonImmutable::parse($job->completedAt)->format('j.n.Y H.i') }}
+ @endif +
{{ __('jobs.fields.created_at') }}
{{ CarbonImmutable::parse($job->createdAt)->format('j.n.Y H.i') }}, {{ $job->createdByName }}
diff --git a/database/factories/CustomerJobFactory.php b/database/factories/CustomerJobFactory.php index 0acc819..2bdc822 100644 --- a/database/factories/CustomerJobFactory.php +++ b/database/factories/CustomerJobFactory.php @@ -57,6 +57,11 @@ class CustomerJobFactory extends Factory }); } + public function quoted(int $cents): static + { + return $this->state(fn (): array => ['quoted_amount_cents' => $cents]); + } + /** * @param list $userIds */ diff --git a/database/migrations/2026_11_24_094017_add_money_fields_to_customer_jobs_table.php b/database/migrations/2026_11_24_094017_add_money_fields_to_customer_jobs_table.php new file mode 100644 index 0000000..91d27e5 --- /dev/null +++ b/database/migrations/2026_11_24_094017_add_money_fields_to_customer_jobs_table.php @@ -0,0 +1,30 @@ +unsignedInteger('quoted_amount_cents')->nullable()->after('estimated_hours'); + $table->timestamp('completed_at')->nullable()->after('created_by'); + + $table->index('completed_at'); + }); + } + + public function down(): void + { + Schema::table('customer_jobs', function (Blueprint $table) { + $table->dropIndex(['completed_at']); + $table->dropColumn(['quoted_amount_cents', 'completed_at']); + }); + } +}; diff --git a/docs/maarittely.md b/docs/maarittely.md index 97edede..9aa5d3b 100644 --- a/docs/maarittely.md +++ b/docs/maarittely.md @@ -119,7 +119,7 @@ Nimi, sähköposti, rooli (yksi per käyttäjä, *K13*), aktiivinen, harjoitteli | Prioriteetti | `priority` | ks. 5.4 | | Maksuperuste | `payment_basis` | ks. 5.5 | | Arvioidut tunnit | `estimated_hours` | *(K24)* | -| Budjetti / tarjoussumma | `quoted_amount` | Rahamäärä (2.3) *(K24)* | +| Budjetti / tarjoussumma | `quoted_amount_cents` | Rahamäärä ALV 0; näkyy ja muokattavissa vain Tarjoukset-oikeudella (2.3), muiden tallennus säilyttää sen *(K24, valmis)* | | Asiakkaan viite (PO) | `customer_reference` | *(K24)* | | Tagit | `tags` | Haku listasta tai uuden lisäys, joka tallentuu listaan *(K25)* | | Linkit | `links` | Linkit tiedostoihin (Drive, Dropbox ym.), otsikko + URL *(K26)* | @@ -199,7 +199,7 @@ Käyttäjän "omat työt" ovat työt, joissa hän on vastuuhenkilö, kontaktihen ### 5.7 Muutoshistoria *(K27)* -Asiakastyön kenttien muutokset tallennetaan: kuka, milloin, mikä kenttä, vanha ja uusi arvo. Historia näkyy työn sivulla. Rahakenttien muutokset näkyvät vain niille, joilla on oikeus rahamääriin. +Asiakastyön kenttien muutokset tallennetaan: kuka, milloin, mikä kenttä, vanha ja uusi arvo. Historia näkyy työn sivulla. Rahakenttien muutokset näkyvät vain niille, joilla on oikeus rahamääriin (tarjoussumma: Tarjoukset-oikeus) *(valmis)*. ## 6. Uuden työn vastaanotto *(K46, K47)* @@ -341,7 +341,7 @@ Taulut ja tärkeimmät sarakkeet. Kaikissa tauluissa on `id` ja `created_at`/`up - `settings` *(V2, olemassa)*: `key` (uniikki, omistajamoduulin etuliite, esim. `catalog.mileage_rate_cents`), `value` (esim. km-hinta, työajanseuranta päällä) **Jobs** (asiakastyöt) -- `customer_jobs`: `number`, `name`, `description`, `customer_id`, `starts_on`, `due_on`, `planned_from`, `planned_to`, `kind`, `status`, `priority`, `payment_basis`, `estimated_hours`, `customer_reference`, `instructions`, `responsible_user_id`, `contact_user_id`, `customer_contact_id`, `biller_user_id`, `billing_unit_id`, `created_by` *(olemassa)*; `quoted_amount_cents`, `completed_at` *(V2)* +- `customer_jobs`: `number`, `name`, `description`, `customer_id`, `starts_on`, `due_on`, `planned_from`, `planned_to`, `kind`, `status`, `priority`, `payment_basis`, `estimated_hours`, `customer_reference`, `instructions`, `responsible_user_id`, `contact_user_id`, `customer_contact_id`, `biller_user_id`, `billing_unit_id`, `created_by` *(olemassa)*; `quoted_amount_cents`, `completed_at` (asetetaan, kun työ saavuttaa lukitsevan tilan; tyhjennetään, kun lukitus avataan) *(V2, olemassa)* - `job_number_sequences`: `year`, `last_number` - `customer_job_service_unit`: `customer_job_id`, `service_unit_id` - `customer_job_members`: `customer_job_id`, `user_id` @@ -465,6 +465,10 @@ Jokainen sivu täyttää saavutettavuusvaatimukset ja saa selaintestin (vaalea/t - Johdon Tuntien hyväksyntä -sivu (`/time/approvals`, valikossa "Hyväksyntä") ja toisen käyttäjän viikkonäkymä lukutilassa (`/time?kayttaja=ID`), jossa hyväksyntä ja sen peruminen (vahvistus dialogissa). - Valmiin työn siirto takaisin aiempaan tilaan vain johdolle (Kanban ja työn lomake). +### 17.5 Asiakastyön rahakentät *(valmis)* +- Työn lomakkeella "Budjetti / tarjoussumma (€, ALV 0)" ja työn sivulla sen arvo vain Tarjoukset-oikeudella; muutoshistorian tarjoussummarivit näytetään vain heille. +- Työn sivulla "Valmistunut", kun työ on siirretty Valmis-tilaan (tai myöhempään); aika säilyy laskutustiloissa ja tyhjenee, kun johto avaa lukituksen. + ## 18. Avoimet kysymykset 1. ~~**Lukitus**~~: ratkaistu – vain johto avaa valmiin työn lukituksen siirtämällä työn aiempaan tilaan, ja avaus näkyy muutoshistoriassa (5.3). diff --git a/lang/fi/jobs.php b/lang/fi/jobs.php index 82e82c7..a4ed2cb 100644 --- a/lang/fi/jobs.php +++ b/lang/fi/jobs.php @@ -1,6 +1,7 @@ '(ALV 0)', 'title' => 'Asiakastyöt', 'new' => 'Uusi asiakastyö', 'create_title' => 'Uusi asiakastyö', @@ -125,6 +126,10 @@ return [ 'payment_basis' => 'Maksuperuste', 'estimated_hours' => 'Arvioidut tunnit', 'estimated_hours_description' => 'Tunteina, esimerkiksi 7,5.', + 'quoted_amount' => 'Budjetti / tarjoussumma', + 'quoted_amount_input' => 'Budjetti / tarjoussumma (€, ALV 0)', + 'quoted_amount_description' => 'Veroton summa, esim. 4500. Näkyy vain niille, joilla on oikeus tarjouksiin.', + 'completed_at' => 'Valmistunut', 'customer_reference' => 'Asiakkaan viite (PO)', 'instructions' => 'Ohjeistus', 'instructions_description' => 'Työhön liittyvät asiat ja huomiot tekijöille.', @@ -232,6 +237,7 @@ return [ 'due_before_start' => 'Määräaika ei voi olla ennen alkamispäivää.', 'plan_ends_before_start' => 'Suunniteltu loppu ei voi olla ennen suunniteltua alkua.', 'invalid_hours' => 'Anna tunnit nollana tai suurempana, vartin tarkkuudella (esim. 7,25).', + 'invalid_amount' => 'Anna summa euroina, esim. 4500 tai 4500,50 (enintään kaksi desimaalia).', 'invalid_tag' => 'Tagin pituus on 1–50 merkkiä.', 'invalid_link' => 'Anna linkille otsikko ja osoite, joka alkaa http:// tai https://.', 'invalid_reference' => 'Valinta ei ole enää käytettävissä. Valitse uudelleen.', diff --git a/tests/Browser/JobsAccessibilityTest.php b/tests/Browser/JobsAccessibilityTest.php index 40e78a9..1bb3e30 100644 --- a/tests/Browser/JobsAccessibilityTest.php +++ b/tests/Browser/JobsAccessibilityTest.php @@ -22,6 +22,7 @@ beforeEach(function () { 'priority' => 'immediate', 'due_on' => now()->subDay()->toDateString(), 'instructions' => 'Kuvaus klo 9.', + 'quoted_amount_cents' => 450000, ]); DB::table('customer_job_links')->insert(['customer_job_id' => $this->job->id, 'title' => 'Briiffi', 'url' => 'https://drive.example.test/b', 'sort_order' => 1]); }); diff --git a/tests/Fakes/InMemoryCustomerJobRepository.php b/tests/Fakes/InMemoryCustomerJobRepository.php index e79e96a..15d5848 100644 --- a/tests/Fakes/InMemoryCustomerJobRepository.php +++ b/tests/Fakes/InMemoryCustomerJobRepository.php @@ -32,7 +32,7 @@ class InMemoryCustomerJobRepository implements CustomerJobRepository, JobNumberS public function save(CustomerJob $job): CustomerJob { $id = $job->id() ?? $this->nextId++; - $this->jobs[$id] = CustomerJob::reconstitute($id, $job->number(), $job->details(), $job->createdBy()); + $this->jobs[$id] = CustomerJob::reconstitute($id, $job->number(), $job->details(), $job->createdBy(), $job->completedAt()); return clone $this->jobs[$id]; } diff --git a/tests/Feature/Modules/Jobs/JobMoneyFieldsTest.php b/tests/Feature/Modules/Jobs/JobMoneyFieldsTest.php new file mode 100644 index 0000000..218195c --- /dev/null +++ b/tests/Feature/Modules/Jobs/JobMoneyFieldsTest.php @@ -0,0 +1,93 @@ +job = CustomerJobModel::factory()->quoted(450000)->create(['name' => 'Messuvideo', 'status' => JobStatus::Order]); +}); + +it('lets management set the quoted amount and see it with its history', function () { + $this->actingAs(User::factory()->management()->create()); + + Livewire::test('jobs::form', ['job' => $this->job->id]) + ->assertSet('form.quoted_amount', '4500,00') + ->set('form.quoted_amount', '5200,50') + ->call('save') + ->assertHasNoErrors(); + + expect($this->job->refresh()->quoted_amount_cents)->toBe(520050); + + $this->get(route('jobs.show', $this->job->id)) + ->assertSee('Budjetti / tarjoussumma') + ->assertSee("5\u{00A0}200,50\u{00A0}€", false) + ->assertSee("4\u{00A0}500,00\u{00A0}€", false); +}); + +it('rejects an invalid quoted amount with a Finnish field error', function () { + $this->actingAs(User::factory()->management()->create()); + + Livewire::test('jobs::form', ['job' => $this->job->id]) + ->set('form.quoted_amount', '12,345') + ->call('save') + ->assertHasErrors(['form.quoted_amount' => [__('jobs.errors.invalid_amount')]]); +}); + +it('hides the quoted amount from employees without the quotes permission and keeps it when they save', function () { + $this->actingAs(User::factory()->management()->create()); + Livewire::test('jobs::form', ['job' => $this->job->id])->set('form.quoted_amount', '5000')->call('save'); + + $this->actingAs(User::factory()->employee()->create()); + + $this->get(route('jobs.show', $this->job->id)) + ->assertOk() + ->assertDontSee('Budjetti / tarjoussumma') + ->assertDontSee("5\u{00A0}000,00\u{00A0}€", false); + + Livewire::test('jobs::form', ['job' => $this->job->id]) + ->assertSet('form.quoted_amount', '') + ->assertDontSee('Budjetti / tarjoussumma') + ->set('form.quoted_amount', '1') + ->set('form.name', 'Messuvideo 2') + ->call('save') + ->assertHasNoErrors(); + + expect($this->job->refresh()->quoted_amount_cents)->toBe(500000) + ->and($this->job->name)->toBe('Messuvideo 2'); +}); + +it('shows the quoted amount to an employee with the quotes permission', function () { + $this->actingAs(User::factory()->employee()->withMoneyPermissions(MoneyPermission::Quotes)->create()); + + $this->get(route('jobs.show', $this->job->id))->assertSee("4\u{00A0}500,00\u{00A0}€", false); + + Livewire::test('jobs::form', ['job' => $this->job->id]) + ->set('form.quoted_amount', '') + ->call('save') + ->assertHasNoErrors(); + + expect($this->job->refresh()->quoted_amount_cents)->toBeNull(); +}); + +it('records when a job is done and clears it when management opens the lock', function () { + $this->actingAs(User::factory()->management()->create()); + + Livewire::test('jobs::board')->set('mine', false)->call('moveJob', $this->job->id, 'done'); + $completedAt = $this->job->refresh()->completed_at; + + expect($completedAt)->not->toBeNull(); + + Livewire::test('jobs::board')->set('mine', false)->call('moveJob', $this->job->id, 'invoiced'); + expect($this->job->refresh()->completed_at?->equalTo($completedAt))->toBeTrue(); + + $this->get(route('jobs.show', $this->job->id))->assertSee('Valmistunut'); + + Livewire::test('jobs::board')->set('mine', false)->call('moveJob', $this->job->id, 'in_production'); + expect($this->job->refresh()->completed_at)->toBeNull(); +}); diff --git a/tests/Unit/Modules/Jobs/Application/CustomerJobUseCasesTest.php b/tests/Unit/Modules/Jobs/Application/CustomerJobUseCasesTest.php index cc6a16c..1ff185e 100644 --- a/tests/Unit/Modules/Jobs/Application/CustomerJobUseCasesTest.php +++ b/tests/Unit/Modules/Jobs/Application/CustomerJobUseCasesTest.php @@ -34,8 +34,8 @@ beforeEach(function () { $factory = new JobDetailsFactory($this->customers, $this->catalog, $this->users); $this->create = new CreateCustomerJob($this->jobs, $this->jobs, $factory, $transactions, $this->clock); - $this->update = new UpdateCustomerJob($this->jobs, $factory, $transactions); - $this->changeStatus = new ChangeJobStatus($this->jobs, $transactions); + $this->update = new UpdateCustomerJob($this->jobs, $factory, $transactions, $this->clock); + $this->changeStatus = new ChangeJobStatus($this->jobs, $transactions, new FakeClock(new DateTimeImmutable('2026-05-01'))); }); /** diff --git a/tests/Unit/Modules/Jobs/Application/TaskUseCasesTest.php b/tests/Unit/Modules/Jobs/Application/TaskUseCasesTest.php index f8937a9..d4e51db 100644 --- a/tests/Unit/Modules/Jobs/Application/TaskUseCasesTest.php +++ b/tests/Unit/Modules/Jobs/Application/TaskUseCasesTest.php @@ -55,7 +55,7 @@ beforeEach(function () { $this->changeTaskStatus = new ChangeTaskStatus($this->tasks, $transactions); $this->usage = new FakeTaskUsage; $this->removeTask = new RemoveTask($this->tasks, $this->usage, $transactions); - $this->changeJobStatus = new ChangeJobStatus($this->jobs, $transactions); + $this->changeJobStatus = new ChangeJobStatus($this->jobs, $transactions, new FakeClock(new DateTimeImmutable('2026-05-01'))); }); it('allows tasks from a quote request on, not before and not when archived', function () { diff --git a/tests/Unit/Modules/Jobs/Domain/JobDomainTest.php b/tests/Unit/Modules/Jobs/Domain/JobDomainTest.php index b0112ad..59dbdd4 100644 --- a/tests/Unit/Modules/Jobs/Domain/JobDomainTest.php +++ b/tests/Unit/Modules/Jobs/Domain/JobDomainTest.php @@ -16,6 +16,7 @@ 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 $overrides @@ -97,3 +98,29 @@ it('returns only the fields that changed', function () { ->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(); +});