generated from liutk/owl-admin-base
main
parent
783bbde30e
commit
86648e67fc
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace App\Admin\Services;
|
namespace App\Admin\Services;
|
||||||
|
|
||||||
use App\Contracts\Checkable;
|
|
||||||
use App\Enums\CheckStatus;
|
use App\Enums\CheckStatus;
|
||||||
use App\Enums\CheckType;
|
use App\Enums\CheckType;
|
||||||
use App\Models\Employee;
|
use App\Models\Employee;
|
||||||
|
|
@ -34,16 +33,16 @@ 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->value) {
|
||||||
return $this->setError('已经审核通过');
|
admin_abort('已经审核通过');
|
||||||
}
|
}
|
||||||
if ($check->check_status === CheckStatus::Processing->value) {
|
if ($check->check_status === CheckStatus::Processing->value) {
|
||||||
return $this->setError('正在审核中');
|
admin_abort('正在审核中');
|
||||||
}
|
}
|
||||||
|
|
||||||
$workflow = Workflow::where('key', $check->key)->first();
|
$workflow = Workflow::where('key', $check->key)->first();
|
||||||
// 没有配置审核流程, 直接通过
|
// 没有配置审核流程, 直接通过
|
||||||
if (! $workflow || ! $workflow->config) {
|
if (! $workflow || ! $workflow->config) {
|
||||||
$this->success();
|
$this->success($check);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -76,8 +75,7 @@ class WorkFlowService extends BaseService
|
||||||
$checkValue = $checkUser->id;
|
$checkValue = $checkUser->id;
|
||||||
$checkName = $checkUser->name;
|
$checkName = $checkUser->name;
|
||||||
} else {
|
} else {
|
||||||
return $this->setError('未知的审核类型: '.$item['type']);
|
admin_abort('未知的审核类型: '.$item['type']);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
$check->logs()->create([
|
$check->logs()->create([
|
||||||
'batch_id' => $batchId,
|
'batch_id' => $batchId,
|
||||||
|
|
@ -142,10 +140,10 @@ class WorkFlowService extends BaseService
|
||||||
public function check(Employee $user, WorkflowLog $log, $status, $options = [])
|
public function check(Employee $user, WorkflowLog $log, $status, $options = [])
|
||||||
{
|
{
|
||||||
if ($log->check_status != CheckStatus::Processing) {
|
if ($log->check_status != CheckStatus::Processing) {
|
||||||
return $this->setError('不可操作, 等待前面的审核完成');
|
admin_abort('不可操作, 等待前面的审核完成');
|
||||||
}
|
}
|
||||||
if (! $this->authCheck($user, $log)) {
|
if (! $this->authCheck($user, $log)) {
|
||||||
return $this->setError('没有权限');
|
admin_abort('没有权限');
|
||||||
}
|
}
|
||||||
$attributes = ['check_status' => $status ? CheckStatus::Success : CheckStatus::Fail];
|
$attributes = ['check_status' => $status ? CheckStatus::Success : CheckStatus::Fail];
|
||||||
$attributes['checked_at'] = data_get($options, 'time', now());
|
$attributes['checked_at'] = data_get($options, 'time', now());
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filters;
|
||||||
|
|
||||||
|
use EloquentFilter\ModelFilter;
|
||||||
|
|
||||||
|
class ReimbursementFilter extends ModelFilter
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,158 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Admin\Services\WorkFlowService;
|
||||||
|
use App\Exceptions\RuntimeException;
|
||||||
|
use App\Http\Resources\ReimbursementResource;
|
||||||
|
use App\Models\Keyword;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class ReimbursementController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
/** @var \App\Models\Employee */
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
$reimbursements = $user->reimbursements()
|
||||||
|
->filter($request->input())
|
||||||
|
->latest('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();
|
||||||
|
|
||||||
|
/** @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']),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class ReimbursementResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'type' => KeywordResource::make($this->whenLoaded('type')),
|
||||||
|
'workflow_check' => WorkflowCheckResource::make($this->whenLoaded('workflow')),
|
||||||
|
'expense' => $this->expense,
|
||||||
|
'reason' => $this->reason,
|
||||||
|
'photos' => $this->photos,
|
||||||
|
'created_at' => $this->created_at?->getTimestamp(),
|
||||||
|
'updated_at' => $this->updated_at?->getTimestamp(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class WorkflowCheckResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'check_status' => $this->check_status,
|
||||||
|
'checked_at' => $this->checked_at?->getTimestamp(),
|
||||||
|
'check_remarks' => (string) $this->check_remarks,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -84,6 +84,14 @@ class Employee extends Model implements AuthenticatableContract
|
||||||
return $this->hasMany(StoreMasterCommission::class, 'store_master_id');
|
return $this->hasMany(StoreMasterCommission::class, 'store_master_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 报销
|
||||||
|
*/
|
||||||
|
public function reimbursements()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Reimbursement::class);
|
||||||
|
}
|
||||||
|
|
||||||
// 管理的门店(店长)
|
// 管理的门店(店长)
|
||||||
// public function masterStore()
|
// public function masterStore()
|
||||||
// {
|
// {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\CheckStatus;
|
||||||
|
use App\Filters\ReimbursementFilter;
|
||||||
use App\Traits\HasCheckable;
|
use App\Traits\HasCheckable;
|
||||||
use App\Traits\HasDateTimeFormatter;
|
use App\Traits\HasDateTimeFormatter;
|
||||||
use EloquentFilter\Filterable;
|
use EloquentFilter\Filterable;
|
||||||
|
|
@ -32,6 +34,21 @@ class Reimbursement extends Model
|
||||||
return $this->belongsTo(Keyword::class, 'reimbursement_type_id', 'key');
|
return $this->belongsTo(Keyword::class, 'reimbursement_type_id', 'key');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function modelFilter(): string
|
||||||
|
{
|
||||||
|
return ReimbursementFilter::class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canUpdate(): bool
|
||||||
|
{
|
||||||
|
return in_array($this->workflow?->check_status, [CheckStatus::None, CheckStatus::Fail, CheckStatus::Cancel]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canDelete(): bool
|
||||||
|
{
|
||||||
|
return in_array($this->workflow?->check_status, [CheckStatus::None, CheckStatus::Fail, CheckStatus::Cancel]);
|
||||||
|
}
|
||||||
|
|
||||||
protected function photos(): Attribute
|
protected function photos(): Attribute
|
||||||
{
|
{
|
||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ use App\Http\Controllers\Api\FeedbackController;
|
||||||
use App\Http\Controllers\Api\KeywordController;
|
use App\Http\Controllers\Api\KeywordController;
|
||||||
use App\Http\Controllers\Api\Ledger\LedgerController;
|
use App\Http\Controllers\Api\Ledger\LedgerController;
|
||||||
use App\Http\Controllers\Api\Ledger\LotteryTypeController;
|
use App\Http\Controllers\Api\Ledger\LotteryTypeController;
|
||||||
|
use App\Http\Controllers\Api\ReimbursementController;
|
||||||
use App\Http\Controllers\Api\StatsController;
|
use App\Http\Controllers\Api\StatsController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
|
@ -36,6 +37,9 @@ Route::group([
|
||||||
Route::get('/ledger/ledgers/{date}', [LedgerController::class, 'show']);
|
Route::get('/ledger/ledgers/{date}', [LedgerController::class, 'show']);
|
||||||
Route::get('/ledger/lottery-types', [LotteryTypeController::class, 'index']);
|
Route::get('/ledger/lottery-types', [LotteryTypeController::class, 'index']);
|
||||||
|
|
||||||
|
// 报销管理
|
||||||
|
Route::apiResource('reimbursements', ReimbursementController::class);
|
||||||
|
|
||||||
// 举报投诉
|
// 举报投诉
|
||||||
Route::post('complaints', [ComplaintController::class, 'store']);
|
Route::post('complaints', [ComplaintController::class, 'store']);
|
||||||
// 意见箱
|
// 意见箱
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue