ERPShipmentAction.cs
30.4 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
using System;
using System.Collections.Generic;
using System.Data;
using Infrastructure;
using Quartz;
using WebApp;
using WebRepository;
using System.Linq;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal;
namespace WebMvc
{
/// <summary>
/// 铜箔出库单分解之前想ERP申请数量和单据号,通过后标记
/// </summary>
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class ERPShipmentAction
{
private string ConnString { set; get; }
IJobExecutionContext Context { set; get; }
public ERPShipmentAction(string _ConnString, IJobExecutionContext _Context)
{
ConnString = _ConnString;
Context = _Context;
}
public void Execute(JobContainer jobContainer)
{
UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
string str = "";
try
{
List<ApiShipmentHeader> ApiShipmentHeaders = _unitWork.Find<ApiShipmentHeader>(n => n.Status == 0 && (n.Type == "CallCopperFoil" || n.Type == "6")).OrderBy(n => n.CreateTime).ToList();
foreach (var ApishipmentHeader in ApiShipmentHeaders)
{
ApiShipmentDetail apiShipmentDetail = _unitWork.Find<ApiShipmentDetail>(n => n.SourceCode == ApishipmentHeader.SourceCode).FirstOrDefault();
if (apiShipmentDetail == null)
{
ApishipmentHeader.Status = 2;
_unitWork.Update(ApishipmentHeader);
continue;
}
str = selectInv(ApishipmentHeader, apiShipmentDetail);
if (str != "")
{
ApiInterfaceLog Apiinter = _unitWork.Find<ApiInterfaceLog>(n => n.TaskNo == apiShipmentDetail.SourceCode).FirstOrDefault();
if (Apiinter != null)
{
//回传单头
InAndBackModel inAndBackModel = new InAndBackModel();
inAndBackModel.State = "E";
inAndBackModel.MO = apiShipmentDetail.Lot;
inAndBackModel.PN = apiShipmentDetail.MaterialCode;
inAndBackModel.TaskNumber = Apiinter.TaskNo;
//回传单身
List<InAndBackDetailModel> inAndBackDetails = new List<InAndBackDetailModel>();
InAndBackDetailModel inAndBackDetail = new InAndBackDetailModel();
inAndBackDetail.LWH = apiShipmentDetail.LWH;
inAndBackDetail.LWHSL = apiShipmentDetail.LWHSL;
inAndBackDetail.Quantity = (decimal)apiShipmentDetail.Qty;
inAndBackDetail.Lot = apiShipmentDetail.Lot;
inAndBackDetail.Supllier = "";
inAndBackDetail.Barcode = apiShipmentDetail.Project;
inAndBackDetail.Uow = "";
inAndBackDetails.Add(inAndBackDetail);
inAndBackModel.DeliveryResults = inAndBackDetails;
ApiRequest apiRequest = new ApiRequest("MES");
MesResponse MESesponse = apiRequest.TokenPost<MesResponse>(JsonHelper.Instance.Serialize(inAndBackModel), "WmsService/ReportMaterialDistribution", "出库异常回传");
if (MESesponse.Status == "S")
{
Apiinter.Status = 4;
Apiinter.Message = str;
Apiinter.UpdateBy = "ERPShipmentAction";
_unitWork.Update(Apiinter);
ApiInterfaceLog Back = new ApiInterfaceLog();
Back.TaskNo = Apiinter.TaskNo;
Back.Request = inAndBackModel.ToJson();
Back.Type = "出库ERP查询异常回传成功";
Back.Status = 4;
Back.Message = str;
Back.CreateTime = DateTime.Now;
Back.CreateBy = "ERPShipmentAction";
_unitWork.Add(Back);
}
else
{
ApiInterfaceLog Back = new ApiInterfaceLog();
Back.TaskNo = Apiinter.TaskNo;
Back.Request = inAndBackModel.ToJson();
Back.Type = "出库异常回传失败";
Back.Status = 4;
Back.Message = MESesponse.Status+","+ MESesponse.ErrorMsg+"," + DateTime.Now;
Back.CreateTime = DateTime.Now;
Back.CreateBy = "ERPShipmentAction";
_unitWork.Add(Back);
}
}
ApishipmentHeader.Status = 2;
_unitWork.Update(ApishipmentHeader);
apiShipmentDetail.Error = str;
_unitWork.Update(apiShipmentDetail);
continue;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
//向ERP查询物料数量
public string selectInv(ApiShipmentHeader ApishipmentHeader, ApiShipmentDetail apiShipmentDetail)
{
string strError = "";
UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
try
{
//向ERP申请查询库存物料
StationRoadway stationRoadway = _unitWork.Find<StationRoadway>(n => n.StationCode == ApishipmentHeader.Station).FirstOrDefault();
var Roadway = 0;
if (stationRoadway != null)
{
Roadway = (int)stationRoadway.RoadWay;
}
Inventory inventory = new Inventory();
//铜箔出库
if (ApishipmentHeader.Type == "CallCopperFoil")
{
//根据出库子表数据创建任务主表数据:物料编码加供应商,一个出库单据只出一个托盘
inventory = _unitWork.Find<Inventory>(n => n.MaterialCode == apiShipmentDetail.MaterialCode && n.TaskStatus == InventoryTaskType.无任务 && n.Supplier == ApishipmentHeader.ShipTo && n.Retreat.Contains("退料")).OrderBy(n => n.CreateTime).FirstOrDefault();
if (inventory == null)
{
inventory = _unitWork.Find<Inventory>(n => n.MaterialCode == apiShipmentDetail.MaterialCode && n.TaskStatus == InventoryTaskType.无任务 && n.Supplier == ApishipmentHeader.ShipTo).OrderBy(n => n.CreateTime).FirstOrDefault();
}
}
else if (ApishipmentHeader.Type == "6")
{
//指定物料二维码出库
inventory = _unitWork.Find<Inventory>(n => n.QrCode == apiShipmentDetail.Project && n.TaskStatus == InventoryTaskType.无任务 && n.WarehouseType == "TB_WareCell").OrderBy(n => n.CreateTime).FirstOrDefault();
}
if (inventory != null)
{
//建立回传 XmlDocument
int i_doc_isgood = 0;
XmlDocument doc = new XmlDocument();
var config = AppSettingsJson.GetAppSettings();
//加载模板
doc.Load(config.GetSection("ErpInvService:Url").Value);
ConfigXmlDocument Config = new ConfigXmlDocument();
ServiceReference9.TIPTOPServiceGateWayPortTypeClient selectInv = new ServiceReference9.TIPTOPServiceGateWayPortTypeClient();
XmlNodeList xmlNodeLists = doc.SelectSingleNode(string.Format("Request/RequestContent/Document/RecordSet/Master/Record")).ChildNodes;
//账套
XmlNodeList Organizations = doc.SelectNodes("//Organization");
foreach (XmlElement Organization in Organizations)
{
Organization.SetAttribute("name", ApishipmentHeader.Organization);
}
foreach (XmlElement xmlNodeList in xmlNodeLists)
{
//物料二维码
if (xmlNodeList.GetAttribute("name") == "gls_ag01")
{
xmlNodeList.SetAttribute("value", inventory.QrCode);
}
//料号
else if (xmlNodeList.GetAttribute("name") == "gls_ag06")
{
xmlNodeList.SetAttribute("value", inventory.MaterialCode);
}
//仓库
else if (xmlNodeList.GetAttribute("name") == "gls_ag02")
{
xmlNodeList.SetAttribute("value", apiShipmentDetail.WH);
}
//库位
else if (xmlNodeList.GetAttribute("name") == "gls_ag03")
{
xmlNodeList.SetAttribute("value", apiShipmentDetail.WHSL);
}
//批号
else if (xmlNodeList.GetAttribute("name") == "gls_ag04")
{
xmlNodeList.SetAttribute("value", inventory.Lot);
}
}
//回传数据
var response = selectInv.CxwmsKucun(doc.InnerXml);
//处理调用后回传数据
Config.LoadXml(response);
XmlElement element = (XmlElement)Config.SelectSingleNode("//Status");
string code = element.GetAttribute("code");
string msg = element.GetAttribute("description");
//string code = "0";
//string msg = "";
if (code == "0")
{
string Qrcode = "";
string materialCode = "";
//仓库
string Wh = "";
//储位
string Whsl = "";
//大批号
string Blot = "";
string qty = "";
string Bqty = "";
//与ERP返回库存数据进行对比
XmlDocument Inv = new XmlDocument();
Inv.LoadXml(msg);
XmlNodeList Invlists = Inv.SelectSingleNode(string.Format("Record/Info")).ChildNodes;
foreach (XmlElement Invlist in Invlists)
{
if (Invlist.Name == "gls_ag01")
{
Qrcode = Invlist.InnerText;
}
else if (Invlist.Name == "gls_ag06")
{
materialCode = Invlist.InnerText;
}
else if (Invlist.Name == "gls_ag02")
{
Wh = Invlist.InnerText;
}
else if (Invlist.Name == "gls_ag03")
{
Whsl = Invlist.InnerText;
}
else if (Invlist.Name == "gls_ag04")
{
Blot = Invlist.InnerText;
}
else if (Invlist.Name == "gls_ag05")
{
qty = Invlist.InnerText;
}
else if (Invlist.Name == "img10")
{
Bqty = Invlist.InnerText;
}
}
if (inventory.Qty > decimal.Parse(qty))
{
InterfaceLog invNumInterfaceLog = new InterfaceLog();
invNumInterfaceLog.TaskNo = ApishipmentHeader.SourceCode;
invNumInterfaceLog.Request = doc.InnerXml;
invNumInterfaceLog.Type = "对比数量失败";
invNumInterfaceLog.Status = 4;
invNumInterfaceLog.Message = "库存ID" + inventory.Id + "的物料数量与ERP中的不等,ERP为" + msg;
invNumInterfaceLog.CreateTime = DateTime.Now;
_unitWork.Add(invNumInterfaceLog);
strError = "库存中" + inventory.Id + "的物料数量与ERP中的不等,ERP中的数量为" + qty;
return strError;
}
InterfaceLog invInterfaceLog = new InterfaceLog();
invInterfaceLog.TaskNo = ApishipmentHeader.SourceCode;
invInterfaceLog.Request = doc.InnerXml;
invInterfaceLog.Type = "出库对比数量成功";
invInterfaceLog.Status = 1;
invInterfaceLog.Message = msg;
invInterfaceLog.CreateTime = DateTime.Now;
_unitWork.Add(invInterfaceLog);
string Station = apiShipmentDetail.LWHSL;
//转换站台
switch (apiShipmentDetail.LWHSL)
{
case "ProductStationD01":
Station = "DT1";
break;
case "ProductStationD02":
Station = "DT2";
break;
case "ProductStationD03":
Station = "DT3";
break;
case "ProductStationD04":
Station = "DT4";
break;
case "ProductStationD05":
Station = "DT5";
break;
case "ProductStationD06":
Station = "DT6";
break;
case "ProductStationD07":
Station = "DT7";
break;
case "ProductStationD08":
Station = "DT8";
break;
default:
break;
}
//申请ERP单号
//建立回传 XmlDocument
int i_doc_isgoodOut = 0;
XmlDocument docOut = new XmlDocument();
var configOut = AppSettingsJson.GetAppSettings();
//加载模板
docOut.Load(configOut.GetSection("ErpSourceCodeService:Url").Value);
//建立中转的XmlDocument(要以模板创建新的xml进行回传)
XmlDocument doc_isgood_copyOut = new XmlDocument();
ConfigXmlDocument ConfigOut = new ConfigXmlDocument();
ServiceReference9.TIPTOPServiceGateWayPortTypeClient OutBack = new ServiceReference9.TIPTOPServiceGateWayPortTypeClient();
//账套
XmlNodeList OrganizationsOut = docOut.SelectNodes("//Organization");
foreach (XmlElement OrganizationOut in OrganizationsOut)
{
OrganizationOut.SetAttribute("name", ApishipmentHeader.Organization);
}
XmlElement xmlElement_Document = doc_isgood_copyOut.CreateElement("Document");
doc_isgood_copyOut.AppendChild(xmlElement_Document);
List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == inventory.ContainerCode).ToList();
foreach (var inventorie in inventories)
{
if (string.IsNullOrEmpty(inventorie.BLot))
{
inventorie.BLot = Blot;
_unitWork.Update(inventorie);
}
//创建RecordSet标签
XmlElement xmlElement_RecordSet = doc_isgood_copyOut.CreateElement("RecordSet");
xmlElement_RecordSet.SetAttribute("id", (++i_doc_isgoodOut).ToString());
XmlNode node_RecordSet = doc_isgood_copyOut.SelectSingleNode("//Document").AppendChild(xmlElement_RecordSet);
//添加表头
XmlElement xmlElement_Master = doc_isgood_copyOut.CreateElement("Master");
xmlElement_Master.SetAttribute("name", "Master");
XmlNode node_Master = node_RecordSet.AppendChild(xmlElement_Master);
XmlElement xmlElement_HaerRecord = doc_isgood_copyOut.CreateElement("Record");
XmlNode node_HaerRecord = node_Master.AppendChild(xmlElement_HaerRecord);
//添加表单
XmlElement xmlElement_Detail = doc_isgood_copyOut.CreateElement("Detail");
xmlElement_Detail.SetAttribute("name", "report_detail");
node_Master = node_RecordSet.AppendChild(xmlElement_Detail);
XmlElement xmlElement_DeatilRecord = doc_isgood_copyOut.CreateElement("Record");
XmlNode node_DetailRecord = node_Master.AppendChild(xmlElement_DeatilRecord);
//获取表头数据
XmlNode node_Head = docOut.SelectSingleNode(string.Format("Request/RequestContent/Document/RecordSet/Master/Record"));
//获取表单
XmlNode node_Detail = docOut.SelectSingleNode(string.Format("Request/RequestContent/Document/RecordSet/Detail/Record"));
node_HaerRecord.InnerXml = node_Head.InnerXml;
node_DetailRecord.InnerXml = node_Detail.InnerXml;
//修改表头内容
XmlNodeList nodes_Hears = node_HaerRecord.ChildNodes;
foreach (XmlElement nodes_Hear in nodes_Hears)
{
//叫料类型
if (nodes_Hear.GetAttribute("name") == "type")
{
nodes_Hear.SetAttribute("value", "1");
}
//MES单号
else if (nodes_Hear.GetAttribute("name") == "wo_no")
{
nodes_Hear.SetAttribute("value", ApishipmentHeader.SourceCode);
}
//调拨单别
else if (nodes_Hear.GetAttribute("name") == "imm01")
{
nodes_Hear.SetAttribute("value", ApishipmentHeader.ErpListType);
}
//调拨日期
else if (nodes_Hear.GetAttribute("name") == "imm02")
{
nodes_Hear.SetAttribute("value", DateTime.Now.ToString());
}
//部门
else if (nodes_Hear.GetAttribute("name") == "imm14")
{
nodes_Hear.SetAttribute("value", "0703");
}
//申请人
else if (nodes_Hear.GetAttribute("name") == "imm16")
{
nodes_Hear.SetAttribute("value", "tiptop");
}
//是否过账
else if (nodes_Hear.GetAttribute("name") == "is_posting")
{
nodes_Hear.SetAttribute("value", "N");
}
}
//修改表单内容
XmlNodeList nodes_Deatils = node_DetailRecord.ChildNodes;
foreach (XmlElement nodes_Deatil in nodes_Deatils)
{
if (nodes_Deatil.GetAttribute("name") == "imn03")
{
nodes_Deatil.SetAttribute("value", inventorie.MaterialCode);
}
//拨出仓库(起始点)
else if (nodes_Deatil.GetAttribute("name") == "imn04")
{
nodes_Deatil.SetAttribute("value", apiShipmentDetail.WH);
}
//拨出库位
else if (nodes_Deatil.GetAttribute("name") == "imn05")
{
nodes_Deatil.SetAttribute("value", apiShipmentDetail.WHSL);
}
//拨出批号
else if (nodes_Deatil.GetAttribute("name") == "imn06")
{
nodes_Deatil.SetAttribute("value", Blot);
}
//库存单位
else if (nodes_Deatil.GetAttribute("name") == "imn09")
{
nodes_Deatil.SetAttribute("value", inventorie.Unit);
}
//拨出数量
else if (nodes_Deatil.GetAttribute("name") == "imn10")
{
nodes_Deatil.SetAttribute("value", inventorie.Qty.ToString());
}
//拨入仓库(目的地)
else if (nodes_Deatil.GetAttribute("name") == "imn15")
{
nodes_Deatil.SetAttribute("value", apiShipmentDetail.LWH);
}
//拨入库位
else if (nodes_Deatil.GetAttribute("name") == "imn16")
{
nodes_Deatil.SetAttribute("value", Station);
}
//拨入批号
else if (nodes_Deatil.GetAttribute("name") == "imn17")
{
nodes_Deatil.SetAttribute("value", Blot);
}
//库存单位
else if (nodes_Deatil.GetAttribute("name") == "imn20")
{
nodes_Deatil.SetAttribute("value", inventorie.Unit);
}
//单位转换率
else if (nodes_Deatil.GetAttribute("name") == "imn21")
{
nodes_Deatil.SetAttribute("value", "");
}
//转换后数量
else if (nodes_Deatil.GetAttribute("name") == "imn22")
{
nodes_Deatil.SetAttribute("value", "");
}
//拨出条码
else if (nodes_Deatil.GetAttribute("name") == "barcode_out")
{
nodes_Deatil.SetAttribute("value", inventorie.QrCode);
}
//拨入条码
else if (nodes_Deatil.GetAttribute("name") == "barcode_in")
{
nodes_Deatil.SetAttribute("value", inventorie.QrCode);
}
//厂商
else if (nodes_Deatil.GetAttribute("name") == "sfa36")
{
nodes_Deatil.SetAttribute("value", inventorie.Supplier);
}
//
else if (nodes_Deatil.GetAttribute("name") == "checkud")
{
nodes_Deatil.SetAttribute("value", "");
}
//备注
else if (nodes_Deatil.GetAttribute("name") == "remark")
{
nodes_Deatil.SetAttribute("value", "");
}
}
}
docOut.SelectSingleNode("//RequestContent").RemoveAll();
docOut.SelectSingleNode("//RequestContent").InnerXml = doc_isgood_copyOut.InnerXml;
//回传数据
var responseOut = OutBack.CreateMesAimt324(docOut.InnerXml);
//处理调用后回传数据
ConfigOut.LoadXml(responseOut);
XmlElement elementOut = (XmlElement)ConfigOut.SelectSingleNode("//Status");
string codeOut = elementOut.GetAttribute("code");
string msgOut = elementOut.GetAttribute("description");
//string codeOut = "0";
//string msgOut = "";
//申请单号成功
if (codeOut == "0")
{
var erpSourcecode = msgOut.Split("/")[0];
ApishipmentHeader.Status = 1;
ApishipmentHeader.ContainerCode = inventory.ContainerCode;
ApishipmentHeader.ErpSourceCode = erpSourcecode;
_unitWork.Update(ApishipmentHeader);
InterfaceLog SourceCodeInterfaceLog = new InterfaceLog();
SourceCodeInterfaceLog.TaskNo = ApishipmentHeader.SourceCode;
SourceCodeInterfaceLog.Request = docOut.InnerXml;
SourceCodeInterfaceLog.Type = "出库申请单号成功";
SourceCodeInterfaceLog.Status = 1;
SourceCodeInterfaceLog.Message = msgOut;
SourceCodeInterfaceLog.CreateTime = DateTime.Now;
_unitWork.Add(SourceCodeInterfaceLog);
}
//申请单号失败
else
{
InterfaceLog SourceCodeInterfaceLog = new InterfaceLog();
SourceCodeInterfaceLog.TaskNo = ApishipmentHeader.SourceCode;
SourceCodeInterfaceLog.Request = docOut.InnerXml;
SourceCodeInterfaceLog.Type = "出库申请单号失败";
SourceCodeInterfaceLog.Status = 4;
SourceCodeInterfaceLog.Message = ConfigOut.InnerXml;
SourceCodeInterfaceLog.CreateTime = DateTime.Now;
_unitWork.Add(SourceCodeInterfaceLog);
strError = ApishipmentHeader.SourceCode + "单据向ERP申请单号失败!!";
return strError;
}
}
//查询数量失败
else
{
InterfaceLog invInterfaceLog = new InterfaceLog();
invInterfaceLog.TaskNo = ApishipmentHeader.SourceCode;
invInterfaceLog.Request = doc.InnerXml;
invInterfaceLog.Type = "对比数量失败";
invInterfaceLog.Status = 4;
invInterfaceLog.Message = Config.InnerXml;
invInterfaceLog.CreateTime = DateTime.Now;
_unitWork.Add(invInterfaceLog);
strError = "库存中"+ inventory.Id + "的物料在ERP中不存在!!";
return strError;
}
}
else
{
strError = "未找到符合"+ ApishipmentHeader.SourceCode + "单据的库存";
return strError;
}
}
catch (Exception ex)
{
strError = ex.Message;
return strError;
}
return strError;
}
}
}