72 lines
1.7 KiB
PHP
72 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Models\PatientRecord;
|
|
use App\ModelFilters\PatientRecordFilter;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class TotalRecordService extends BaseService
|
|
{
|
|
protected string $modelName = PatientRecord::class;
|
|
|
|
protected array $withRelationships = ['patient'];
|
|
|
|
protected string $modelFilterName = PatientRecordFilter::class;
|
|
|
|
public function getModelFilter()
|
|
{
|
|
return $this->modelFilterName;
|
|
}
|
|
|
|
public function primaryKey()
|
|
{
|
|
return 'patient_id';
|
|
}
|
|
|
|
public function sortColumn()
|
|
{
|
|
return 'patient_id';
|
|
}
|
|
|
|
public function listQuery()
|
|
{
|
|
$filter = $this->getModelFilter();
|
|
|
|
$query = $this->query();
|
|
if ($this->withRelationships) {
|
|
$query->with($this->withRelationships);
|
|
}
|
|
|
|
if ($filter) {
|
|
$query->filter(request()->input(), $filter);
|
|
}
|
|
|
|
$query->select([
|
|
'patient_id',
|
|
'type_id',
|
|
DB::raw('count(1) as count'),
|
|
DB::raw('sum(`sell_price`) as `sell_price`'),
|
|
DB::raw('min(`treat_at`) as `min_treat_at`'),
|
|
DB::raw('max(`treat_at`) as `max_treat_at`'),
|
|
])->groupBy('patient_id', 'type_id');
|
|
|
|
$this->sortable($query);
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function list()
|
|
{
|
|
$query = $this->listQuery();
|
|
|
|
$list = (clone $query)->paginate(request()->input('perPage', 20));
|
|
$items = $list->items();
|
|
$total = $list->total();
|
|
$count = floatval((new PatientRecordService())->listQuery()->count());
|
|
$sell_price = floatval((new PatientRecordService())->listQuery()->sum('sell_price'));
|
|
|
|
return compact('items', 'total', 'sell_price', 'count');
|
|
}
|
|
}
|