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]>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Expenses\Infrastructure;
|
||||
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
|
||||
use App\Modules\Expenses\Application\DTOs\ExpenseJob;
|
||||
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\CustomerJobSummary;
|
||||
|
||||
/**
|
||||
* Customer jobs for expenses, read through the Jobs module's queries.
|
||||
*/
|
||||
class JobsForExpenses implements ExpenseJobs
|
||||
{
|
||||
public function __construct(private CustomerJobQueries $jobs) {}
|
||||
|
||||
public function find(int $jobId): ?ExpenseJob
|
||||
{
|
||||
return $this->findMany([$jobId])[$jobId] ?? null;
|
||||
}
|
||||
|
||||
public function findMany(array $jobIds): array
|
||||
{
|
||||
return array_map(
|
||||
fn (CustomerJobSummary $job): ExpenseJob => new ExpenseJob($job->id, $job->number, $job->name, $job->status->locksTime()),
|
||||
$this->jobs->summariesByIds($jobIds),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Expenses\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
|
||||
use App\Modules\Expenses\Domain\Enums\ExpensePayer;
|
||||
use Database\Factories\ExpenseFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Expenses Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $customer_job_id
|
||||
* @property int $user_id
|
||||
* @property int $recorded_by
|
||||
* @property ExpenseCategory $category
|
||||
* @property ExpensePayer $paid_by
|
||||
* @property Carbon $spent_on
|
||||
* @property string|null $description
|
||||
* @property int $cost_cents
|
||||
* @property string|null $kilometers
|
||||
* @property string|null $destination
|
||||
* @property string|null $purpose
|
||||
* @property bool $is_billable
|
||||
* @property int|null $markup_basis_points
|
||||
* @property int|null $billed_amount_cents
|
||||
*/
|
||||
#[Table('expenses')]
|
||||
#[Fillable([
|
||||
'customer_job_id', 'user_id', 'recorded_by', 'category', 'paid_by', 'spent_on', 'description', 'cost_cents',
|
||||
'kilometers', 'destination', 'purpose', 'is_billable', 'markup_basis_points', 'billed_amount_cents',
|
||||
])]
|
||||
#[UseFactory(ExpenseFactory::class)]
|
||||
class ExpenseModel extends Model
|
||||
{
|
||||
/** @use HasFactory<ExpenseFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'customer_job_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'recorded_by' => 'integer',
|
||||
'category' => ExpenseCategory::class,
|
||||
'paid_by' => ExpensePayer::class,
|
||||
'spent_on' => 'date',
|
||||
'cost_cents' => 'integer',
|
||||
'kilometers' => 'decimal:1',
|
||||
'is_billable' => 'boolean',
|
||||
'markup_basis_points' => 'integer',
|
||||
'billed_amount_cents' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Expenses\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseQueries;
|
||||
use App\Modules\Expenses\Application\DTOs\ExpenseJob;
|
||||
use App\Modules\Expenses\Application\DTOs\ExpenseSummary;
|
||||
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class EloquentExpenseQueries implements ExpenseQueries
|
||||
{
|
||||
public function __construct(
|
||||
private ExpenseJobs $jobs,
|
||||
private UserAccountQueries $users,
|
||||
) {}
|
||||
|
||||
public function forJob(int $jobId): array
|
||||
{
|
||||
return $this->summaries(ExpenseModel::query()->where('customer_job_id', $jobId));
|
||||
}
|
||||
|
||||
public function forUserInMonth(int $userId, string $month): array
|
||||
{
|
||||
if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month) !== 1) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$start = Carbon::createFromFormat('!Y-m', $month);
|
||||
|
||||
return $this->summaries(ExpenseModel::query()
|
||||
->where('user_id', $userId)
|
||||
->whereBetween('spent_on', [$start->toDateString(), $start->copy()->endOfMonth()->toDateString()]));
|
||||
}
|
||||
|
||||
public function find(int $id): ?ExpenseSummary
|
||||
{
|
||||
return $this->summaries(ExpenseModel::query()->whereKey($id))[0] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<ExpenseModel> $query
|
||||
* @return list<ExpenseSummary>
|
||||
*/
|
||||
private function summaries(Builder $query): array
|
||||
{
|
||||
/** @var Collection<int, ExpenseModel> $models */
|
||||
$models = $query->orderByDesc('spent_on')->orderByDesc('id')->get();
|
||||
|
||||
if ($models->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$jobs = $this->jobs->findMany($models->pluck('customer_job_id')->unique()->values()->all());
|
||||
$names = $this->users->namesByIds($models->pluck('user_id')->merge($models->pluck('recorded_by'))->unique()->values()->all());
|
||||
|
||||
return $models
|
||||
->map(fn (ExpenseModel $model): ExpenseSummary => new ExpenseSummary(
|
||||
$model->id,
|
||||
$jobs[$model->customer_job_id] ?? new ExpenseJob($model->customer_job_id, '', '', true),
|
||||
$model->user_id,
|
||||
$names[$model->user_id] ?? '',
|
||||
$model->recorded_by,
|
||||
$names[$model->recorded_by] ?? '',
|
||||
$model->category,
|
||||
$model->paid_by,
|
||||
$model->spent_on->toDateString(),
|
||||
$model->description,
|
||||
$model->cost_cents,
|
||||
$model->kilometers === null ? null : (int) round((float) $model->kilometers * 10),
|
||||
$model->destination,
|
||||
$model->purpose,
|
||||
$model->is_billable,
|
||||
$model->markup_basis_points,
|
||||
$model->billed_amount_cents,
|
||||
))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Expenses\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Expenses\Domain\Contracts\ExpenseRepository;
|
||||
use App\Modules\Expenses\Domain\Entities\Expense;
|
||||
use App\Modules\Expenses\Domain\ValueObjects\ExpenseBilling;
|
||||
use App\Modules\Expenses\Domain\ValueObjects\ExpenseDetails;
|
||||
use App\Modules\Expenses\Domain\ValueObjects\Kilometers;
|
||||
use App\Modules\Expenses\Domain\ValueObjects\MarkupPercentage;
|
||||
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class EloquentExpenseRepository implements ExpenseRepository
|
||||
{
|
||||
public function findById(int $id): ?Expense
|
||||
{
|
||||
$model = ExpenseModel::query()->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function save(Expense $expense): Expense
|
||||
{
|
||||
$model = $expense->id() === null ? new ExpenseModel : ExpenseModel::query()->findOrFail($expense->id());
|
||||
$details = $expense->details();
|
||||
|
||||
$model->fill([
|
||||
'customer_job_id' => $expense->jobId(),
|
||||
'user_id' => $expense->userId(),
|
||||
'recorded_by' => $expense->recordedBy(),
|
||||
'category' => $details->category,
|
||||
'paid_by' => $details->paidBy,
|
||||
'spent_on' => $details->spentOn->format('Y-m-d'),
|
||||
'description' => $details->description,
|
||||
'cost_cents' => $details->cost->cents,
|
||||
'kilometers' => $details->kilometers === null ? null : $details->kilometers->tenths / 10,
|
||||
'destination' => $details->destination,
|
||||
'purpose' => $details->purpose,
|
||||
'is_billable' => $details->isBillable(),
|
||||
'markup_basis_points' => $details->billing?->markup?->basisPoints,
|
||||
'billed_amount_cents' => $details->billing?->amount->cents,
|
||||
])->save();
|
||||
|
||||
return $this->toEntity($model->refresh());
|
||||
}
|
||||
|
||||
public function delete(Expense $expense): void
|
||||
{
|
||||
ExpenseModel::query()->whereKey($expense->id())->delete();
|
||||
}
|
||||
|
||||
private function toEntity(ExpenseModel $model): Expense
|
||||
{
|
||||
$billing = $model->is_billable && $model->billed_amount_cents !== null
|
||||
? ExpenseBilling::for(
|
||||
Money::fromCents($model->cost_cents),
|
||||
$model->markup_basis_points === null ? null : new MarkupPercentage($model->markup_basis_points),
|
||||
Money::fromCents($model->billed_amount_cents),
|
||||
)
|
||||
: null;
|
||||
|
||||
return Expense::reconstitute(
|
||||
$model->id,
|
||||
$model->customer_job_id,
|
||||
$model->user_id,
|
||||
$model->recorded_by,
|
||||
ExpenseDetails::reconstitute(
|
||||
$model->category,
|
||||
$model->paid_by,
|
||||
DateTimeImmutable::createFromInterface($model->spent_on),
|
||||
$model->description,
|
||||
Money::fromCents($model->cost_cents),
|
||||
$model->kilometers === null ? null : new Kilometers((int) round((float) $model->kilometers * 10)),
|
||||
$model->destination,
|
||||
$model->purpose,
|
||||
$billing,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Expenses\Infrastructure;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
|
||||
use App\Modules\Expenses\Application\Contracts\ExpensePricing;
|
||||
|
||||
/**
|
||||
* Expense prices from the Catalog module's price list.
|
||||
*/
|
||||
class PriceListForExpenses implements ExpensePricing
|
||||
{
|
||||
public function __construct(private PriceListQueries $priceList) {}
|
||||
|
||||
public function mileageRateCents(): ?int
|
||||
{
|
||||
return $this->priceList->mileageRateCents();
|
||||
}
|
||||
|
||||
public function defaultMarkupBasisPoints(): int
|
||||
{
|
||||
return $this->priceList->defaultExpenseMarkupBasisPoints();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Expenses\Infrastructure\Providers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
|
||||
use App\Modules\Expenses\Application\Contracts\ExpensePricing;
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseQueries;
|
||||
use App\Modules\Expenses\Domain\Contracts\ExpenseRepository;
|
||||
use App\Modules\Expenses\Infrastructure\JobsForExpenses;
|
||||
use App\Modules\Expenses\Infrastructure\Persistence\Repositories\EloquentExpenseQueries;
|
||||
use App\Modules\Expenses\Infrastructure\Persistence\Repositories\EloquentExpenseRepository;
|
||||
use App\Modules\Expenses\Infrastructure\PriceListForExpenses;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Livewire\Livewire;
|
||||
|
||||
/**
|
||||
* Wires the Expenses module (expenses on customer jobs) into Laravel:
|
||||
* bindings, routes, the `expenses::` Livewire namespace and the
|
||||
* `expenses.*` abilities.
|
||||
*/
|
||||
class ExpensesServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* @var array<class-string, class-string>
|
||||
*/
|
||||
public array $bindings = [
|
||||
ExpenseRepository::class => EloquentExpenseRepository::class,
|
||||
ExpenseQueries::class => EloquentExpenseQueries::class,
|
||||
ExpenseJobs::class => JobsForExpenses::class,
|
||||
ExpensePricing::class => PriceListForExpenses::class,
|
||||
];
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$presentationPath = dirname(__DIR__, 2).'/Presentation';
|
||||
|
||||
Livewire::addNamespace('expenses', viewPath: $presentationPath.'/Livewire');
|
||||
|
||||
if (! $this->app->routesAreCached()) {
|
||||
$this->loadRoutesFrom($presentationPath.'/Routes/web.php');
|
||||
}
|
||||
|
||||
// Everyone may list their own expenses; subcontractors' are recorded by management (spec 10).
|
||||
Gate::define('expenses.use', fn (User $user): bool => true);
|
||||
|
||||
Gate::define('expenses.record', fn (User $user): bool => $user->role !== Role::Subcontractor);
|
||||
|
||||
// Admins and management record expenses for others, e.g. subcontractors.
|
||||
Gate::define('expenses.record-for-others', fn (User $user): bool => $user->role->seesAllMoney());
|
||||
|
||||
Gate::define('expenses.change', fn (User $user, int $ownerId, int $recordedBy): bool => $user->role->seesAllMoney()
|
||||
|| ($user->role !== Role::Subcontractor && in_array($user->id, [$ownerId, $recordedBy], true)));
|
||||
|
||||
// Everyone sees the amounts of their own expenses; others' need the expenses permission.
|
||||
Gate::define('expenses.see-amount', fn (User $user, int $ownerId, int $recordedBy): bool => in_array($user->id, [$ownerId, $recordedBy], true)
|
||||
|| $user->can('money.expenses'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user