Esempio n. 1
0
        public ResultModel CreateStockReceipt(UserModel user, NFMT.WareHouse.Model.StockReceipt stockReceipt, List<NFMT.WareHouse.Model.StockReceiptDetail> details, List<NFMT.WareHouse.Model.StockReceiptDetail> saleDetails, bool isAudit)
        {
            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();
                NFMT.WareHouse.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 = subDAL.Get(user, stockReceipt.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)
                    {
                        if (detail.ReceiptAmount > 0)
                        {
                            //验证库存
                            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.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;
                        }
                    }

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

                    //赋值StockReceipt
                    stockReceipt.ContractId = sub.ContractId;
                    stockReceipt.ContractSubId = sub.SubId;
                    stockReceipt.PreNetAmount = sumNetAmount;
                    stockReceipt.QtyMiss = sumMissAmount;
                    stockReceipt.QtyRate = missRate;
                    stockReceipt.ReceiptAmount = sumReceiptAmount;
                    stockReceipt.Receipter = user.EmpId;
                    stockReceipt.ReceiptDate = DateTime.Now;

                    if (contract.TradeDirection == (int)NFMT.Contract.TradeDirectionEnum.Buy)
                        stockReceipt.ReceiptType = (int)WareHouse.ReceiptTypeEnum.StockInReceipt;
                    else
                        stockReceipt.ReceiptType = (int)WareHouse.ReceiptTypeEnum.StockOutReceipt;

                    stockReceipt.UnitId = sub.UnitId;

                    //新增StockReceipt
                    result = stockreceiptDAL.Insert(user, stockReceipt);
                    if (result.ResultStatus != 0)
                        return result;

                    int receiptId = 0;
                    if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out receiptId))
                    {
                        result.ResultStatus = -1;
                        result.Message = "新增库存回执失败";
                        return result;
                    }

                    //新增明细
                    foreach (NFMT.WareHouse.Model.StockReceiptDetail detail in details)
                    {
                        detail.ReceiptId = receiptId;
                        result = detailDAL.Insert(user, detail);
                        if (result.ResultStatus != 0)
                            return result;
                    }

                    stockReceipt.ReceiptId = receiptId;
                    string resultStr = Newtonsoft.Json.JsonConvert.SerializeObject(stockReceipt);

                    //if(isAudit)
                    //{
                    //    stockReceipt.ReceiptId = receiptId;
                    //    NFMT.WorkFlow.AutoSubmit submit = new NFMT.WorkFlow.AutoSubmit();
                    //    result = submit.Submit(user, stockReceipt, new TaskProvider.StockReceiptTaskProvider(), NFMT.WorkFlow.MasterEnum.库存净重回执审核);
                    //    if (result.ResultStatus != 0)
                    //        return result;
                    //}

                    #region 销售合约库存回执

                    if (saleDetails != null && saleDetails.Any())
                    {
                        foreach (int subId in saleDetails.Select(a => a.ContractSubId).Distinct())
                        {
                            //验证合约
                            result = subDAL.Get(user, subId);
                            if (result.ResultStatus != 0)
                                return result;

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

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

                            //明细验证
                            foreach (NFMT.WareHouse.Model.StockReceiptDetail detail in saleDetails.Where(a => a.ContractSubId == subId))
                            {
                                if (detail.ReceiptAmount > 0)
                                {
                                    //验证库存
                                    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 != subId)
                                    {
                                        result.ResultStatus = -1;
                                        result.Message = "库存流水与合约不匹配";
                                        return result;
                                    }

                                    detail.ContractId = saleSub.ContractId;
                                    detail.ContractSubId = saleSub.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;
                                }
                            }

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

                            //赋值StockReceipt
                            stockReceipt.ContractId = saleSub.ContractId;
                            stockReceipt.ContractSubId = saleSub.SubId;
                            stockReceipt.PreNetAmount = sumNetAmount;
                            stockReceipt.ReceiptAmount = sumReceiptAmount;
                            stockReceipt.UnitId = saleSub.UnitId;
                            stockReceipt.QtyMiss = sumMissAmount;
                            stockReceipt.QtyRate = missRate;
                            stockReceipt.Receipter = user.EmpId;
                            stockReceipt.ReceiptDate = DateTime.Now;

                            if (saleSub.TradeDirection == (int)NFMT.Contract.TradeDirectionEnum.Buy)
                                stockReceipt.ReceiptType = (int)WareHouse.ReceiptTypeEnum.StockInReceipt;
                            else
                                stockReceipt.ReceiptType = (int)WareHouse.ReceiptTypeEnum.StockOutReceipt;

                            //新增StockReceipt
                            result = stockreceiptDAL.Insert(user, stockReceipt);
                            if (result.ResultStatus != 0)
                                return result;

                            receiptId = 0;
                            if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out receiptId))
                            {
                                result.ResultStatus = -1;
                                result.Message = "新增库存回执失败";
                                return result;
                            }

                            //新增明细
                            foreach (NFMT.WareHouse.Model.StockReceiptDetail detail in saleDetails.Where(a => a.ContractSubId == subId))
                            {
                                detail.ReceiptId = receiptId;
                                result = detailDAL.Insert(user, detail);
                                if (result.ResultStatus != 0)
                                    return result;
                            }
                        }

                        //if (isAudit)
                        //{
                        //    stockReceipt.ReceiptId = receiptId;
                        //    NFMT.WorkFlow.AutoSubmit submit = new NFMT.WorkFlow.AutoSubmit();
                        //    result = submit.Submit(user, stockReceipt, new TaskProvider.StockReceiptTaskProvider(), NFMT.WorkFlow.MasterEnum.库存净重回执审核);
                        //    if (result.ResultStatus != 0)
                        //        return result;
                        //}
                    }

                    #endregion

                    stockReceipt.ReceiptId = receiptId;
                    if (result.ResultStatus == 0)
                    {
                        if (saleDetails != null && saleDetails.Any())
                            result.ReturnValue = stockReceipt;
                        else
                            result.ReturnValue = null;

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

            return result;
        }
Esempio n. 2
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;
        }