generated from liutk/owl-admin-base
api 出差报备
parent
8a695cd2c9
commit
9dceb459fc
|
|
@ -65,12 +65,14 @@ class HolidayApplyService extends BaseService
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $model->update($data);
|
$model->update($data);
|
||||||
|
$this->currentModel = $model;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete(string $ids): mixed
|
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) {
|
foreach ($list as $item) {
|
||||||
if (!$item->canUpdate()) {
|
if (!$item->canUpdate()) {
|
||||||
return $this->setError('审核中, 无法删除');
|
return $this->setError('审核中, 无法删除');
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,7 @@ class OfficalBusinessService extends BaseService
|
||||||
|
|
||||||
public function validate($data, $model = null)
|
public function validate($data, $model = null)
|
||||||
{
|
{
|
||||||
// 验证申请时间是否重叠
|
// todo 验证申请时间是否重复
|
||||||
// todo
|
|
||||||
$createRules = [
|
$createRules = [
|
||||||
'employee_id' => ['required'],
|
'employee_id' => ['required'],
|
||||||
'start_at' => ['required'],
|
'start_at' => ['required'],
|
||||||
|
|
@ -51,4 +50,35 @@ class OfficalBusinessService extends BaseService
|
||||||
|
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,10 +33,10 @@ class WorkFlowService extends BaseService
|
||||||
*/
|
*/
|
||||||
public function apply(WorkflowCheck $check, Employee $user)
|
public function apply(WorkflowCheck $check, Employee $user)
|
||||||
{
|
{
|
||||||
if ($check->check_status === CheckStatus::Success->value) {
|
if ($check->check_status === CheckStatus::Success) {
|
||||||
admin_abort('已经审核通过');
|
admin_abort('已经审核通过');
|
||||||
}
|
}
|
||||||
if ($check->check_status === CheckStatus::Processing->value) {
|
if ($check->check_status === CheckStatus::Processing) {
|
||||||
admin_abort('正在审核中');
|
admin_abort('正在审核中');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -69,7 +69,6 @@ class OvertimeController extends Controller
|
||||||
if (!$service->update($id, $request->all())) {
|
if (!$service->update($id, $request->all())) {
|
||||||
throw new RuntimeException($service->getError());
|
throw new RuntimeException($service->getError());
|
||||||
}
|
}
|
||||||
$model = $service->getCurrentModel();
|
|
||||||
$workflow = WorkFlowService::make();
|
$workflow = WorkFlowService::make();
|
||||||
if (!$workflow->apply($model->workflow, $user)) {
|
if (!$workflow->apply($model->workflow, $user)) {
|
||||||
throw new RuntimeException($workflow->getError());
|
throw new RuntimeException($workflow->getError());
|
||||||
|
|
@ -86,7 +85,7 @@ class OvertimeController extends Controller
|
||||||
public function destroy($id, OvertimeApplyService $service)
|
public function destroy($id, OvertimeApplyService $service)
|
||||||
{
|
{
|
||||||
$user = $this->guard()->user();
|
$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 {
|
try {
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,12 @@ class WorkflowController extends Controller
|
||||||
'holiday_applies' => HolidayApplyResource::class,
|
'holiday_applies' => HolidayApplyResource::class,
|
||||||
'overtime_applies' => OvertimeApplyResource::class,
|
'overtime_applies' => OvertimeApplyResource::class,
|
||||||
'agreements' => AgreementResource::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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,8 +19,8 @@ class OfficalBusiness extends Model
|
||||||
protected $fillable = ['store_id', 'employee_id', 'start_at', 'end_at', 'address', 'reason'];
|
protected $fillable = ['store_id', 'employee_id', 'start_at', 'end_at', 'address', 'reason'];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'start_at' => 'date:Y-m-d',
|
'start_at' => 'date:Y-m-d H:i',
|
||||||
'end_at' => 'date:Y-m-d',
|
'end_at' => 'date:Y-m-d H:i',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function modelFilter()
|
public function modelFilter()
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,9 @@ Route::group([
|
||||||
// 加班申请
|
// 加班申请
|
||||||
Route::apiResource('hr/overtimes', \App\Http\Controllers\Api\Hr\OvertimeController::class);
|
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);
|
Route::apiResource('reimbursements', \App\Http\Controllers\Api\ReimbursementController::class);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue