diff --git a/app/Admin/Services/HolidayApplyService.php b/app/Admin/Services/HolidayApplyService.php index 83fa8d0..90fdb4a 100644 --- a/app/Admin/Services/HolidayApplyService.php +++ b/app/Admin/Services/HolidayApplyService.php @@ -65,12 +65,14 @@ class HolidayApplyService extends BaseService return false; } - return $model->update($data); + $model->update($data); + $this->currentModel = $model; + return true; } public function delete(string $ids): mixed { - $list = HolidayApply::with(['workflow'])->whereIn('id', explode(',', $ids))->get(); + $list = $this->query()->with(['workflow'])->whereIn('id', explode(',', $ids))->get(); foreach ($list as $item) { if (!$item->canUpdate()) { return $this->setError('审核中, 无法删除'); diff --git a/app/Admin/Services/OfficalBusinessService.php b/app/Admin/Services/OfficalBusinessService.php index 610bf1b..53d2e5f 100644 --- a/app/Admin/Services/OfficalBusinessService.php +++ b/app/Admin/Services/OfficalBusinessService.php @@ -35,8 +35,7 @@ class OfficalBusinessService extends BaseService public function validate($data, $model = null) { - // 验证申请时间是否重叠 - // todo + // todo 验证申请时间是否重复 $createRules = [ 'employee_id' => ['required'], 'start_at' => ['required'], @@ -51,4 +50,35 @@ class OfficalBusinessService extends BaseService 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; + } + + $model->update($data); + $this->currentModel = $model; + return true; + } + + public function delete(string $ids): mixed + { + $list = $this->query()->with(['workflow'])->whereIn('id', explode(',', $ids))->get(); + foreach ($list as $item) { + if (!$item->canUpdate()) { + return $this->setError('审核中, 无法删除'); + } + $item->delete(); + } + return true; + } } diff --git a/app/Admin/Services/WorkFlowService.php b/app/Admin/Services/WorkFlowService.php index 4f23596..5184333 100644 --- a/app/Admin/Services/WorkFlowService.php +++ b/app/Admin/Services/WorkFlowService.php @@ -33,10 +33,10 @@ class WorkFlowService extends BaseService */ public function apply(WorkflowCheck $check, Employee $user) { - if ($check->check_status === CheckStatus::Success->value) { + if ($check->check_status === CheckStatus::Success) { admin_abort('已经审核通过'); } - if ($check->check_status === CheckStatus::Processing->value) { + if ($check->check_status === CheckStatus::Processing) { admin_abort('正在审核中'); } diff --git a/app/Http/Controllers/Api/Hr/OfficalBusinessController.php b/app/Http/Controllers/Api/Hr/OfficalBusinessController.php new file mode 100644 index 0000000..11824d8 --- /dev/null +++ b/app/Http/Controllers/Api/Hr/OfficalBusinessController.php @@ -0,0 +1,103 @@ +guard()->user(); + $list = OfficalBusiness::with(['workflow']) + ->where('employee_id', $user->id) + ->filter($request->all()) + ->orderBy('id', 'desc') + ->paginate($request->input('per_page')); + + return OfficalBusinessResource::collection($list); + } + + public function show($id) + { + $info = OfficalBusiness::with(['employee', 'store', 'workflow'])->findOrFail($id); + + return OfficalBusinessResource::make($info); + } + + public function store(Request $request, OfficalBusinessService $service) + { + $user = $this->guard()->user(); + $data = $request->all(); + $data['employee_id'] = $user->id; + + try { + DB::beginTransaction(); + if (!$service->store($data)) { + throw new RuntimeException($result); + } + $model = $service->getCurrentModel(); + $workflow = WorkFlowService::make(); + if (!$workflow->apply($model->workflow, $user)) { + throw new RuntimeException($workflow->getError()); + } + + DB::commit(); + return response()->noContent(); + } catch (\Exception $e) { + DB::rollBack(); + throw new RuntimeException($e->getMessage()); + } + } + + public function update($id, Request $request, OfficalBusinessService $service) + { + $user = $this->guard()->user(); + $model = OfficalBusiness::where('employee_id', $user->id)->findOrFail($id); + + try { + DB::beginTransaction(); + if (!$service->update($id, $request->all())) { + throw new RuntimeException($service->getError()); + } + $workflow = WorkFlowService::make(); + if (!$workflow->apply($model->workflow, $user)) { + throw new RuntimeException($workflow->getError()); + } + + DB::commit(); + return response()->noContent(); + } catch (\Exception $e) { + DB::rollBack(); + throw new RuntimeException($e->getMessage()); + } + } + + public function destroy($id, OfficalBusinessService $service) + { + $user = $this->guard()->user(); + $model = OfficalBusiness::where('employee_id', $user->id)->findOrFail($id); + + try { + DB::beginTransaction(); + if (!$service->delete($id)) { + throw new RuntimeException($service->getError()); + } + + DB::commit(); + return response()->noContent(); + } catch (\Exception $e) { + DB::rollBack(); + throw new RuntimeException($e->getMessage()); + } + } +} diff --git a/app/Http/Controllers/Api/Hr/OvertimeController.php b/app/Http/Controllers/Api/Hr/OvertimeController.php index a4e1ec0..fd0f52d 100644 --- a/app/Http/Controllers/Api/Hr/OvertimeController.php +++ b/app/Http/Controllers/Api/Hr/OvertimeController.php @@ -69,7 +69,6 @@ class OvertimeController extends Controller if (!$service->update($id, $request->all())) { throw new RuntimeException($service->getError()); } - $model = $service->getCurrentModel(); $workflow = WorkFlowService::make(); if (!$workflow->apply($model->workflow, $user)) { throw new RuntimeException($workflow->getError()); @@ -86,7 +85,7 @@ class OvertimeController extends Controller public function destroy($id, OvertimeApplyService $service) { $user = $this->guard()->user(); - $model = OvertimeApply::with(['workflow'])->where('employee_id', $user->id)->findOrFail($id); + $model = OvertimeApply::where('employee_id', $user->id)->findOrFail($id); try { DB::beginTransaction(); diff --git a/app/Http/Controllers/Api/WorkflowController.php b/app/Http/Controllers/Api/WorkflowController.php index d89a8c5..85d1102 100644 --- a/app/Http/Controllers/Api/WorkflowController.php +++ b/app/Http/Controllers/Api/WorkflowController.php @@ -128,7 +128,12 @@ class WorkflowController extends Controller 'holiday_applies' => HolidayApplyResource::class, 'overtime_applies' => OvertimeApplyResource::class, 'agreements' => AgreementResource::class, + 'offical_business' => OfficalBusinessResource::class, ]; - return data_get($map, $key); + $resource = data_get($map, $key); + if (!$resource) { + throw new RuntimeException('未知的 subject_type resource: ' . $key); + } + return $resource; } } diff --git a/app/Http/Resources/OfficalBusinessResource.php b/app/Http/Resources/OfficalBusinessResource.php new file mode 100644 index 0000000..62655da --- /dev/null +++ b/app/Http/Resources/OfficalBusinessResource.php @@ -0,0 +1,35 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + + 'employee_id' => $this->employee_id, + 'employee' => EmployeeResource::make($this->whenLoaded('employee')), + 'store_id' => $this->store_id, + 'store' => StoreResource::make($this->whenLoaded('store')), + + 'workflow_check' => WorkflowCheckResource::make($this->whenLoaded('workflow')), + + 'start_at' => $this->start_at->timestamp, + 'end_at' => $this->end_at->timestamp, + 'address' => $this->address, + 'reason' => $this->reason, + + 'created_at' => $this->created_at->timestamp, + ]; + } +} diff --git a/app/Models/OfficalBusiness.php b/app/Models/OfficalBusiness.php index dd7282c..f916015 100644 --- a/app/Models/OfficalBusiness.php +++ b/app/Models/OfficalBusiness.php @@ -19,8 +19,8 @@ class OfficalBusiness extends Model protected $fillable = ['store_id', 'employee_id', 'start_at', 'end_at', 'address', 'reason']; protected $casts = [ - 'start_at' => 'date:Y-m-d', - 'end_at' => 'date:Y-m-d', + 'start_at' => 'date:Y-m-d H:i', + 'end_at' => 'date:Y-m-d H:i', ]; public function modelFilter() diff --git a/routes/api.php b/routes/api.php index d8a1b06..861f7d2 100644 --- a/routes/api.php +++ b/routes/api.php @@ -68,6 +68,9 @@ Route::group([ // 加班申请 Route::apiResource('hr/overtimes', \App\Http\Controllers\Api\Hr\OvertimeController::class); + // 出差报备 + Route::apiResource('hr/offical-bussiness', \App\Http\Controllers\Api\Hr\OfficalBusinessController::class); + // 报销管理 Route::apiResource('reimbursements', \App\Http\Controllers\Api\ReimbursementController::class);