Files
petriandClaude Opus 5.5 b4f2f1cfaa Add the task calendar, holidays, workflow templates and customer removal
Task calendar (/jobs/calendar) replaces per-unit task Kanban columns
(K34): a two-week Gantt of tasks per person. Bars move, resize and
change person by dragging or through a keyboard dialog. Each task's
estimate is booked evenly over working days into task_bookings, and
what exceeds the daily maximum (jobs.daily_capacity_hours, 8 h) moves
to the next working day. Tasks get planned_from/planned_to on the form.

Holidays (/settings/holidays): Finnish public holidays calculated per
year, which the admin can take out of use, plus company days off.

Also includes the earlier uncommitted work: workflow templates with
adding tasks from a template, and removing customers no module refers to.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 18:13:11 +02:00

259 lines
12 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
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 App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->actingAs(User::factory()->create());
$this->customer = CustomerModel::factory()->company('0737546-2')->create(['name' => 'Mainos Oy']);
});
it('shows the customer with its contacts and billing units', function () {
CustomerContactModel::factory()->create([
'customer_id' => $this->customer->id,
'name' => 'Maija Meikäläinen',
'phone' => '040 123 4567',
'email' => '[email protected]',
]);
BillingUnitModel::factory()->eInvoice()->create(['customer_id' => $this->customer->id, 'name' => 'Mainos Oy, markkinointi']);
CustomerContactModel::factory()->create(['name' => 'Toisen asiakkaan kontakti']);
$this->get(route('customers.show', $this->customer->id))
->assertOk()
->assertSee('Mainos Oy')
->assertSee('0737546-2')
->assertSee('Maija Meikäläinen')
->assertSee('tel:0401234567', false)
->assertSee('Mainos Oy, markkinointi')
->assertSee('003712345678')
->assertDontSee('Toisen asiakkaan kontakti');
});
it('says when a customer has no contacts or billing units yet', function () {
$this->get(route('customers.show', $this->customer->id))
->assertOk()
->assertSee('Asiakkaalle ei ole vielä lisätty yhteyshenkilöitä.')
->assertSee('Asiakkaalle ei ole vielä lisätty laskutusyksiköitä.');
});
it('adds and edits a contact', function () {
$this->get(route('customers.contacts.create', $this->customer->id))->assertOk()->assertSee('Uusi yhteyshenkilö');
Livewire::test('customers::contact-form', ['customer' => $this->customer->id])
->set('form.name', 'Maija Meikäläinen')
->set('form.title', 'Markkinointipäällikkö')
->set('form.email', '[email protected]')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('customers.show', $this->customer->id));
$contact = CustomerContactModel::query()->sole();
expect($contact->customer_id)->toBe($this->customer->id)
->and($contact->email)->toBe('[email protected]')
->and($contact->phone)->toBeNull();
Livewire::test('customers::contact-form', ['customer' => $this->customer->id, 'contact' => $contact->id])
->assertSet('form.name', 'Maija Meikäläinen')
->set('form.phone', '040 123 4567')
->call('save')
->assertHasNoErrors();
expect($contact->fresh()->phone)->toBe('040 123 4567');
});
it('shows contact form errors in Finnish', function () {
Livewire::test('customers::contact-form', ['customer' => $this->customer->id])
->set('form.email', 'maija@')
->call('save')
->assertHasErrors(['form.name', 'form.email'])
->assertSee('Anna yhteyshenkilön nimi.')
->assertSee('Sähköpostiosoite ei ole kelvollinen.');
});
it('adds and edits a billing unit, named after the customer by default', function () {
Livewire::test('customers::billing-unit-form', ['customer' => $this->customer->id])
->assertSet('form.name', 'Mainos Oy')
->set('form.e_invoice_address', '0037 1234 5678')
->set('form.e_invoice_operator', '003721291126')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('customers.show', $this->customer->id));
$billingUnit = BillingUnitModel::query()->sole();
expect($billingUnit->e_invoice_address)->toBe('003712345678');
Livewire::test('customers::billing-unit-form', ['customer' => $this->customer->id, 'billingUnit' => $billingUnit->id])
->set('form.reference', 'PO-42')
->call('save')
->assertHasNoErrors();
expect($billingUnit->fresh()->reference)->toBe('PO-42');
});
it('asks for an operator when an e-invoice address is given', function () {
Livewire::test('customers::billing-unit-form', ['customer' => $this->customer->id])
->set('form.e_invoice_address', '003712345678')
->call('save')
->assertHasErrors(['form.e_invoice_operator'])
->assertSee('Anna välittäjätunnus, koska verkkolaskuosoite on annettu.');
expect(BillingUnitModel::query()->count())->toBe(0);
});
it('removes a contact and a billing unit after confirmation', function () {
$contact = CustomerContactModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'Maija']);
$billingUnit = BillingUnitModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'Mainos Oy']);
Livewire::test('customers::show', ['customer' => $this->customer->id])
->call('confirmRemoval', 'contact', $contact->id)
->assertSee('Poistetaanko yhteyshenkilö Maija?')
->call('remove')
->assertSee('Yhteyshenkilö Maija poistettu.')
->call('confirmRemoval', 'billing_unit', $billingUnit->id)
->call('remove')
->assertSee('Laskutusyksikkö Mainos Oy poistettu.');
expect(CustomerContactModel::query()->exists())->toBeFalse()
->and(BillingUnitModel::query()->exists())->toBeFalse();
});
it('does not remove a row of another customer', function () {
$otherContact = CustomerContactModel::factory()->create();
Livewire::test('customers::show', ['customer' => $this->customer->id])
->call('confirmRemoval', 'contact', $otherContact->id)
->assertSet('removalId', null)
->call('remove');
expect($otherContact->fresh())->not->toBeNull();
});
it('does not let the row to remove be changed in the browser', function () {
Livewire::test('customers::show', ['customer' => $this->customer->id])
->set('removalId', 1);
})->throws(CannotUpdateLockedPropertyException::class);
it('returns 404 for an unknown customer or a row of another customer', function (string $route) {
$otherContact = CustomerContactModel::factory()->create();
$otherBillingUnit = BillingUnitModel::factory()->create();
$url = match ($route) {
'unknown customer' => route('customers.show', 999),
'contact of another customer' => route('customers.contacts.edit', [$this->customer->id, $otherContact->id]),
'billing unit of another customer' => route('customers.billing-units.edit', [$this->customer->id, $otherBillingUnit->id]),
};
$this->get($url)->assertNotFound();
})->with(['unknown customer', 'contact of another customer', 'billing unit of another customer']);
it('keeps subcontractors out of the customer pages', function () {
$this->actingAs(User::factory()->subcontractor()->create());
$this->get(route('customers.show', $this->customer->id))->assertForbidden();
$this->get(route('customers.contacts.create', $this->customer->id))->assertForbidden();
$this->get(route('customers.billing-units.create', $this->customer->id))->assertForbidden();
});
it('lists contacts and billing units for other modules, ordered by name', function () {
CustomerContactModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'Pekka']);
CustomerContactModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'Anna']);
BillingUnitModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'B-yksikkö']);
BillingUnitModel::factory()->create(['customer_id' => $this->customer->id, 'name' => 'A-yksikkö']);
CustomerContactModel::factory()->create(['name' => 'Toinen']);
$queries = app(CustomerQueries::class);
expect(array_map(fn ($contact) => $contact->name, $queries->contactsOf($this->customer->id)))->toBe(['Anna', 'Pekka'])
->and(array_map(fn ($unit) => $unit->name, $queries->billingUnitsOf($this->customer->id)))->toBe(['A-yksikkö', 'B-yksikkö']);
});
it('deletes the contacts and billing units of a deleted customer', function () {
CustomerContactModel::factory()->create(['customer_id' => $this->customer->id]);
BillingUnitModel::factory()->create(['customer_id' => $this->customer->id]);
$this->customer->delete();
expect(CustomerContactModel::query()->count())->toBe(0)
->and(BillingUnitModel::query()->count())->toBe(0);
});
it('removes a customer that has nothing linked to it and returns to the list', function () {
Livewire::test('customers::show', ['customer' => $this->customer->id])
->call('confirmRemoval', 'customer', $this->customer->id)
->assertSee('Poistetaanko asiakas Mainos Oy?')
->call('remove')
->assertRedirect(route('customers.index'));
expect($this->customer->fresh())->toBeNull()
->and(session('status'))->toBe('Asiakas Mainos Oy poistettu.');
});
it('keeps a customer that has a contact or a customer job and tells why', function (string $link) {
match ($link) {
'contact' => CustomerContactModel::factory()->create(['customer_id' => $this->customer->id]),
'customer job' => CustomerJobModel::factory()->create(['customer_id' => $this->customer->id]),
};
Livewire::test('customers::show', ['customer' => $this->customer->id])
->call('confirmRemoval', 'customer', $this->customer->id)
->call('remove')
->assertNoRedirect()
->assertSee('Asiakasta Mainos Oy ei poistettu');
expect($this->customer->fresh())->not->toBeNull();
})->with(['contact', 'customer job']);
it('has no remove button for the internal customer and does not remove it', function () {
$internal = CustomerModel::query()->where('is_internal', true)->sole();
Livewire::test('customers::show', ['customer' => $internal->id])
->assertDontSee('Poista asiakas')
->call('confirmRemoval', 'customer', $internal->id)
->call('remove')
->assertSee('Sisäistä asiakasta ei voi poistaa.');
expect($internal->fresh())->not->toBeNull();
});
it('does not remove another customer than the one on the page', function () {
$other = CustomerModel::factory()->create();
Livewire::test('customers::show', ['customer' => $this->customer->id])
->call('confirmRemoval', 'customer', $other->id)
->assertSet('removalId', null)
->call('remove');
expect($other->fresh())->not->toBeNull()
->and($this->customer->fresh())->not->toBeNull();
});
it('shows the remove customer button only when nothing is linked to the customer', function (?string $link) {
match ($link) {
null => null,
'contact' => CustomerContactModel::factory()->create(['customer_id' => $this->customer->id]),
'billing unit' => BillingUnitModel::factory()->create(['customer_id' => $this->customer->id]),
'customer job' => CustomerJobModel::factory()->create(['customer_id' => $this->customer->id]),
'quick sale' => QuickSaleModel::factory()->create(['customer_id' => $this->customer->id]),
};
$page = Livewire::test('customers::show', ['customer' => $this->customer->id]);
$link === null ? $page->assertSee('Poista asiakas') : $page->assertDontSee('Poista asiakas');
})->with([
'nothing' => [null],
'contact' => ['contact'],
'billing unit' => ['billing unit'],
'customer job' => ['customer job'],
'quick sale' => ['quick sale'],
]);