Esempio n. 1
0
        public ResultModel PayApplyCreate(UserModel user, Apply apply, PayApply payApply, List<StockPayApply> stockDetails, int subId, bool isAudit)
        {
            ResultModel result = new ResultModel();

            try
            {
                ApplyDAL applyDAL = new ApplyDAL();
                ContractPayApplyDAL contractPayApplyDAL = new ContractPayApplyDAL();
                StockPayApplyDAL stockPayApplyDAL = new StockPayApplyDAL();
                StockDAL stockDAL = new StockDAL();
                StockLogDAL stockLogDAL = new StockLogDAL();
                ContractSubDAL subDAL = new ContractSubDAL();

                using (TransactionScope scope = new TransactionScope())
                {
                    //新增主申请
                    apply.ApplyStatus = StatusEnum.已录入;
                    apply.ApplyType = ApplyType.PayApply;
                    apply.EmpId = user.EmpId;
                    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.Message = "主申请新增失败";
                        result.ResultStatus = -1;
                        return result;
                    }
                    apply.ApplyId = applyId;

                    //新增付款申请
                    payApply.ApplyId = apply.ApplyId;
                    payApply.PayApplySource = (int)FundsStyleEnum.ContractPayApply;
                    if (stockDetails != null && stockDetails.Count > 0)
                        payApply.PayApplySource = (int)FundsStyleEnum.StockPayApply;

                    result = this.payapplyDAL.Insert(user, payApply);
                    if (result.ResultStatus != 0)
                        return result;

                    int payApplyId = 0;
                    if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out payApplyId) || payApplyId <= 0)
                    {
                        result.Message = "付款申请新增失败";
                        result.ResultStatus = -1;
                        return result;
                    }
                    payApply.PayApplyId = payApplyId;

                    //获取子合约
                    result = subDAL.Get(user, subId);
                    if (result.ResultStatus != 0)
                        return result;

                    ContractSub sub = result.ReturnValue as ContractSub;
                    if (sub == null || sub.SubId <= 0)
                    {
                        result.Message = "子合约获取失败";
                        result.ResultStatus = -1;
                        return result;
                    }

                    if (sub.SubStatus != StatusEnum.已生效)
                    {
                        result.Message = "子合约状态不正确,不能进行付款申请";
                        result.ResultStatus = -1;
                        return result;
                    }
                    //新增合约付款申请
                    ContractPayApply contractPayApply = new ContractPayApply();
                    contractPayApply.ApplyBala = payApply.ApplyBala;
                    contractPayApply.ContractId = sub.ContractId;
                    contractPayApply.ContractSubId = sub.SubId;
                    contractPayApply.PayApplyId = payApply.PayApplyId;
                    contractPayApply.Status = StatusEnum.已录入;
                    result = contractPayApplyDAL.Insert(user, contractPayApply);
                    if (result.ResultStatus != 0)
                        return result;

                    int contractPayApplyId = 0;
                    if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out contractPayApplyId) || contractPayApplyId <= 0)
                    {
                        result.Message = "合约关联新增失败";
                        result.ResultStatus = -1;
                        return result;
                    }
                    contractPayApply.RefId = contractPayApplyId;

                    //新增库存付款申请
                    if (stockDetails != null && stockDetails.Count > 0)
                    {
                        foreach (StockPayApply detail in stockDetails)
                        {
                            detail.ContractId = contractPayApply.ContractId;
                            detail.ContractRefId = contractPayApply.RefId;
                            detail.PayApplyId = contractPayApply.PayApplyId;
                            detail.RefStatus = StatusEnum.已生效;
                            detail.SubId = contractPayApply.ContractSubId;
                            result = stockPayApplyDAL.Insert(user, detail);
                            if (result.ResultStatus != 0)
                                return result;
                        }
                    }

                    if (result.ResultStatus == 0)
                        result.ReturnValue = payApply;

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

            return result;
        }
