SupplierApiController.php
25 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Filter\SupplierFilter;
use App\Http\Services\AdminUserService;
use App\Http\Services\CompanyService;
use App\Http\Services\LogService;
use App\Http\Services\SupplierAuditService;
use App\Http\Services\SupplierService;
use App\Http\Services\SyncSupplierService;
use App\Http\Transformers\SupplierTransformer;
use App\Http\Validators\SupplierValidator;
use App\Model\LogModel;
use App\Model\RedisModel;
use App\Model\SupplierChannelModel;
use Illuminate\Http\Request;
class SupplierApiController extends Controller
{
//默认要接收的数据
private $channelMap = [
'supplier_id',
'supplier_code',
//基础资料
'supplier_name',
'register_company_name',
'supplier_name_en',
'stockup_type',
'supplier_group',
'region',
'currency',
'tax_number',
'supplier_address',
'main_brands',
'purchase_uid',
'upload_file',
'legal_representative',
'established_time',
'trading_method',
'phone',
//省市
'province_city',
//收发货地址
'return_phone',
'return_address',
'return_consignee',
'shipping_address',
//系数和货期
'cn_ratio',
'us_ratio',
'credit_investigation',
'us_delivery_time',
'cn_delivery_time_period',
'cn_delivery_time',
'us_delivery_time_period',
//附加费
'cn',
'hk',
//商品上传规则
'sku_upload_ruler',
'sku_audit_ruler',
//标签
'system_tags',
'customer_tags',
'level',
'has_certification',
'sku_mode',
'sku_tag',
'main_customers',
'ticket_time',
'billing_period_detail',
'settlement_type',
//付款方式
'pay_type',
'pay_type_value',
'pay_type_extra',
'supplier_type',
'registered_capital',
'is_business_abnormal',
'has_legal_ID_card',
];
public function Entrance(Request $request, $id)
{
$this->$id($request, $id);
}
//添加供应商
public function AddSupplier($request)
{
//先去表单验证
$validator = new SupplierValidator();
$data = $request->all();
//直接提交的不需要做复杂的表单校验
$isAudit = false;
if (request()->get('direct_apply')) {
$isAudit = true;
$validateResult = $validator->checkSave($data, $isAudit);
} else {
$validateResult = $validator->checkSave($data, $isAudit);
}
if ($validateResult) {
$this->response(-1, $validateResult);
}
$channelMap = $this->channelMap;
$channelMap = array_merge($channelMap, [
'supplier_consignee',
'supplier_position',
'supplier_email',
'supplier_mobile',
'supplier_telephone',
'supplier_qq',
'supplier_fax',
'can_check_uids',
//附件
'file_name',
'file_url',
'field_name',
'validity_type',
'validity_period',
'description',
//银行信息
'receipt_type',
'bank_name',
'bank_adderss',
'account_no',
'account_name',
'account_adderss',
'certificate',
'swift_code',
]);
$channel = $request->only($channelMap);
//如果是有直接新增权限的(绕过天眼查等校验)
if (checkPerm('IgnoreCompanyCheck')) {
$channel['is_standard_add'] = -1;
}
if (SupplierChannelModel::where('supplier_name', trim($channel['supplier_name']))->exists()) {
$this->response(0, '操作成功');
}
$service = new SupplierService();
$supplierId = $service->saveSupplier($channel);
if (!$supplierId) {
$this->response(-1, '操作失败');
}
//如果是申请审核,还要去修改审核状态
if ($isAudit) {
$supplierService = new SupplierService();
$auditData[] = [
'supplier_id' => $supplierId,
//供应商类型为临时供应商,才会去存申请理由
'apply_audit_reason' => $channel['supplier_type'] == 2 ? $request->input('apply_audit_reason') : '',
];
$supplierService->batchApplyInReviewSupplier($auditData);
}
$this->response(0, '操作成功');
}
//添加供应商
public function UpdateSupplier($request)
{
//先去表单验证
$validator = new SupplierValidator();
$data = $request->all();
$isAudit = (bool)$request->input('is_audit');
$supplierId = $request->input('supplier_id');
$validateResult = $validator->checkSave($data, $isAudit);
if ($validateResult) {
$this->response(-1, $validateResult);
}
$channelMap = array_merge($this->channelMap, config('field.AttachmentFields'));
$channel = $request->only($channelMap);
$service = new SupplierService();
$result = $service->saveSupplier($channel);
if (!$result) {
$this->response(-1, '修改失败');
}
//如果是申请审核,还要去修改审核状态,
if ($isAudit) {
$supplierService = new SupplierService();
$auditData[] = [
'supplier_id' => $supplierId,
//供应商类型为临时供应商,才会去存申请理由
'apply_audit_reason' => $channel['supplier_type'] == 2 ? $request->input('apply_audit_reason') : '',
];
$supplierService->batchApplyInReviewSupplier($auditData);
}
$this->response(0, $isAudit ? '申请审核成功' : '修改成功');
}
//获取供应商信息变更记录
public function GetSupplierList(Request $request)
{
$model = new SupplierChannelModel();
$query = $model->orderBy('update_time', 'desc');
$filter = new SupplierFilter();
$query = $filter->listFilter($request->all(), $query);
$limit = $request->get('limit', 10);
$list = $query->paginate($limit)->toArray();
$transformer = new SupplierTransformer();
$list['data'] = $transformer->transformList($list['data']);
$this->response(0, 'ok', $list['data'], $list['total']);
}
public function DisableSupplier($request)
{
//禁用不是直接修改为无法交易,而是改为审核中,然后审核通过后,变成无法交易
$supplierId = $request->get('supplier_id');
$disableReason = trim($request->get('disable_reason'));
if (empty($disableReason)) {
$this->response(-1, '禁用理由必填');
}
$model = new SupplierChannelModel();
//先保存原来的状态
$supplier = $model->where('supplier_id', $supplierId)->first()->toArray();
$redis = new RedisModel();
$redis->hset('supplier_status_before_disable', $supplierId, $supplier['status']);
$result = $model->where('supplier_id', $supplierId)->update([
'update_time' => time(),
'status' => $model::STATUS_DISABLE,
'disable_reason' => $disableReason,
]);
if ($result) {
//写日志
$logService = new LogService();
$logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '禁用', '禁用供应商');
$this->response(0, '禁用成功');
} else {
$this->response(-1, '禁用失败');
}
}
//修改状态(禁用,启用(审核通过),驳回,草稿,取消黑名单)
public function ChangeSupplierStatus($request)
{
$status = $request->get('status');
$supplierId = $request->get('supplier_id');
if (empty($status) || empty($supplierId)) {
$this->response(-1, '参数缺失');
}
$model = new SupplierChannelModel();
$supplier = $model->where('supplier_id', $supplierId)->first()->toArray();
$data = [
'status' => $status,
'update_time' => time(),
];
//审核通过或者驳回,要更新审核时间
if (($supplier['status'] == 1 || $supplier['status'] == 3)) {
if ($status == 2) {
$data['audit_time'] = time();
$data['audit_name'] = $request->user->name;
$data['audit_uid'] = $request->user->userId;
}
}
if ($status == 3) {
$data['audit_time'] = time();
$data['audit_name'] = $request->user->name;
$data['audit_uid'] = $request->user->userId;
}
if ($status == 3) {
$rejectReason = trim($request->get('reject_reason'));
if (empty($rejectReason)) {
$this->response(-1, '没有填写驳回原因');
}
$data['reject_reason'] = $rejectReason;
}
$result = $model->where('supplier_id', $supplierId)->update($data);
//保存日志
$logService = new LogService();
$action = array_get(config('fixed.SupplierStatus'), $status);
$logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, $action, $action . '-' . $supplier['supplier_name']);
if (!$result) {
$this->response(-1, '修改状态失败');
}
//发送队列消息同步到金蝶
$service = new SyncSupplierService();
$service->syncSupplierToErp($supplierId);
$this->response(0, '修改状态成功');
}
//检查供应商名称是否相似或者重复
public function CheckSupplierName($request)
{
$supplierName = trim($request->get('supplier_name'));
$taxNumber = $request->get('tax_number');
$supplierId = $request->get('supplier_id');
$region = $request->get('region');
$model = new SupplierChannelModel();
//如果是编辑操作,则要忽略非当前
if ($supplierId) {
$originSupplierName = $model->where('supplier_id', $supplierId)->value('supplier_name');
$existedSupplierName = $model->where('supplier_name', $supplierName)->where('supplier_name', '!=',
$originSupplierName)
->value('supplier_name');
if ($supplierName !== $existedSupplierName) {
} else {
$this->response(-1, '供应商已存在,请重新输入或者咨询审批人');
}
} else {
$existedSupplierName = $model->where('supplier_name', $supplierName)
->value('supplier_name');
if ($existedSupplierName) {
$this->response(-1, '供应商已存在,请重新输入或者咨询审批人');
}
}
$company = [];
//判断用户选的地区是不是需要校验的,不需要直接返回即可
if (in_array($region, config('field.NeedCheckCompanyRegion'))) {
$regionType = $region == SupplierChannelModel::REGION_CN ? 1 : 2;
//还要去请求天眼查获取资料
$company = (new CompanyService())->getCompanyInfo($supplierName, $taxNumber, $regionType);
if (!$company) {
$this->response(-2, '公司未进行工商注册,请修改后重新提交');
}
} else {
$this->response(-2, '供应商跳过天眼查校验');
}
$this->response(0, '供应商名称合理', $company);
}
//审核供应商
public function AuditSupplier($request)
{
$status = $request->get('status');
$rejectReason = $request->get('reject_reason');
$rejectReason = trim($rejectReason);
if (empty($status)) {
$this->response(-1, '必须选择一个审核意见');
}
if ($status == 3 && empty($rejectReason)) {
$this->response(-1, '不同意时必须填写原因');
}
$supplierId = $request->get('supplier_id');
$service = new SupplierAuditService();
$result = $service->auditSupplier($supplierId, $status, $rejectReason);
if (!$result) {
$this->response(-1, '审核失败');
}
$this->response(0, '审核成功');
}
//分配渠道开发员
public function AllocatePurchaseUser($request)
{
$purchaseUid = $request->get('purchase_uid');
$supplierId = $request->get('supplier_id');
if (empty($purchaseUid)) {
$this->response(-1, '渠道开发员不能为空');
}
$adminService = new AdminUserService();
$check = $adminService->checkIsResignedByCodeId($purchaseUid);
if ($check) {
$this->response(-1, '该渠道开发员已经离职,请选择其他人员');
}
$service = new SupplierService();
$result = $service->allocatePurchaseUser($supplierId, $purchaseUid);
if (!$result) {
$this->response(-1, '分配渠道开发员失败');
}
$this->response(0, '分配渠道开发员成功');
}
//批量修改渠道开发员
//修改后自动触发转正,资料不完善,进入待审核
public function BatchAllocatePurchaseUser($request)
{
$purchaseUid = $request->get('purchase_uid');
$supplierIds = $request->get('supplier_ids');
$supplierIds = explode(',', $supplierIds);
$supplierService = new SupplierService();
if (empty($purchaseUid)) {
$this->response(-1, '请选择渠道开发员');
}
$supplierService->batchAllocatePurchaseUser($supplierIds, $purchaseUid);
$this->response(0, '批量分配渠道员成功');
}
//分配采购员(支持批量操作)
public function AllocateChannelUser($request)
{
$channelUid = $request->get('channel_uid');
$supplierId = $request->get('supplier_id');
if (empty($channelUid)) {
$this->response(-1, '采购员不能为空');
}
$adminService = new AdminUserService();
$check = $adminService->checkIsResignedByCodeId($channelUid);
if ($check) {
$this->response(-1, '该采购员已经离职,请选择其他人员');
}
$model = new SupplierChannelModel();
$supplier = $model->where('supplier_id', $supplierId)->first();
$supplier = $supplier ? $supplier->toArray() : [];
$preChannelUid = $supplier['channel_uid'];
$preChannelUid = explode(',', $preChannelUid);
//如果之前已经存在对应的采购,直接返回错误
if (in_array($channelUid, $preChannelUid)) {
$this->response(-1, '此采购员已经存在,请重新选择');
}
$service = new SupplierService();
$result = $service->allocateChannelUser($supplierId, $channelUid);
if (!$result) {
$this->response(-1, '添加采购员失败');
}
$this->response(0, '添加采购员成功');
}
//分配采购员(支持批量操作)
public function BatchAllocateChannelUser($request)
{
$channelUid = $request->get('channel_uid');
$supplierIds = $request->get('supplier_ids');
if (empty($channelUid)) {
$this->response(-1, '采购员不能为空');
}
$adminService = new AdminUserService();
$check = $adminService->checkIsResignedByCodeId($channelUid);
if ($check) {
$this->response(-1, '该采购员已经离职,请选择其他人员');
}
$supplierIds = explode(',', $supplierIds);
foreach ($supplierIds as $supplierId) {
$service = new SupplierService();
$result = $service->allocateChannelUser($supplierId, $channelUid);
if (!$result) {
$this->response(-1, '添加采购员失败');
}
}
$this->response(0, '添加采购员成功');
}
//转正供应商
public function ChangeSupplierIsType($request)
{
$supplierId = $request->get('supplier_id');
$isType = $request->get('is_type');
$service = new SupplierService();
$result = $service->changeSupplierIsType($supplierId, $isType);
if (!$result) {
$this->response(-1, '转正供应商失败');
}
$this->response(0, '转正供应商成功');
}
//发送金蝶同步请求
public function SyncToErp($request)
{
$supplierId = $request->get('supplier_id');
$service = new SyncSupplierService();
$service->syncSupplierToErp($supplierId);
$this->response(0, '已发送同步请求');
}
//发送一体化同步请求
public function SyncToUnited($request)
{
$supplierId = $request->get('supplier_id');
$supplier = SupplierChannelModel::where('supplier_id', $supplierId)->first()->toArray();
if (in_array($supplier['supplier_name'], config('field.SkipChangeSupplierTypeNames'))) {
$service = new SyncSupplierService();
$service->syncSupplierToUnited($supplierId);
$this->response(0, '属于代购供应商,直接发送同步');
}
//去调取天眼查数据,有数据的话,更新
$companyInfo = (new CompanyService())->getCompanyInfo($supplier['supplier_name'], '');
//先判断是否是标准添加,如果标准添加,国内地区,但是没有税号的,不允许同步
if ($supplier['is_standard_add'] == 1 && $supplier['region'] == SupplierChannelModel::REGION_CN && !$supplier['tax_number'] && $companyInfo) {
$taxNumber = $companyInfo['tax_number'];
SupplierChannelModel::where('supplier_id', $supplierId)->update([
'tax_number' => $taxNumber,
]);
// $this->response(-1, '该供应商属于国内,但是没有税号,不允许同步');
}
if ($supplier['is_standard_add'] == 1 && $supplier['region'] != SupplierChannelModel::REGION_CN) {
// if (empty($companyInfo)) {
// $this->response(-1, '该供应商属于海外,但是无法查询公司信息,不允许同步');
// }
}
$service = new SyncSupplierService();
$service->syncSupplierToUnited($supplierId);
$this->response(0, '已发送同步请求');
}
//设置云芯账号
public function SetYunxinChannelUid($request)
{
$supplierId = $request->get('supplier_id');
$channelUid = $request->get('yunxin_channel_uid');
$model = new SupplierChannelModel();
$result = $model->where('supplier_id', $supplierId)->update([
'yunxin_channel_uid' => $channelUid,
]);
if ($result) {
//修改供应商状态
$model->where('supplier_id', $supplierId)->update([
'update_time' => time(),
'status' => SupplierChannelModel::STATUS_PENDING,
]);
$adminService = new AdminUserService();
$user = $adminService->getAdminUserInfoByCodeId($channelUid);
$logService = new LogService();
$logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '设置SKU采购员', '设置SKU采购员为 : ' . $user['name']);
$this->response(0, '设置云芯采购成功');
} else {
$this->response(-1, '设置云芯采购失败');
}
}
//检查能否能申请审核
public function CheckCanApplyInReview($request)
{
$supplierIds = $request->get('supplier_ids');
if (!$supplierIds) {
$this->response(-1, '请选择供应商');
}
$supplierIds = explode(',', $supplierIds);
$supplierAuditService = new SupplierAuditService();
$check = $supplierAuditService->CheckCanApplyInReview($supplierIds);
if ($check === true) {
$this->response(0, '可以申请审核');
} else {
$this->response(-1, $check);
}
}
//检查能否审核(只有最后一次修改人和当前审核人是属于同部门的,才能审核)
public function CheckCanAuditSupplier($request)
{
$supplierId = $request->get('supplier_id');
$supplierAuditService = new SupplierAuditService();
$check = $supplierAuditService->CheckCanAuditSupplier($supplierId);
if ($check) {
$this->response(0, '可以审核');
} else {
$this->response(-1, '无法进行审核操作,因为该供应商最后(修改人/创建人)和您不属于同一个部门');
}
}
//拉黑供应商
public function BlockSupplier($request)
{
$supplierId = $request->get('supplier_id');
$blockReason = $request->get('block_reason');
if (empty(trim($blockReason))) {
$this->response(-1, '必须填写拉黑原因');
}
if (mb_strlen($blockReason) > 200) {
$this->response(-1, '拉黑原因不能超过200字');
}
$channelModel = new SupplierChannelModel();
$result = $channelModel->where('supplier_id', $supplierId)->update([
'block_reason' => $request->get('block_reason'),
'status' => $channelModel::STATUS_BLOCK,
'update_time' => time(),
]);
if ($result) {
$logService = new LogService();
$logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '拉黑', '拉黑供应商');
$this->response(0, '拉黑成功');
} else {
$this->response(-1, '拉黑操作失败');
}
}
//取消拉黑供应商
public function CancelBlockSupplier($request)
{
$supplierId = $request->get('supplier_id');
$channelModel = new SupplierChannelModel();
$result = $channelModel->where('supplier_id', $supplierId)->update([
'status' => SupplierChannelModel::STATUS_IN_REVIEW,
'update_time' => time(),
]);
if ($result) {
$logService = new LogService();
$logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '取消拉黑', '取消拉黑供应商');
$this->response(0, '取消拉黑成功');
} else {
$this->response(-1, '取消拉黑操作失败');
}
}
//取消禁用供应商
public function CancelDisableSupplier($request)
{
$supplierId = $request->get('supplier_id');
$channelModel = new SupplierChannelModel();
//先找出原来的状态
$redis = new RedisModel();
$previousStatus = $redis->hget('supplier_status_before_disable', $supplierId);
$result = $channelModel->where('supplier_id', $supplierId)->update([
'status' => $previousStatus,
'update_time' => time(),
]);
if ($result) {
//删除redis状态
$redis->hdel('supplier_status_before_disable', $supplierId);
$logService = new LogService();
$logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '取消禁用', '取消禁用供应商');
$this->response(0, '取消禁用成功');
} else {
$this->response(-1, '取消禁用操作失败');
}
}
//批量申请审核供应商
public function BatchApplyInReviewSupplier($request)
{
$supplierIds = $request->get('supplier_id');
$supplierTypeList = $request->get('supplier_type');
$applyAuditReasonList = $request->get('apply_audit_reason');
$auditData = [];
foreach ($supplierIds as $key => $supplierId) {
$supplierType = array_get($supplierTypeList, $key);
$reason = array_get($applyAuditReasonList, $key);
if ($supplierType == SupplierChannelModel::SUPPLIER_TYPE_TEMPORARY && !$reason) {
$this->response(-1, '临时类型的供应商申请审核必须填写申请原因');
}
$errorMsg = SupplierService::checkSupplierFieldCompleted($supplierId);
if ($errorMsg) {
$this->response(-1, $errorMsg);
}
$auditData[] = [
'supplier_id' => $supplierId,
'apply_audit_reason' => $reason,
];
}
$supplierService = new SupplierService();
$supplierService->batchApplyInReviewSupplier($auditData);
$this->response(0, '申请审核成功');
}
//批量新增标签
public function BatchAddTags($request)
{
$supplierIds = $request->get('supplier_ids');
$systemTags = $request->get('system_tags');
$customTags = $request->get('custom_tags');
$supplierIds = $supplierIds ? explode(',', $supplierIds) : [];
$systemTags = $systemTags ? explode(',', $systemTags) : [];
$customTags = $customTags ? explode(',', $customTags) : [];
$supplierService = new SupplierService();
if (empty($systemTags) && empty($customTags)) {
$this->response(-1, '请设置标签');
}
$supplierService->batchAddTags($supplierIds, $systemTags, $customTags);
$this->response(0, '批量新增标签成功');
}
}