d4deb8855e
- 新增 pi_no 参数:模糊匹配备货单 pi_no -> 单id -> 审批实例biz_id (whereHas,todo/history 场景通用) - 顺带修复:历史页一直发送的 keyword 参数后端从未处理(搜索框是摆设), 现按 单号/PI号/客户名称 任一模糊匹配;与 pi_no 同传时取交集 Co-Authored-By: Claude <noreply@anthropic.com>
268 lines
11 KiB
PHP
268 lines
11 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\User;
|
|
use app\common\model\UserGroupMembers;
|
|
use support\Db;
|
|
use app\common\validate\ApprovalRecordsValidate;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\ApprovalRecords;
|
|
use support\Request;
|
|
|
|
class ApprovalRecordsService
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = ApprovalRecords::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = ApprovalRecordsValidate::class;
|
|
|
|
/**
|
|
* 获取验证器
|
|
*/
|
|
public function getValidator()
|
|
{
|
|
return new $this->validator;
|
|
}
|
|
|
|
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
|
{
|
|
$p = $request->all();
|
|
|
|
$config = [];
|
|
$config['model'] = $this->modelClass;
|
|
switch ($scene) {
|
|
case 'index':
|
|
$config['order']['id'] = 'desc';
|
|
|
|
$config['with'] = [
|
|
'instance',
|
|
'approver',
|
|
'instance.flows',
|
|
'instance.initiator',
|
|
'instance.bizInfo'
|
|
];
|
|
|
|
// $config['dataLimit']['type'] = 'relation';
|
|
// $config['dataLimit']['userId'] = $request->user->uid;
|
|
// $config['dataLimit']['userField'] = 'approver_id';
|
|
break;
|
|
case 'todo':
|
|
|
|
if ($p['scene'] === 'todo') {
|
|
// if (!in_array(1, $request->user->role_ids)) {
|
|
// $config['where']['approver_id'] = $request->user->uid;
|
|
// }
|
|
$roles_id = array_intersect([1,6], $request->user->role_ids);
|
|
if(count($roles_id) == 0){
|
|
if(in_array(4, $request->user->role_ids)){
|
|
$report = User::where('report_to',$request->user->uid)->pluck('id')->toArray();
|
|
$report = array_merge($report, [$request->user->uid]);
|
|
$config['where']['approver_id'] = ['in',$report];
|
|
} elseif (in_array(5, $request->user->role_ids)){ //内勤
|
|
$userGroupMember = UserGroupMembers::where('user_id',$request->user->uid)
|
|
->where('is_leader',1)
|
|
->first();
|
|
if($userGroupMember){
|
|
$report = UserGroupMembers::where('group_id',$userGroupMember->group_id)
|
|
->pluck('user_id')
|
|
->toArray();
|
|
$report = array_merge($report, [$request->user->uid]);
|
|
$config['where']['approver_id'] = ['in',$report];
|
|
}else{ //内勤没有负责的组时
|
|
$config['where']['approver_id'] = $request->user->uid;
|
|
}
|
|
} else {
|
|
$config['where']['approver_id'] = $request->user->uid;
|
|
}
|
|
}
|
|
$config['where']['status'] = 'pending';
|
|
$config['where']['instance.status'] = 'processing';
|
|
}
|
|
|
|
if ($p['scene'] === 'history') {
|
|
// if (!in_array(1, $request->user->role_ids) && !in_array(5, $request->user->role_ids)) {
|
|
// $config['where']['approver_id'] = $request->user->uid;
|
|
// }
|
|
$roles_id = array_intersect([1,6], $request->user->role_ids);
|
|
if(count($roles_id) == 0){
|
|
if(in_array(4, $request->user->role_ids)){
|
|
$report = User::where('report_to',$request->user->uid)->pluck('id')->toArray();
|
|
$report = array_merge($report, [$request->user->uid]);
|
|
$config['where']['approver_id'] = ['in',$report];
|
|
} elseif (in_array(5, $request->user->role_ids)){ //内勤
|
|
$userGroupMember = UserGroupMembers::where('user_id',$request->user->uid)
|
|
->where('is_leader',1)
|
|
->first();
|
|
if($userGroupMember){
|
|
$report = UserGroupMembers::where('group_id',$userGroupMember->group_id)
|
|
->pluck('user_id')
|
|
->toArray();
|
|
$report = array_merge($report, [$request->user->uid]);
|
|
$config['where']['approver_id'] = ['in',$report];
|
|
}else{ //内勤没有负责的组时
|
|
$config['where']['approver_id'] = $request->user->uid;
|
|
}
|
|
} else {
|
|
$config['where']['approver_id'] = $request->user->uid;
|
|
}
|
|
}
|
|
$config['where']['status'] = ['!=', 'pending'];
|
|
}
|
|
|
|
// 业务筛选:pi_no 精确到PI号;keyword 匹配 单号/PI号(历史页原有搜索框此前后端未实现)
|
|
// 两个条件同时传时取交集
|
|
$bizIdFilter = null;
|
|
if (!empty($p['pi_no'])) {
|
|
$ids = Db::table('stock_orders')
|
|
->where('pi_no', 'like', '%' . trim((string)$p['pi_no']) . '%')
|
|
->pluck('id')->toArray();
|
|
$bizIdFilter = $ids ?: [0];
|
|
}
|
|
if (!empty($p['keyword'])) {
|
|
$kw = '%' . trim((string)$p['keyword']) . '%';
|
|
$ids = Db::table('stock_orders')
|
|
->where(function ($q) use ($kw) {
|
|
$q->where('order_no', 'like', $kw)
|
|
->orWhere('pi_no', 'like', $kw)
|
|
->orWhere('customer_snapshot', 'like', $kw);
|
|
})
|
|
->pluck('id')->toArray();
|
|
$ids = $ids ?: [0];
|
|
$bizIdFilter = $bizIdFilter === null ? $ids : array_values(array_intersect($bizIdFilter, $ids));
|
|
}
|
|
if ($bizIdFilter !== null) {
|
|
$config['where']['instance.biz_id'] = ['in', $bizIdFilter];
|
|
}
|
|
|
|
$config['order']['id'] = 'desc';
|
|
|
|
$config['with'] = [
|
|
'instance',
|
|
'approver',
|
|
'instance.flows',
|
|
'instance.initiator',
|
|
'instance.bizInfo'
|
|
];
|
|
break;
|
|
case 'show':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
case 'store':
|
|
break;
|
|
case 'update':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
case 'destroy':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
case 'logs':
|
|
$config['where']['instance_id'] = $p['instance_id'];
|
|
$config['order']['id'] = 'desc';
|
|
break;
|
|
case 'border':
|
|
$config['where']['id'] = $p['id'];
|
|
$config['order']['id'] = 'desc';
|
|
$config['with'] = [
|
|
'instance',
|
|
'approver',
|
|
'instance.flows',
|
|
'instance.initiator',
|
|
'instance.bizInfo'
|
|
];
|
|
break;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
|
|
public function serviceApprove(Request $request)
|
|
{
|
|
$param = $request->all();
|
|
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
|
|
return $appro->approve($param['instance_id'], $request->user->uid, $param['action'], $param['comment']);
|
|
}
|
|
|
|
|
|
public function serviceLogs(Request $request)
|
|
{
|
|
$config = $this->buildQueryConfig($request, 'logs');
|
|
$item = $this->logicListAll($config);
|
|
return $this->postProcess('Logs', $item);
|
|
}
|
|
|
|
|
|
public function serviceBusinessOrder(Request $request, array $data = [])
|
|
{
|
|
$config = $this->buildQueryConfig($request, 'border');
|
|
$config = $this->borderConfig($config, $data['flow_type']);
|
|
$item = $this->logicShow($config);
|
|
$bizInfo = [];
|
|
$service = match ($data['flow_type']) {
|
|
'stock_orders' => new \app\common\service\StockOrdersService(),
|
|
'stock_orders_nullify' => new \app\common\service\StockOrdersService(),
|
|
'purchase_orders' => new \app\common\service\PurchaseOrdersService(),
|
|
default => new \app\common\service\StockOrdersService()
|
|
};
|
|
$bizInfo = $service->formatData($item->instance->bizInfo);
|
|
return $this->postProcess('Border', $bizInfo);
|
|
}
|
|
|
|
public function borderConfig($config, $type = 'stock_orders')
|
|
{
|
|
$with = match ($type) {
|
|
'stock_orders' => [
|
|
'instance.bizInfo.items',
|
|
'instance.bizInfo.items.options',
|
|
'instance.bizInfo.items.moneyValues',
|
|
'instance.bizInfo.accounts',
|
|
'instance.bizInfo.moneyValues',
|
|
'instance.bizInfo.salesperson',
|
|
],
|
|
'stock_orders_nullify' => [
|
|
'instance.bizInfo.items',
|
|
'instance.bizInfo.items.options',
|
|
'instance.bizInfo.items.moneyValues',
|
|
'instance.bizInfo.accounts',
|
|
'instance.bizInfo.moneyValues',
|
|
'instance.bizInfo.salesperson',
|
|
],
|
|
'purchase_orders' => [
|
|
'instance.bizInfo.items',
|
|
'instance.bizInfo.items.stockOrder',
|
|
'instance.bizInfo.items.stockOrderItems'
|
|
]
|
|
};
|
|
$config['with'] = array_merge($config['with'], $with);
|
|
return $config;
|
|
}
|
|
|
|
|
|
public function transferApproval(Request $request, array $data = [])
|
|
{
|
|
$service = new \app\common\utils\approval\service\ApprovalFlowService();
|
|
$result = $service->transferApproval($data['instance_id'], $data['step_index'], $request->user->uid, $data['to_uid'], $data['reason'], 'name');
|
|
return $result;
|
|
}
|
|
|
|
|
|
public function serviceApprovalTimeline(Request $request, array $data = [])
|
|
{
|
|
$instance = \app\common\model\ApprovalInstances::where('biz_id', $data['id'])->where('flow_type', $data['flow_type'])->first();
|
|
if (!$instance) {
|
|
return [];
|
|
}
|
|
$result = \app\common\model\ApprovalRecords::where('instance_id', $instance->id)->with('approver')->orderBy('id', 'desc')->get();
|
|
return $result;
|
|
}
|
|
} |