Esempio n. 2
0
        public ResultModel PayApplyContractUpdate(UserModel user, PayApply payApply, string memo, int deptId, int corpId)
        {
            ResultModel result = new ResultModel();

            try
            {
                ContractSubDAL subDAL = new ContractSubDAL();
                ApplyDAL applyDAL = new ApplyDAL();
                PayApplyDAL payApplyDAL = new PayApplyDAL();
                ContractPayApplyDAL contractPayApplyDAL = new ContractPayApplyDAL();

                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    //验证付款申请
                    result = payApplyDAL.Get(user, payApply.PayApplyId);
                    if (result.ResultStatus != 0)
                        return result;
                    PayApply resultObj = result.ReturnValue as PayApply;
                    if (resultObj == null || resultObj.PayApplyId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "付款申请不存在";
                        return result;
                    }

                    //验证申请主表
                    result = applyDAL.Get(user, resultObj.ApplyId);
                    if (result.ResultStatus != 0)
                        return result;
                    Apply apply = result.ReturnValue as Apply;
                    if (apply == null || apply.ApplyId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "申请主表不存在";
                        return result;
                    }

                    //验证合约付款申请关联表
                    result = contractPayApplyDAL.GetByPayApplyId(user, resultObj.PayApplyId);
                    if (result.ResultStatus != 0)
                        return result;
                    ContractPayApply contractPayApply = result.ReturnValue as ContractPayApply;
                    if (contractPayApply == null || contractPayApply.RefId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "与合约关联错误";
                        return result;
                    }

                    //验证子合约信息
                    result = subDAL.Get(user, contractPayApply.ContractSubId);
                    if (result.ResultStatus != 0)
                        return result;
                    ContractSub sub = result.ReturnValue as ContractSub;
                    if (sub == null || sub.SubId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "子合约不存在";
                        return result;
                    }

                    //验证可申请金额
                    //NFMT.Data.Model.FuturesPrice futuresPrice = NFMT.Data.BasicDataProvider.FuturesPrices[0];

                    //更新申请主表
                    Department dept = UserProvider.Departments.FirstOrDefault(temp => temp.DeptId == deptId);
                    if (dept == null || dept.DeptId < 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "申请部门不存在";
                        return result;
                    }

                    apply.ApplyDept = dept.DeptId;
                    apply.ApplyCorp = corpId;
                    apply.ApplyDesc = memo;
                    apply.ApplyType = ApplyType.付款申请;
                    apply.EmpId = user.EmpId;

                    result = applyDAL.Update(user, apply);
                    if (result.ResultStatus != 0)
                        return result;

                    //更新付款申请表
                    resultObj.PayApplySource = (int)FundsStyleEnum.ContractPayApply;
                    resultObj.RecCorpId = payApply.RecCorpId;
                    resultObj.RecBankId = payApply.RecBankId;
                    resultObj.RecBankAccountId = payApply.RecBankAccountId;
                    resultObj.RecBankAccount = payApply.RecBankAccount;
                    resultObj.ApplyBala = payApply.ApplyBala;
                    resultObj.CurrencyId = payApply.CurrencyId;
                    resultObj.PayMode = payApply.PayMode;
                    resultObj.PayDeadline = payApply.PayDeadline;
                    resultObj.PayMatter = payApply.PayMatter;
                    resultObj.SpecialDesc = payApply.SpecialDesc;

                    result = payapplyDAL.Update(user, resultObj);
                    if (result.ResultStatus != 0)
                        return result;

                    //更新Fun_ContractPayApply_Ref合约付款申请
                    contractPayApply.ApplyBala = payApply.ApplyBala;
                    result = contractPayApplyDAL.Update(user, contractPayApply);
                    if (result.ResultStatus != 0)
                        return result;

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

            return result;
        }
Esempio n. 3
0
        public ResultModel PayApplyContractCreate(UserModel user, PayApply payApply, int subId, string memo, int deptId, int corpId)
        {
            ResultModel result = new ResultModel();

            try
            {
                ContractSubDAL subDAL = new ContractSubDAL();
                ApplyDAL applyDAL = new ApplyDAL();
                PayApplyDAL payApplyDAL = new PayApplyDAL();
                ContractPayApplyDAL contractPayApplyDAL = new ContractPayApplyDAL();

                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    //验证子合约信息
                    result = subDAL.Get(user, subId);
                    if (result.ResultStatus != 0)
                        return result;
                    ContractSub sub = result.ReturnValue as ContractSub;
                    if (sub == null || sub.SubId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "子合约不存在";
                        return result;
                    }

                    //验证可申请金额
                    //NFMT.Data.Model.FuturesPrice futuresPrice = NFMT.Data.BasicDataProvider.FuturesPrices[0];

                    //新增申请主表
                    Department dept = UserProvider.Departments.FirstOrDefault(temp => temp.DeptId == deptId);
                    if (dept == null || dept.DeptId < 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "申请部门不存在";
                        return result;
                    }

                    int applyId = 0;
                    Apply apply = new Apply();

                    apply.ApplyDept = dept.DeptId;
                    apply.ApplyCorp = corpId;
                    apply.ApplyDesc = memo;
                    apply.ApplyTime = DateTime.Now;
                    apply.ApplyType = ApplyType.付款申请;
                    apply.EmpId = user.EmpId;

                    result = applyDAL.Insert(user, apply);
                    if (result.ResultStatus != 0)
                        return result;

                    if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out applyId))
                    {
                        result.ResultStatus = -1;
                        result.Message = "申请主表新增失败";
                        return result;
                    }

                    //新增付款申请表
                    payApply.ApplyId = applyId;
                    payApply.PayApplySource = (int)FundsStyleEnum.ContractPayApply;
                    result = payapplyDAL.Insert(user, payApply);
                    if (result.ResultStatus != 0)
                        return result;
                    int payApplyId = 0;
                    if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out payApplyId))
                    {
                        result.ResultStatus = -1;
                        result.Message = "付款申请新增失败";
                        return result;
                    }

                    //新增Fun_ContractPayApply_Ref合约付款申请
                    ContractPayApply contractPayApply = new ContractPayApply();
                    contractPayApply.ContractId = sub.ContractId;
                    contractPayApply.ContractSubId = sub.SubId;
                    contractPayApply.PayApplyId = payApplyId;
                    contractPayApply.ApplyBala = payApply.ApplyBala;

                    result = contractPayApplyDAL.Insert(user, contractPayApply);
                    if (result.ResultStatus != 0)
                        return result;

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

            return result;
        }
