ShipmentContainerHeaderApp.cs
16.6 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using WebRepository;
namespace WebApp
{
/// <summary>
/// 出库组盘明细表
/// </summary>
public partial class ShipmentContainerHeaderApp
{
private IUnitWork _unitWork;
public IRepository<ShipmentContainerHeader> _app;
public IRepository<ShipmentDetail> _appsd;
public IRepository<ShipmentHeader> _appsh;
public IRepository<Task> _appt;
public IRepository<TaskDetail> _apptd;
private BaseDBContext _context;
private static IHostingEnvironment _hostingEnvironment;
ExcelHelper imp = new ExcelHelper(_hostingEnvironment);
public ShipmentContainerHeaderApp(IUnitWork unitWork, IRepository<ShipmentContainerHeader> repository, IRepository<ShipmentHeader> repositorysh, IRepository<Task> repositoryt, IRepository<TaskDetail> repositorytd, IRepository<ShipmentDetail> repositorysd, IHostingEnvironment hostingEnvironment, BaseDBContext context)
{
_unitWork = unitWork;
_app = repository;
_appsh = repositorysh;
_appsd = repositorysd;
_appt = repositoryt;
_apptd = repositorytd;
_context = context;
_hostingEnvironment = hostingEnvironment;
}
public ShipmentContainerHeaderApp SetLoginInfo(LoginInfo loginInfo)
{
_app._loginInfo = loginInfo;
_appsh._loginInfo = loginInfo;
_appsd._loginInfo = loginInfo;
_appt._loginInfo = loginInfo;
_apptd._loginInfo = loginInfo;
return this;
}
public TableData Load(PageReq pageRequest, ShipmentContainerHeader entity)
{
return _app.Load(pageRequest, entity);
}
public void Ins(ShipmentContainerHeader entity)
{
_app.Add(entity);
}
public void Upd(ShipmentContainerHeader entity)
{
_app.Update(entity);
}
public void DelByIds(int[] ids)
{
_app.Delete(u => ids.Contains(u.Id.Value));
}
public ShipmentContainerHeader FindSingle(Expression<Func<ShipmentContainerHeader, bool>> exp)
{
return _app.FindSingle(exp);
}
public IQueryable<ShipmentContainerHeader> Find(Expression<Func<ShipmentContainerHeader, bool>> exp)
{
return _app.Find(exp);
}
public Response ImportIn(IFormFile excelfile)
{
Response result = new Infrastructure.Response();
List<ShipmentContainerHeader> exp = imp.ConvertToModel<ShipmentContainerHeader>(excelfile);
string sErrorMsg = "";
for (int i = 0; i < exp.Count; i++)
{
try
{
ShipmentContainerHeader e = exp[i];
e.Id = null;
_app.Add(e);
}
catch (Exception ex)
{
sErrorMsg += "第" + (i + 2) + "行:" + ex.Message + "<br>";
result.Message = sErrorMsg;
break;
}
}
if (sErrorMsg.Equals(string.Empty))
{
if (exp.Count == 0)
{
sErrorMsg += "没有发现有效数据, 请确定模板是否正确, 或是否有填充数据!";
result.Message = sErrorMsg;
}
else
{
result.Message = "导入完成";
}
}
else
{
result.Status = false;
result.Message = result.Message;
}
return result;
}
public TableData ExportData(ShipmentContainerHeader entity)
{
return _app.ExportData(entity);
}
public TableData Query(ShipmentContainerHeader entity)
{
var result = new TableData();
var data = _app.Find(EntityToExpression<ShipmentContainerHeader>.GetExpressions(entity));
GetData(data, result);
result.count = data.Count();
return result;
}
public void GetData(IQueryable<ShipmentContainerHeader> data, TableData result, PageReq pageRequest = null)
{
_app.GetData(data, result, pageRequest);
}
//任务添加
public bool AddTaskInfo(ShipmentDetail shipmentDetail, Inventory inventory, int dividedqty)
{
using (var tran = _context.Database.BeginTransaction())
{
try
{
ShipmentHeader shipmentHeader = _unitWork.Find<ShipmentHeader>(n => n.Code == shipmentDetail.ShipmentCode).FirstOrDefault();
Task task = new Task();
TaskDetail taskDetail = new TaskDetail();
var tno = "";
int tcount = _unitWork.Find<Task>(n => n.OrderCode == shipmentDetail.ShipmentCode).Count();
if (tcount > 0)
{
var tasknum = _unitWork.Find<Task>(n => n.OrderCode == shipmentDetail.ShipmentCode).Select(a => a.TaskNo).First();
tno = tasknum;
}
else
{
tno = _app.GetTaskNo(TaskNo.出库手动分配);
task.TaskNo = tno;
task.SourceCode = shipmentDetail.SourceCode;
task.OrderCode = shipmentDetail.ShipmentCode;
task.BusinessType = shipmentHeader.Type;
task.TotalQty = shipmentHeader.TotalQty;
task.TotalLines = shipmentHeader.TotalLines;
task.FirstStatus = shipmentHeader.FirstStatus;
task.LastStatus = shipmentHeader.LastStatus;
task.Project = shipmentDetail.Project;
task.CreateTime = DateTime.Now;
_appt.Add(task);
}
//var outtype = _unitWork.Find<SysDictData>(u => u.DictType == "shipmentType").Select(a => a.DictValue).ToList();
if (_unitWork.IsExist<TaskDetail>(n => n.MaterialCode == shipmentDetail.MaterialCode && (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == inventory.ContainerCode && !n.TaskNo.StartsWith("I")))
{
decimal? taskhadqty = _unitWork.Find<TaskDetail>(n => n.MaterialCode == shipmentDetail.MaterialCode && (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == inventory.ContainerCode && !n.TaskNo.StartsWith("I")).Select(a => a.ContainerQty).Sum();
inventory.Qty = inventory.Qty - taskhadqty;
}
else
{
inventory.Qty = inventory.Qty - 0;
}
taskDetail.TaskNo = tno;
taskDetail.SourceCode = shipmentDetail.SourceCode;
taskDetail.OrderCode = shipmentDetail.ShipmentCode;
taskDetail.ContainerCode = inventory.ContainerCode;
if (inventory.Qty > dividedqty)
{
taskDetail.TaskType = TaskType.分拣出库;
}
else
{
taskDetail.TaskType = TaskType.整盘出库;
}
taskDetail.MaterialCode = shipmentDetail.MaterialCode;
taskDetail.MaterialName = _unitWork.Find<Material>(n => n.Code == shipmentDetail.MaterialCode).Select(a => a.Name).First();
taskDetail.Batch = shipmentDetail.Batch;
taskDetail.Lot = shipmentDetail.Lot;
taskDetail.OderQty = shipmentDetail.Qty;
taskDetail.ContainerQty = dividedqty;
taskDetail.SourceLocation = inventory.LocationCode;
taskDetail.DestinationLocation = shipmentHeader.Station;
taskDetail.HadQty = 0;
int lcount = _unitWork.Find<Location>(n => n.Code == inventory.LocationCode).Count();
if (lcount > 0)
{
taskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == inventory.LocationCode).Select(a => a.Roadway).First();
}
else
{
taskDetail.Roadway = 0;
}
taskDetail.Station = shipmentHeader.Station;
taskDetail.Priority = shipmentHeader.Priority;
taskDetail.Status = TaskStatus.待下发任务;
taskDetail.CreateTime = DateTime.Now;
_apptd.Add(taskDetail);
//建立容器出库任务
if (_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == shipmentDetail.MaterialCode && n.OrderCode == shipmentDetail.ShipmentCode))
{
int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == taskDetail.ContainerCode && n.TaskType == TaskType.容器出库).Count();
if (ptdcount < 1)
{
Task ptask = new Task();
TaskDetail ptaskDetail = new TaskDetail();
var taskNo = _app.GetTaskNo(TaskNo.容器出库);
ptask.TaskNo = taskNo;
ptask.OrderCode = taskNo;
ptask.BusinessType = BusinessType.出库_其他出库单;
ptask.FirstStatus = TaskStatus.待下发任务;
ptask.LastStatus = TaskStatus.待下发任务;
ptask.CreateTime = DateTime.Now;
_appt.Add(ptask);
ptaskDetail.TaskNo = taskNo;
ptaskDetail.OrderCode = taskNo;
ptaskDetail.TaskType = TaskType.容器出库;
ptaskDetail.ContainerCode = taskDetail.ContainerCode;
ptaskDetail.SourceLocation = taskDetail.SourceLocation;
ptaskDetail.DestinationLocation = taskDetail.DestinationLocation;
ptaskDetail.OderQty = 0;
ptaskDetail.ContainerQty = 0;
ptaskDetail.HadQty = 0;
ptaskDetail.Roadway = taskDetail.Roadway;
ptaskDetail.Station = taskDetail.Station;
ptaskDetail.Status = TaskStatus.待下发任务;
if (taskDetail.Priority != null)
{
ptaskDetail.Priority = taskDetail.Priority;
}
else
{
ptaskDetail.Priority = 0;
}
ptaskDetail.CreateTime = DateTime.Now;
_apptd.Add(ptaskDetail);
}
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
return false;
}
return true;
}
}
public TableData DivideToTask(ShipmentDetail shipmentDtl, Inventory inv, int dqty)
{
TableData tab = new TableData();
tab.code = 200;
try
{
if (inv.TaskStatus != "idle")
{
tab.code = 300;
tab.msg = "出库手动分配失败!原因:正在盘点!";
return tab;
}
if (AddTaskInfo(shipmentDtl, inv, dqty))
{
if (string.IsNullOrEmpty(shipmentDtl.QtyDivided.ToString()))
{
shipmentDtl.QtyDivided = 0;
}
shipmentDtl.QtyDivided = shipmentDtl.QtyDivided + dqty;
if (shipmentDtl.Qty == shipmentDtl.QtyDivided)
{
shipmentDtl.Status = ShipmentHeaderStatus.分配完成;
}
_appsd.Update(shipmentDtl);
tab.code = 200;
tab.msg = "分配成功!";
ShipmentHeader shipmentHeader = _unitWork.Find<ShipmentHeader>(n => n.Code == shipmentDtl.ShipmentCode && n.LastStatus <= ShipmentHeaderStatus.分配完成).FirstOrDefault();
int statuscount = _unitWork.Find<ShipmentDetail>(n => n.ShipmentCode == shipmentDtl.ShipmentCode && n.Status < ShipmentHeaderStatus.分配完成 && n.Qty > n.QtyDivided).Count();
if (statuscount > 0)
{
shipmentHeader.FirstStatus = ShipmentHeaderStatus.分配完成;
_appsh.Update(shipmentHeader);
}
else
{
shipmentHeader.FirstStatus = ShipmentHeaderStatus.分配完成;
shipmentHeader.LastStatus = ShipmentHeaderStatus.分配完成;
_appsh.Update(shipmentHeader);
}
}
else
{
tab.code = 300;
tab.msg = "出库手动分配失败!";
}
}
catch (Exception ex)
{
tab.code = 300;
tab.msg += ex.Message;
}
return tab;
}
public TableData GetContainerQtyApp(Inventory inventory)
{
var result = new TableData();
try
{
//var outtype = _unitWork.Find<SysDictData>(u => u.DictType == "shipmentType").Select(a => a.DictValue).ToList();
if (_unitWork.IsExist<TaskDetail>(n => n.MaterialCode == inventory.MaterialCode && n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.ContainerCode == inventory.ContainerCode && !n.TaskNo.StartsWith("I")))
{
decimal? taskhadqty = _unitWork.Find<TaskDetail>(n => n.MaterialCode == inventory.MaterialCode && n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.ContainerCode == inventory.ContainerCode && !n.TaskNo.StartsWith("I")).Select(n => n.ContainerQty).Sum();
inventory.Qty = inventory.Qty - taskhadqty;
}
else
{
inventory.Qty = inventory.Qty - 0;
}
JObject jObject = new JObject();
jObject.Add("yqty", inventory.Qty);
result.code = 200;
result.data = jObject;
result.count = 1;
}
catch (Exception ex)
{
result.msg = ex.Message;
}
return result;
}
public TableData GetShipmentStatus(string Code)
{
var result = new TableData();
try
{
int TotalLines = 0;
decimal TotalQty = 0;
decimal TotalQtyCompleted = 0;
var shipmentDetail = _unitWork.Find<ShipmentDetail>(u => u.ShipmentCode.Equals(Code));
foreach (var item in shipmentDetail)
{
TotalLines += 1;
TotalQty += item.Qty.Value;
TotalQtyCompleted += item.QtyDivided.Value;
}
JObject jObject = new JObject();
jObject.Add("TotalLines", TotalLines);
jObject.Add("TotalQty", TotalQty);
jObject.Add("TotalQtyCompleted", TotalQtyCompleted);
result.data = jObject;
result.count = 1;
}
catch (Exception ex)
{
result.msg = ex.Message;
}
return result;
}
}
}