between($from, $to) ->where('paid_by', ExpensePayer::Employee) ->orderBy('user_id') ->orderBy('spent_on') ->orderBy('id') ->get() ->map(fn (ExpenseModel $expense): ReimbursableExpense => new ReimbursableExpense( $expense->id, $expense->user_id, $expense->customer_job_id, $expense->category, $expense->spent_on->format('Y-m-d'), $expense->description, $expense->destination, $expense->purpose, $expense->kilometers === null ? null : (int) round((float) $expense->kilometers * 10), $expense->cost_cents, )) ->all(); } public function costsByJobBetween(string $from, string $to): array { return $this->between($from, $to) ->groupBy('customer_job_id') ->selectRaw('customer_job_id, SUM(cost_cents) as cost_cents') ->pluck('cost_cents', 'customer_job_id') ->mapWithKeys(fn ($cents, $jobId): array => [(int) $jobId => (int) $cents]) ->all(); } /** * Days may be stored with a time, so a period ends before the next day. * * @return Builder */ private function between(string $from, string $to): Builder { return ExpenseModel::query() ->where('spent_on', '>=', $from) ->where('spent_on', '<', (new DateTimeImmutable($to))->modify('+1 day')->format('Y-m-d')); } }