SQLite stores date casts as "Y-m-d 00:00:00", so a between ending at "Y-m-d" left out the month's last day and the week's Sunday (the own expenses test failed on the 30th). Ranges now end before the next day. Co-Authored-By: Claude Opus 5.5 <[email protected]>
193 lines
8.7 KiB
PHP
193 lines
8.7 KiB
PHP
<?php
|
||
|
||
use App\Models\User;
|
||
use App\Modules\Catalog\Application\UseCases\SetDefaultExpenseMarkup;
|
||
use App\Modules\Catalog\Application\UseCases\SetMileageRate;
|
||
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
|
||
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
|
||
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||
use App\Modules\Shared\Application\Contracts\Settings;
|
||
use App\Modules\Users\Domain\Enums\MoneyPermission;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Livewire\Livewire;
|
||
|
||
uses(RefreshDatabase::class);
|
||
|
||
beforeEach(function () {
|
||
$this->employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']);
|
||
$this->job = CustomerJobModel::factory()->create(['name' => 'Messuvideo', 'status' => JobStatus::Order]);
|
||
app(Settings::class)->put(SetMileageRate::SETTING_KEY, '59');
|
||
$this->actingAs($this->employee);
|
||
});
|
||
|
||
it('records a purchase with the default markup', function () {
|
||
app(Settings::class)->put(SetDefaultExpenseMarkup::SETTING_KEY, '1500');
|
||
|
||
Livewire::test('expenses::form', ['job' => $this->job->id])
|
||
->assertSet('form.markup', '15')
|
||
->set('form.description', 'Tulostuspaperi')
|
||
->set('form.cost', '100')
|
||
->set('form.paid_by', 'employee')
|
||
->assertSee("Laskutetaan 115,00\u{00A0}€ (hankintahinta + kate).")
|
||
->call('save')
|
||
->assertHasNoErrors()
|
||
->assertRedirect(route('expenses.job', $this->job->id));
|
||
|
||
$expense = ExpenseModel::query()->sole();
|
||
|
||
expect($expense->only(['user_id', 'recorded_by', 'cost_cents', 'billed_amount_cents', 'markup_basis_points']))->toBe([
|
||
'user_id' => $this->employee->id,
|
||
'recorded_by' => $this->employee->id,
|
||
'cost_cents' => 10000,
|
||
'billed_amount_cents' => 11500,
|
||
'markup_basis_points' => 1500,
|
||
]);
|
||
});
|
||
|
||
it('records kilometres priced with the mileage rate and no default markup', function () {
|
||
app(Settings::class)->put(SetDefaultExpenseMarkup::SETTING_KEY, '1500');
|
||
|
||
Livewire::test('expenses::form', ['job' => $this->job->id])
|
||
->set('form.category', 'mileage')
|
||
->assertSet('form.markup', '')
|
||
->set('form.kilometers', '40')
|
||
->assertSee("Hankintahinta: 40 km × 0,59\u{00A0}€ = 23,60\u{00A0}€")
|
||
->set('form.destination', 'Tampere')
|
||
->set('form.purpose', 'Kuvaukset')
|
||
->call('save')
|
||
->assertHasNoErrors();
|
||
|
||
expect(ExpenseModel::query()->sole()->only(['category', 'cost_cents', 'billed_amount_cents']))->toBe([
|
||
'category' => ExpenseCategory::Mileage,
|
||
'cost_cents' => 2360,
|
||
'billed_amount_cents' => 2360,
|
||
]);
|
||
});
|
||
|
||
it('shows expense rule violations as Finnish field errors', function (array $fields, string $field, string $message) {
|
||
$component = Livewire::test('expenses::form', ['job' => $this->job->id]);
|
||
|
||
foreach ($fields as $name => $value) {
|
||
$component->set("form.{$name}", $value);
|
||
}
|
||
|
||
$component->call('save')->assertHasErrors(["form.{$field}" => [__($message)]]);
|
||
})->with([
|
||
'description' => [['cost' => '10'], 'description', 'expenses.errors.description_required'],
|
||
'cost' => [['description' => 'Paperi', 'cost' => '10,123'], 'cost', 'expenses.errors.invalid_amount'],
|
||
'billed amount' => [['description' => 'Paperi', 'cost' => '10', 'billed_amount' => 'x'], 'billed_amount', 'expenses.errors.invalid_amount'],
|
||
'mileage' => [['category' => 'mileage', 'kilometers' => '40', 'destination' => 'Tampere'], 'purpose', 'expenses.errors.mileage_details_required'],
|
||
]);
|
||
|
||
it('hides the amounts of others\' expenses without the expenses permission', function () {
|
||
$colleague = User::factory()->employee()->create(['name' => 'Kaisa Kollega']);
|
||
ExpenseModel::factory()->create(['customer_job_id' => $this->job->id, 'user_id' => $this->employee->id, 'description' => 'Oma hankinta', 'cost_cents' => 1234, 'billed_amount_cents' => 1234]);
|
||
ExpenseModel::factory()->create(['customer_job_id' => $this->job->id, 'user_id' => $colleague->id, 'description' => 'Kollegan hankinta', 'cost_cents' => 9876, 'billed_amount_cents' => 9876]);
|
||
|
||
$this->get(route('expenses.job', $this->job->id))
|
||
->assertOk()
|
||
->assertSee('Oma hankinta')
|
||
->assertSee("12,34\u{00A0}€", false)
|
||
->assertSee('Kollegan hankinta')
|
||
->assertDontSee("98,76\u{00A0}€", false)
|
||
->assertDontSee(__('expenses.totals.cost'));
|
||
|
||
$this->actingAs(User::factory()->employee()->withMoneyPermissions(MoneyPermission::Expenses)->create())
|
||
->get(route('expenses.job', $this->job->id))
|
||
->assertSee("98,76\u{00A0}€", false)
|
||
->assertSee("111,10\u{00A0}€", false);
|
||
});
|
||
|
||
it('locks the expenses of a done job', function () {
|
||
$expense = ExpenseModel::factory()->create(['customer_job_id' => $this->job->id, 'user_id' => $this->employee->id]);
|
||
$this->job->update(['status' => JobStatus::Done]);
|
||
|
||
$this->get(route('expenses.job', $this->job->id))
|
||
->assertSee(__('expenses.locked'))
|
||
->assertDontSee(route('expenses.create', $this->job->id), false)
|
||
->assertDontSee(route('expenses.edit', $expense->id), false);
|
||
|
||
Livewire::test('expenses::job', ['job' => $this->job->id])
|
||
->call('confirmRemoval', $expense->id)
|
||
->assertSet('removalExpenseId', null);
|
||
|
||
expect(ExpenseModel::query()->count())->toBe(1);
|
||
});
|
||
|
||
it('removes an own expense after confirmation but not a colleague\'s', function () {
|
||
$own = ExpenseModel::factory()->create(['customer_job_id' => $this->job->id, 'user_id' => $this->employee->id, 'description' => 'Paperi']);
|
||
$colleagues = ExpenseModel::factory()->create(['customer_job_id' => $this->job->id]);
|
||
|
||
Livewire::test('expenses::job', ['job' => $this->job->id])
|
||
->call('confirmRemoval', $colleagues->id)
|
||
->assertSet('removalExpenseId', null)
|
||
->call('confirmRemoval', $own->id)
|
||
->call('removeExpense')
|
||
->assertSet('statusMessage', 'Kulu "Paperi" poistettu.');
|
||
|
||
expect(ExpenseModel::query()->pluck('id')->all())->toBe([$colleagues->id]);
|
||
|
||
$this->get(route('expenses.edit', $colleagues->id))->assertForbidden();
|
||
});
|
||
|
||
it('lets management record a subcontractor\'s expense, which the subcontractor sees but cannot change', function () {
|
||
$subcontractor = User::factory()->subcontractor()->create(['name' => 'Alihankkija Oy']);
|
||
$this->actingAs(User::factory()->management()->create());
|
||
|
||
Livewire::test('expenses::form', ['job' => $this->job->id])
|
||
->set('form.user_id', (string) $subcontractor->id)
|
||
->set('form.description', 'Vuokrakalusto')
|
||
->set('form.cost', '250')
|
||
->call('save')
|
||
->assertHasNoErrors();
|
||
|
||
$expense = ExpenseModel::query()->sole();
|
||
|
||
$this->actingAs($subcontractor)
|
||
->get(route('expenses.mine', ['kuukausi' => now()->format('Y-m')]))
|
||
->assertOk()
|
||
->assertSee('Vuokrakalusto')
|
||
->assertSee("250,00\u{00A0}€", false)
|
||
->assertDontSee(route('expenses.edit', $expense->id), false);
|
||
|
||
$this->get(route('expenses.job', $this->job->id))->assertForbidden();
|
||
$this->get(route('expenses.create', $this->job->id))->assertForbidden();
|
||
});
|
||
|
||
it('does not let an employee record an expense for someone else', function () {
|
||
$colleague = User::factory()->employee()->create();
|
||
|
||
Livewire::test('expenses::form', ['job' => $this->job->id])
|
||
->set('form.user_id', (string) $colleague->id)
|
||
->set('form.description', 'Paperi')
|
||
->set('form.cost', '10')
|
||
->call('save')
|
||
->assertHasNoErrors();
|
||
|
||
expect(ExpenseModel::query()->sole()->user_id)->toBe($this->employee->id);
|
||
});
|
||
|
||
it('links the job page to its expenses and shows the own expenses by month', function () {
|
||
ExpenseModel::factory()->create(['user_id' => $this->employee->id, 'spent_on' => '2026-10-15', 'description' => 'Lokakuun hankinta']);
|
||
|
||
$this->get(route('jobs.show', $this->job->id))->assertSee(route('expenses.job', $this->job->id), false);
|
||
|
||
$this->get(route('expenses.mine', ['kuukausi' => '2026-10']))
|
||
->assertOk()
|
||
->assertSee('Lokakuu 2026')
|
||
->assertSee('Lokakuun hankinta');
|
||
});
|
||
|
||
it('lists an expense of the month\'s last day on the own expenses page', function () {
|
||
$user = User::factory()->create();
|
||
ExpenseModel::factory()->create(['customer_job_id' => $this->job->id, 'user_id' => $user->id, 'description' => 'Helmikuun viimeinen hankinta', 'spent_on' => '2026-02-28']);
|
||
ExpenseModel::factory()->create(['customer_job_id' => $this->job->id, 'user_id' => $user->id, 'description' => 'Maaliskuun hankinta', 'spent_on' => '2026-03-01']);
|
||
|
||
$this->actingAs($user)
|
||
->get(route('expenses.mine', ['kuukausi' => '2026-02']))
|
||
->assertOk()
|
||
->assertSee('Helmikuun viimeinen hankinta')
|
||
->assertDontSee('Maaliskuun hankinta');
|
||
});
|