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
@@ -36,9 +36,9 @@ it('registers a company customer', function () {
->set('form.business_id', '0737546-2')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('customers.show', CustomerModel::query()->sole()->id));
->assertRedirect(route('customers.show', CustomerModel::query()->where('is_internal', false)->sole()->id));
$customer = CustomerModel::query()->sole();
$customer = CustomerModel::query()->where('is_internal', false)->sole();
expect($customer->name)->toBe('Mainos Oy')
->and($customer->type)->toBe(CustomerType::Company)
@@ -53,7 +53,7 @@ it('ignores a business id typed before switching to consumer', function () {
->call('save')
->assertHasNoErrors();
expect(CustomerModel::query()->sole()->business_id)->toBeNull();
expect(CustomerModel::query()->where('is_internal', false)->sole()->business_id)->toBeNull();
});
it('shows validation and business rule errors on the fields', function (array $input, string $field, string $message) {
@@ -65,7 +65,7 @@ it('shows validation and business rule errors on the fields', function (array $i
->assertHasErrors([$field])
->assertSee($message);
expect(CustomerModel::query()->count())->toBe(1);
expect(CustomerModel::query()->where('is_internal', false)->count())->toBe(1);
})->with([
'missing name' => [['form.name' => ''], 'form.name', 'Anna asiakkaan nimi.'],
'bad check digit' => [['form.name' => 'Mainos Oy', 'form.business_id' => '0737546-3'], 'form.business_id', 'Y-tunnus ei ole kelvollinen.'],
@@ -30,8 +30,8 @@ it('updates a stored customer', function () {
$customer->changeDetails(new CustomerName('Maija Meikäläinen'), CustomerType::Consumer, null);
$repository->save($customer);
expect(CustomerModel::query()->count())->toBe(1)
->and(CustomerModel::query()->sole()->type)->toBe(CustomerType::Consumer);
expect(CustomerModel::query()->where('is_internal', false)->count())->toBe(1)
->and(CustomerModel::query()->where('is_internal', false)->sole()->type)->toBe(CustomerType::Consumer);
});
it('tells whether another customer has the business id', function () {
@@ -52,6 +52,6 @@ it('returns summaries filtered by name or business id, ordered by name', functio
expect(array_map(fn ($summary) => $summary->name, $queries->search('media')))->toBe(['Alfa Media Oy', 'Bravo Media Oy'])
->and($queries->paginate('0737546')->total())->toBe(1)
->and($queries->paginate(null)->total())->toBe(3)
->and($queries->paginate(null)->total())->toBe(4) // and the internal customer
->and($queries->search('100%'))->toBe([]);
});
@@ -0,0 +1,206 @@
<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\BillingUnitModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerContactModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->create(['name' => 'Eero Työntekijä']);
$this->actingAs($this->employee);
$this->customer = CustomerModel::factory()->create(['name' => 'Mainos Oy']);
$this->media = ServiceUnitModel::query()->where('name', 'Mediatuotanto')->sole();
});
/**
* Fills the job form with the required fields.
*/
function fillJobForm(mixed $form, int $customerId, int $serviceUnitId): mixed
{
return $form
->set('form.name', 'Messuvideo')
->call('selectCustomer', $customerId)
->set('form.service_unit_ids', [(string) $serviceUnitId]);
}
it('creates a job with the next number of the year and its history', function () {
$this->travelTo(now()->setDate(2026, 3, 1));
fillJobForm(Livewire::test('jobs::form'), $this->customer->id, $this->media->id)
->set('form.tags', ['messut'])
->set('form.links', [['title' => 'Briiffi', 'url' => 'https://drive.example.test/briiffi']])
->call('save')
->assertHasNoErrors();
fillJobForm(Livewire::test('jobs::form'), $this->customer->id, $this->media->id)->call('save')->assertHasNoErrors();
$jobs = CustomerJobModel::query()->orderBy('id')->get();
expect($jobs->pluck('number')->all())->toBe(['2026-0001', '2026-0002'])
->and($jobs[0]->responsible_user_id)->toBe($this->employee->id)
->and(DB::table('tags')->pluck('name')->all())->toBe(['messut'])
->and(DB::table('customer_job_changes')->where('customer_job_id', $jobs[0]->id)->value('field'))->toBe('created');
$this->get(route('jobs.show', $jobs[0]->id))
->assertOk()
->assertSee('2026-0001 Messuvideo')
->assertSee('Briiffi')
->assertSee('Eero Työntekijä loi työn 2026-0001.');
});
it('adds a new customer from the job form', function () {
Livewire::test('jobs::form')
->set('customerSearch', 'Uusi Asiakas Oy')
->assertSee('Lisää uusi asiakas: Uusi Asiakas Oy')
->call('createCustomer')
->assertSee('Asiakas Uusi Asiakas Oy lisätty ja valittu.');
$customer = CustomerModel::query()->where('name', 'Uusi Asiakas Oy')->sole();
expect($customer->type->value)->toBe('company');
});
it('finds customers by name and offers only their contacts and billing units', function () {
CustomerContactModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'Maija']);
CustomerContactModel::factory()->create(['name' => 'Toisen kontakti']);
BillingUnitModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'Mainos Oy, markkinointi']);
Livewire::test('jobs::form')
->set('customerSearch', 'main')
->assertSee('Valitse Mainos Oy')
->call('selectCustomer', $this->customer->id)
->assertSee('Maija')
->assertSee('Mainos Oy, markkinointi')
->assertDontSee('Toisen kontakti');
});
it('records changed fields and shows them in the history', function () {
$job = CustomerJobModel::factory()->create(['customer_id' => $this->customer->id, 'responsible_user_id' => $this->employee->id]);
Livewire::test('jobs::form', ['job' => $job->id])
->set('form.priority', 'urgent')
->set('form.due_on', '2026-06-30')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('jobs.show', $job->id));
$this->get(route('jobs.show', $job->id))
->assertSee('Prioriteetti: Normaali → Kiire')
->assertSee('Määräaika: – → 30.6.2026');
});
it('shows Finnish errors for dates and missing fields', function () {
Livewire::test('jobs::form')
->set('form.starts_on', '2026-05-10')
->set('form.due_on', '2026-05-01')
->call('selectCustomer', $this->customer->id)
->set('form.service_unit_ids', [(string) $this->media->id])
->set('form.name', 'Messuvideo')
->call('save')
->assertHasErrors(['form.due_on'])
->assertSee('Määräaika ei voi olla ennen alkamispäivää.');
Livewire::test('jobs::form')
->call('save')
->assertHasErrors(['form.name', 'form.customer_id', 'form.service_unit_ids'])
->assertSee('Valitse vähintään yksi palveluyksikkö.');
});
it('lists my jobs by default, and all jobs on request', function () {
$mine = CustomerJobModel::factory()->create(['name' => 'Oma työ', 'responsible_user_id' => $this->employee->id]);
$member = CustomerJobModel::factory()->withMembers([$this->employee->id])->create(['name' => 'Jäsenenä']);
CustomerJobModel::factory()->create(['name' => 'Muiden työ']);
CustomerJobModel::factory()->create(['name' => 'Arkistoitu työ', 'status' => 'archived', 'responsible_user_id' => $this->employee->id]);
Livewire::test('jobs::index')
->assertSee('Oma työ')
->assertSee('Jäsenenä')
->assertDontSee('Muiden työ')
->assertDontSee('Arkistoitu työ')
->set('mine', false)
->assertSee('Muiden työ')
->set('archived', true)
->assertSee('Arkistoitu työ')
->set('search', 'mainos')
->assertDontSee('Muiden työ');
expect($mine->id)->not->toBe($member->id);
});
it('lets a subcontractor see only the jobs they are a member of, read only', function () {
$subcontractor = User::factory()->subcontractor()->create();
$theirs = CustomerJobModel::factory()->withMembers([$subcontractor->id])->create(['name' => 'Alihankintatyö', 'instructions' => 'Kuvaus klo 9.']);
$other = CustomerJobModel::factory()->create(['name' => 'Muu työ']);
$this->actingAs($subcontractor);
$this->get(route('jobs.index'))->assertOk()->assertSee('Alihankintatyö')->assertDontSee('Muu työ')->assertDontSee('Uusi asiakastyö');
$this->get(route('jobs.show', $theirs->id))->assertOk()->assertSee('Kuvaus klo 9.')->assertDontSee('Muokkaa');
$this->get(route('jobs.show', $other->id))->assertForbidden();
$this->get(route('jobs.edit', $theirs->id))->assertForbidden();
$this->get(route('jobs.create'))->assertForbidden();
});
it('lets only management and the biller set billing statuses, and only management archive', function () {
$job = CustomerJobModel::factory()->create(['customer_id' => $this->customer->id, 'status' => 'done', 'responsible_user_id' => $this->employee->id]);
Livewire::test('jobs::form', ['job' => $job->id])
->assertDontSee('Lasku luotu')
->set('form.status', 'invoiced')
->call('save')
->assertHasErrors(['form.status']);
$job->update(['biller_user_id' => $this->employee->id]);
Livewire::test('jobs::form', ['job' => $job->id])
->assertSee('Lasku luotu')
->assertDontSee('Arkistoitu')
->set('form.status', 'invoiced')
->call('save')
->assertHasNoErrors();
Livewire::actingAs(User::factory()->management()->create())
->test('jobs::form', ['job' => $job->id])
->set('form.status', 'archived')
->call('save')
->assertHasNoErrors();
expect($job->fresh()->status->value)->toBe('archived');
});
it('blocks removing a billing unit or contact a job uses', function () {
$contact = CustomerContactModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'Maija']);
$billingUnit = BillingUnitModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'Mainos Oy']);
CustomerJobModel::factory()->create(['customer_id' => $this->customer->id, 'customer_contact_id' => $contact->id, 'billing_unit_id' => $billingUnit->id]);
Livewire::test('customers::show', ['customer' => $this->customer->id])
->call('confirmRemoval', 'billing_unit', $billingUnit->id)
->call('remove')
->assertSee('Laskutusyksikköä Mainos Oy ei poistettu')
->call('confirmRemoval', 'contact', $contact->id)
->call('remove')
->assertSee('Yhteyshenkilöä Maija ei poistettu');
expect($billingUnit->fresh())->not->toBeNull()
->and($contact->fresh())->not->toBeNull();
});
it('has the internal customer', function () {
expect(CustomerModel::query()->where('is_internal', true)->sole()->name)->toBe('Oma yritys (sisäinen)');
$this->get(route('customers.index'))->assertSee('(Sisäinen)');
});
it('does not let the job id be changed in the browser, and returns 404 for unknown jobs', function () {
$this->get(route('jobs.show', 999))->assertNotFound();
Livewire::test('jobs::form')->set('jobId', 1);
})->throws(CannotUpdateLockedPropertyException::class);
@@ -19,7 +19,7 @@ it('rolls back everything when the work fails', function () {
throw new RuntimeException('fail');
}))->toThrow(RuntimeException::class);
expect(CustomerModel::query()->count())->toBe(0);
expect(CustomerModel::query()->where('is_internal', false)->count())->toBe(0);
});
it('publishes events only after the transaction commits', function () {