- New Expenses module: purchases (materials, other) with a description and cost, and mileage priced with the price list's mileage rate. - Who paid and whether the expense is billed are separate: an employee's own payments are reimbursed elsewhere. A billed expense has a markup and a billed amount, cost plus markup unless given. - A job's expenses page lists, adds, edits and removes them; amounts of others' expenses need the expenses permission, own ones always show. Done jobs lock their expenses. - The Kulut menu shows the user's own expenses by month. Management records subcontractors' expenses, which they see but cannot change. - The price list gets a default markup for new purchases. - Number fields that update a calculation now use wire:model.live.blur: in Livewire 4 plain .blur no longer sends the value to the server, which also fixes the product form's VAT calculation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
144 lines
6.2 KiB
PHP
144 lines
6.2 KiB
PHP
<?php
|
|
|
|
use App\Modules\Expenses\Application\DTOs\ExpenseData;
|
|
use App\Modules\Expenses\Application\UseCases\ExpenseDetailsFactory;
|
|
use App\Modules\Expenses\Application\UseCases\RecordExpense;
|
|
use App\Modules\Expenses\Application\UseCases\RemoveExpense;
|
|
use App\Modules\Expenses\Application\UseCases\UpdateExpense;
|
|
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
|
|
use App\Modules\Expenses\Domain\Enums\ExpensePayer;
|
|
use App\Modules\Expenses\Domain\Exceptions\ExpenseCostRequired;
|
|
use App\Modules\Expenses\Domain\Exceptions\ExpenseDescriptionRequired;
|
|
use App\Modules\Expenses\Domain\Exceptions\ExpenseJobNotFound;
|
|
use App\Modules\Expenses\Domain\Exceptions\ExpenseLocked;
|
|
use App\Modules\Expenses\Domain\Exceptions\InvalidKilometers;
|
|
use App\Modules\Expenses\Domain\Exceptions\InvalidMarkup;
|
|
use App\Modules\Expenses\Domain\Exceptions\MileageDetailsRequired;
|
|
use App\Modules\Expenses\Domain\Exceptions\MileageRateMissing;
|
|
use App\Modules\Expenses\Domain\ValueObjects\Kilometers;
|
|
use App\Modules\Expenses\Domain\ValueObjects\MarkupPercentage;
|
|
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
|
use App\Modules\Shared\Domain\ValueObjects\Money;
|
|
use Tests\Fakes\FakeExpenseJobs;
|
|
use Tests\Fakes\FakeExpensePricing;
|
|
use Tests\Fakes\FakeTransactionManager;
|
|
use Tests\Fakes\InMemoryExpenseRepository;
|
|
|
|
beforeEach(function () {
|
|
$this->expenses = new InMemoryExpenseRepository;
|
|
$this->jobs = new FakeExpenseJobs;
|
|
$this->pricing = new FakeExpensePricing;
|
|
$transactions = new FakeTransactionManager;
|
|
$factory = new ExpenseDetailsFactory($this->pricing);
|
|
|
|
$this->record = new RecordExpense($this->expenses, $this->jobs, $factory, $transactions);
|
|
$this->update = new UpdateExpense($this->expenses, $this->jobs, $factory, $transactions);
|
|
$this->remove = new RemoveExpense($this->expenses, $this->jobs, $transactions);
|
|
|
|
$this->jobs->add(1);
|
|
$this->jobs->add(2, locked: true);
|
|
});
|
|
|
|
function purchase(array $fields = []): ExpenseData
|
|
{
|
|
return new ExpenseData(...array_merge([
|
|
'userId' => 5,
|
|
'category' => ExpenseCategory::Materials,
|
|
'paidBy' => ExpensePayer::Company,
|
|
'spentOn' => '2026-11-20',
|
|
'description' => 'Tulostuspaperi',
|
|
'cost' => '100',
|
|
], $fields));
|
|
}
|
|
|
|
function mileage(array $fields = []): ExpenseData
|
|
{
|
|
return purchase(array_merge([
|
|
'category' => ExpenseCategory::Mileage,
|
|
'description' => '',
|
|
'cost' => '',
|
|
'kilometers' => '42,5',
|
|
'destination' => 'Tampere',
|
|
'purpose' => 'Kuvaukset',
|
|
], $fields));
|
|
}
|
|
|
|
it('records a purchase billed at cost plus markup', function () {
|
|
$id = $this->record->execute(1, purchase(['markup' => '15', 'paidBy' => ExpensePayer::Employee]), recordedBy: 9);
|
|
|
|
$expense = $this->expenses->findById($id);
|
|
|
|
expect($expense?->userId())->toBe(5)
|
|
->and($expense?->recordedBy())->toBe(9)
|
|
->and($expense?->details()->cost->cents)->toBe(10000)
|
|
->and($expense?->details()->billing?->amount->cents)->toBe(11500)
|
|
->and($expense?->details()->paidBy)->toBe(ExpensePayer::Employee);
|
|
});
|
|
|
|
it('bills a given amount instead of cost plus markup', function () {
|
|
$id = $this->record->execute(1, purchase(['markup' => '15', 'billedAmount' => '130']), 5);
|
|
|
|
expect($this->expenses->findById($id)?->details()->billing?->amount->cents)->toBe(13000);
|
|
});
|
|
|
|
it('keeps no billed amount for an expense that is not billed', function () {
|
|
$id = $this->record->execute(1, purchase(['isBillable' => false, 'markup' => '15', 'billedAmount' => '130']), 5);
|
|
|
|
expect($this->expenses->findById($id)?->details()->billing)->toBeNull();
|
|
});
|
|
|
|
it('prices kilometres with the mileage rate', function () {
|
|
$id = $this->record->execute(1, mileage(), 5);
|
|
|
|
$details = $this->expenses->findById($id)?->details();
|
|
|
|
expect($details?->cost->cents)->toBe(2508)
|
|
->and($details?->kilometers?->tenths)->toBe(425)
|
|
->and($details?->billing?->amount->cents)->toBe(2508);
|
|
});
|
|
|
|
it('enforces expense rules', function (Closure $record, string $exception) {
|
|
expect(fn () => $record->call($this))->toThrow($exception);
|
|
})->with([
|
|
'description' => [fn () => $this->record->execute(1, purchase(['description' => ' ']), 5), ExpenseDescriptionRequired::class],
|
|
'cost' => [fn () => $this->record->execute(1, purchase(['cost' => '']), 5), ExpenseCostRequired::class],
|
|
'cost format' => [fn () => $this->record->execute(1, purchase(['cost' => '10,999']), 5), InvalidMoneyAmount::class],
|
|
'mileage details' => [fn () => $this->record->execute(1, mileage(['purpose' => '']), 5), MileageDetailsRequired::class],
|
|
'kilometres format' => [fn () => $this->record->execute(1, mileage(['kilometers' => '4,25']), 5), InvalidKilometers::class],
|
|
'markup format' => [fn () => $this->record->execute(1, purchase(['markup' => 'paljon']), 5), InvalidMarkup::class],
|
|
'locked job' => [fn () => $this->record->execute(2, purchase(), 5), ExpenseLocked::class],
|
|
'unknown job' => [fn () => $this->record->execute(99, purchase(), 5), ExpenseJobNotFound::class],
|
|
]);
|
|
|
|
it('needs a mileage rate for kilometres', function () {
|
|
$this->pricing->mileageRateCents = null;
|
|
|
|
$this->record->execute(1, mileage(), 5);
|
|
})->throws(MileageRateMissing::class);
|
|
|
|
it('changes and removes expenses only while the job is open', function () {
|
|
$id = $this->record->execute(1, purchase(), 5);
|
|
|
|
$this->update->execute($id, purchase(['userId' => 6, 'cost' => '80']));
|
|
expect($this->expenses->findById($id)?->userId())->toBe(6)
|
|
->and($this->expenses->findById($id)?->details()->cost->cents)->toBe(8000);
|
|
|
|
$this->jobs->add(1, locked: true);
|
|
|
|
expect(fn () => $this->update->execute($id, purchase()))->toThrow(ExpenseLocked::class)
|
|
->and(fn () => $this->remove->execute($id))->toThrow(ExpenseLocked::class);
|
|
|
|
$this->jobs->add(1);
|
|
$this->remove->execute($id);
|
|
|
|
expect($this->expenses->items)->toBe([]);
|
|
});
|
|
|
|
it('parses kilometres, markups and percentages', function () {
|
|
expect(Kilometers::parse('42,5 km')?->tenths)->toBe(425)
|
|
->and(Kilometers::parse('')?->tenths)->toBeNull()
|
|
->and((new Kilometers(400))->format())->toBe('40')
|
|
->and(MarkupPercentage::parse('12,5 %')?->basisPoints)->toBe(1250)
|
|
->and(Money::fromCents(1999)->addPercent(1500)->cents)->toBe(2299);
|
|
});
|