public async Task <ApiData> Create(CreateInboundOrderArgs args) { InboundOrder inboundOrder = new InboundOrder(); string prefix = $"IBO{DateTime.Now:yyMMdd}"; int next = await _appSeqService.GetNextAsync(prefix); inboundOrder.InboundOrderCode = $"{prefix}{next:00000}"; inboundOrder.BizType = args.BizType; inboundOrder.BizOrder = args.BizOrder; inboundOrder.Comment = args.Comment; if (args.Lines == null || args.Lines.Count == 0) { throw new InvalidOperationException("入库单应至少有一个入库行。"); } foreach (var lineInfo in args.Lines) { InboundLine line = new InboundLine(); 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.QuantityExpected = lineInfo.QuantityExpected; line.QuantityReceived = 0; line.Batch = lineInfo.Batch; line.StockStatus = lineInfo.StockStatus; line.Uom = lineInfo.Uom; inboundOrder.AddLine(line); _logger.Information("已添加入库单明细,物料 {materialCode},批号 {batch},应入数量 {quantity}", line.Material.MaterialCode, line.Batch, line.QuantityReceived); } await _session.SaveAsync(inboundOrder); _logger.Information("已创建入库单 {inboundOrder}", inboundOrder); _ = await _opHelper.SaveOpAsync(inboundOrder.InboundOrderCode); return(this.Success()); }
public async Task <ApiData> Edit(int id, EditInboundOrderArgs args) { InboundOrder inboundOrder = _session.Get <InboundOrder>(id); if (inboundOrder == null) { String errMsg = String.Format("入库单不存在。Id 是 {0}。", id); throw new InvalidOperationException(errMsg); } if (inboundOrder.Closed) { String errMsg = String.Format("入库单已关闭,不能编辑。单号:{0}。", inboundOrder.InboundOrderCode); throw new InvalidOperationException(errMsg); } var movingDown = await _session.Query <Port>().AnyAsync(x => x.CurrentUat == inboundOrder); if (movingDown) { String errMsg = String.Format("入库单正在下架,不能编辑。单号:{0}。", inboundOrder.InboundOrderCode); throw new InvalidOperationException(errMsg); } inboundOrder.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 = inboundOrder.Lines.Single(x => x.InboundLineId == lineInfo.InboundLineId); if (line.Dirty) { string errMsg = String.Format("已发生过入库操作的明细不能删除。入库行#{0}。", line.InboundLineId); throw new InvalidOperationException(errMsg); } inboundOrder.RemoveLine(line); _logger.Information("已删除入库单明细 {inboundLineId}", line.InboundLineId); } break; case "edit": { var line = inboundOrder.Lines.Single(x => x.InboundLineId == lineInfo.InboundLineId); line.QuantityExpected = lineInfo.QuantityExpected; if (line.QuantityReceived < line.QuantityReceived) { _logger.Warning("入库单明细 {inboundLineId} 的应入数量修改后小于已入数量", line.InboundLineId); } } break; case "added": { InboundLine line = new InboundLine(); 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.QuantityExpected = lineInfo.QuantityExpected; line.QuantityReceived = 0; line.Batch = lineInfo.Batch; line.StockStatus = lineInfo.StockStatus; line.Uom = lineInfo.Uom; inboundOrder.AddLine(line); _logger.Information("已添加入库单明细,物料 {materialCode},批号 {batch},应入数量 {quantity}", line.Material.MaterialCode, line.Batch, line.QuantityReceived); } break; default: break; } } await _session.UpdateAsync(inboundOrder); _logger.Information("已更新入库单 {inboundOrder}", inboundOrder); _ = await _opHelper.SaveOpAsync("{0}", inboundOrder); return(this.Success()); }