Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
1.6 kB
3
Indexable
Never
<?php

namespace App\Repositories;

use DB;
use App\Services\Filter;
use Illuminate\Support\Arr;
use App\Models\DWH\WriteOffSP;
use Illuminate\Support\Collection;
use App\Interfaces\Repositories\IWriteOffRepository;

class WriteOffRepository implements IWriteOffRepository
{
    /**
     * {@inheritdoc}
     */
    public function baseList(Filter $filter): Collection
    {
        return WriteOffSP::when(
            !empty($filter->shops),
            fn ($query) => $query->whereIn('department_id', Arr::pluck($filter->shops, 'id'))
        )
            ->where(function ($q) use ($filter) {
                $code = !is_null($filter->shopSection) ? $filter->shopSection->storeplace_code : 'TOTAL';

                return $q->where('storeplace_code', $code);
            })
            ->where(function ($q) use ($filter) {
                if (!$filter->dateFrom->eq($filter->dateTo)) {
                    $q->whereBetween('date_id', [$filter->dateFrom, $filter->dateTo]);
                } else {
                    $q->whereYear('date_id', $filter->dateFrom->year)
                        ->whereMonth('date_id', $filter->dateFrom->month)
                        ->whereDay('date_id', $filter->dateFrom->day);
                }
            })
            ->select(
                [
                    DB::raw('trunc(date_id) as date_id'),
                    DB::raw('sum(cost_amt) as cost_amt'),
                ]
            )
            ->orderByRaw('trunc(date_id)')
            ->groupByRaw('trunc(date_id)')
            ->toBase()
            ->get();
    }
}