Example #1
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;
        }
Example #2
0
        public ResultModel UpdateBusinessInvoice(UserModel user, Operate.Model.Invoice invoice, BusinessInvoice invoiceBusiness, List<BusinessInvoiceDetail> details)
        {
            ResultModel result = new ResultModel();

            try
            {
                BusinessInvoiceDAL businessInvoiceDAL = new BusinessInvoiceDAL();
                InvoiceDAL invoiceDAL = new InvoiceDAL();
                BusinessInvoiceDetailDAL detailDAL = new BusinessInvoiceDetailDAL();
                StockReceiptDetailDAL stockReceiptDetailDAL = new StockReceiptDetailDAL();

                using (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;

                    BusinessInvoice resultBusinessInvoice = result.ReturnValue as 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;

                    Operate.Model.Invoice resultInvoice = result.ReturnValue as 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<BusinessInvoiceDetail> resultDetails = result.ReturnValue as List<BusinessInvoiceDetail>;
                    if (resultDetails == null)
                    {
                        result.ResultStatus = -1;
                        result.Message = "获取发票明细失败";
                        return result;
                    }

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

                        result = detailDAL.Invalid(user, detail);
                        if (result.ResultStatus != 0)
                            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 != resultBusinessInvoice.SubContractId)
                            {
                                result.ResultStatus = -1;
                                result.Message = "合约与库存流水不匹配";
                                return result;
                            }

                            detail.DetailStatus = StatusEnum.已生效;
                            detail.InvoiceId = resultInvoice.InvoiceId;
                            detail.BusinessInvoiceId = resultBusinessInvoice.BusinessInvoiceId;
                            detail.StockId = stockLog.StockId;
                            detail.StockLogId = stockLog.StockLogId;

                            if (resultInvoice.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;
                        }
                    }

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

            return result;
        }