public ResultModel ContractOutCreateStockOperate(UserModel user, NFMT.Contract.Model.Contract contract, int subId, int outCorpId, List<NFMT.WareHouse.Model.StockOutApplyDetail> details) { ResultModel result = new ResultModel(); NFMT.Operate.DAL.ApplyDAL applyDAL = new NFMT.Operate.DAL.ApplyDAL(); NFMT.WareHouse.DAL.StockOutApplyDAL outApplyDAL = new NFMT.WareHouse.DAL.StockOutApplyDAL(); NFMT.WareHouse.DAL.StockOutApplyDetailDAL detailDAL = new NFMT.WareHouse.DAL.StockOutApplyDetailDAL(); NFMT.WareHouse.DAL.StockExclusiveDAL exclusiveDAL = new NFMT.WareHouse.DAL.StockExclusiveDAL(); NFMT.WareHouse.DAL.StockDAL stockDAL = new NFMT.WareHouse.DAL.StockDAL(); try { //验证分配库存 if (details == null || details.Count == 0) { result.ResultStatus = -1; result.Message = "未分配任务库存"; return result; } //添加主申请表 NFMT.Operate.Model.Apply apply = new NFMT.Operate.Model.Apply(); apply.ApplyDept = user.DeptId; apply.ApplyCorp = user.CorpId; apply.ApplyTime = DateTime.Now; apply.ApplyDesc = string.Empty; apply.ApplyType = NFMT.Operate.ApplyType.出库申请; apply.EmpId = user.EmpId; apply.ApplyStatus = NFMT.Common.StatusEnum.绑定合约; result = applyDAL.Insert(user, apply); if (result.ResultStatus != 0) return result; int applyId = 0; if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out applyId) || applyId <= 0) { result.ResultStatus = -1; result.Message = "申请主表添加失败"; return result; } decimal sumNetAmount = details.Sum(temp => temp.NetAmount); int sumBundles = details.Sum(temp => temp.Bundles); //添加出库申请主表 NFMT.WareHouse.Model.StockOutApply outApply = new NFMT.WareHouse.Model.StockOutApply(); outApply.ApplyId = applyId; outApply.ContractId = contract.ContractId; outApply.SubContractId = subId; outApply.NetAmount = sumNetAmount; outApply.Bundles = sumBundles; outApply.UnitId = contract.UnitId; outApply.BuyCorpId = outCorpId; foreach (NFMT.WareHouse.Model.StockOutApplyDetail applyDetail in details) { //验证库存 result = stockDAL.Get(user, applyDetail.StockId); if (result.ResultStatus != 0) return result; NFMT.WareHouse.Model.Stock stock = result.ReturnValue as NFMT.WareHouse.Model.Stock; if (stock == null || stock.StockId <= 0) { result.ResultStatus = -1; result.Message = "库存不存在"; return result; } //判断库存品种是否与合约品种相同 if (stock.AssetId != contract.AssetId) { result.ResultStatus = -1; result.Message = "分配库存的品种与合约品种不一致"; return result; } //验证关境 if (contract.TradeBorder == (int)NFMT.Contract.TradeBorderEnum.外贸 && stock.CustomsType != (int)NFMT.WareHouse.CustomTypeEnum.关外) { result.ResultStatus = -1; result.Message = "库存关境与合约不一致"; return result; } else if (contract.TradeBorder == (int)NFMT.Contract.TradeBorderEnum.内贸 && stock.CustomsType != (int)NFMT.WareHouse.CustomTypeEnum.关内) { result.ResultStatus = -1; result.Message = "库存关境与合约不一致"; return result; } //不允许配货库存 int planStockInStatus = (int)NFMT.WareHouse.StockStatusEnum.预入库存; int planCustomsStatus = (int)NFMT.WareHouse.StockStatusEnum.预报关库存; int stockStatus = (int)stock.StockStatus; if (stockStatus > planCustomsStatus || stockStatus < planStockInStatus) { result.ResultStatus = -1; result.Message = "访笔库存不能进行出库申请分配"; return result; } applyDetail.ContractId = contract.ContractId; applyDetail.DetailStatus = StatusEnum.已生效; applyDetail.SubContractId = subId; if (contract.ContractId == (int)NFMT.Contract.TradeBorderEnum.ForeignTrade) applyDetail.GrossAmount = stock.GrossAmount; else applyDetail.GrossAmount = applyDetail.NetAmount; } decimal sumGrossAmount = details.Sum(temp => temp.GrossAmount); outApply.GrossAmount = sumGrossAmount; outApply.CreateFrom = (int)NFMT.Common.CreateFromEnum.销售合约库存创建; result = outApplyDAL.Insert(user, outApply); if (result.ResultStatus != 0) return result; int outApplyId = 0; if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out outApplyId) || outApplyId <= 0) { result.ResultStatus = -1; result.Message = "出库申请添加失败"; return result; } foreach (NFMT.WareHouse.Model.StockOutApplyDetail applyDetail in details) { NFMT.WareHouse.Model.StockOutApplyDetail appDetail = new NFMT.WareHouse.Model.StockOutApplyDetail(); appDetail.StockOutApplyId = outApplyId; appDetail.Bundles = applyDetail.Bundles; appDetail.ContractId = applyDetail.ContractId; appDetail.DetailStatus = StatusEnum.已生效; appDetail.GrossAmount = applyDetail.GrossAmount; appDetail.NetAmount = applyDetail.NetAmount; appDetail.StockId = applyDetail.StockId; appDetail.SubContractId = applyDetail.SubContractId; result = detailDAL.Insert(user, appDetail); if (result.ResultStatus != 0) return result; int detailApplyId = 0; if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out detailApplyId) || detailApplyId <= 0) { result.ResultStatus = -1; result.Message = "库存明细添加失败"; return result; } //验证库存 result = stockDAL.Get(user, applyDetail.StockId); if (result.ResultStatus != 0) return result; NFMT.WareHouse.Model.Stock stock = result.ReturnValue as NFMT.WareHouse.Model.Stock; if (stock == null || stock.StockId <= 0) { result.ResultStatus = -1; result.Message = "库存不存在"; return result; } //排他表校验 result = exclusiveDAL.LoadByStockId(user, stock.StockId); if (result.ResultStatus != 0) return result; List<NFMT.WareHouse.Model.StockExclusive> excs = result.ReturnValue as List<NFMT.WareHouse.Model.StockExclusive>; if (excs == null) { result.ResultStatus = -1; result.Message = "获取排他库存信息失败"; return result; } decimal excAmount = excs.Sum(temp => temp.ExclusiveAmount); if (excAmount + applyDetail.NetAmount > stock.CurNetAmount) { result.ResultStatus = -1; result.Message = "该笔库存剩余净重不足,配货失败"; return result; } //排他表新增 NFMT.WareHouse.Model.StockExclusive exculsive = new NFMT.WareHouse.Model.StockExclusive(); exculsive.ApplyId = applyId; exculsive.DetailApplyId = detailApplyId; exculsive.ExclusiveStatus = StatusEnum.已生效; exculsive.StockApplyId = outApplyId; exculsive.StockId = stock.StockId; exculsive.ExclusiveAmount = applyDetail.NetAmount; result = exclusiveDAL.Insert(user, exculsive); if (result.ResultStatus != 0) return result; } } catch (Exception ex) { result.ResultStatus = -1; result.Message = ex.Message; } return result; }
public override IModel CreateModel(SqlDataReader dr) { StockOutApplyDetail stockoutapplydetail = new StockOutApplyDetail(); int indexDetailId = dr.GetOrdinal("DetailId"); stockoutapplydetail.DetailId = Convert.ToInt32(dr[indexDetailId]); int indexStockOutApplyId = dr.GetOrdinal("StockOutApplyId"); if (dr["StockOutApplyId"] != DBNull.Value) { stockoutapplydetail.StockOutApplyId = Convert.ToInt32(dr[indexStockOutApplyId]); } int indexStockId = dr.GetOrdinal("StockId"); if (dr["StockId"] != DBNull.Value) { stockoutapplydetail.StockId = Convert.ToInt32(dr[indexStockId]); } int indexDetailStatus = dr.GetOrdinal("DetailStatus"); if (dr["DetailStatus"] != DBNull.Value) { stockoutapplydetail.DetailStatus = (StatusEnum)Convert.ToInt32(dr[indexDetailStatus]); } int indexContractId = dr.GetOrdinal("ContractId"); if (dr["ContractId"] != DBNull.Value) { stockoutapplydetail.ContractId = Convert.ToInt32(dr[indexContractId]); } int indexSubContractId = dr.GetOrdinal("SubContractId"); if (dr["SubContractId"] != DBNull.Value) { stockoutapplydetail.SubContractId = Convert.ToInt32(dr[indexSubContractId]); } int indexNetAmount = dr.GetOrdinal("NetAmount"); if (dr["NetAmount"] != DBNull.Value) { stockoutapplydetail.NetAmount = Convert.ToDecimal(dr[indexNetAmount]); } int indexGrossAmount = dr.GetOrdinal("GrossAmount"); if (dr["GrossAmount"] != DBNull.Value) { stockoutapplydetail.GrossAmount = Convert.ToDecimal(dr[indexGrossAmount]); } int indexBundles = dr.GetOrdinal("Bundles"); if (dr["Bundles"] != DBNull.Value) { stockoutapplydetail.Bundles = Convert.ToInt32(dr[indexBundles]); } return stockoutapplydetail; }