Add the expense reimbursements report
The expenses employees paid themselves in a period, by person with kilometres and amounts, printable and as CSV for payroll. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
+58
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Expenses\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseReportQueries;
|
||||
use App\Modules\Expenses\Application\DTOs\ReimbursableExpense;
|
||||
use App\Modules\Expenses\Domain\Enums\ExpensePayer;
|
||||
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
|
||||
use DateTimeImmutable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class EloquentExpenseReportQueries implements ExpenseReportQueries
|
||||
{
|
||||
public function employeePaidBetween(string $from, string $to): array
|
||||
{
|
||||
return $this->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<ExpenseModel>
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,11 @@ 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\Application\Contracts\ExpenseReportQueries;
|
||||
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\EloquentExpenseReportQueries;
|
||||
use App\Modules\Expenses\Infrastructure\Persistence\Repositories\EloquentExpenseRepository;
|
||||
use App\Modules\Expenses\Infrastructure\PriceListForExpenses;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
@@ -29,6 +31,7 @@ class ExpensesServiceProvider extends ServiceProvider
|
||||
public array $bindings = [
|
||||
ExpenseRepository::class => EloquentExpenseRepository::class,
|
||||
ExpenseQueries::class => EloquentExpenseQueries::class,
|
||||
ExpenseReportQueries::class => EloquentExpenseReportQueries::class,
|
||||
ExpenseJobs::class => JobsForExpenses::class,
|
||||
ExpensePricing::class => PriceListForExpenses::class,
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user