69 lines
2.3 KiB
PHP
69 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\ModelFilters\PatientFilter;
|
|
use App\Models\Patient;
|
|
use App\Models\PatientRecord;
|
|
|
|
class TotalPatientService extends BaseService
|
|
{
|
|
protected string $modelName = Patient::class;
|
|
|
|
protected array $withRelationships = ['type'];
|
|
|
|
protected string $modelFilterName = PatientFilter::class;
|
|
|
|
public function getModelFilter()
|
|
{
|
|
return $this->modelFilterName;
|
|
}
|
|
|
|
public function listQuery()
|
|
{
|
|
$filter = $this->getModelFilter();
|
|
|
|
$query = $this->query();
|
|
if ($this->withRelationships) {
|
|
$query->with($this->withRelationships);
|
|
}
|
|
|
|
if ($filter) {
|
|
$query->filter(request()->only(['id', 'type_id']), $filter);
|
|
}
|
|
$subQuery = fn($q) => $q->filter(request()->only(['treat_range', 'type_id']));
|
|
$query->select(['id', 'name', 'type_id'])->with(['records' => $subQuery]);
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function list()
|
|
{
|
|
$query = $this->listQuery();
|
|
|
|
$list = (clone $query)->paginate(request()->input('perPage', 20));
|
|
|
|
$items = collect($list->items())->map(function ($item) {
|
|
$item['sell_price'] = round($item->records->sum('sell_price'), 2, PHP_ROUND_HALF_DOWN);
|
|
$item['origin_price'] = round($item->records->sum('origin_price'), 2, PHP_ROUND_HALF_DOWN);
|
|
$item['records_count'] = $item->records->count();
|
|
return $item;
|
|
});
|
|
$filter = [
|
|
'patient_id' => request('id'),
|
|
'type_id' => request('type_id'),
|
|
'treat_range' => request('treat_range'),
|
|
];
|
|
$records_count = PatientRecord::filter($filter)->count();
|
|
$origin_price = PatientRecord::filter($filter)->sum('origin_price');
|
|
$sell_price = PatientRecord::filter($filter)->sum('sell_price');
|
|
// $allList = (clone $query)->get();
|
|
// $records_count = round($allList->sum(fn($item) => $item->records->count()), 2, PHP_ROUND_HALF_DOWN);
|
|
// $origin_price = round($allList->sum(fn($item) => $item->records->sum('origin_price')), 2, PHP_ROUND_HALF_DOWN);
|
|
// $sell_price = round($allList->sum(fn($item) => $item->records->sum('sell_price')), 2, PHP_ROUND_HALF_DOWN);
|
|
$total = $list->total();
|
|
|
|
return compact('items', 'total', 'sell_price', 'origin_price', 'records_count');
|
|
}
|
|
}
|