Files
petriandClaude Opus 5.5 70e107f6e2 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]>
2026-11-23 18:57:18 +02:00

207 lines
9.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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);