Add customer jobs with numbering, members, tags, links and history

Phase 1, item 4. New Jobs module:
- Customer jobs with the spec's fields except money (phase 2): yearly
  running numbers (2026-0001) from a locked counter row, service units,
  schedule, kind, status, priority, payment basis, estimated hours,
  PO reference, instructions, members (responsible, contact person,
  customer contact, biller, billing unit, other members), tags shared
  across jobs, and links.
- Every change is recorded field by field and shown on the job page with
  names and Finnish dates.
- References to other modules are checked through their query
  contracts: contact and billing unit must be the customer's own;
  service units and users must be in use unless already on the job.
- Job list with search, filters and "my jobs" on by default; job form
  with an accessible customer search and quick add; job page.
- Access: subcontractors see only jobs they are members of, read only;
  billing statuses for management and the job's biller; archiving for
  management.

Related changes:
- Customers: one internal customer (created by a migration) for internal
  work; contacts and billing units used by a job cannot be removed.
- Users and Catalog: batch name lookups and active user options with
  roles for other modules.
- <x-checkbox-group> component; spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-23 18:57:18 +02:00
co-authored by Claude Opus 5.5
parent 51b2d4a252
commit 70e107f6e2
95 changed files with 4631 additions and 26 deletions
+72
View File
@@ -0,0 +1,72 @@
<?php
namespace Database\Factories;
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
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\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\DB;
/**
* @extends Factory<CustomerJobModel>
*/
class CustomerJobFactory extends Factory
{
protected $model = CustomerJobModel::class;
private static int $sequence = 9000;
/**
* @return array<string, mixed>
*/
public function definition(): array
{
$responsible = User::factory();
return [
'number' => sprintf('2026-%04d', ++self::$sequence),
'name' => fake()->sentence(3),
'customer_id' => CustomerModel::factory(),
'kind' => JobKind::OneOff,
'status' => JobStatus::Tentative,
'priority' => Priority::Normal,
'responsible_user_id' => $responsible,
'created_by' => $responsible,
];
}
/**
* Every job has a service unit: the first active one unless given.
*/
public function configure(): static
{
return $this->afterCreating(function (CustomerJobModel $job): void {
if (DB::table('customer_job_service_unit')->where('customer_job_id', $job->id)->exists()) {
return;
}
$unitId = ServiceUnitModel::query()->where('is_shared', false)->orderBy('sort_order')->value('id')
?? ServiceUnitModel::factory()->create()->id;
DB::table('customer_job_service_unit')->insert(['customer_job_id' => $job->id, 'service_unit_id' => $unitId]);
});
}
/**
* @param list<int> $userIds
*/
public function withMembers(array $userIds): static
{
return $this->afterCreating(function (CustomerJobModel $job) use ($userIds): void {
DB::table('customer_job_members')->insert(array_map(
fn (int $userId): array => ['customer_job_id' => $job->id, 'user_id' => $userId],
$userIds,
));
});
}
}
@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Marks the company's own customer, whose jobs hold internal work.
*/
public function up(): void
{
Schema::table('customers', function (Blueprint $table) {
$table->boolean('is_internal')->default(false)->after('type');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('customers', function (Blueprint $table) {
$table->dropColumn('is_internal');
});
}
};
@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* The one internal customer (the company itself), so internal work has
* a customer job to be recorded on. Its name can be changed later.
*/
public function up(): void
{
if (DB::table('customers')->where('is_internal', true)->exists()) {
return;
}
DB::table('customers')->insert([
'name' => 'Oma yritys (sisäinen)',
'type' => 'company',
'is_internal' => true,
'created_at' => now(),
'updated_at' => now(),
]);
}
/**
* The internal customer is not removed: jobs may refer to it.
*/
public function down(): void {}
};
@@ -0,0 +1,107 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Customer jobs with their numbering, service units, members, tags,
* links and history. Money columns come in phase 2. Rows other modules
* own (customers, users, contacts, billing units, service units) cannot
* be deleted while a job refers to them.
*/
public function up(): void
{
Schema::create('job_number_sequences', function (Blueprint $table) {
$table->unsignedSmallInteger('year')->primary();
$table->unsignedInteger('last_number');
});
Schema::create('customer_jobs', function (Blueprint $table) {
$table->id();
$table->string('number', 12)->unique();
$table->string('name');
$table->text('description')->nullable();
$table->foreignId('customer_id')->constrained()->restrictOnDelete();
$table->date('starts_on')->nullable();
$table->date('due_on')->nullable()->index();
$table->date('planned_from')->nullable();
$table->date('planned_to')->nullable();
$table->string('kind', 20);
$table->string('status', 20)->index();
$table->string('priority', 20);
$table->string('payment_basis', 20)->nullable();
$table->decimal('estimated_hours', 7, 2)->nullable();
$table->string('customer_reference')->nullable();
$table->text('instructions')->nullable();
$table->foreignId('responsible_user_id')->constrained('users')->restrictOnDelete();
$table->foreignId('contact_user_id')->nullable()->constrained('users')->restrictOnDelete();
$table->foreignId('customer_contact_id')->nullable()->constrained('customer_contacts')->restrictOnDelete();
$table->foreignId('biller_user_id')->nullable()->constrained('users')->restrictOnDelete();
$table->foreignId('billing_unit_id')->nullable()->constrained('billing_units')->restrictOnDelete();
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
$table->timestamps();
});
Schema::create('customer_job_service_unit', function (Blueprint $table) {
$table->foreignId('customer_job_id')->constrained()->cascadeOnDelete();
$table->foreignId('service_unit_id')->constrained()->restrictOnDelete();
$table->primary(['customer_job_id', 'service_unit_id']);
});
Schema::create('customer_job_members', function (Blueprint $table) {
$table->foreignId('customer_job_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->restrictOnDelete();
$table->primary(['customer_job_id', 'user_id']);
});
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name', 50)->unique();
$table->timestamps();
});
Schema::create('customer_job_tag', function (Blueprint $table) {
$table->foreignId('customer_job_id')->constrained()->cascadeOnDelete();
$table->foreignId('tag_id')->constrained()->restrictOnDelete();
$table->primary(['customer_job_id', 'tag_id']);
});
Schema::create('customer_job_links', function (Blueprint $table) {
$table->id();
$table->foreignId('customer_job_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->string('url', 2000);
$table->unsignedSmallInteger('sort_order');
});
Schema::create('customer_job_changes', function (Blueprint $table) {
$table->id();
$table->foreignId('customer_job_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->restrictOnDelete();
$table->string('field', 40);
$table->text('old_value')->nullable();
$table->text('new_value')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->index(['customer_job_id', 'id']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('customer_job_changes');
Schema::dropIfExists('customer_job_links');
Schema::dropIfExists('customer_job_tag');
Schema::dropIfExists('tags');
Schema::dropIfExists('customer_job_members');
Schema::dropIfExists('customer_job_service_unit');
Schema::dropIfExists('customer_jobs');
Schema::dropIfExists('job_number_sequences');
}
};