SyncSupplierService.php
8.41 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
<?php
namespace App\Http\Services;
//后台用户相关信息服务
use App\Model\RedisModel;
use App\Model\SupplierAttachmentsModel;
use App\Model\SupplierChannelModel;
use App\Model\SupplierContactModel;
use App\Model\SupplierSyncModel;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class SyncSupplierService
{
//同步供应商到erp
public function syncSupplierToErp($supplierId)
{
//先去获取供应商的信息
$supplierModel = new SupplierChannelModel();
$supplier = $supplierModel->where('supplier_id', $supplierId)->first();
if (empty($supplier)) {
return false;
}
//只推送禁用和启用状态的
if (!in_array($supplier['status'], [2, -2, -3])) {
return true;
}
//先去sync表查询记录是否存在,不存在新增,存在修改
$supplierSyncModel = new SupplierSyncModel();
$exist = $supplierSyncModel->where('supplier_id', $supplierId)->count();
if (!empty($exist)) {
$supplierSyncModel->where('supplier_id', $supplierId)->update([
'erp_sync_status' => 0,
'update_time' => time(),
'last_update_source' => 2,
]);
} else {
$supplierSyncModel->insert([
'erp_sync_status' => 0,
'supplier_id' => $supplierId,
'add_time' => time(),
'last_update_source' => 2,
]);
}
$contactModel = new SupplierContactModel();
$contact = $contactModel->where('supplier_id', $supplier['supplier_id'])->orderBy('contact_id', 'asc')->first();
$purchases = $this->getUserCodeSn($supplier['channel_uid']);
$message = [
'name' => $supplier['supplier_name'],
'PTID' => $supplier['supplier_code'],
'STATUS' => $supplier['status'] == 2 ? 1 : 2,
];
if (!empty($supplier['supplier_group'])) {
$message['groupNumber'] = array_get(config('sync.SyncSupplierType'), $supplier['supplier_group'], '');
}
if (!empty($supplier['tax_number'])) {
$message['taxRegisterNo'] = $supplier['tax_number'];
}
if (!empty($purchases)) {
$message['personName'] = $purchases;
}
if (!empty($supplier['supplier_address'])) {
$message['address'] = $supplier['supplier_address'];
}
if (!empty(array_get($contact, 'supplier_consignee'))) {
$message['contactPerson'] = array_get($contact, 'supplier_consignee');
}
if (!empty(array_get($contact, 'supplier_telephone'))) {
$message['phone'] = array_get($contact, 'supplier_telephone');
}
if (!empty($supplier['currency'])) {
$message['currencyNumber'] = array_get(config('sync.SyncCurrencyType'), $supplier['currency']);
}
$conn = new AMQPStreamConnection(config('database.connections.rabbitmq.host'),
config('database.connections.rabbitmq.port'),
config('database.connections.rabbitmq.login'),
config('database.connections.rabbitmq.password'));
$channel = $conn->channel();
$channel->queue_declare('supplier_sync', false, true, false, false);
$msg = new AMQPMessage(json_encode($message),
array('content_type' => 'text/plain'));
$result = $channel->basic_publish($msg, '', 'supplier_sync');
// $result = $channel->basic_publish($msg);
}
//获取采购人员列表
private function getUserCodeSn($purchaseUid)
{
$purchaseUid = explode(',', $purchaseUid);
if (empty($purchaseUid)) {
return [];
}
$adminIds = DB::connection()->table('lie_intracode')->whereIn('code_id', $purchaseUid)->pluck('admin_id');
return DB::connection()->table('user_info')->whereIn('userId', $adminIds)->pluck('code_sn');
}
/**
* 推送参数:
* {
* "system_id": "1",//来源系统id 1猎芯科技,2猎芯供应链,3crm
* "source_sn": "1", //源编码 各个系统标识公司的标识号
* "company_type": "1",//公司类型,1客户,2供应商
* "company_name": "测试公司", //公司名称
* "company_tax_no": "1234567",//公司税号
* "init_nature": "1234567",//来源公司初鉴性质
* "region": "1",//地区,1大陆,2海外
* "pi_file_url": "dasdsa" //pi附件地址
* }
*/
//同步供应商信息到一体化中心
public function syncSupplierToUnited($supplierId)
{
$supplier = SupplierChannelModel::where('supplier_id', $supplierId)->first()->toArray();
$fileUrl = SupplierAttachmentsModel::where('supplier_id', $supplierId)
->where('field_name', 'registration_certificate')
->value('file_url');
//构建需要同步的数据
$syncData = [
'system_id' => 1,
'source_sn' => (string)$supplier['supplier_id'],
'company_type' => 2,
'company_name' => $supplier['supplier_name'],
'company_tax_no' => $supplier['tax_number'],
'init_nature' => Arr::get(config('fixed.SupplierGroup'), $supplier['supplier_group']),
'region' => $supplier['region'] == SupplierChannelModel::REGION_CN ? 1 : 2,
'pi_file_url' => $fileUrl ?: '',
];
(new QueueDeliveryService())->push(QueueDeliveryService::PUSH_TYPE_SYNC_HTTP, '/sync/Company/syncCompany',
$syncData);
}
/**
* 推送参数为:
* {
* "group_code":"K0000013", //集团编码
* "source_sn":"1",//来源id,其实就是供应商id,因为新增的时候丢过去的就是供应商id
* "company_name":"\u6d4b\u8bd5\u516c\u53f8",//公司名
* "init_nature":"1234567",//初鉴性质
* "company_nature":"1234567"//公司真实性质
* "company_category":"普通供应商"//公司类别,只要不是普"通供应商",就是要拉黑的
* }
**/
//接收一体化系统处理好的供应商数据,可能是第一次新增返回的数据,也可能是一体化那边修改的数据需要同步到供应商
public function syncSupplierToUnitedResult($syncResult)
{
$groupCode = array_get($syncResult, 'group_code');
$sourceSn = array_get($syncResult, 'source_sn');
$companyCategory = array_get($syncResult, 'company_category', '');
$isEntity = array_get($syncResult, 'is_entity', 0);
$supplier = SupplierChannelModel::where('supplier_id', $sourceSn)->orWhere('group_code', $groupCode)->first();
$supplier = !empty($supplier) ? $supplier->toArray() : [];
$supplierId = $supplier['supplier_id'];
if ($companyCategory != '') {
//实体名单和黑名单都要拉黑,如果不属于黑名单,那么就要将状态改成审核中
if ($companyCategory != '普通供应商') {
$data['status'] = SupplierChannelModel::STATUS_BLOCK;
$data['block_reason'] = '一体化系统黑名单供应商';
} elseif ($isEntity) {
$data['status'] = SupplierChannelModel::STATUS_BLOCK;
$data['block_reason'] = '一体化系统黑名单供应商';
} else {
//判断原来是拉黑状态,才变成审核中,因为有可能不是修改公司类型,只是修改公司性质
if ($supplier['status'] == SupplierChannelModel::STATUS_BLOCK) {
$data['status'] = SupplierChannelModel::STATUS_IN_REVIEW;
}
}
}
$data['sync_united_status'] = SupplierChannelModel::SYNC_UNITED_STATUS_OK;
$data['group_code'] = $groupCode;
$data['company_nature'] = $syncResult['company_nature'];
$data['company_category'] = $companyCategory;
//$sourceSn不为空的话,就是代表这是第一次新增的供应商,所以会回传供应商id过来
if ($sourceSn) {
$data['supplier_name'] = $syncResult['company_name'];
$result = SupplierChannelModel::where('supplier_id', $supplierId)
->update($data);
if ($result) {
//再次同步供应商到金蝶
$this->syncSupplierToErp($supplierId);
}
return $result;
} else {
return SupplierChannelModel::where('group_code', $groupCode)
->update($data);
}
}
}