Esempio n. 4
0
        public ResultModel CreateDirect(UserModel user, Operate.Model.Invoice invoice, BusinessInvoice invoiceBusiness, List<BusinessInvoiceDetail> details)
        {
            ResultModel result = new ResultModel();

            ContractSubDAL subDAL = new ContractSubDAL();
            result = subDAL.Get(user, invoiceBusiness.SubContractId);
            if(result.ResultStatus!=0)
                return result;

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

            StockLogDAL stockLogDAL = new StockLogDAL();
            //明细表数据:毛重、金额计算
            foreach (BusinessInvoiceDetail detail in details)
            {
                detail.Bala = Math.Round(detail.NetAmount * detail.UnitPrice, 2, MidpointRounding.AwayFromZero);

                if (sub.TradeBorder == (int)TradeBorderEnum.内贸)
                {
                    //内贸毛重==净重
                    detail.IntegerAmount = detail.NetAmount;
                }
                else
                {
                    //整笔开票情况下,外贸毛重等于库存流水毛重;分笔开票情况下,等于净重。
                    result = stockLogDAL.Get(user, detail.StockLogId);
                    if (result.ResultStatus != 0)
                        return result;

                    StockLog stockLog = result.ReturnValue as StockLog;
                    if (stockLog == null || stockLog.StockLogId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "当前合约不存在该笔库存";
                        return result;
                    }

                    if (stockLog.NetAmount == detail.NetAmount)
                    {
                        detail.IntegerAmount = stockLog.GrossAmount;
                    }
                    else
                    {
                        detail.IntegerAmount = detail.NetAmount;
                    }
                }
            }

            //主表数据:毛重、净重、均价、总额计算
            decimal sumGrossAmount = details.Sum(temp => temp.IntegerAmount);
            invoiceBusiness.IntegerAmount = sumGrossAmount;

            decimal sumNetAmount = details.Sum(temp => temp.NetAmount);
            if (sumNetAmount != invoiceBusiness.NetAmount)
            {
                result.ResultStatus = -1;
                result.Message = "明细净重之和与总净重不相等";
                return result;
            }

            decimal sumBala = details.Sum(temp => temp.Bala);
            if (Math.Abs(sumBala - invoice.InvoiceBala) >= 1)
            {
                result.ResultStatus = -1;
                result.Message = "明细金额之和与总金额不相等";
                return result;
            }

            decimal avgPrice = Math.Round(sumBala / sumNetAmount, 4, MidpointRounding.AwayFromZero);
            if (avgPrice != invoiceBusiness.UnitPrice)
            {
                result.ResultStatus = -1;
                result.Message = "计算均价错误";
                return result;
            }

            //验证是否已开临票
            //result = this.businessinvoiceDAL.LoadBySubId(user, invoiceBusiness.SubContractId, InvoiceTypeEnum.ProvisionalInvoice);
            //if (result.ResultStatus != 0)
            //    return result;

            //List<Model.BusinessInvoice> proInvoices = result.ReturnValue as List<Model.BusinessInvoice>;
            //if (proInvoices == null)
            //{
            //    result.ResultStatus = -1;
            //    result.Message = "验证是否拥有临票失败";
            //    return result;
            //}
            //if (proInvoices.Count > 0)
            //{
            //    result.ResultStatus = -1;
            //    result.Message = "当前合约已拥有临票,直接终票失败";
            //    return result;
            //}

            result = this.CreateBusinessInvoice(user, invoice, invoiceBusiness, details, InvoiceTypeEnum.DirectFinalInvoice);

            return result;
        }
Esempio n. 5
0
        public ResultModel CreateBusinessInvoice(UserModel user, Operate.Model.Invoice invoice, BusinessInvoice invoiceBusiness, List<BusinessInvoiceDetail> details, InvoiceTypeEnum invoiceType)
        {
            ResultModel result = new ResultModel();

            try
            {
                BusinessInvoiceDAL businessInvoiceDAL = new BusinessInvoiceDAL();
                InvoiceDAL invoiceDAL = new InvoiceDAL();
                BusinessInvoiceDetailDAL detailDAL = new BusinessInvoiceDetailDAL();
                ContractSubDAL subDAL = new ContractSubDAL();
                StockReceiptDetailDAL stockReceiptDetailDAL = new StockReceiptDetailDAL();
                SubDetailDAL subDetailDAL = new SubDetailDAL();

                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    //验证子合约
                    result = subDAL.Get(user, invoiceBusiness.SubContractId);
                    if (result.ResultStatus != 0)
                        return result;

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

                    if (sub.SubStatus != StatusEnum.已生效)
                    {
                        result.ResultStatus = -1;
                        result.Message = "子合约非已生效状态,不能新增临票";
                        return result;
                    }

                    //获取子合约明细
                    result = subDetailDAL.GetDetailBySubId(user, sub.SubId);
                    if (result.ResultStatus != 0)
                        return result;

                    SubDetail subDetail = result.ReturnValue as SubDetail;
                    if (subDetail == null || subDetail.SubDetailId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "子合约明细获取失败";
                        return result;
                    }

                    //获取子合约下所有
                    result = this.businessinvoiceDAL.LoadBySubId(user, invoiceBusiness.SubContractId, invoiceType);
                    if (result.ResultStatus != 0)
                        return result;

                    List<BusinessInvoice> bis = result.ReturnValue as List<BusinessInvoice>;
                    decimal sumNetAmount = bis.Sum(temp => temp.NetAmount);
                    decimal maxNetAmount = sub.SignAmount * (1 + subDetail.MoreOrLess);
                    //if (maxNetAmount < sumNetAmount + invoiceBusiness.NetAmount)
                    //{
                    //    result.ResultStatus = -1;
                    //    result.Message = "开票重量超额";
                    //    return result;
                    //}

                    //新增发票主表
                    int invoiceId = 0;
                    invoice.InvoiceType = (int)invoiceType;
                    invoice.InvoiceStatus = StatusEnum.已录入;
                    Corporation outCorp = UserProvider.Corporations.FirstOrDefault(temp => temp.CorpId == invoice.OutCorpId);
                    if (outCorp == null || outCorp.CorpId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "开票公司信息错误";
                        return result;
                    }
                    invoice.OutCorpName = outCorp.CorpName;

                    Corporation inCorp = UserProvider.Corporations.FirstOrDefault(temp => temp.CorpId == invoice.InCorpId);
                    if (inCorp == null || inCorp.CorpId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "收票公司信息错误";
                        return result;
                    }
                    invoice.InCorpName = inCorp.CorpName;

                    result = invoiceDAL.Insert(user, invoice);
                    if (result.ResultStatus != 0)
                        return result;

                    if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out invoiceId))
                    {
                        result.ResultStatus = -1;
                        result.Message = "发票添加失败";
                        return result;
                    }

                    //新增业务发票表
                    int businessInvoiceId = 0;
                    invoiceBusiness.InvoiceId = invoiceId;
                    result = businessInvoiceDAL.Insert(user, invoiceBusiness);
                    if (result.ResultStatus != 0)
                        return result;

                    if (result.ReturnValue == null || !int.TryParse(result.ReturnValue.ToString(), out businessInvoiceId))
                    {
                        result.ResultStatus = -1;
                        result.Message = "新增发票失败";
                        return result;
                    }

                    //新增业务发票明细
                    StockLogDAL stockLogDAL = new StockLogDAL();
                    StockDAL stockDAL = new StockDAL();
                    foreach (BusinessInvoiceDetail detail in details)
                    {
                        if (detail.NetAmount != 0 && detail.StockId > 0)
                        {
                            //验证库存
                            result = stockDAL.Get(user, detail.StockId);
                            if (result.ResultStatus != 0)
                                return result;
                            Stock stock = result.ReturnValue as 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;

                            StockLog stockLog = result.ReturnValue as 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 != invoiceBusiness.SubContractId)
                            {
                                result.ResultStatus = -1;
                                result.Message = "合约与库存流水不匹配";
                                return result;
                            }

                            detail.DetailStatus = StatusEnum.已生效;
                            detail.InvoiceId = invoiceId;
                            detail.BusinessInvoiceId = businessInvoiceId;
                            detail.StockId = stockLog.StockId;
                            detail.StockLogId = stockLog.StockLogId;

                            if (invoice.InvoiceType == (int)InvoiceTypeEnum.SuppleFinalInvoice)
                            {
                                //获取当前流水回执信息
                                result = stockReceiptDetailDAL.LoadByStockLogId(user, stockLog.StockLogId);
                                List<StockReceiptDetail> stockReceiptDetails = result.ReturnValue as List<StockReceiptDetail>;
                                if (stockReceiptDetails == null)
                                {
                                    result.ResultStatus = -1;
                                    result.Message = "当前流水未回执,不能补零";
                                    return result;
                                }

                                decimal sumQtyMiss = stockReceiptDetails.Sum(temp => temp.QtyMiss);
                                detail.IntegerAmount = sumQtyMiss;
                                detail.NetAmount = sumQtyMiss;
                            }
                            //else
                            //{
                            //    detail.IntegerAmount = stockLog.GrossAmount;
                            //    detail.NetAmount = stockLog.NetAmount;
                            //}
                            result = detailDAL.Insert(user, detail);
                            if (result.ResultStatus != 0)
                                return result;
                        }
                    }

                    invoice.InvoiceId = invoiceId;
                    if (result.ResultStatus == 0) result.ReturnValue = invoice;

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

            return result;
        }
