public ResultModel PayApplyInvoiceUpdate(UserModel user, Apply apply, PayApply payApply, List<InvoicePayApply> details) { ResultModel result = new ResultModel(); try { InvoicePayApplyDAL invoicePayApplyDAL = new InvoicePayApplyDAL(); ApplyDAL applyDAL = new ApplyDAL(); PayApplyDAL payApplyDAL = new PayApplyDAL(); InvoiceDAL invoiceDAL = new InvoiceDAL(); SIDAL sIDAL = new SIDAL(); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { if (details == null || details.Count == 0) { result.ResultStatus = -1; result.Message = "未选中任务发票"; return result; } //验证总额 decimal sumApplyBala = details.Sum(temp => temp.ApplyBala); if (sumApplyBala != payApply.ApplyBala) { result.ResultStatus = -1; result.Message = "申请总额与分项总额不相等"; return result; } //获取付款申请 result = this.payapplyDAL.Get(user, payApply.PayApplyId); if (result.ResultStatus != 0) return result; PayApply resultPayApply = result.ReturnValue as PayApply; if (resultPayApply == null || resultPayApply.PayApplyId <= 0) { result.ResultStatus = -1; result.Message = "付款申请不存在"; return result; } //获取发票申请列表 result = invoicePayApplyDAL.Load(user, resultPayApply.PayApplyId); if (result.ResultStatus != 0) return result; List<InvoicePayApply> resultDetails = result.ReturnValue as List<InvoicePayApply>; if (resultDetails == null) { result.ResultStatus = -1; result.Message = "付款申请发票明细列表获取失败"; return result; } //作废原有发票申请明细 foreach (InvoicePayApply detail in resultDetails) { detail.DetailStatus = StatusEnum.已录入; result = invoicePayApplyDAL.Invalid(user, detail); if (result.ResultStatus != 0) return result; } //验证发票 int payDept = 0; int outCorpId = 0; foreach (InvoicePayApply detail in details) { //获取价外票 result = sIDAL.Get(user, detail.SIId); if (result.ResultStatus != 0) return result; SI sI = result.ReturnValue as SI; if (sI == null || sI.SIId <= 0) { result.ResultStatus = -1; result.Message = "价外票不存在"; return result; } //获取发票 result = invoiceDAL.Get(user, detail.InvoiceId); if (result.ResultStatus != 0) return result; Operate.Model.Invoice invoice = result.ReturnValue as Operate.Model.Invoice; if (invoice == null || invoice.InvoiceId <= 0) { result.ResultStatus = -1; result.Message = "发票不存在"; return result; } //验证币种 if (payApply.CurrencyId != invoice.CurrencyId) { result.ResultStatus = -1; result.Message = "发票币种不一致"; return result; } //验证开票抬头 if (outCorpId == 0) { outCorpId = invoice.OutCorpId; } if (outCorpId != invoice.OutCorpId) { result.ResultStatus = -1; result.Message = "发票开票公司不一致"; return result; } //验证成本部门 if (payDept == 0) { payDept = sI.PayDept; } if (payDept != sI.PayDept) { result.ResultStatus = -1; result.Message = "发票成本部门不一致"; return result; } //验证可申请余额 result = invoicePayApplyDAL.LoadByInvoice(user, detail.InvoiceId); if (result.ResultStatus != 0) return result; resultDetails = result.ReturnValue as List<InvoicePayApply>; if (resultDetails == null) { result.ResultStatus = -1; result.Message = "获取发票已申请列表失败"; return result; } decimal applyBala = resultDetails.Sum(temp => temp.ApplyBala); if (applyBala >= invoice.InvoiceBala) { result.ResultStatus = -1; result.Message = string.Format("发票{0}款项已全部申请", invoice.InvoiceNo); return result; } if (invoice.InvoiceBala - applyBala < detail.ApplyBala) { result.ResultStatus = -1; result.Message = string.Format("发票{0}申请金额超过可申请余额,申请失败", invoice.InvoiceNo); return result; } } //更新申请主表 Department dept = UserProvider.Departments.FirstOrDefault(temp => temp.DeptId == apply.ApplyDept); if (dept == null || dept.DeptId < 0) { result.ResultStatus = -1; result.Message = "申请部门不存在"; return result; } //获取主申请 result = applyDAL.Get(user, resultPayApply.ApplyId); if (result.ResultStatus != 0) return result; Apply resultApply = result.ReturnValue as Apply; if (resultApply == null || resultApply.ApplyId <= 0) { result.ResultStatus = -1; result.Message = "主申请获取失败"; return result; } resultApply.ApplyDept = dept.DeptId; resultApply.ApplyCorp = apply.ApplyCorp; resultApply.ApplyDesc = apply.ApplyDesc; resultApply.ApplyTime = apply.ApplyTime; resultApply.EmpId = user.EmpId; result = applyDAL.Update(user, resultApply); if (result.ResultStatus != 0) return result; //更新付款申请表 resultPayApply.RecCorpId = payApply.RecCorpId; resultPayApply.RecBankId = payApply.RecBankId; resultPayApply.RecBankAccountId = payApply.RecBankAccountId; resultPayApply.RecBankAccount = payApply.RecBankAccount; resultPayApply.CurrencyId = payApply.CurrencyId; resultPayApply.ApplyBala = sumApplyBala; resultPayApply.PayMode = payApply.PayMode; resultPayApply.PayDeadline = payApply.PayDeadline; resultPayApply.PayMatter = payApply.PayMatter; resultPayApply.SpecialDesc = payApply.SpecialDesc; result = payapplyDAL.Update(user, resultPayApply); if (result.ResultStatus != 0) return result; //新增发票付款申请关联表 foreach (InvoicePayApply detail in details) { detail.DetailStatus = StatusEnum.已生效; detail.PayApplyId = resultPayApply.PayApplyId; result = invoicePayApplyDAL.Insert(user, detail); if (result.ResultStatus != 0) return result; } scope.Complete(); } } catch (Exception ex) { result.ResultStatus = -1; result.Message = ex.Message; } return result; }
public ResultModel PayApplyInvoiceCreate(UserModel user, Apply apply, PayApply payApply, List<InvoicePayApply> details) { ResultModel result = new ResultModel(); try { InvoicePayApplyDAL invoicePayApplyDAL = new InvoicePayApplyDAL(); ApplyDAL applyDAL = new ApplyDAL(); PayApplyDAL payApplyDAL = new PayApplyDAL(); InvoiceDAL invoiceDAL = new InvoiceDAL(); SIDAL sIDAL = new SIDAL(); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { if (details == null || details.Count == 0) { result.ResultStatus = -1; result.Message = "未选中任务发票"; return result; } //验证总额 decimal sumApplyBala = details.Sum(temp => temp.ApplyBala); if (sumApplyBala != payApply.ApplyBala) { result.ResultStatus = -1; result.Message = "申请总额与分项总额不相等"; return result; } //验证发票 int payDept = 0; int outCorpId = 0; foreach (InvoicePayApply detail in details) { //获取价外票 result = sIDAL.Get(user, detail.SIId); if (result.ResultStatus != 0) return result; SI sI = result.ReturnValue as SI; if (sI == null || sI.SIId <= 0) { result.ResultStatus = -1; result.Message = "价外票不存在"; return result; } //获取发票 result = invoiceDAL.Get(user, detail.InvoiceId); if (result.ResultStatus != 0) return result; Operate.Model.Invoice invoice = result.ReturnValue as Operate.Model.Invoice; if (invoice == null || invoice.InvoiceId <= 0) { result.ResultStatus = -1; result.Message = "发票不存在"; return result; } //验证币种 if (payApply.CurrencyId != invoice.CurrencyId) { result.ResultStatus = -1; result.Message = "发票币种不一致"; return result; } //验证开票抬头 if (outCorpId == 0) { outCorpId = invoice.OutCorpId; } if (outCorpId != invoice.OutCorpId) { result.ResultStatus = -1; result.Message = "发票开票公司不一致"; return result; } //验证成本部门 if (payDept == 0) { payDept = sI.PayDept; } if (payDept != sI.PayDept) { result.ResultStatus = -1; result.Message = "发票成本部门不一致"; return result; } //验证可申请余额 result = invoicePayApplyDAL.LoadByInvoice(user, detail.InvoiceId); if (result.ResultStatus != 0) return result; List<InvoicePayApply> resultDetails = result.ReturnValue as List<InvoicePayApply>; if (resultDetails == null) { result.ResultStatus = -1; result.Message = "获取发票已申请列表失败"; return result; } decimal applyBala = resultDetails.Sum(temp => temp.ApplyBala); if (applyBala >= invoice.InvoiceBala) { result.ResultStatus = -1; result.Message = string.Format("发票{0}款项已全部申请", invoice.InvoiceNo); return result; } if (invoice.InvoiceBala - applyBala < detail.ApplyBala) { result.ResultStatus = -1; result.Message = string.Format("发票{0}申请金额超过可申请余额,申请失败", invoice.InvoiceNo); return result; } } //新增申请主表 Department dept = UserProvider.Departments.FirstOrDefault(temp => temp.DeptId == apply.ApplyDept); if (dept == null || dept.DeptId < 0) { result.ResultStatus = -1; result.Message = "申请部门不存在"; return result; } int applyId = 0; apply.ApplyDept = dept.DeptId; apply.ApplyType = ApplyType.付款申请; apply.EmpId = user.EmpId; apply.ApplyStatus = StatusEnum.已录入; 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.InvoicePayApply; payApply.ApplyBala = sumApplyBala; 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; } //新增发票付款申请关联表 foreach (InvoicePayApply detail in details) { detail.DetailStatus = StatusEnum.已生效; detail.PayApplyId = payApplyId; result = invoicePayApplyDAL.Insert(user, detail); if (result.ResultStatus != 0) return result; } scope.Complete(); } } catch (Exception ex) { result.ResultStatus = -1; result.Message = ex.Message; } return result; }