Esempio n. 1
0
        public void GenerateNextDataset()
        {
            var projectId     = 148;//测试收益分配报告-中港
            var assetModifier = new AssetModifier("TestUserName");

            assetModifier.Load(projectId, new DateTime(2016, 3, 1));
            assetModifier.GenerateNextDataset(projectId);
        }
        public ActionResult PrepayAmortizationByAsset(string projectGuid, string paymentDate, int assetId,
                                                      string prepayDate, double money, string distributionType, string distributionDetail, string comment)
        {
            return(ActionUtils.Json(() =>
            {
                CommUtils.Assert(money >= 0, "提前偿付金额不能为负数");
                var type = CommUtils.ParseEnum <PrepayDistrubutionType>(distributionType);

                var project = new ProjectLogicModel(CurrentUserName, projectGuid);
                var dataset = project.DealSchedule.GetByPaymentDay(DateUtils.ParseDigitDate(paymentDate)).Dataset;
                var asset = dataset.Assets.Single(x => x.AssetId == assetId);

                CommUtils.Assert(DateUtils.ParseDigitDate(prepayDate) >= dataset.DatasetSchedule.AsOfDateBegin &&
                                 DateUtils.ParseDigitDate(prepayDate) <= dataset.DatasetSchedule.AsOfDateEnd,
                                 "提前偿付日期必须在当期时间范围(" + dataset.DatasetSchedule.AsOfDateBegin.ToShortDateString()
                                 + "~" + dataset.DatasetSchedule.AsOfDateEnd.ToShortDateString() + ")内");

                if (asset.AmortizationType == AmortizationType.UserDefined)
                {
                    var amortization = dataset.AmortizationSchedule;
                    amortization.AddPrepayment(assetId, DateUtils.ParseDigitDate(prepayDate), money, type, distributionDetail);
                    amortization.Save();

                    var history = new AssetPrepaymentHistory();
                    history.ProjectId = project.Instance.ProjectId;
                    history.DatasetId = dataset.Instance.DatasetId;
                    history.AssetId = assetId;
                    history.PrepayAmount = money;
                    history.PrepayTime = DateUtils.ParseDigitDate(prepayDate);
                    history.DistributionType = distributionType;
                    history.DistributionDetail = distributionDetail;
                    history.Comment = comment;
                    history.TimeStamp = DateTime.Now;
                    history.TimeStampUserName = CurrentUserName;
                    m_dbAdapter.PaymentHistory.NewAssetPrepaymentHistory(history);

                    var viewModel = new PrepayRecordListViewModel(dataset.DatasetSchedule, amortization.SelectByAsset(assetId));
                    return ActionUtils.Success(viewModel);
                }

                CommUtils.Assert(type != PrepayDistrubutionType.Custom, "等额本金、等额本息、一次偿付类的资产不支持自定义偿付。");

                //处理等额本金、等额本息、一次偿付类的提前偿付,需要下期模型存在
                if (dataset.Next == null)
                {
                    //下期模型不存在,自动生成下一期模型
                    CommUtils.AssertNotNull(dataset.DatasetSchedule.Next, "找不到下期模型封包日,提前偿付测算失败");

                    CreateDataset(project.Instance, dataset.DatasetSchedule.Next.AsOfDateBegin.ToString("yyyyMMdd"));

                    var assetModifier = new AssetModifier(CurrentUserName);
                    assetModifier.Load(project.Instance.ProjectId, dataset.DatasetSchedule.AsOfDateBegin);
                    assetModifier.GenerateNextDataset(project.Instance.ProjectId);
                }
                CommUtils.AssertNotNull(dataset.Next, "无法找到下期模型数据,提前偿付测算失败");

                if (IsEqualPmtOrEqualPrin(asset.AmortizationType))
                {
                    var curDatasetBasicAnalyticsData = NancyUtils.GetBasicAnalyticsData(project.Instance.ProjectId, null, dataset.DatasetSchedule.AsOfDateBegin);

                    double curDatasetPrincipal = 0;
                    if (IsEqualPmtOrEqualPrin(asset.SecurityData.PaymentMethod))
                    {
                        //当期未发生过提前偿付,当期偿付金额是Nancy测算金额
                        curDatasetPrincipal = curDatasetBasicAnalyticsData.BasicAssetCashflow.BasicAssetCashflowItems
                                              .Single(x => x.AssetId == assetId && x.PaymentDate == dataset.DatasetSchedule.PaymentDate).Principal;
                    }
                    else
                    {
                        var nextDatasetAsset = dataset.Next.Assets.Single(x => x.AssetId == assetId);
                        CommUtils.Assert(IsEqualPmtOrEqualPrin(nextDatasetAsset.AmortizationType),
                                         "第[" + dataset.Next.DatasetSchedule.PaymentDate.ToString("yyyyMMdd") + "]期偿付类型错误,提前偿付测算失败");

                        //当期发生过提前偿付,当期偿付金额是AmortizationSchedule中金额
                        var curAmortizationRecord = dataset.AmortizationSchedule.Single(x => x.ReductionDate == dataset.DatasetSchedule.PaymentDate);
                        //当期未发生本次提前偿付的偿付本金
                        curDatasetPrincipal = (double)curAmortizationRecord.ReductionAmount;
                    }

                    //保存AmortizationSchedule
                    var amortization = dataset.AmortizationSchedule;
                    amortization.Add(new AmortizationScheduleRecord {
                        AssetId = assetId,
                        ReductionAmount = money + curDatasetPrincipal,
                        ReductionDate = DateUtils.ParseDigitDate(prepayDate)
                    });
                    amortization.Save();

                    //更新PaymentMethod
                    dataset.CollateralCsv.UpdateCellValue(assetId, "PaymentMethod", "");
                    dataset.CollateralCsv.Save();

                    var sumFutureDatasetPrincipal = curDatasetBasicAnalyticsData.BasicAssetCashflow.BasicAssetCashflowItems
                                                    .Where(x => x.AssetId == assetId && x.PaymentDate > dataset.DatasetSchedule.PaymentDate)
                                                    .Sum(x => x.Principal);

                    var records = new List <AmortizationScheduleRecord>();
                    //当期的新的本金偿付值是 根据等额本金/等额本息计算出的本金值 加上提前偿付金额
                    records.Add(new AmortizationScheduleRecord
                    {
                        AssetId = assetId,
                        ReductionAmount = curDatasetPrincipal + money,
                        ReductionDate = dataset.DatasetSchedule.PaymentDate
                    });

                    //更新下期的本金期末余额(减去提前偿付金额),重新根据等额本金/等额本息计算本金
                    dataset.Next.CollateralCsv.UpdateCellValue(assetId, "PrincipalBalance", (sumFutureDatasetPrincipal - money).ToString());
                    dataset.Next.CollateralCsv.Save();

                    var nextDatasetBasicAnalyticsData = NancyUtils.GetBasicAnalyticsData(project.Instance.ProjectId, null, dataset.DatasetSchedule.Next.AsOfDateBegin);
                    var nextAssetCashflowItems = nextDatasetBasicAnalyticsData.BasicAssetCashflow.BasicAssetCashflowItems
                                                 .Where(x => x.AssetId == assetId && x.PaymentDate > dataset.DatasetSchedule.PaymentDate && x.Principal != 0)
                                                 .OrderBy(x => x.PaymentDate);

                    foreach (var item in nextAssetCashflowItems)
                    {
                        records.Add(new AmortizationScheduleRecord
                        {
                            AssetId = assetId,
                            ReductionAmount = item.Principal,
                            ReductionDate = item.PaymentDate
                        });
                    }

                    var viewModel = new PrepayRecordListViewModel(dataset.DatasetSchedule, records);
                    return ActionUtils.Success(viewModel);
                }
                else if (asset.AmortizationType == AmortizationType.SingleAmortization)
                {
                    var nextDatasetAsset = dataset.Next.Assets.Single(x => x.AssetId == assetId);
                    CommUtils.AssertEquals(nextDatasetAsset.SecurityData.PaymentMethod, ZEnums.EPaymentMethod.UNDEFINEDENUM,
                                           "无法识别的偿付类型,提前偿付测算失败。");

                    var nextAmortizationRecords = dataset.Next.AmortizationSchedule.SelectByAsset(assetId);
                    CommUtils.AssertEquals(nextAmortizationRecords.Count, 0,
                                           "第[" + dataset.Next.DatasetSchedule.PaymentDate.ToString("yyyyMMdd") + "]期已发生提前偿付, 提前偿付测算失败");

                    var records = dataset.AmortizationSchedule.SelectByAsset(assetId);
                    var prepayRecord = new AmortizationScheduleRecord
                    {
                        AssetId = assetId,
                        ReductionAmount = money,
                        ReductionDate = DateUtils.ParseDigitDate(prepayDate)
                    };

                    records.Add(prepayRecord);

                    var sumPrepayAmount = records.Sum(x => x.ReductionAmount);
                    CommUtils.Assert(sumPrepayAmount <= asset.SecurityData.PrincipalBalance,
                                     "提前偿付金额[" + sumPrepayAmount + "]大于剩余未偿付金额[" + asset.SecurityData.PrincipalBalance + "]");

                    records.Add(new AmortizationScheduleRecord()
                    {
                        AssetId = assetId,
                        ReductionAmount = asset.SecurityData.PrincipalBalance - sumPrepayAmount,
                        ReductionDate = asset.SecurityData.MaturityDate
                    });

                    dataset.AmortizationSchedule.AddPrepayment(assetId, DateUtils.ParseDigitDate(prepayDate), money, type, distributionDetail);
                    dataset.AmortizationSchedule.Save();

                    dataset.Next.CollateralCsv.UpdateCellValue(assetId, "PrincipalBalance",
                                                               (asset.SecurityData.PrincipalBalance - sumPrepayAmount).ToString());
                    dataset.Next.CollateralCsv.Save();

                    var viewModel = new PrepayRecordListViewModel(dataset.DatasetSchedule, records);
                    return ActionUtils.Success(viewModel);
                }

                return ActionUtils.Success("无法识别的偿付类型,projectGuid=["
                                           + projectGuid + "] + paymentDate=[" + paymentDate + "] + assetId=[" + assetId + "]");
            }));
        }