Esempio n. 6
0
        public ResultModel UpdateDirect(UserModel user, Operate.Model.Invoice invoice, BusinessInvoice invoiceBusiness, List<BusinessInvoiceDetail> details)
        {
            ResultModel result = new ResultModel();
            ContractSubDAL subDAL = new ContractSubDAL();
            result = subDAL.Get(user, invoiceBusiness.SubContractId);
            if (result.ResultStatus != 0)
                return result;

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

            StockLogDAL stockLogDAL = new StockLogDAL();
            //明细表数据:毛重、金额计算
            foreach (BusinessInvoiceDetail detail in details)
            {
                detail.Bala = Math.Round(detail.NetAmount * detail.UnitPrice, 2, MidpointRounding.AwayFromZero);

                if (sub.TradeBorder == (int)TradeBorderEnum.内贸)
                {
                    //内贸毛重==净重
                    detail.IntegerAmount = detail.NetAmount;
                }
                else
                {
                    //整笔开票情况下,外贸毛重等于库存流水毛重;分笔开票情况下,等于净重。
                    result = stockLogDAL.Get(user, detail.StockLogId);
                    if (result.ResultStatus != 0)
                        return result;

                    StockLog stockLog = result.ReturnValue as StockLog;
                    if (stockLog == null || stockLog.StockLogId <= 0)
                    {
                        result.ResultStatus = -1;
                        result.Message = "当前合约不存在该笔库存";
                        return result;
                    }

                    if (stockLog.NetAmount == detail.NetAmount)
                    {
                        detail.IntegerAmount = stockLog.GrossAmount;
                    }
                    else
                    {
                        detail.IntegerAmount = detail.NetAmount;
                    }
                }
            }

            //主表数据:毛重、净重、均价、总额计算
            decimal sumGrossAmount = details.Sum(temp => temp.IntegerAmount);
            invoiceBusiness.IntegerAmount = sumGrossAmount;

            decimal sumNetAmount = details.Sum(temp => temp.NetAmount);
            if (sumNetAmount != invoiceBusiness.NetAmount)
            {
                result.ResultStatus = -1;
                result.Message = "明细净重之和与总净重不相等";
                return result;
            }

            decimal sumBala = details.Sum(temp => temp.Bala);
            if (Math.Abs(sumBala - invoice.InvoiceBala) >=1)
            {
                result.ResultStatus = -1;
                result.Message = "明细金额之和与总金额不相等";
                return result;
            }

            decimal avgPrice = Math.Round(sumBala / sumNetAmount, 4, MidpointRounding.AwayFromZero);
            if (avgPrice != invoiceBusiness.UnitPrice)
            {
                result.ResultStatus = -1;
                result.Message = "计算均价错误";
                return result;
            }

            return this.UpdateBusinessInvoice(user, invoice, invoiceBusiness, details);
            //ResultModel result = new ResultModel();

            //try
            //{
            //    DAL.BusinessInvoiceDAL businessInvoiceDAL = new BusinessInvoiceDAL();
            //    NFMT.Operate.DAL.InvoiceDAL invoiceDAL = new Operate.DAL.InvoiceDAL();
            //    DAL.BusinessInvoiceDetailDAL detailDAL = new BusinessInvoiceDetailDAL();

            //    using (System.Transactions.TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
            //    {
            //        //验证发票
            //        if (invoiceBusiness.BusinessInvoiceId <= 0)
            //        {
            //            result.Message = "直发票不存在";
            //            return result;
            //        }

            //        result = businessinvoiceDAL.Get(user, invoiceBusiness.BusinessInvoiceId);
            //        if (result.ResultStatus != 0)
            //            return result;

            //        NFMT.Invoice.Model.BusinessInvoice resultBusinessInvoice = result.ReturnValue as NFMT.Invoice.Model.BusinessInvoice;
            //        if (resultBusinessInvoice == null || resultBusinessInvoice.BusinessInvoiceId <= 0)
            //        {
            //            result.ResultStatus = -1;
            //            result.Message = "发票不存在";
            //            return result;
            //        }

            //        //更新发票
            //        resultBusinessInvoice.IntegerAmount = invoiceBusiness.IntegerAmount;
            //        resultBusinessInvoice.NetAmount = invoiceBusiness.NetAmount;
            //        resultBusinessInvoice.VATRatio = invoiceBusiness.VATRatio;
            //        resultBusinessInvoice.VATBala = invoiceBusiness.VATBala;
            //        resultBusinessInvoice.UnitPrice = invoiceBusiness.UnitPrice;

            //        resultBusinessInvoice.Status = StatusEnum.已录入;

            //        result = businessinvoiceDAL.Update(user, resultBusinessInvoice);
            //        if (result.ResultStatus != 0)
            //            return result;

            //        //获取发票主表
            //        result = invoiceDAL.Get(user, resultBusinessInvoice.InvoiceId);
            //        if (result.ResultStatus != 0)
            //            return result;

            //        NFMT.Operate.Model.Invoice resultInvoice = result.ReturnValue as NFMT.Operate.Model.Invoice;
            //        if (resultInvoice == null || resultInvoice.InvoiceId <= 0)
            //        {
            //            result.ResultStatus = -1;
            //            result.Message = "发票不存在";
            //            return result;
            //        }

            //        //更新发票
            //        resultInvoice.InvoiceDate = invoice.InvoiceDate;
            //        resultInvoice.InvoiceName = invoice.InvoiceName;
            //        resultInvoice.InvoiceBala = invoice.InvoiceBala;
            //        resultInvoice.OutCorpId = invoice.OutCorpId;
            //        resultInvoice.InCorpId = invoice.InCorpId;
            //        resultInvoice.Memo = invoice.Memo;

            //        result = invoiceDAL.Update(user, resultInvoice);
            //        if (result.ResultStatus != 0)
            //            return result;

            //        //获取发票明细
            //        result = detailDAL.Load(user, resultBusinessInvoice.BusinessInvoiceId);
            //        if (result.ResultStatus != 0)
            //            return result;

            //        List<Model.BusinessInvoiceDetail> resultDetails = result.ReturnValue as List<Model.BusinessInvoiceDetail>;
            //        if (resultDetails == null)
            //        {
            //            result.ResultStatus = -1;
            //            result.Message = "获取发票明细失败";
            //            return result;
            //        }

            //        //作废发票明细
            //        foreach (Model.BusinessInvoiceDetail detail in resultDetails)
            //        {
            //            if (detail.DetailStatus == StatusEnum.已生效)
            //                detail.DetailStatus = StatusEnum.已录入;

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

            //        //新增发票明细
            //        foreach (Model.BusinessInvoiceDetail detail in details)
            //        {
            //            if (detail.NetAmount > 0 && detail.Bala > 0 && detail.StockId > 0)
            //            {
            //                detail.DetailStatus = StatusEnum.已生效;
            //                detail.InvoiceId = invoice.InvoiceId;
            //                detail.BusinessInvoiceId = invoiceBusiness.BusinessInvoiceId;
            //                result = detailDAL.Insert(user, detail);
            //                if (result.ResultStatus != 0)
            //                    return result;
            //            }
            //        }

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

            //return result;
        }