借用
This commit is contained in:
@@ -52,19 +52,4 @@ class InvItemController extends BaseController
|
||||
$data['manage_user_name'] = User::where('id', $data['manage_user_id'])->value('name');
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function requisitionList(Request $request){
|
||||
try {
|
||||
$res = $this->service->getList();
|
||||
$result = [];
|
||||
foreach ($res as $value) {
|
||||
$value['company_name'] = Corp::where('id', $value['company_id'])->value('name');
|
||||
$value['manage_user_name'] = User::where('id', $value['manage_user_id'])->value('name');
|
||||
$result[] = $value;
|
||||
}
|
||||
return $this->success($result, '审核列表');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,13 @@
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\model\InvRequisitionApprove;
|
||||
use app\common\model\InvRequisitionAttachment;
|
||||
use app\common\model\InvRequisitionItem;
|
||||
use app\common\service\OssService;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use app\admin\service\InvRequisitionService;
|
||||
use support\Request;
|
||||
|
||||
class InvRequisitionController extends BaseController
|
||||
{
|
||||
@@ -16,18 +21,85 @@ class InvRequisitionController extends BaseController
|
||||
$this->service = new InvRequisitionService();
|
||||
}
|
||||
|
||||
/**
|
||||
* 审核
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \support\Response
|
||||
*/
|
||||
public function approve(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $this->service->serviceApprove($request);
|
||||
return $this->success($data);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage().$e->getFile().$e->getLine().$e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return mixed 返回分页数据及总数
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
try {
|
||||
$raw = $this->service->serviceList($request);
|
||||
$data = $this->transformData($raw, method_exists($this, 'indexFormat')
|
||||
? [$this, 'indexFormat']
|
||||
: null);
|
||||
return $this->success($data);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表页数据格式化
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
// protected function indexFormat($data)
|
||||
// {
|
||||
// if (isset($data['created_at']))
|
||||
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
// if (isset($data['updated_at']))
|
||||
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
// return $data;
|
||||
// }
|
||||
protected function indexFormat($data)
|
||||
{
|
||||
// if (isset($data['id'])){
|
||||
// $data['approve'] = InvRequisitionApprove::where('requisition_id', $data['id'])->get();
|
||||
// }
|
||||
if (isset($data['created_at']))
|
||||
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
if (isset($data['updated_at']))
|
||||
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
if (isset($data['id'])){
|
||||
$data['item'] = InvRequisitionItem::where('requisition_id', $data['id'])->get();
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表页数据格式化
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function showFormat($data)
|
||||
{
|
||||
if (isset($data['id'])){
|
||||
$data['approve'] = InvRequisitionApprove::where('requisition_id', $data['id'])->get();
|
||||
}
|
||||
if (isset($data['created_at']))
|
||||
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
if (isset($data['updated_at']))
|
||||
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
if (isset($data['id'])){
|
||||
$data['item'] = InvRequisitionItem::where('requisition_id', $data['id'])->get();
|
||||
$data['attachment'] = InvRequisitionAttachment::where('requisition_id', $data['id'])->get();
|
||||
$oss = new OssService();
|
||||
foreach ($data['attachment'] as $key => $value) {
|
||||
$data['attachment'][$key]['file_path_pull'] = $oss->generatePrivateUrl($value['file_path']);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace app\admin\service;
|
||||
use app\common\validate\InvRequisitionValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\InvRequisition;
|
||||
use support\Request;
|
||||
|
||||
class InvRequisitionService
|
||||
{
|
||||
@@ -28,4 +29,60 @@ class InvRequisitionService
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造 CrudUtil 查询配置
|
||||
*/
|
||||
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
||||
{
|
||||
$p = $request->all();
|
||||
|
||||
$config = [];
|
||||
$config['model'] = $this->modelClass;
|
||||
switch ($scene) {
|
||||
case 'default':
|
||||
//$config['where']['status'] = 1;
|
||||
$config['order']['id'] = 'desc';
|
||||
break;
|
||||
case 'index':
|
||||
if(isset($p['status']) && $p['status']){
|
||||
$config['where']['status'] = $p['status'];
|
||||
}
|
||||
if(isset($p['applicant_id']) && $p['applicant_id']){
|
||||
$config['where']['applicant_id'] = $p['applicant_id'];
|
||||
}
|
||||
$config['order']['id'] = 'desc';
|
||||
break;
|
||||
case 'show':
|
||||
$config['where']['id'] = $p['id'];
|
||||
//$config['where']['status'] = 1;
|
||||
break;
|
||||
case 'store':
|
||||
break;
|
||||
case 'update':
|
||||
$config['where']['id'] = $p['id'];
|
||||
break;
|
||||
case 'destroy':
|
||||
$config['where']['id'] = $p['id'];
|
||||
break;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 审批
|
||||
* @param Request $request
|
||||
* @return mixed|string[]|null
|
||||
*/
|
||||
public function serviceApprove(Request $request)
|
||||
{
|
||||
$param = $request->all();
|
||||
//$appro = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
$appro = new \app\common\service\InvRequisitionApproveService();
|
||||
//requisition_id:申请id
|
||||
//action:审批动作:1通过 2驳回
|
||||
//comment:审批意见
|
||||
return $appro->approve($param['requisition_id'], $param['approver_id'], $request->user->uid, $param['action'], $param['comment']);
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,7 @@ class InvRequisitionApproveService
|
||||
}
|
||||
|
||||
/** 执行审批 */
|
||||
public function approve(int $requisitionId, int $approverId, string $action, string $comment = '')
|
||||
public function approve(int $requisitionId, int $approverId, int $uid, string $action, string $comment = '')
|
||||
{
|
||||
if (!in_array($action, ['1','2'], true)) { //审批动作:1通过 2驳回
|
||||
throw new BusinessException("无效的审批操作");
|
||||
@@ -103,12 +103,12 @@ class InvRequisitionApproveService
|
||||
// $instance = Db::table('approval_instances')->where('id', $instanceId)->lockForUpdate()->first();
|
||||
$requisition = Db::table('inv_requisition')->where('id', $requisitionId)->lockForUpdate()->first();
|
||||
if (!$requisition) throw new BusinessException("审批实例未找到");
|
||||
if ($requisition->status !== 'processing') {
|
||||
if ($requisition->status !== 10 && $requisition->status !== 11) {
|
||||
return ['status' => 'noop', 'msg' => '实例未处理'];
|
||||
}
|
||||
|
||||
//审批流程
|
||||
$flow = Db::table('approval_flows')->where('type', $requisition->flow_type)->first();
|
||||
$flow = Db::table('approval_flows')->where('type', 'inv_requisition')->first();
|
||||
if (!$flow) throw new BusinessException("审批流程定义未找到");
|
||||
|
||||
$steps = json_decode($flow->steps, true);
|
||||
@@ -119,7 +119,7 @@ class InvRequisitionApproveService
|
||||
// 找到该用户在本步的 pending 记录(加锁)
|
||||
$pendingRecord = Db::table('inv_requisition_approve')
|
||||
->where('requisition_id', $requisitionId)
|
||||
->where('approver_id', $approverId)
|
||||
->where('id', $approverId)
|
||||
->where('action', 0)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
@@ -140,26 +140,26 @@ class InvRequisitionApproveService
|
||||
return ['status' => 'noop', 'msg' => '无待处理记录'];
|
||||
}
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$now = time();
|
||||
|
||||
// 1) 更新当前人的审批记录
|
||||
Db::table('inv_requisition_approve')->where('id', $pendingRecord->id)->update([
|
||||
'action' => $action,
|
||||
'comment' => $comment,
|
||||
'approved_at' => $now,
|
||||
'action_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
// 2) 已通过步骤与总步数相等则结束审核流程
|
||||
$pass = Db::table('inv_requisition_approve')
|
||||
->where('requisition_id', $requisitionId)
|
||||
->where('approver_id', $approverId)
|
||||
->where('action', 1)
|
||||
->count();
|
||||
if($action == 1){
|
||||
if(count($steps) == $pass){
|
||||
$requisition->status = 12;
|
||||
$requisition->save();
|
||||
Db::table('inv_requisition')->where('id', $requisition->id)->update(['status'=>12]);
|
||||
// $requisition->status = 12;
|
||||
// $requisition->save();
|
||||
return ['status' => 'complete', 'msg' => '已通过'];
|
||||
}
|
||||
}
|
||||
@@ -173,28 +173,41 @@ class InvRequisitionApproveService
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
if(count($steps) == $pass){
|
||||
$requisition->status = 13;
|
||||
$requisition->save();
|
||||
Db::table('inv_requisition')->where('id', $requisition->id)->update(['status'=>12]);
|
||||
// $requisition->status = 13;
|
||||
// $requisition->save();
|
||||
return ['status' => 'complete', 'msg' => '拒绝'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 4) 通过 => 进入下一步或结束
|
||||
$formData = ['manage_user_id'=>''];
|
||||
$requisitionItem = Db::table('inv_requisition_item')->where('requisition_id', $requisitionId)->first();
|
||||
$item_id = Db::table('inv_item')->where('id', $requisitionItem->item_id)->first();
|
||||
$formData = ['manage_user_id'=>$item_id->manage_user_id];
|
||||
|
||||
$nextIndex = $pass;
|
||||
$nextStep = $steps[$nextIndex];
|
||||
$service = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
|
||||
$instance = (object)[
|
||||
'initiator_id' => $requisition->applicant_id, //申请人id
|
||||
'form_data'=>json_encode($formData, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
|
||||
];
|
||||
$approverIds = $service->StetApprovers($nextStep, $instance, true);
|
||||
// if($approverIds == "manage_user_id"){ //物品管理人审核步骤查询管理人
|
||||
// $requisitionItem = Db::table('inv_requisition_item')->where('id', $requisitionId)->first();
|
||||
// $invItem = Db::table('inv_requisition_item')->where('id', $requisitionItem->item_id)->first();
|
||||
//
|
||||
// if(empty($invItem)){
|
||||
// return ['status' => 'noop', 'msg' => '未查询到物品管理人'];
|
||||
// }
|
||||
// $approverIds = [$invItem->manage_user_id];
|
||||
// }
|
||||
$approver = User::where('id', array_pop($approverIds))->first();
|
||||
Db::table('inv_requisition_approve')->insertGetId([
|
||||
'requisition_id' => $requisitionId, //申请单ID
|
||||
'node_type' => $nextStep['approvers']['initiator_leader'],
|
||||
'node_type' => $nextStep['approvers'][0]['value'],
|
||||
'approver_id' => $approver->id,
|
||||
'approver_name' => $approver->name,
|
||||
'action' => 0,
|
||||
|
||||
@@ -139,7 +139,7 @@ class InvRequisitionService
|
||||
return $res;
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
throw new BusinessException("创建物品申请失败:" . $e->getMessage());
|
||||
throw new BusinessException("创建物品申请失败:" . $e->getMessage().$e->getLine().$e->getFile().$e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,11 +149,12 @@ class InvRequisitionService
|
||||
public function startApproval($requisition)
|
||||
{
|
||||
// 启动审批
|
||||
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
//$appro = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
$appro = new \app\common\service\InvRequisitionApproveService();
|
||||
$formData = [
|
||||
'manage_user_id' => $requisition['manage_user_id'],
|
||||
];
|
||||
$appro->start('inv_requisition', 'inv_requisition', $requisition['id'], $formData, $requisition['applicant_id']);
|
||||
return $appro->start('inv_requisition', 'inv_requisition', $requisition['id'], $formData, $requisition['applicant_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+3
-1
@@ -516,7 +516,9 @@ Route::group('/admin', function () {
|
||||
Route::any('/item/destroy', [app\admin\controller\InvItemController::class, 'destroy']);
|
||||
|
||||
Route::group('/requisition', function () {
|
||||
Route::any('/list', [app\admin\controller\InvItemController::class, 'requisitionList']);
|
||||
Route::any('/list', [app\admin\controller\InvRequisitionController::class, 'index']);
|
||||
Route::any('/show', [app\admin\controller\InvRequisitionController::class, 'show']);
|
||||
Route::any('/approve', [app\admin\controller\InvRequisitionController::class, 'approve']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user