api 加班申请

main
panliang 2024-04-16 12:10:57 +08:00
parent f084d81dba
commit feecda3d8b
10 changed files with 199 additions and 13 deletions

View File

@ -101,12 +101,14 @@ class OvertimeController extends AdminController
$detail = amisMake()->Property()->items([
['label' => __('overtime_apply.store_id'), 'content' => '${store.title}'],
['label' => __('overtime_apply.employee_id'), 'content' => '${employee.name}'],
['label' => __('overtime_apply.date'), 'content' => '${date}'],
['label' => __('overtime_apply.start_at'), 'content' => '${start_at}'],
['label' => __('overtime_apply.end_at'), 'content' => '${end_at}'],
['label' => __('overtime_apply.hours'), 'content' => '${hours}'],
['label' => __('overtime_apply.created_at'), 'content' => '${created_at}'],
['label' => __('overtime_apply.reason'), 'content' => '${reason}', 'span' => 2],
['label' => __('overtime_apply.date'), 'content' => [
amisMake()->Tag()->label('${date}'),
amisMake()->Tag()->label('${start_at}'),
amisMake()->Tag()->label('${end_at}'),
], 'span' => 2],
['label' => __('overtime_apply.hours'), 'content' => '${hours}'],
['label' => __('overtime_apply.reason'), 'content' => '${reason}', 'span' => 3],
['label' => __('workflow_log.check_status'), 'content' => amisMake()->Mapping()->name('workflow.check_status')->map(CheckStatus::options())],
['label' => __('workflow_log.checked_at'), 'content' => '${workflow.checked_at}'],
['label' => __('workflow_log.remarks'), 'content' => '${workflow.check_remarks}'],

View File

@ -75,6 +75,7 @@ class HolidayApplyService extends BaseService
if (!$item->canUpdate()) {
return $this->setError('审核中, 无法删除');
}
$item->delete();
}
return true;
}

View File

@ -29,6 +29,10 @@ class OvertimeApplyService extends BaseService
$data['start_at'] = $start;
$data['end_at'] = $end;
$data['date'] = $start->format('Y-m-d');
}
if (isset($data['start_at']) && isset($data['end_at'])) {
$start = $data['start_at'] instanceof \DateTime ? $data['start_at'] : Carbon::parse($data['start_at']);
$end = $data['end_at'] instanceof \DateTime ? $data['end_at'] : Carbon::parse($data['end_at']);
$data['hours'] = $start->diffInHours($end);
}
@ -37,10 +41,10 @@ class OvertimeApplyService extends BaseService
public function validate($data, $model = null)
{
// 验证申请时间是否重叠
// todo
// todo 验证申请时间是否重复
$createRules = [
'employee_id' => ['required'],
'date' => ['required'],
'start_at' => ['required'],
'end_at' => ['required'],
];
@ -52,4 +56,33 @@ class OvertimeApplyService 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;
}
return $model->update($data);
}
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;
}
}

View File

@ -3,7 +3,7 @@
namespace App\Http\Controllers\Api\Hr;
use App\Http\Controllers\Api\Controller;
use Illuminate\Http\{Request, Response};
use Illuminate\Http\Request;
use App\Models\HolidayApply;
use App\Http\Resources\HolidayApplyResource;
use App\Admin\Services\{HolidayApplyService, WorkFlowService};

View File

@ -0,0 +1,105 @@
<?php
namespace App\Http\Controllers\Api\Hr;
use App\Http\Controllers\Api\Controller;
use Illuminate\Http\Request;
use App\Models\OvertimeApply;
use App\Http\Resources\OvertimeApplyResource;
use App\Admin\Services\{OvertimeApplyService, WorkFlowService};
use App\Exceptions\RuntimeException;
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())
->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)
{
$user = $this->guard()->user();
$data = $request->all();
$data['employee_id'] = $user->id;
try {
DB::beginTransaction();
$data = $service->resloveData($data);
$result = $service->validate($data);
if ($result !== true) {
throw new RuntimeException($result);
}
$model = OvertimeApply::create($data);
$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, OvertimeApplyService $service)
{
$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::with(['workflow'])->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());
}
}
}

View File

@ -4,7 +4,7 @@ namespace App\Http\Controllers\Api;
use Illuminate\Http\{Request, Response};
use Illuminate\Database\Eloquent\Relations\Relation;
use App\Http\Resources\{ReimbursementResource, WorkflowLogResource, EmployeeSignRepairResource, HolidayApplyResource};
use App\Http\Resources\{ReimbursementResource, WorkflowLogResource, EmployeeSignRepairResource, HolidayApplyResource, OvertimeApplyResource};
use App\Enums\CheckStatus;
use App\Models\{WorkflowLog, WorkflowCheck};
use Illuminate\Support\Facades\DB;
@ -126,6 +126,7 @@ class WorkflowController extends Controller
'reimbursements' => ReimbursementResource::class,
'employee_sign_repairs' => EmployeeSignRepairResource::class,
'holiday_applies' => HolidayApplyResource::class,
'overtime_applies' => OvertimeApplyResource::class,
];
return data_get($map, $key);
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class OvertimeApplyResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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')),
'date' => $this->date->timestamp,
'start_at' => $this->start_at->timestamp,
'end_at' => $this->end_at->timestamp,
'hours' => $this->hours,
'reason' => $this->reason,
'created_at' => $this->created_at->timestamp,
];
}
}

View File

@ -3,8 +3,7 @@
namespace App\Models;
use App\Enums\{CheckStatus};
use App\Traits\HasCheckable;
use App\Traits\HasDateTimeFormatter;
use App\Traits\{HasCheckable, HasDateTimeFormatter};
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
@ -19,8 +18,8 @@ class OvertimeApply extends Model
protected $casts = [
'date' => 'date:Y-m-d',
'start_at' => 'datetime',
'end_at' => 'datetime',
'start_at' => 'datetime:H:i',
'end_at' => 'datetime:H:i',
];
public function modelFilter()

View File

@ -4,6 +4,7 @@ namespace App\Traits;
use App\Models\WorkflowCheck;
use Illuminate\Support\Str;
use App\Enums\CheckStatus;
trait HasCheckable
{
@ -38,6 +39,11 @@ trait HasCheckable
}
public function canUpdate(): bool
{
return in_array($this->workflow?->check_status, [CheckStatus::None, CheckStatus::Fail, CheckStatus::Cancel]);
}
/**
* 关联审核流水
*/

View File

@ -62,6 +62,9 @@ Route::group([
// 请假管理
Route::apiResource('hr/holidays', \App\Http\Controllers\Api\Hr\HolidayController::class);
// 加班申请
Route::apiResource('hr/overtimes', \App\Http\Controllers\Api\Hr\OvertimeController::class);
// 报销管理
Route::apiResource('reimbursements', \App\Http\Controllers\Api\ReimbursementController::class);