generated from liutk/owl-admin-base
161 lines
4.9 KiB
PHP
161 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Admin\Services\WorkFlowService;
|
|
use App\Exceptions\RuntimeException;
|
|
use App\Http\Resources\ReimbursementResource;
|
|
use App\Models\{Keyword, Reimbursement, WorkflowCheck};
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
use App\Enums\CheckStatus;
|
|
use Throwable;
|
|
|
|
class ReimbursementController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
/** @var \App\Models\Employee */
|
|
$user = $request->user();
|
|
|
|
$reimbursements = $user->reimbursements()
|
|
->filter($request->input())
|
|
->sort('id')
|
|
->simplePaginate($request->input('per_page', 20));
|
|
|
|
return ReimbursementResource::collection(
|
|
$reimbursements->loadMissing(['type', 'workflow']),
|
|
);
|
|
}
|
|
|
|
public function store(Request $request, WorkFlowService $workFlowService): ReimbursementResource
|
|
{
|
|
$validated = $request->validate(
|
|
rules: [
|
|
'reimbursement_type_id' => ['bail', 'required', Rule::exists(Keyword::class, 'key')],
|
|
'expense' => ['bail', 'required', 'numeric', 'min:0'],
|
|
'reason' => ['bail', 'required', 'max:255'],
|
|
'photos' => ['bail', 'required', 'array'],
|
|
],
|
|
attributes: [
|
|
'reimbursement_type_id' => '报销分类',
|
|
'expense' => '报销金额',
|
|
'reason' => '报销原因',
|
|
'photos' => '报销凭证',
|
|
],
|
|
);
|
|
|
|
/** @var \App\Models\Employee */
|
|
$user = $request->user();
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$validated['store_id'] = $user->store_id;
|
|
/** @var \App\Models\Reimbursement */
|
|
$reimbursement = $user->reimbursements()->create($validated);
|
|
|
|
$workFlowService->apply($reimbursement->workflow, $user);
|
|
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
|
|
throw tap($th, fn ($th) => report($th));
|
|
}
|
|
|
|
return ReimbursementResource::make(
|
|
$reimbursement->load(['type', 'workflow']),
|
|
);
|
|
}
|
|
|
|
public function show($id, Request $request): ReimbursementResource
|
|
{
|
|
/** @var \App\Models\Employee */
|
|
$user = $request->user();
|
|
|
|
/** @var \App\Models\Reimbursement */
|
|
$reimbursement = $user->reimbursements()->find($id);
|
|
|
|
if (is_null($reimbursement)) {
|
|
throw new RuntimeException('报销记录未找到');
|
|
}
|
|
|
|
return ReimbursementResource::make(
|
|
$reimbursement->load(['type', 'workflow', 'employee', 'store']),
|
|
);
|
|
}
|
|
|
|
public function update($id, Request $request, WorkFlowService $workFlowService): ReimbursementResource
|
|
{
|
|
$validated = $request->validate(
|
|
rules: [
|
|
'reimbursement_type_id' => ['bail', 'required', Rule::exists(Keyword::class, 'key')],
|
|
'expense' => ['bail', 'required', 'numeric', 'min:0'],
|
|
'reason' => ['bail', 'required', 'max:255'],
|
|
'photos' => ['bail', 'required', 'array'],
|
|
],
|
|
attributes: [
|
|
'reimbursement_type_id' => '报销分类',
|
|
'expense' => '报销金额',
|
|
'reason' => '报销原因',
|
|
'photos' => '报销凭证',
|
|
],
|
|
);
|
|
|
|
/** @var \App\Models\Employee */
|
|
$user = $request->user();
|
|
|
|
/** @var \App\Models\Reimbursement */
|
|
$reimbursement = $user->reimbursements()->find($id);
|
|
|
|
if (is_null($reimbursement)) {
|
|
throw new RuntimeException('报销记录未找到');
|
|
}
|
|
|
|
if (! $reimbursement->canUpdate()) {
|
|
throw new RuntimeException('['.$reimbursement->workflow->check_status->text().']报销记录不可修改');
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$reimbursement->update($validated);
|
|
|
|
$workFlowService->apply($reimbursement->workflow, $user);
|
|
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
|
|
throw tap($th, fn ($th) => report($th));
|
|
}
|
|
|
|
return ReimbursementResource::make(
|
|
$reimbursement->load(['type', 'workflow']),
|
|
);
|
|
}
|
|
|
|
public function destroy($id, Request $request)
|
|
{
|
|
/** @var \App\Models\Employee */
|
|
$user = $request->user();
|
|
|
|
/** @var \App\Models\Reimbursement */
|
|
$reimbursement = $user->reimbursements()->find($id);
|
|
|
|
if (is_null($reimbursement)) {
|
|
throw new RuntimeException('报销记录未找到');
|
|
}
|
|
|
|
if (! $reimbursement->canDelete()) {
|
|
throw new RuntimeException('['.$reimbursement->workflow->check_status->text().']报销记录不可删除');
|
|
}
|
|
|
|
$reimbursement->delete();
|
|
|
|
return response()->noContent();
|
|
}
|
|
}
|