api 出差报备

main
panliang 2024-04-16 13:14:11 +08:00
parent 8a695cd2c9
commit 9dceb459fc
9 changed files with 188 additions and 11 deletions

View File

@ -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('审核中, 无法删除');

View File

@ -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;
}
}

View File

@ -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('正在审核中');
}

View File

@ -0,0 +1,103 @@
<?php
namespace App\Http\Controllers\Api\Hr;
use App\Http\Controllers\Api\Controller;
use Illuminate\Http\Request;
use App\Models\OfficalBusiness;
use App\Http\Resources\OfficalBusinessResource;
use App\Admin\Services\{OfficalBusinessService, WorkFlowService};
use App\Exceptions\RuntimeException;
use Illuminate\Support\Facades\DB;
/**
* 出差报备
*/
class OfficalBusinessController extends Controller
{
public function index(Request $request)
{
$user = $this->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());
}
}
}

View File

@ -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();

View File

@ -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;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class OfficalBusinessResource 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')),
'start_at' => $this->start_at->timestamp,
'end_at' => $this->end_at->timestamp,
'address' => $this->address,
'reason' => $this->reason,
'created_at' => $this->created_at->timestamp,
];
}
}

View File

@ -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()

View File

@ -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);