IReceiptApp.cs
28.2 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.Linq;
using Infrastructure;
using WebRepository;
namespace WebApp
{
/// <summary>
/// 入库单下发接口App
/// </summary>
public partial class IReceiptApp : ApiApp
{
public IRepository<ReceiptHeader> _app;
public IReceiptApp(IUnitWork unitWork, IAuth auth, BaseDBContext context, IRepository<ReceiptHeader> repository) : base(unitWork, auth, context)
{
_app = repository;
}
public string InsertReceipt(IReceiptModel receipts)
{
Response Response = new Response();
if (!CheckLogin())
{
Response.Code = 500;
Response.Status = false;
Response.Message = "请先登录!";
return JsonHelper.Instance.Serialize(Response);
}
try
{
#region 保存入库单接口主表
IreceiptHeader headdata = _unitWork.FindSingle<IreceiptHeader>(u => u.SourceCode.Equals(receipts.receiptHeader.SourceCode));
receipts.receiptHeader.ReceiveTime = DateTime.Now;
receipts.receiptHeader.Id = (headdata?.Id);
if (receipts.receiptHeader.Id == null)
{
if (receipts.receiptHeader.CreateBy == null)
{
receipts.receiptHeader.CreateBy = "wms";
receipts.receiptHeader.CreateTime = DateTime.Now;
}
_unitWork.Add(receipts.receiptHeader);
}
else
{
if (receipts.receiptHeader.UpdateBy == null)
{
receipts.receiptHeader.UpdateBy = "wms";
receipts.receiptHeader.UpdateTime = DateTime.Now;
}
_unitWork.Update(receipts.receiptHeader);
}
#endregion
#region 保存入库单主表
ReceiptHeader receiptHeader = _unitWork.FindSingle<ReceiptHeader>(u => u.SourceCode.Equals(receipts.receiptHeader.SourceCode));
if (receiptHeader != null)
{
if (receiptHeader.FirstStatus != ReceiptHeaderStatus.新建)
{
throw new Exception("单据进入订单池后,不允许修改!");
}
}
else
{
receiptHeader = new ReceiptHeader();
}
receiptHeader.Type = receipts.receiptHeader.Type;
receiptHeader.SourceCode = receipts.receiptHeader.SourceCode;
receiptHeader.Station = receipts.receiptHeader.Station ?? "";
receiptHeader.WarehouseType = receipts.receiptHeader.WarehouseType;
receiptHeader.SupplierId = receipts.receiptHeader.SupplierId;
receiptHeader.SupplierCode = receipts.receiptHeader.SupplierCode;
receiptHeader.SupplierName = receipts.receiptHeader.SupplierName;
receiptHeader.CreateTime = receipts.receiptHeader.CreateTime;
receiptHeader.CreateBy = receipts.receiptHeader.CreateBy;
receiptHeader.UpdateTime = receipts.receiptHeader.UpdateTime;
receiptHeader.UpdateBy = receipts.receiptHeader.UpdateBy;
receiptHeader.Code = string.IsNullOrEmpty(receiptHeader.Code) ? _unitWork.GetTaskNo(receipts.receiptHeader.Type) : receiptHeader.Code;
receiptHeader.UploadStatus = 0;
receiptHeader.FirstStatus = ReceiptHeaderStatus.新建;
receiptHeader.LastStatus = ReceiptHeaderStatus.新建;
receiptHeader.TotalLines = 0;
receiptHeader.TotalQty = 0;
if (receiptHeader.Id == null)
{
if (receiptHeader.CreateBy == null)
{
receiptHeader.CreateBy = "wms";
receiptHeader.CreateTime = DateTime.Now;
}
_unitWork.Add(receiptHeader);
}
else
{
if (receiptHeader.UpdateBy == null)
{
receiptHeader.UpdateBy = "wms";
receiptHeader.UpdateTime = DateTime.Now;
}
_unitWork.Update(receiptHeader);
}
#endregion
short? totalLines = 0;
decimal? qty = 0;
foreach (var item in receipts.receiptDetails)
{
try
{
#region 保存入库单接口子表
IreceiptDetail data = _context.Set<IreceiptDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
item.ReceiveTime = DateTime.Now;
item.Id = (data?.Id);
if (item.Id == null)
{
if (item.CreateBy == null)
{
item.CreateBy = "wms";
item.CreateTime = DateTime.Now;
}
_unitWork.Add(item);
}
else
{
if (item.UpdateBy == null)
{
item.UpdateBy = "wms";
item.UpdateTime = DateTime.Now;
}
_unitWork.Update(item);
}
#endregion
#region 保存入库单子表
ReceiptDetail receiptDetail = _context.Set<ReceiptDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
if (receiptDetail == null)
{
receiptDetail = new ReceiptDetail();
}
receiptDetail.SourceCode = item.SourceCode;
receiptDetail.Station = item.Station ?? "";
receiptDetail.MaterialCode = item.MaterialCode;
receiptDetail.Lot = item.Lot;
receiptDetail.InventoryStatus = item.InventoryStatus;
receiptDetail.Qty = item.Qty;
receiptDetail.CreateTime = item.CreateTime;
receiptDetail.CreateBy = item.CreateBy;
receiptDetail.UpdateTime = item.UpdateTime;
receiptDetail.UpdateBy = item.UpdateBy;
receiptDetail.ReceiptId = receiptHeader.Id;
receiptDetail.ReceiptCode = receiptHeader.Code;
receiptDetail.QtyDivided = 0;
receiptDetail.QtyCompleted = 0;
receiptDetail.Price = 0;
receiptDetail.Status = ReceiptHeaderStatus.新建;
if (receiptDetail.Id == null)
{
if (receiptDetail.CreateBy == null)
{
receiptDetail.CreateBy = "wms";
receiptDetail.CreateTime = DateTime.Now;
}
_unitWork.Add(receiptDetail);
}
else
{
if (receiptDetail.UpdateBy == null)
{
receiptDetail.UpdateBy = "wms";
receiptDetail.UpdateTime = DateTime.Now;
}
_unitWork.Update(receiptDetail);
}
#endregion
totalLines += 1;
qty += item.Qty;
}
catch (Exception ex)
{
#region 记录入库单接口子表错误信息
IreceiptDetail data = _context.Set<IreceiptDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
if (data != null)
{
data.ErrorMessage = ex.Message;
if (data.UpdateBy == null)
{
data.UpdateBy = "wms";
data.UpdateTime = DateTime.Now;
}
_unitWork.Update(data);
}
#endregion
Response.Code = 500;
Response.Status = false;
Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "SourceCode:" + item.SourceCode + ",MaterialCode:" + item.MaterialCode + "同步失败:" + ex.Message;
}
}
#region 更新入库单主表汇总信息
receiptHeader.TotalLines += (short)_unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == receiptHeader.Code).ToList().Count;
receiptHeader.TotalQty += _unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == receiptHeader.Code).Sum(x => x.Qty);
if (receiptHeader.UpdateBy == null)
{
receiptHeader.UpdateBy = "wms";
receiptHeader.UpdateTime = DateTime.Now;
}
_unitWork.Update(receiptHeader);
#endregion
}
catch (Exception ex)
{
#region 记录入库单接口主表错误信息
try
{
IreceiptHeader data = _context.Set<IreceiptHeader>().AsQueryable().Where(u => u.SourceCode.Equals(receipts.receiptHeader.SourceCode)).SingleOrDefault();
if (data != null)
{
data.ErrorMessage = ex.Message;
if (data.UpdateBy == null)
{
data.UpdateBy = "wms";
data.UpdateTime = DateTime.Now;
}
_unitWork.Update(data);
}
}
catch (Exception)
{
}
#endregion
Response.Code = 500;
Response.Status = false;
Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "同步失败:" + ex.Message;
}
return JsonHelper.Instance.Serialize(Response);
}
public Response InsertReceiptOut(IReceiptModel receipts)
{
Response Response = new Response();
using (var tran = _context.Database.BeginTransaction())
{
if (!CheckLogin())
{
Response.Code = 500;
Response.Status = false;
Response.Message = "请先登录!";
return Response;
}
try
{
#region 保存入库单接口主表
IreceiptHeader headdata = _unitWork.FindSingle<IreceiptHeader>(u => u.SourceCode.Equals(receipts.receiptHeader.SourceCode));
receipts.receiptHeader.ReceiveTime = DateTime.Now;
receipts.receiptHeader.Id = (headdata?.Id);
if (receipts.receiptHeader.Id == null)
{
if (receipts.receiptHeader.CreateBy == null)
{
receipts.receiptHeader.CreateBy = "wms";
receipts.receiptHeader.CreateTime = DateTime.Now;
}
_unitWork.Add(receipts.receiptHeader);
}
else
{
if (receipts.receiptHeader.UpdateBy == null)
{
receipts.receiptHeader.UpdateBy = "wms";
receipts.receiptHeader.UpdateTime = DateTime.Now;
}
_unitWork.Update(receipts.receiptHeader);
}
#endregion
#region 保存入库单主表
ReceiptHeader receiptHeader = _unitWork.FindSingle<ReceiptHeader>(u => u.SourceCode.Equals(receipts.receiptHeader.SourceCode));
if (receiptHeader != null)
{
if (receiptHeader.FirstStatus != ReceiptHeaderStatus.新建)
{
throw new Exception("单据进入订单池后,不允许修改!");
}
}
else
{
receiptHeader = new ReceiptHeader();
}
receiptHeader.Type = receipts.receiptHeader.Type;
receiptHeader.SourceCode = receipts.receiptHeader.SourceCode;
receiptHeader.Station = receipts.receiptHeader.Station ?? "";
receiptHeader.WarehouseType = receipts.receiptHeader.WarehouseType;
receiptHeader.SupplierId = receipts.receiptHeader.SupplierId;
receiptHeader.SupplierCode = receipts.receiptHeader.SupplierCode;
receiptHeader.SupplierName = receipts.receiptHeader.SupplierName;
receiptHeader.CreateTime = receipts.receiptHeader.CreateTime;
receiptHeader.CreateBy = receipts.receiptHeader.CreateBy;
receiptHeader.UpdateTime = receipts.receiptHeader.UpdateTime;
receiptHeader.UpdateBy = receipts.receiptHeader.UpdateBy;
receiptHeader.Code = string.IsNullOrEmpty(receiptHeader.Code) ? _unitWork.GetTaskNo(receipts.receiptHeader.Type) : receiptHeader.Code;
receiptHeader.UploadStatus = 0;
receiptHeader.FirstStatus = ReceiptHeaderStatus.回传;
receiptHeader.LastStatus = ReceiptHeaderStatus.回传;
receiptHeader.TotalLines = 0;
receiptHeader.TotalQty = 0;
if (receiptHeader.Id == null)
{
if (receiptHeader.CreateBy == null)
{
receiptHeader.CreateBy = "wms";
receiptHeader.CreateTime = DateTime.Now;
}
_unitWork.Add(receiptHeader);
}
else
{
if (receiptHeader.UpdateBy == null)
{
receiptHeader.UpdateBy = "wms";
receiptHeader.UpdateTime = DateTime.Now;
}
_unitWork.Update(receiptHeader);
}
#endregion
//验证站台提前获取仓位
StationRoadway stationRoadway = _unitWork.Find<StationRoadway>(n => n.StationCode == receiptHeader.Station).FirstOrDefault();
if (stationRoadway == null)
{
throw new Exception(receiptHeader.Station + "无此站台");
}
Location location = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.Roadway == stationRoadway.RoadWay && n.IsStop == false).FirstOrDefault();
location.Status = LocationStatus.任务锁定中;
_unitWork.Update(location);
//插入对应物料和容器信息
Container container = _unitWork.Find<Container>(n => n.Code == receiptHeader.ContainerCode).FirstOrDefault();
if (container != null)
{
throw new Exception("库内有该托盘号" + receiptHeader.ContainerCode);
}
else if (string.IsNullOrEmpty(receiptHeader.ContainerCode))
{
throw new Exception("请填写容器");
}
Container c = new Container();
c.Code = receiptHeader.ContainerCode;
c.LocationCode = location.Code;
c.Status = ContainerStatus.有;
c.IsLock = 1;
c.Type = receiptHeader.ContainerType;
c.PrintCount = 0;
_unitWork.Add(c);
foreach (var item in receipts.receiptDetails)
{
try
{
#region 保存入库单接口子表
IreceiptDetail data = _context.Set<IreceiptDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
item.ReceiveTime = DateTime.Now;
item.Id = (data?.Id);
if (item.Id == null)
{
if (item.CreateBy == null)
{
item.CreateBy = "wms";
item.CreateTime = DateTime.Now;
}
_unitWork.Add(item);
}
else
{
if (item.UpdateBy == null)
{
item.UpdateBy = "wms";
item.UpdateTime = DateTime.Now;
}
_unitWork.Update(item);
}
#endregion
#region 保存入库单子表
ReceiptDetail receiptDetail = _context.Set<ReceiptDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
if (receiptDetail == null)
{
receiptDetail = new ReceiptDetail();
}
receiptDetail.SourceCode = item.SourceCode;
receiptDetail.Station = item.Station ?? "";
receiptDetail.MaterialCode = item.MaterialCode;
receiptDetail.Lot = item.Lot;
receiptDetail.InventoryStatus = item.InventoryStatus;
receiptDetail.Qty = item.Qty;
receiptDetail.CreateTime = item.CreateTime;
receiptDetail.CreateBy = item.CreateBy;
receiptDetail.UpdateTime = item.UpdateTime;
receiptDetail.UpdateBy = item.UpdateBy;
receiptDetail.ReceiptId = receiptHeader.Id;
receiptDetail.ReceiptCode = receiptHeader.Code;
receiptDetail.QtyDivided = 0;
receiptDetail.QtyCompleted = 0;
receiptDetail.Price = 0;
receiptDetail.Status = ReceiptHeaderStatus.回传;
if (receiptDetail.Id == null)
{
if (receiptDetail.CreateBy == null)
{
receiptDetail.CreateBy = "wms";
receiptDetail.CreateTime = DateTime.Now;
}
_unitWork.Add(receiptDetail);
}
else
{
if (receiptDetail.UpdateBy == null)
{
receiptDetail.UpdateBy = "wms";
receiptDetail.UpdateTime = DateTime.Now;
}
_unitWork.Update(receiptDetail);
}
#endregion
#region 库存添加
Inventory inventory = new Inventory();
inventory.Lot = receiptDetail.Lot;
inventory.WarehouseType = receiptHeader.WarehouseType;
inventory.LocationCode = location.Code;
inventory.Batch = receiptDetail.Batch;
inventory.ReceiptId = receiptHeader.Id;
inventory.ReceiptCode = receiptHeader.Code;
inventory.SourceLine = receiptDetail.SourceLine;
inventory.ReceiptDetailId = receiptDetail.Id;
inventory.Project = receiptDetail.Project;
inventory.ContainerCode = receiptHeader.ContainerCode;
inventory.SourceCode = receiptDetail.SourceCode;
inventory.MaterialCode = receiptDetail.MaterialCode;
inventory.MaterialId = receiptDetail.MaterialId;
inventory.SourceCode = receiptDetail.SourceCode;
inventory.Status = receiptDetail.InventoryStatus;
inventory.ContainerStatus = ContainerStatus.有;
inventory.Qty = receiptDetail.Qty;
inventory.TaskStatus = InventoryTaskType.无盘点任务;
_unitWork.Add(inventory);
InventoryTransaction inventoryTransaction = new InventoryTransaction();
inventoryTransaction.WarehouseType = inventory.WarehouseType;
inventoryTransaction.SourceCode = receiptHeader.SourceCode;
inventoryTransaction.BillCode = receiptDetail.ReceiptCode;
inventoryTransaction.Type = TransactionType.入库;
inventoryTransaction.ContainerCode = inventory.ContainerCode;
inventoryTransaction.MaterialCode = inventory.MaterialCode;
inventoryTransaction.MaterialName = _unitWork.Find<Material>(n => n.Code == inventory.ContainerCode).Select(a => a.Name).FirstOrDefault();
inventoryTransaction.Batch = inventory.Batch;
inventoryTransaction.Lot = inventory.Lot;
inventoryTransaction.LocationCode = inventory.LocationCode;
inventoryTransaction.Qty = receiptDetail.Qty;
inventoryTransaction.TaskQty = receiptDetail.Qty;
inventoryTransaction.Status = inventory.Status;
_unitWork.Add(inventoryTransaction);
#endregion
}
catch (Exception ex)
{
#region 记录入库单接口子表错误信息
IreceiptDetail data = _context.Set<IreceiptDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
if (data != null)
{
data.ErrorMessage = ex.Message;
if (data.UpdateBy == null)
{
data.UpdateBy = "wms";
data.UpdateTime = DateTime.Now;
}
_unitWork.Update(data);
}
#endregion
Response.Code = 500;
Response.Status = false;
Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "SourceCode:" + item.SourceCode + ",MaterialCode:" + item.MaterialCode + "同步失败:" + ex.Message;
}
}
#region 建立回库任务
Task task = new Task();
TaskDetail taskDetail = new TaskDetail();
var taskNo = _app.GetTaskNo(TaskNo.查看容器回库);
task.TaskNo = taskNo;
task.OrderCode = taskNo;
task.BusinessType = BusinessType.入库_其他入库单;
task.FirstStatus = TaskStatus.待下发任务;
task.LastStatus = TaskStatus.待下发任务;
_unitWork.Add(task);
taskDetail.TaskNo = taskNo;
taskDetail.OrderCode = taskNo;
taskDetail.TaskType = TaskType.容器回库;
taskDetail.ContainerCode = receiptHeader.ContainerCode;
taskDetail.SourceLocation = receiptHeader.Station;
taskDetail.DestinationLocation = location.Code;
taskDetail.OderQty = 0;
taskDetail.ContainerQty = 0;
taskDetail.HadQty = 0;
taskDetail.Roadway = stationRoadway.RoadWay;
taskDetail.Station = receiptHeader.Station;
taskDetail.Status = TaskStatus.待下发任务;
taskDetail.Priority = 99;
_unitWork.Add(taskDetail);
#endregion
#region 更新入库单主表汇总信息
receiptHeader.TotalLines += (short)_unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == receiptHeader.Code).ToList().Count;
receiptHeader.TotalQty += _unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == receiptHeader.Code).Sum(x => x.Qty);
if (receiptHeader.UpdateBy == null)
{
receiptHeader.UpdateBy = "wms";
receiptHeader.UpdateTime = DateTime.Now;
}
_unitWork.Update(receiptHeader);
#endregion
tran.Commit();
//如果都成功则给erp回传数据
}
catch (Exception ex)
{
#region 记录入库单接口主表错误信息
try
{
tran.Rollback();
IreceiptHeader data = _context.Set<IreceiptHeader>().AsQueryable().Where(u => u.SourceCode.Equals(receipts.receiptHeader.SourceCode)).SingleOrDefault();
if (data != null)
{
data.ErrorMessage = ex.Message;
if (data.UpdateBy == null)
{
data.UpdateBy = "wms";
data.UpdateTime = DateTime.Now;
}
_unitWork.Update(data);
}
}
catch (Exception)
{
}
#endregion
Response.Code = 500;
Response.Status = false;
Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "同步失败:" + ex.Message;
}
}
return Response;
}
}
}