- 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]>
85 lines
3.0 KiB
PHP
85 lines
3.0 KiB
PHP
<?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();
|
|
}
|
|
}
|