public async Task <ApiData> CreateOutboundOrder(CreateOutboundOrderArgs args) { OutboundOrder outboundOrder = new OutboundOrder(); string prefix = $"OBO{DateTime.Now:yyMMdd}"; int next = await _appSeqService.GetNextAsync(prefix); outboundOrder.OutboundOrderCode = $"{prefix}{next:00000}"; outboundOrder.BizType = args.BizType; outboundOrder.BizOrder = args.BizOrder; outboundOrder.Comment = args.Comment; if (args.Lines == null || args.Lines.Count == 0) { throw new InvalidOperationException("出库单应至少有一个出库行。"); } foreach (var lineInfo in args.Lines) { OutboundLine line = new OutboundLine(); var material = await _session.Query <Material>() .Where(x => x.MaterialCode == lineInfo.MaterialCode) .SingleOrDefaultAsync(); if (material == null) { throw new InvalidOperationException($"未找到编码为 {lineInfo.MaterialCode} 的物料。"); } line.Material = material; line.QuantityRequired = lineInfo.QuantityRequired; line.QuantityDelivered = 0; line.Batch = lineInfo.Batch; line.StockStatus = lineInfo.StockStatus; line.Uom = lineInfo.Uom; outboundOrder.AddLine(line); _logger.Information("已添加出库单明细,物料 {materialCode},批号 {batch},需求数量 {quantity}", line.Material.MaterialCode, line.Batch, line.QuantityRequired); } await _session.SaveAsync(outboundOrder); _logger.Information("已创建出库单 {outboundOrder}", outboundOrder); _ = await _opHelper.SaveOpAsync(outboundOrder.OutboundOrderCode); return(this.Success()); }
public async Task <ApiData> Edit(int id, EditOutboundOrderArgs args) { OutboundOrder outboundOrder = _session.Get <OutboundOrder>(id); if (outboundOrder == null) { String errMsg = String.Format("出库单不存在。", id); throw new InvalidOperationException(errMsg); } if (outboundOrder.Closed) { String errMsg = String.Format("出库单已关闭,不能编辑。单号:{0}。", outboundOrder.OutboundOrderCode); throw new InvalidOperationException(errMsg); } var movingDown = await _session.Query <Port>().AnyAsync(x => x.CurrentUat == outboundOrder); if (movingDown) { String errMsg = String.Format("出库单正在下架,不能编辑。单号:{0}。", outboundOrder.OutboundOrderCode); throw new InvalidOperationException(errMsg); } outboundOrder.Comment = args.Comment; if (args.Lines == null || args.Lines.Count == 0) { throw new InvalidOperationException("出库单应至少有一个出库行。"); } foreach (var lineInfo in args.Lines) { switch (lineInfo.Op) { case "delete": { var line = outboundOrder.Lines.Single(x => x.OutboundLineId == lineInfo.OutboundLineId); if (line.Dirty) { string errMsg = String.Format("已发生过出库操作的明细不能删除。出库行#{0}。", line.OutboundLineId); throw new InvalidOperationException(errMsg); } outboundOrder.RemoveLine(line); _logger.Information("已删除出库单明细 {outboundLineId}", line.OutboundLineId); } break; case "edit": { var line = outboundOrder.Lines.Single(x => x.OutboundLineId == lineInfo.OutboundLineId); line.QuantityRequired = lineInfo.QuantityRequired; if (line.QuantityRequired < line.QuantityDelivered) { _logger.Warning("出库单明细 {outboundLineId} 的需求数量修改后小于已出数量", line.OutboundLineId); } } break; case "add": { OutboundLine line = new OutboundLine(); var material = await _session.Query <Material>() .Where(x => x.MaterialCode == lineInfo.MaterialCode) .SingleOrDefaultAsync(); if (material == null) { throw new InvalidOperationException($"未找到物料。编码 {lineInfo.MaterialCode}。"); } line.Material = material; line.QuantityRequired = lineInfo.QuantityRequired; line.QuantityDelivered = 0; line.Batch = lineInfo.Batch; line.StockStatus = lineInfo.StockStatus; line.Uom = lineInfo.Uom; outboundOrder.AddLine(line); _logger.Information("已添加出库单明细,物料 {materialCode},批号 {batch},需求数量 {quantity}", line.Material.MaterialCode, line.Batch, line.QuantityRequired); } break; default: break; } } await _session.UpdateAsync(outboundOrder); _logger.Information("已更新出库单 {outboundOrder}", outboundOrder); _ = await _opHelper.SaveOpAsync("{0}", outboundOrder); // TODO //// 取消库内分配 //_deliveryOrderAllocator.Value.CancelUnitLoadsOnRack(m); //foreach (var line in m.Lines) //{ // if (line.ComputeNumberAllocated() > line.NumberRequired) // { // String errMsg = String.Format("取消库内分配后,分配数量仍然大于应出数量,请处理完此出库单下所有路上和库外的货载再编辑。出库行#{0}。", line.Id); // throw new InvalidOperationException(errMsg); // } //} return(this.Success()); }