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]>
101 lines
3.6 KiB
PHP
101 lines
3.6 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)
|
|
// An upper bound before the next month: SQLite keeps date casts as "Y-m-d 00:00:00",
|
|
// which a between ending at the month's last "Y-m-d" would leave out.
|
|
->where('spent_on', '>=', $start->toDateString())
|
|
->where('spent_on', '<', $start->copy()->addMonth()->toDateString()));
|
|
}
|
|
|
|
public function find(int $id): ?ExpenseSummary
|
|
{
|
|
return $this->summaries(ExpenseModel::query()->whereKey($id))[0] ?? null;
|
|
}
|
|
|
|
public function unbilledBillable(array $jobIds): array
|
|
{
|
|
if ($jobIds === []) {
|
|
return [];
|
|
}
|
|
|
|
return array_reverse($this->summaries(ExpenseModel::query()
|
|
->whereIn('customer_job_id', $jobIds)
|
|
->where('is_billable', true)
|
|
->whereNotNull('billed_amount_cents')
|
|
->whereNull('billing_export_id')));
|
|
}
|
|
|
|
/**
|
|
* @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();
|
|
}
|
|
}
|