Ejemplo n.º 1
0
        public ResultModel Update(UserModel user, NFMT.WareHouse.Model.StockReceipt stockReceipt, List<NFMT.WareHouse.Model.StockReceiptDetail> details)
        {
            ResultModel result = new ResultModel();

            try
            {
                NFMT.Contract.DAL.ContractSubDAL subDAL = new Contract.DAL.ContractSubDAL();
                NFMT.Contract.DAL.ContractDAL contractDAL = new Contract.DAL.ContractDAL();
                NFMT.WareHouse.DAL.StockDAL stockDAL = new StockDAL();
                DAL.StockLogDAL stockLogDAL = new StockLogDAL();
                DAL.StockReceiptDAL receiptDAL = new StockReceiptDAL();
                DAL.StockReceiptDetailDAL detailDAL = new StockReceiptDetailDAL();

                using (System.Transactions.TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    //验证仓库回执
                    result = receiptDAL.Get(user, stockReceipt.ReceiptId);
                    if (result.ResultStatus != 0)
                        return result;

                    Model.StockReceipt resultReceipt = result.ReturnValue as Model.StockReceipt;
                    if (resultReceipt == null || resultReceipt.ReceiptId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "仓库回执不存在";
                        return result;
                    }

                    //获取仓库回执明细
                    result = detailDAL.Load(user, resultReceipt.ReceiptId);
                    if (result.ResultStatus != 0)
                        return result;

                    List<Model.StockReceiptDetail> resultDetails = result.ReturnValue as List<Model.StockReceiptDetail>;
                    if (resultDetails == null)
                    {
                        result.ResultStatus = -1;
                        result.Message = "仓库回执明细获取失败";
                        return result;
                    }

                    //作废现有仓库回执明细
                    foreach (Model.StockReceiptDetail d in resultDetails)
                    {
                        if (d.DetailStatus == StatusEnum.已生效)
                            d.DetailStatus = StatusEnum.已录入;

                        result = detailDAL.Invalid(user, d);
                        if (result.ResultStatus != 0)
                            return result;
                    }

                    //验证合约
                    result = subDAL.Get(user, resultReceipt.ContractSubId);
                    if (result.ResultStatus != 0)
                        return result;

                    NFMT.Contract.Model.ContractSub sub = result.ReturnValue as NFMT.Contract.Model.ContractSub;
                    if (sub.SubId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "子合约不存在";
                        return result;
                    }

                    result = contractDAL.Get(user, sub.ContractId);
                    if (result.ResultStatus != 0)
                        return result;

                    NFMT.Contract.Model.Contract contract = result.ReturnValue as NFMT.Contract.Model.Contract;
                    if (contract == null || contract.ContractId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "合约不存在";
                        return result;
                    }

                    decimal sumNetAmount = 0;
                    decimal sumReceiptAmount = 0;
                    decimal sumMissAmount = 0;
                    decimal missRate = 0;

                    //明细验证
                    foreach (NFMT.WareHouse.Model.StockReceiptDetail detail in details)
                    {
                        //验证库存
                        result = stockDAL.Get(user, detail.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;
                        }

                        //验证流水
                        result = stockLogDAL.Get(user, detail.StockLogId);
                        if (result.ResultStatus != 0)
                            return result;

                        Model.StockLog stockLog = result.ReturnValue as Model.StockLog;
                        if (stockLog == null || stockLog.StockLogId <= 0)
                        {
                            result.ResultStatus = -1;
                            result.Message = "库存流水不存在";
                            return result;
                        }

                        if (stockLog.StockId != stock.StockId)
                        {
                            result.ResultStatus = -1;
                            result.Message = "库存与库存流水不匹配";
                            return result;
                        }

                        if (stockLog.SubContractId != stockReceipt.ContractSubId)
                        {
                            result.ResultStatus = -1;
                            result.Message = "库存流水与合约不匹配";
                            return result;
                        }

                        detail.ReceiptId = resultReceipt.ReceiptId;
                        detail.ContractId = sub.ContractId;
                        detail.ContractSubId = sub.SubId;
                        detail.DetailStatus = StatusEnum.已生效;
                        detail.PreNetAmount = stock.NetAmount;
                        detail.QtyMiss = detail.ReceiptAmount - stock.NetAmount;
                        detail.QtyRate = (detail.ReceiptAmount / stock.NetAmount - 1) / 100;

                        sumNetAmount += stock.NetAmount;
                        sumReceiptAmount += detail.ReceiptAmount;

                        //新增仓库回执明细
                        result = detailDAL.Insert(user, detail);
                        if (result.ResultStatus != 0)
                            return result;
                    }

                    sumMissAmount = sumReceiptAmount - sumNetAmount;
                    missRate = (sumReceiptAmount / sumNetAmount - 1) / 100;

                    //赋值StockReceipt
                    resultReceipt.PreNetAmount = sumReceiptAmount;
                    resultReceipt.QtyMiss = sumMissAmount;
                    resultReceipt.QtyRate = missRate;
                    resultReceipt.ReceiptAmount = sumReceiptAmount;
                    resultReceipt.Receipter = user.EmpId;
                    resultReceipt.Memo = stockReceipt.Memo;
                    resultReceipt.ReceiptDate = stockReceipt.ReceiptDate;
                    if (contract.TradeDirection == (int)NFMT.Contract.TradeDirectionEnum.Buy)
                        resultReceipt.ReceiptType = (int)WareHouse.ReceiptTypeEnum.StockInReceipt;
                    else
                        resultReceipt.ReceiptType = (int)WareHouse.ReceiptTypeEnum.StockOutReceipt;
                    resultReceipt.UnitId = sub.UnitId;

                    //修改StockReceipt
                    result = stockreceiptDAL.Update(user, resultReceipt);
                    if (result.ResultStatus != 0)
                        return result;

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                result.ResultStatus = -1;
                result.Message = ex.Message;
            }

            return result;
        }
Ejemplo n.º 2
0
        public ResultModel Invalid(UserModel user, int receiptId)
        {
            ResultModel result = new ResultModel();

            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    result = this.stockreceiptDAL.Get(user, receiptId);
                    if (result.ResultStatus != 0)
                        return result;

                    StockReceipt stockReceipt = result.ReturnValue as StockReceipt;

                    if (stockReceipt == null || stockReceipt.ReceiptId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "该数据不存在,不能作废";
                        return result;
                    }

                    result = this.stockreceiptDAL.Invalid(user, stockReceipt);
                    if (result.ResultStatus != 0)
                        return result;

                    StockReceiptDetailDAL detailDAL = new StockReceiptDetailDAL();
                    result = detailDAL.Load(user, stockReceipt.ReceiptId);
                    if (result.ResultStatus != 0)
                        return result;

                    List<Model.StockReceiptDetail> details = result.ReturnValue as List<Model.StockReceiptDetail>;
                    if (details == null)
                    {
                        result.ResultStatus = -1;
                        result.Message = "获取回执明细失败";
                        return result;
                    }
                    foreach (Model.StockReceiptDetail detail in details)
                    {
                        if (detail.DetailStatus == StatusEnum.已生效)
                            detail.DetailStatus = StatusEnum.已录入;

                        result = detailDAL.Invalid(user, detail);
                        if (result.ResultStatus != 0)
                            return result;
                    }

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                result.ResultStatus = -1;
                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;
        }