generated from liutk/owl-admin-base
136 lines
4.4 KiB
PHP
136 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Admin\Filters\OvertimeApplyFilter;
|
|
use App\Enums\CheckStatus;
|
|
use App\Models\Employee;
|
|
use App\Models\OvertimeApply;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
|
|
class OvertimeApplyService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['store', 'employee', 'workflow'];
|
|
|
|
protected string $modelName = OvertimeApply::class;
|
|
|
|
protected string $modelFilterName = OvertimeApplyFilter::class;
|
|
|
|
public function list()
|
|
{
|
|
$query = $this->listQuery();
|
|
if (request()->input('_all')) {
|
|
$list = (clone $query)->get();
|
|
$items = $list->all();
|
|
$total = $list->count();
|
|
} else {
|
|
$list = (clone $query)->paginate(request()->input('perPage', 20));
|
|
$items = $list->items();
|
|
$total = $list->total();
|
|
}
|
|
$user = Admin::user();
|
|
foreach ($items as &$item) {
|
|
$item->row_actions = $this->rowActions($user, $item);
|
|
}
|
|
|
|
return compact('items', 'total');
|
|
}
|
|
|
|
public function rowActions($user, $model)
|
|
{
|
|
// view, edit, delete
|
|
// apply, cancel
|
|
$actions = [];
|
|
if ($user->can('admin.hr.overtime.view')) {
|
|
array_push($actions, 'view');
|
|
}
|
|
if ($user->can('admin.hr.overtime.delete') && ! in_array($model->workflow->check_status, [CheckStatus::Processing, CheckStatus::Success])) {
|
|
array_push($actions, 'delete');
|
|
}
|
|
if (in_array($model->workflow->check_status, [CheckStatus::None, CheckStatus::Cancel, CheckStatus::Fail])) {
|
|
array_push($actions, 'apply');
|
|
}
|
|
if (in_array($model->workflow->check_status, [CheckStatus::Processing])) {
|
|
array_push($actions, 'cancel');
|
|
}
|
|
|
|
return $actions;
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
// 获取员工所在的门店
|
|
if (! isset($data['store_id']) && isset($data['employee_id'])) {
|
|
$data['store_id'] = Employee::where('id', $data['employee_id'])->value('store_id');
|
|
}
|
|
|
|
if (isset($data['start_at']) && isset($data['duration'])) {
|
|
$data['end_at'] = Carbon::createFromFormat('Y-m-d H:i', $data['start_at'])
|
|
->addMinutes($data['duration'] * 60)
|
|
->rawFormat('Y-m-d H:i');
|
|
} elseif (isset($data['datetime_range'])) {
|
|
$timestamps = explode(',', $data['datetime_range']);
|
|
$start = Carbon::createFromTimestamp($timestamps[0]);
|
|
$end = Carbon::createFromTimestamp($timestamps[1]);
|
|
$data['start_at'] = $start->format('Y-m-d H:i');
|
|
$data['end_at'] = $end->format('Y-m-d H:i');
|
|
}
|
|
|
|
if (isset($data['start_at']) && isset($data['end_at'])) {
|
|
$start = Carbon::createFromFormat('Y-m-d H:i', $data['start_at']);
|
|
$end = Carbon::createFromFormat('Y-m-d H:i', $data['end_at']);
|
|
|
|
$data['date'] = $start->toDateString();
|
|
$data['hours'] = $start->diffInHours($end);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'employee_id' => ['required'],
|
|
'date' => ['required', 'date_format:Y-m-d'],
|
|
'start_at' => ['required', 'date_format:Y-m-d H:i'],
|
|
'end_at' => ['required', 'date_format:Y-m-d H:i'],
|
|
];
|
|
$updateRules = [];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules, []);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$model = $this->query()->whereKey($primaryKey)->firstOrFail();
|
|
if (! $model->canUpdate()) {
|
|
return $this->setError('审核中, 无法修改');
|
|
}
|
|
$data = $this->resloveData($data, $model);
|
|
$validate = $this->validate($data, $model);
|
|
if ($validate !== true) {
|
|
$this->setError($validate);
|
|
|
|
return false;
|
|
}
|
|
|
|
return $model->update($data);
|
|
}
|
|
|
|
public function delete(string $ids): mixed
|
|
{
|
|
$list = $this->query()->with(['workflow'])->whereIn('id', explode(',', $ids))->get();
|
|
foreach ($list as $item) {
|
|
$item->delete();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|