Example #1
0
        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;
        }
Example #2
0
        public override IModel CreateModel(SqlDataReader dr)
        {
            StockOutDetail stockoutdetail = new StockOutDetail();

            int indexDetailId = dr.GetOrdinal("DetailId");
            stockoutdetail.DetailId = Convert.ToInt32(dr[indexDetailId]);

            int indexStockOutId = dr.GetOrdinal("StockOutId");
            stockoutdetail.StockOutId = Convert.ToInt32(dr[indexStockOutId]);

            int indexDetailStatus = dr.GetOrdinal("DetailStatus");
            if (dr["DetailStatus"] != DBNull.Value)
            {
                stockoutdetail.DetailStatus = (StatusEnum)Convert.ToInt32(dr[indexDetailStatus]);
            }

            int indexStockId = dr.GetOrdinal("StockId");
            if (dr["StockId"] != DBNull.Value)
            {
                stockoutdetail.StockId = Convert.ToInt32(dr[indexStockId]);
            }

            int indexStockOutApplyDetailId = dr.GetOrdinal("StockOutApplyDetailId");
            if (dr["StockOutApplyDetailId"] != DBNull.Value)
            {
                stockoutdetail.StockOutApplyDetailId = Convert.ToInt32(dr[indexStockOutApplyDetailId]);
            }

            int indexNetAmount = dr.GetOrdinal("NetAmount");
            if (dr["NetAmount"] != DBNull.Value)
            {
                stockoutdetail.NetAmount = Convert.ToDecimal(dr[indexNetAmount]);
            }

            int indexStockLogId = dr.GetOrdinal("StockLogId");
            if (dr["StockLogId"] != DBNull.Value)
            {
                stockoutdetail.StockLogId = Convert.ToInt32(dr[indexStockLogId]);
            }

            int indexGrossAmount = dr.GetOrdinal("GrossAmount");
            if (dr["GrossAmount"] != DBNull.Value)
            {
                stockoutdetail.GrossAmount = Convert.ToDecimal(dr[indexGrossAmount]);
            }

            int indexBundles = dr.GetOrdinal("Bundles");
            if (dr["Bundles"] != DBNull.Value)
            {
                stockoutdetail.Bundles = Convert.ToInt32(dr[indexBundles]);
            }

            return stockoutdetail;
        }