public ResultModel UpdateStockOut(NFMT.Common.UserModel user, int stockOutId, List<int> detailIds, string memo) { ResultModel result = new ResultModel(); try { //dal init DAL.StockDAL stockDAL = new StockDAL(); DAL.StockOutApplyDAL stockOutApplyDAL = new StockOutApplyDAL(); DAL.StockOutApplyDetailDAL stockOutApplyDetailDAL = new StockOutApplyDetailDAL(); DAL.StockOutDAL stockOutDAL = new StockOutDAL(); DAL.StockOutDetailDAL stockOutDetailDAL = new StockOutDetailDAL(); using (System.Transactions.TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { //获取出库 result = stockOutDAL.Get(user, stockOutId); if (result.ResultStatus != 0) return result; NFMT.WareHouse.Model.StockOut stockOut = result.ReturnValue as NFMT.WareHouse.Model.StockOut; if (stockOut == null || stockOut.StockOutId <= 0) { result.ResultStatus = -1; result.Message = "出库不存在"; return result; } //获取出库申请 result = stockOutApplyDAL.Get(user, stockOut.StockOutApplyId); if (result.ResultStatus != 0) return result; Model.StockOutApply stockOutApply = result.ReturnValue as Model.StockOutApply; if (stockOutApply == null || stockOutApply.StockOutApplyId <= 0) { result.ResultStatus = -1; result.Message = "出库申请不存在"; return result; } //获取出库申请明细 result = stockOutApplyDetailDAL.Load(user, stockOut.StockOutApplyId); if (result.ResultStatus != 0) return result; List<Model.StockOutApplyDetail> stockOutApplyDetails = result.ReturnValue as List<Model.StockOutApplyDetail>; if (stockOutApplyDetails == null) { result.ResultStatus = -1; result.Message = "出库申请明细获取失败"; return result; } //判断detailIds 是否都在出库申请明细中 List<int> applyDetails = new List<int>(); foreach (Model.StockOutApplyDetail detail in stockOutApplyDetails) { applyDetails.Add(detail.DetailId); } foreach (int id in detailIds) { if (!applyDetails.Contains(id)) { result.ResultStatus = -1; result.Message = "出库申请不包含选中库存"; return result; } } //作废原有出库明细 result = stockOutDetailDAL.Load(user, stockOut.StockOutId); if (result.ResultStatus != 0) return result; List<Model.StockOutDetail> stockOutDetails = result.ReturnValue as List<Model.StockOutDetail>; if (stockOutDetails == null) { result.ResultStatus = -1; result.Message = "出库明细获取失败"; return result; } foreach (Model.StockOutDetail stockOutDetail in stockOutDetails) { stockOutDetail.DetailStatus = StatusEnum.已录入; result = stockOutDetailDAL.Invalid(user, stockOutDetail); if (result.ResultStatus != 0) return result; } decimal sumGrossAmount = 0; decimal sumNetAmount = 0; int sumBundles = 0; foreach (int id in detailIds) { Model.StockOutApplyDetail applyDetail = stockOutApplyDetails.FirstOrDefault(temp => temp.DetailId == id); if (applyDetail == null || applyDetail.DetailId <= 0) { result.ResultStatus = -1; result.Message = "出库申请不包含选中库存"; return result; } if (applyDetail.DetailStatus != StatusEnum.已生效) { result.ResultStatus = -1; result.Message = "选中库存存在已作废状态库存"; return result; } //获取库存 result = stockDAL.Get(user, applyDetail.StockId); if (result.ResultStatus != 0) return result; Model.Stock stock = result.ReturnValue as Model.Stock; if (stock == null || stock.StockId <= 0) { result.ResultStatus = -1; result.Message = "库存不存在"; return result; } //库存状态校验 if (stock.StockStatus != StockStatusEnum.在库正常 && stock.StockStatus != StockStatusEnum.新拆库存 && stock.StockStatus != StockStatusEnum.质押库存) { result.ResultStatus = -1; result.Message = "库存不允许出库,出库失败"; return result; } sumGrossAmount += applyDetail.GrossAmount; sumNetAmount += applyDetail.NetAmount; sumBundles += applyDetail.Bundles; } //更新出库表 stockOut.Executor = user.EmpId; stockOut.Memo = memo; stockOut.GrosstAmount = sumGrossAmount; stockOut.NetAmount = sumNetAmount; stockOut.Bundles = sumBundles; result = stockoutDAL.Update(user, stockOut); if (result.ResultStatus != 0) return result; //新增出库明细表 foreach (int id in detailIds) { Model.StockOutApplyDetail applyDetail = stockOutApplyDetails.FirstOrDefault(temp => temp.DetailId == id); Model.StockOutDetail stockOutDetail = new StockOutDetail(); stockOutDetail.DetailStatus = StatusEnum.已生效; stockOutDetail.GrossAmount = applyDetail.GrossAmount; stockOutDetail.NetAmount = applyDetail.NetAmount; stockOutDetail.Bundles = applyDetail.Bundles; stockOutDetail.StockId = applyDetail.StockId; stockOutDetail.StockOutApplyDetailId = applyDetail.DetailId; stockOutDetail.StockOutId = stockOut.StockOutId; result = stockOutDetailDAL.Insert(user, stockOutDetail); if (result.ResultStatus != 0) return result; } scope.Complete(); } } catch (Exception ex) { result.ResultStatus = -1; result.Message = ex.Message; return result; } return result; }
public ResultModel Invalid(UserModel user, int stockOutId) { ResultModel result = new ResultModel(); try { using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { result = stockoutDAL.Get(user, stockOutId); if (result.ResultStatus != 0) return result; StockOut resultObj = result.ReturnValue as StockOut; if (resultObj == null || resultObj.StockOutId <= 0) { result.ResultStatus = -1; result.Message = "该数据不存在,不能作废"; return result; } result = stockoutDAL.Invalid(user, resultObj); if (result.ResultStatus != 0) return result; StockOutDetailDAL stockOutDetailDAL = new StockOutDetailDAL(); result = stockOutDetailDAL.Load(user, resultObj.StockOutId); if (result.ResultStatus != 0) return result; List<Model.StockOutDetail> details = result.ReturnValue as List<Model.StockOutDetail>; if (details == null) { result.ResultStatus = -1; result.Message = "获取出库明细失败"; return result; } foreach (Model.StockOutDetail detail in details) { detail.Status = StatusEnum.已录入; result = stockOutDetailDAL.Invalid(user, detail); if (result.ResultStatus != 0) return result; } scope.Complete(); } } catch (Exception ex) { result.Message = ex.Message; } finally { if (result.ResultStatus != 0) log.ErrorFormat("{0} {1},序号:{2}", user.EmpName, result.Message, result.ReturnValue); else if (log.IsInfoEnabled) log.InfoFormat("{0} {1},序号:{2}", user.EmpName, result.Message, result.ReturnValue); } return result; }