Files
Toiminnanohjaus/database/factories/ExpenseFactory.php
T
petriandClaude Opus 5.5 54d98d9d2f Add expenses on customer jobs
- 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]>
2026-11-24 08:11:27 +02:00

69 lines
2.1 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\User;
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
use App\Modules\Expenses\Domain\Enums\ExpensePayer;
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<ExpenseModel>
*/
class ExpenseFactory extends Factory
{
protected $model = ExpenseModel::class;
/**
* @return array<string, mixed>
*/
public function definition(): array
{
$cost = fake()->numberBetween(500, 50000);
return [
'customer_job_id' => CustomerJobModel::factory(),
'user_id' => User::factory(),
'recorded_by' => fn (array $attributes): int => $attributes['user_id'],
'category' => ExpenseCategory::Materials,
'paid_by' => ExpensePayer::Company,
'spent_on' => fake()->dateTimeBetween('-1 month')->format('Y-m-d'),
'description' => fake()->words(3, true),
'cost_cents' => $cost,
'kilometers' => null,
'destination' => null,
'purpose' => null,
'is_billable' => true,
'markup_basis_points' => null,
'billed_amount_cents' => $cost,
];
}
public function mileage(float $kilometers = 40, int $rateCents = 59): static
{
$cost = (int) round($kilometers * $rateCents);
return $this->state(fn (): array => [
'category' => ExpenseCategory::Mileage,
'description' => null,
'kilometers' => $kilometers,
'destination' => 'Tampere',
'purpose' => 'Asiakastapaaminen',
'cost_cents' => $cost,
'billed_amount_cents' => $cost,
]);
}
public function paidByEmployee(): static
{
return $this->state(fn (): array => ['paid_by' => ExpensePayer::Employee]);
}
public function notBillable(): static
{
return $this->state(fn (): array => ['is_billable' => false, 'markup_basis_points' => null, 'billed_amount_cents' => null]);
}
}