private void Allot(OutBillMaster billMaster, OutBillDetail billDetail, Storage storage, Storage p, decimal allotQuantity, ProgressState ps) { if (storage != null && allotQuantity > 0) { OutBillAllot billAllot = null; billDetail.AllotQuantity += allotQuantity; storage.OutFrozenQuantity += allotQuantity; billAllot = new OutBillAllot() { BillNo = billMaster.BillNo, OutBillDetailId = billDetail.ID, ProductCode = billDetail.ProductCode, CellCode = storage.CellCode, StorageCode = storage.StorageCode, UnitCode = billDetail.UnitCode, AllotQuantity = allotQuantity, RealQuantity = 0, Status = "0" }; billMaster.OutBillAllots.Add(billAllot); decimal sumBillQuantity = billMaster.OutBillDetails.Sum(d => d.BillQuantity); decimal sumAllotQuantity = billMaster.OutBillDetails.Sum(d => d.AllotQuantity); decimal sumBillProductQuantity = billMaster.OutBillDetails.Where(d => d.ProductCode == billDetail.ProductCode) .Sum(d => d.BillQuantity); decimal sumAllotProductQuantity = billMaster.OutBillDetails.Where(d => d.ProductCode == billDetail.ProductCode) .Sum(d => d.AllotQuantity); ps.State = StateType.Processing; ps.TotalProgressName = "分配出库单:" + billMaster.BillNo; ps.TotalProgressValue = (int)(sumAllotQuantity / sumBillQuantity * 100); ps.CurrentProgressName = "分配卷烟:" + billDetail.Product.ProductName; ps.CurrentProgressValue = (int)(sumAllotProductQuantity / sumBillProductQuantity * 100); NotifyConnection(ps.Clone()); } }
/// <summary> /// 出库分配 /// </summary> /// <param name="outBillMaster">出库主单</param> /// <returns></returns> public bool OutAllot(OutBillMaster outBillMaster, Guid employeeId) { try { bool result = false; //出库单出库 var storages = StorageRepository.GetQueryable().Where(s => s.CellCode == outBillMaster.TargetCellCode && s.Quantity - s.OutFrozenQuantity > 0).ToArray(); if (!Locker.Lock(storages)) { throw new Exception("锁定储位失败,储位其他人正在操作,无法取消分配请稍候重试!"); } var outDetails = OutBillDetailRepository.GetQueryableIncludeProduct() .Where(o => o.BillNo == outBillMaster.BillNo); outDetails.ToArray().AsParallel().ForAll( (Action <OutBillDetail>) delegate(OutBillDetail o) { var ss = storages.Where(s => s.ProductCode == o.ProductCode).ToArray(); foreach (var s in ss) { lock (s) { if (o.BillQuantity - o.AllotQuantity > 0) { decimal allotQuantity = s.Quantity; decimal billQuantity = o.BillQuantity - o.AllotQuantity; allotQuantity = allotQuantity < billQuantity ? allotQuantity : billQuantity; o.AllotQuantity += allotQuantity; o.RealQuantity += allotQuantity; s.Quantity -= allotQuantity; var billAllot = new OutBillAllot() { BillNo = outBillMaster.BillNo, OutBillDetailId = o.ID, ProductCode = o.ProductCode, CellCode = s.CellCode, StorageCode = s.StorageCode, UnitCode = o.UnitCode, AllotQuantity = allotQuantity, RealQuantity = allotQuantity, Status = "2" }; lock (outBillMaster.OutBillAllots) { outBillMaster.OutBillAllots.Add(billAllot); } } else { break; } } } if (o.BillQuantity - o.AllotQuantity > 0) { throw new Exception(o.ProductCode + " " + o.Product.ProductName + "库存不足,未能结单!"); } }); result = true; storages.AsParallel().ForAll(s => s.LockTag = string.Empty); //出库结单 outBillMaster.Status = "6"; outBillMaster.VerifyDate = DateTime.Now; outBillMaster.VerifyPersonID = employeeId; outBillMaster.UpdateTime = DateTime.Now; OutBillMasterRepository.SaveChanges(); return(result); } catch (AggregateException ex) { infoStr = "审核失败,详情:" + ex.InnerExceptions.Select(i => i.Message).Aggregate((m, n) => m + n); return(false); } }
public bool Settle(string id, ref string errorInfo) { try { Guid ID = new Guid(id); var sortWork = SortWorkDispatchRepository.GetQueryable().FirstOrDefault(s => s.ID == ID); if (sortWork == null) { errorInfo = "当前选择的调度记录不存在,未能结单!"; return(false); } if (sortWork.DispatchStatus == "1") { errorInfo = "当前选择的调度记录不是执行中,未能结单!"; return(false); } if (sortWork.MoveBillMaster.Status == "1") { errorInfo = "当前选择的调度记录移库单不是执行中,未能结单!"; return(false); } using (var scope = new TransactionScope()) { //移库细单解锁冻结量 var moveDetail = MoveBillDetailRepository.GetQueryable() .Where(m => m.BillNo == sortWork.MoveBillNo && m.Status != "2"); if (moveDetail.Any()) { var sourceStorages = moveDetail.Select(m => m.OutStorage).ToArray(); var targetStorages = moveDetail.Select(m => m.InStorage).ToArray(); if (!Locker.Lock(sourceStorages) || !Locker.Lock(targetStorages)) { errorInfo = "锁定储位失败,储位其他人正在操作,无法取消分配请稍候重试!"; return(false); } moveDetail.AsParallel().ForAll( (Action <MoveBillDetail>) delegate(MoveBillDetail m) { if (m.InStorage.ProductCode == m.ProductCode && m.OutStorage.ProductCode == m.ProductCode && m.InStorage.InFrozenQuantity >= m.RealQuantity && m.OutStorage.OutFrozenQuantity >= m.RealQuantity) { m.InStorage.InFrozenQuantity -= m.RealQuantity; m.OutStorage.OutFrozenQuantity -= m.RealQuantity; m.InStorage.LockTag = string.Empty; m.OutStorage.LockTag = string.Empty; } else { throw new Exception("储位的卷烟或移库冻结量与当前分配不符,信息可能被异常修改,不能结单!"); } } ); //解锁分拣线路调度的状态,以便重新做作业调度; foreach (var sortDisp in sortWork.SortOrderDispatchs) { sortDisp.SortWorkDispatchID = null; sortDisp.WorkStatus = "1"; } } else { //出库单作自动出库 var storages = StorageRepository.GetQueryable().Where(s => s.CellCode == sortWork.SortingLine.CellCode && s.Quantity - s.OutFrozenQuantity > 0).ToArray(); if (!Locker.Lock(storages)) { errorInfo = "锁定储位失败,储位其他人正在操作,无法取消分配请稍候重试!"; return(false); } var outAllots = sortWork.OutBillMaster.OutBillAllots; var outDetails = OutBillDetailRepository.GetQueryableIncludeProduct() .Where(o => o.BillNo == sortWork.OutBillMaster.BillNo); outDetails.ToArray().AsParallel().ForAll( (Action <OutBillDetail>) delegate(OutBillDetail o) { var ss = storages.Where(s => s.ProductCode == o.ProductCode).ToArray(); foreach (var s in ss) { lock (s) { if (o.BillQuantity - o.AllotQuantity > 0) { decimal allotQuantity = s.Quantity; decimal billQuantity = o.BillQuantity - o.AllotQuantity; allotQuantity = allotQuantity < billQuantity ? allotQuantity : billQuantity; o.AllotQuantity += allotQuantity; o.RealQuantity += allotQuantity; s.Quantity -= allotQuantity; var billAllot = new OutBillAllot() { BillNo = sortWork.OutBillMaster.BillNo, OutBillDetailId = o.ID, ProductCode = o.ProductCode, CellCode = s.CellCode, StorageCode = s.StorageCode, UnitCode = o.UnitCode, AllotQuantity = allotQuantity, RealQuantity = allotQuantity, FinishTime = DateTime.Now, Status = "2" }; lock (sortWork.OutBillMaster.OutBillAllots) { sortWork.OutBillMaster.OutBillAllots.Add(billAllot); } } else { break; } } } if (o.BillQuantity - o.AllotQuantity > 0) { throw new Exception(sortWork.SortingLine.SortingLineName + " " + o.ProductCode + " " + o.Product.ProductName + "分拣备货区库存不足,未能结单!"); } } ); storages.AsParallel().ForAll(s => s.LockTag = string.Empty); } //出库结单 var outMaster = OutBillMasterRepository.GetQueryable() .FirstOrDefault(o => o.BillNo == sortWork.OutBillNo); outMaster.Status = "6"; outMaster.UpdateTime = DateTime.Now; //移库结单 var moveMater = MoveBillMasterRepository.GetQueryable() .FirstOrDefault(m => m.BillNo == sortWork.MoveBillNo); moveMater.Status = "4"; moveMater.UpdateTime = DateTime.Now; //分拣作业结单 sortWork.DispatchStatus = "4"; sortWork.UpdateTime = DateTime.Now; SortWorkDispatchRepository.SaveChanges(); scope.Complete(); return(true); } } catch (AggregateException ex) { errorInfo = "结单失败,详情:" + ex.InnerExceptions.Select(i => i.Message).Aggregate((m, n) => m + n); return(false); } catch (Exception e) { errorInfo = "结单失败,详情:" + e.Message; return(false); } }