generated from liutk/owl-admin-base
140 lines
4.3 KiB
PHP
140 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Hr;
|
|
|
|
use App\Admin\Services\OvertimeApplyService;
|
|
use App\Admin\Services\WorkFlowService;
|
|
use App\Exceptions\RuntimeException;
|
|
use App\Http\Controllers\Api\Controller;
|
|
use App\Http\Resources\OvertimeApplyResource;
|
|
use App\Models\OvertimeApply;
|
|
use App\Models\WorkflowCheck;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* 加班申请
|
|
*/
|
|
class OvertimeController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$user = $this->guard()->user();
|
|
$list = OvertimeApply::with(['workflow'])
|
|
->where('employee_id', $user->id)
|
|
->filter($request->all())
|
|
->orderByDesc(WorkflowCheck::checkStatusSortBuilder(new OvertimeApply()))
|
|
->orderBy('id', 'desc')
|
|
->paginate($request->input('per_page'));
|
|
|
|
return OvertimeApplyResource::collection($list);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$info = OvertimeApply::with(['employee', 'store', 'workflow'])->findOrFail($id);
|
|
|
|
return OvertimeApplyResource::make($info);
|
|
}
|
|
|
|
public function store(Request $request, OvertimeApplyService $service)
|
|
{
|
|
$request->validate(
|
|
rules: [
|
|
// 'date' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
'start_at' => ['bail', 'required', 'date_format:Y-m-d H:i'],
|
|
'duration' => ['bail', 'required', 'int', 'min:1'],
|
|
'reason' => ['bail', 'nullable', 'string', 'max:255'],
|
|
],
|
|
attributes: [
|
|
'date' => '日期',
|
|
'start_at' => '开始时间',
|
|
'duration' => '加班时长',
|
|
'reason' => '加班事由',
|
|
],
|
|
);
|
|
|
|
$user = $this->guard()->user();
|
|
$data = $request->all();
|
|
$data['employee_id'] = $user->id;
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
if (! $service->store($data)) {
|
|
throw new RuntimeException($service->getError());
|
|
}
|
|
$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 tap($e, fn ($e) => report($e));
|
|
}
|
|
}
|
|
|
|
public function update($id, Request $request, OvertimeApplyService $service)
|
|
{
|
|
$request->validate(
|
|
rules: [
|
|
// 'date' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
'start_at' => ['bail', 'required', 'date_format:Y-m-d H:i'],
|
|
'duration' => ['bail', 'required', 'int', 'min:1'],
|
|
'reason' => ['bail', 'nullable', 'string', 'max:255'],
|
|
],
|
|
attributes: [
|
|
'date' => '日期',
|
|
'start_at' => '开始时间',
|
|
'duration' => '加班时长',
|
|
'reason' => '加班事由',
|
|
],
|
|
);
|
|
|
|
$user = $this->guard()->user();
|
|
$model = OvertimeApply::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, OvertimeApplyService $service)
|
|
{
|
|
$user = $this->guard()->user();
|
|
$model = OvertimeApply::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());
|
|
}
|
|
}
|
|
}
|