public ResultModel CreateStockOut(NFMT.Common.UserModel user, int stockOutApplyId, 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 = stockOutApplyDAL.Get(user, 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, 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; } } 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; } //新增出库表 Model.StockOut stockOut = new StockOut(); stockOut.Executor = user.EmpId; stockOut.Memo = memo; stockOut.StockOutApplyId = stockOutApply.StockOutApplyId; stockOut.StockOutStatus = StatusEnum.已录入; stockOut.StockOutTime = DateTime.Now; stockOut.Unit = stockOutApply.UnitId; stockOut.GrosstAmount = sumGrossAmount; stockOut.NetAmount = sumNetAmount; stockOut.Bundles = sumBundles; result = stockoutDAL.Insert(user, stockOut); if (result.ResultStatus != 0) return result; int stockOutId = 0; if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out stockOutId)) { result.ResultStatus = -1; result.Message = "出库新增失败"; return result; } stockOut.StockOutId = stockOutId; //新增出库明细表 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 = stockOutId; result = stockOutDetailDAL.Insert(user, stockOutDetail); if (result.ResultStatus != 0) return result; } if (result.ResultStatus == 0) result.ReturnValue = stockOut; scope.Complete(); } } catch (Exception ex) { result.ResultStatus = -1; result.Message = ex.Message; return result; } return result; }
public override IModel CreateModel(SqlDataReader dr) { StockOut stockout = new StockOut(); int indexStockOutId = dr.GetOrdinal("StockOutId"); stockout.StockOutId = Convert.ToInt32(dr[indexStockOutId]); int indexStockOutApplyId = dr.GetOrdinal("StockOutApplyId"); if (dr["StockOutApplyId"] != DBNull.Value) { stockout.StockOutApplyId = Convert.ToInt32(dr[indexStockOutApplyId]); } int indexExecutor = dr.GetOrdinal("Executor"); if (dr["Executor"] != DBNull.Value) { stockout.Executor = Convert.ToInt32(dr[indexExecutor]); } int indexConfirmor = dr.GetOrdinal("Confirmor"); if (dr["Confirmor"] != DBNull.Value) { stockout.Confirmor = Convert.ToInt32(dr[indexConfirmor]); } int indexStockOutTime = dr.GetOrdinal("StockOutTime"); if (dr["StockOutTime"] != DBNull.Value) { stockout.StockOutTime = Convert.ToDateTime(dr[indexStockOutTime]); } int indexGrosstAmount = dr.GetOrdinal("GrosstAmount"); if (dr["GrosstAmount"] != DBNull.Value) { stockout.GrosstAmount = Convert.ToDecimal(dr[indexGrosstAmount]); } int indexNetAmount = dr.GetOrdinal("NetAmount"); if (dr["NetAmount"] != DBNull.Value) { stockout.NetAmount = Convert.ToDecimal(dr[indexNetAmount]); } int indexBundles = dr.GetOrdinal("Bundles"); if (dr["Bundles"] != DBNull.Value) { stockout.Bundles = Convert.ToInt32(dr[indexBundles]); } int indexUnit = dr.GetOrdinal("Unit"); if (dr["Unit"] != DBNull.Value) { stockout.Unit = Convert.ToInt32(dr[indexUnit]); } int indexStockOperateType = dr.GetOrdinal("StockOperateType"); if (dr["StockOperateType"] != DBNull.Value) { stockout.StockOperateType = Convert.ToInt32(dr[indexStockOperateType]); } int indexMemo = dr.GetOrdinal("Memo"); if (dr["Memo"] != DBNull.Value) { stockout.Memo = Convert.ToString(dr[indexMemo]); } int indexStockOutStatus = dr.GetOrdinal("StockOutStatus"); if (dr["StockOutStatus"] != DBNull.Value) { stockout.StockOutStatus = (StatusEnum)Convert.ToInt32(dr[indexStockOutStatus]); } int indexCreatorId = dr.GetOrdinal("CreatorId"); if (dr["CreatorId"] != DBNull.Value) { stockout.CreatorId = Convert.ToInt32(dr[indexCreatorId]); } int indexCreateTime = dr.GetOrdinal("CreateTime"); if (dr["CreateTime"] != DBNull.Value) { stockout.CreateTime = Convert.ToDateTime(dr[indexCreateTime]); } int indexLastModifyId = dr.GetOrdinal("LastModifyId"); if (dr["LastModifyId"] != DBNull.Value) { stockout.LastModifyId = Convert.ToInt32(dr[indexLastModifyId]); } int indexLastModifyTime = dr.GetOrdinal("LastModifyTime"); if (dr["LastModifyTime"] != DBNull.Value) { stockout.LastModifyTime = Convert.ToDateTime(dr[indexLastModifyTime]); } return stockout; }