SupplierAuditService.php
13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
namespace App\Http\Services;
use App\Http\Validators\SupplierValidator;
use App\Model\LogModel;
use App\Model\SupplierChannelModel;
use App\Model\SupplierContactModel;
class SupplierAuditService
{
public function auditSupplier($supplierId, $status, $rejectReason)
{
$model = new SupplierChannelModel();
//先找出原来供应商的状态
$supplierModel = new SupplierChannelModel();
$supplier = $supplierModel->where('supplier_id', $supplierId)->first()->toArray();
//如果需要复审并且审核状态为审核中,则代表这次审核为第一次审核或者供应商类型从临时修改为正式
if ($supplier['status'] == SupplierChannelModel::STATUS_IN_REVIEW && $supplier['need_review'] == 1) {
//第一次审核,通过是将审核状态置为待复审
//通过
if ($status == SupplierChannelModel::STATUS_PASSED) {
$dbStatus = SupplierChannelModel::STATUS_NEED_REVIEW;
} else {
//不通过
$dbStatus = $status;
}
$update = [
'update_time' => time(),
'status' => $dbStatus,
'reject_reason' => $rejectReason,
];
$result = $model->where('supplier_id', $supplierId)->update($update);
//如果是待复审状态,通过的话还要将是否需要复审状态置为0
} elseif ($supplier['status'] === SupplierChannelModel::STATUS_NEED_REVIEW) {
if ($status == SupplierChannelModel::STATUS_PASSED) {
$result = $model->where('supplier_id', $supplierId)->update([
'update_time' => time(),
'status' => $status,
'reject_reason' => $rejectReason,
'need_review' => 0,
]);
$supplier['need_review'] = 0;
} else {
$result = $model->where('supplier_id', $supplierId)->update([
'update_time' => time(),
'status' => $status,
'reject_reason' => $rejectReason,
]);
}
//剩下的就是普通的审核
} else {
$result = $model->where('supplier_id', $supplierId)->update([
'update_time' => time(),
'status' => $status,
'reject_reason' => $rejectReason,
]);
}
$action = '审核供应商';
if ($result) {
//如果状态是复审
if ($supplier['status'] == SupplierChannelModel::STATUS_NEED_REVIEW) {
$auditStatus = $status == SupplierChannelModel::STATUS_PASSED ? '复审通过 ' . $rejectReason : '复审不通过,原因是 : ' . $rejectReason;
$action = '复审供应商';
} else {
$auditStatus = $status == SupplierChannelModel::STATUS_PASSED ? '审核通过 ' . $rejectReason : '审核不通过,原因是 : ' . $rejectReason;
}
$logService = new LogService();
$content = $auditStatus;
$logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, $action, $content);
}
$needReview = SupplierChannelModel::where('supplier_id', $supplierId)->value('need_review');
//复审通过状态的供应商才会同步到金蝶
if ($needReview == 0) {
//发送队列消息同步到金蝶
$service = new SyncSupplierService();
$service->syncSupplierToErp($supplierId);
if ($status == SupplierChannelModel::STATUS_PASSED) {
if (!checkPerm('IgnoreCompanyCheck')) {
//判断有公司信息才允许同步给一体化
$regionType = $supplier['region'] == SupplierChannelModel::REGION_CN ? 1 : 2;
if ($regionType == 2) {
(new SyncSupplierService())->syncSupplierToUnited($supplierId);
} else {
$supplier['tax_number'] = $supplier['supplier_name'] ?: $supplier['tax_number'];
$company = (new CompanyService())->getCompanyInfo($supplier['supplier_name'],
$supplier['tax_number'], $regionType);
if (!empty($company)) {
if (!empty($company['tax_number'])) {
SupplierChannelModel::where('supplier_id', $supplierId)->update([
'tax_number' => $company['tax_number'],
]);
}
//同步给一体化系统
(new SyncSupplierService())->syncSupplierToUnited($supplierId);
}
}
} else {
(new SyncSupplierService())->syncSupplierToUnited($supplierId);
}
}
}
return $result;
}
public function CheckCanApplyInReview($supplierIds)
{
foreach ($supplierIds as $supplierId) {
$supplier = SupplierChannelModel::where('supplier_id', $supplierId)->first()->toArray();
//要将部分字段从0改成'',要不然表单验证会有问题
$needChangeField = [
'pay_type',
'currency',
'supplier_group',
'supplier_type',
'settlement_type',
];
foreach ($supplier as $key => &$value) {
if (in_array($key, $needChangeField) && empty($value)) {
$value = '';
}
}
unset($value);
$validator = new SupplierValidator();
return $validator->checkSave($supplier, true);
}
}
//判断是否是可以由当前用户审核
//要判断审核供应商的最后修改人,是不是属于当前审批人的部门或者部下,是才能让他审核
public function CheckCanAuditSupplier($supplierId)
{
//忽略上面所说的限制(权限)
if (checkPerm('IgnoreAuditCheck')) {
return true;
}
$supplierModel = new SupplierChannelModel();
$supplier = $supplierModel->where('supplier_id', $supplierId)->first()->toArray();
$auditUserId = request()->user->userId;
$logModel = new LogModel();
//找出不能跳过审核检查并且是更新操作的日志,默认都是不能跳过
$log = $logModel->where('supplier_id', $supplierId)
->where('type', LogModel::UPDATE_OPERATE)
->where('ignore_audit_check', 0)->orderBy('id', 'desc')->first();
$log = $log ? $log->toArray() : [];
//判断供应商,如果是第一次新增的供应商,要去判断创建人是否属于审核人相关的部门
//别问我为啥不加多一个状态,这个审核流程是突然改成这样子的,没法加...
if (empty($log)) {
$lastUpdateUserId = $supplier['create_uid'];
} else {
$lastUpdateUserId = $log['admin_id'];
if ($lastUpdateUserId == 1000) {
return true;
}
}
//专门给采购助理这个账号要做特殊逻辑,采购系统那边没有按照正常流程去判断,导致这个账号的部门分布完全不合理,所以供应商系统这边只能恶心自己去加特殊跳过了
if ($lastUpdateUserId == config('field.AssistantUserId')) {
return true;
}
//审批人只能审批自己的部下修改的供应商
$departmentService = new DepartmentService();
$subordinateUserIds = $departmentService->getSubordinateUserIds($auditUserId);
if (in_array($lastUpdateUserId, $subordinateUserIds) || in_array($supplier['create_uid'],
$subordinateUserIds)) {
return true;
}
return false;
}
//判断是否为待跟进供应商
public function checkIsNeedToFollowUpSupplier($supplierId)
{
$model = new SupplierChannelModel();
$supplier = $model->where('supplier_id', $supplierId)->first();
//针对非禁止交易状态的供应商重新分配渠道员,且必填信息不完整
if (!empty($supplier) && ($supplier['status'] != SupplierChannelModel::STATUS_DISABLE)) {
//开始检查
//如果全部必填字段都有了,就返回false(不需要跟进)
if ($this->checkHasAllRequireField($supplier)) {
return false;
} else {
return true;
}
}
//或者单纯只是禁用状态的供应商重新分配渠道员,也是要处理待跟进状态
if (!empty($supplier) && ($supplier['status'] == SupplierChannelModel::STATUS_DISABLE)) {
//开始检查
return true;
}
return false;
}
//检查必填字段,false缺少必填字段
public function checkHasAllRequireField($supplier)
{
//先检查基础字段
if (empty($supplier['supplier_name']) || empty($supplier['region']) || empty($supplier['legal_representative'])) {
return false;
}
if (empty($supplier['stockup_type']) || empty($supplier['supplier_group']) || empty($supplier['qualification_photos'])) {
return false;
}
//再检查营业执照是否有
if (isset($supplier['qualification_photos']['business_license']) && empty($supplier['qualification_photos']['business_license'])) {
return false;
}
//检查是否有联系方式
$contactModel = new SupplierContactModel();
$count = $contactModel->where('supplier_id', $supplier['supplier_id'])->count();
//没有联系方式,就需要去跟进
if (!$count) {
return false;
}
//联系方式和当前采购数量对不上,也要跟进
$channelUserNumber = count(explode(',', $supplier['channel_uid']));
if ($channelUserNumber != $count) {
return false;
}
return true;
}
//判断是否要进入审核中状态,因为部分字段修改是不需要走审核的,这是更新之前的校验
public function checkNeedAudit($supplierId, $channel)
{
$notNeedAuditField = [
'register_company_name',
'supplier_name_en',
'province_id',
'city_id',
'established_time',
'credit_investigation',
'system_tags',
'customer_tags',
'sku_tag',
'sku_tag',
'sku_tag',
'sku_upload_ruler',
'sku_audit_ruler',
'stockup_type'
];
//先找出目前数据库里面的数据
$selectField = array_keys($channel);
$model = new SupplierChannelModel;
$status = $model->where('supplier_id', $supplierId)->value('status');
//只要是未通过的,都要进入审核
if ($status == SupplierChannelModel::STATUS_REJECT) {
return true;
}
$supplier = $model->select($selectField)->where('supplier_id', $supplierId)->first()->toArray();
//如果有忽略审核的权限并且不需要复审,那么状态就是不需要审核直接通过
$canIgnoreAudit = $this->checkCanIgnoreSupplierAudit($supplierId);
if ($canIgnoreAudit) {
return false;
}
$changeField = [];
foreach ($supplier as $key => $value) {
if ($value != $channel[$key]) {
$changeField[] = $key;
}
}
foreach ($changeField as $field) {
//只要有一个不存在于不需要审核的字段,就返回需要审核
if (!in_array($field, $notNeedAuditField)) {
return true;
} else {
if ($field === 'stockup_type') {
//还要单独判断供应商的stockup_type,只有是操作芯链这个值的时候,才不去审核,真是服了
$oldStockupType = explode(',', trim($supplier['stockup_type'], ''));
$newStockupType = explode(',', trim($channel['stockup_type'], ''));
if (count($oldStockupType) > count($newStockupType)) {
$modifyStockupType = array_diff($oldStockupType, $newStockupType);
} else {
$modifyStockupType = array_diff($newStockupType, $oldStockupType);
}
if (array_values($modifyStockupType) != ['5']) {
return true;
}
}
}
}
return false;
}
//判断是否可以直接忽略修改审核
public function checkCanIgnoreSupplierAudit($supplierId)
{
$needReview = SupplierChannelModel::where('supplier_id', $supplierId)->value('need_review');
//如果有忽略审核的权限并且不需要复审,那么状态就是不需要审核直接通过
if (checkPerm('IgnoreUpdateAudit') && !$needReview) {
return true;
} else {
return false;
}
}
}