internal void CalculateInterest(IllustrationDetailViewModel model)
        {
            model.AnnualGrossInterestEarned = 0;
            model.TotalDeposit = 0;

            foreach (var investment in model.ProposedPortfolio.ProposedInvestments)
            {
                model.AnnualGrossInterestEarned += investment.AnnualInterest;
                model.TotalDeposit += Convert.ToDouble(investment.DepositSize);
            }

            model.ProposedPortfolio.AnnualGrossInterestEarned = model.AnnualGrossInterestEarned;
            model.ProposedPortfolio.TotalDeposited            = Convert.ToDecimal(model.TotalDeposit);
            model.GrossAverageYield = (model.ProposedPortfolio.AnnualGrossInterestEarned / Convert.ToDecimal(model.TotalDeposit)) * 100;

            if (model.TotalDeposit.Value >= 50000 && model.TotalDeposit <= 299999)
            {
                model.ProposedPortfolio.FeePercentage = 0.25M;
            }

            if (model.TotalDeposit.Value >= 300000 && model.TotalDeposit <= 999999)
            {
                model.ProposedPortfolio.FeePercentage = 0.20M;
            }

            model.NetAverageYield = (model.GrossAverageYield - model.ProposedPortfolio.FeePercentage);

            model.ProposedPortfolio.Fee = (model.ProposedPortfolio.TotalDeposited * (decimal)(model.ProposedPortfolio.FeePercentage / 100));

            model.AnnualNetInterestEarned = (model.ProposedPortfolio.AnnualGrossInterestEarned - model.ProposedPortfolio.Fee);
        }
        internal IllustrationDetailViewModel GetIllustrationByUniqueReferenceId(string uniqueReferenceID)
        {
            /*
             * Get illustration from unique reference id from db
             * Arguments:-
             *  Unique Reference ID
             * Return:-
             *  Illustration DetailViewModel
             */
            IllustrationDetail dbIllustration = _context.IllustrationDetails.Include(x => x.IllustrationProposedPortfolio).FirstOrDefault(x => x.IllustrationUniqueReference == uniqueReferenceID);
            //map db entity to view model ProposedPortfolio. Investment count is 0 after Mapping
            IllustrationDetailViewModel result = _mapper.Map <IllustrationDetailViewModel>(dbIllustration);

            result.ProposedPortfolio = new Insignis.Asset.Management.Tools.Sales.SCurveOutput();



            foreach (var item in dbIllustration.IllustrationProposedPortfolio)
            {
                Insignis.Asset.Management.Tools.Sales.SCurveOutputRow   row  = new Insignis.Asset.Management.Tools.Sales.SCurveOutputRow();
                Insignis.Asset.Management.Clients.Helper.InvestmentTerm term = new Insignis.Asset.Management.Clients.Helper.InvestmentTerm();
                row.AnnualInterest          = item.AnnualInterest;
                row.DepositSize             = item.DepositSize;
                row.InstitutionName         = item.InstitutionName;
                row.Rate                    = item.Rate;
                row.InvestmentTerm          = term;
                row.InvestmentTerm.TermText = item.InvestmentTerm;
                row.InstitutionShortName    = item.InstitutionShortName;

                result.ProposedPortfolio.ProposedInvestments.Add(row);
            }


            CalculateInterest(result);

            return(result);
        }
        internal bool SaveIllustraionAsync(IllustrationDetailViewModel model)
        {
            //check for existence
            bool exits = _context.IllustrationDetails.Any(x => x.IllustrationUniqueReference == model.IllustrationUniqueReference);
            var  illustrationDetail = new IllustrationDetail();

            if (exits)
            {
                illustrationDetail = _context.IllustrationDetails.FirstOrDefault(x => x.IllustrationUniqueReference == model.IllustrationUniqueReference);

                //illustrationDetail.AdviserName = model.AdviserName;
                illustrationDetail.ClientName = model.ClientName;
                illustrationDetail.ClientType = model.ClientType;

                illustrationDetail.Currency     = model.Currency;
                illustrationDetail.EasyAccess   = Convert.ToDouble(model.EasyAccess);
                illustrationDetail.GenerateDate = model.GenerateDate;



                illustrationDetail.NineMonths          = Convert.ToDouble(model.NineMonths);
                illustrationDetail.OneMonth            = Convert.ToDouble(model.OneMonth);
                illustrationDetail.OneYear             = Convert.ToDouble(model.OneYear);
                illustrationDetail.PartnerEmail        = model.PartnerEmail;
                illustrationDetail.PartnerName         = model.PartnerName;
                illustrationDetail.PartnerOrganisation = model.PartnerOrganisation;
                illustrationDetail.ReferredBy          = model.ReferredBy;
                illustrationDetail.SixMonths           = Convert.ToDouble(model.SixMonths);
                illustrationDetail.Status         = model.Status;
                illustrationDetail.ThreeMonths    = Convert.ToDouble(model.ThreeMonths);
                illustrationDetail.ThreeYearsPlus = Convert.ToDouble(model.ThreeYearsPlus);
                illustrationDetail.TotalDeposit   = Convert.ToDouble(model.TotalDeposit);
                illustrationDetail.TwoYears       = Convert.ToDouble(model.TwoYears);



                illustrationDetail.Status = "Created";
                //Save interests
                illustrationDetail.AnnualGrossInterestEarned     = model.ProposedPortfolio.AnnualGrossInterestEarned;
                illustrationDetail.AnnualNetInterestEarned       = model.ProposedPortfolio.AnnualNetInterestEarned;
                illustrationDetail.GrossAverageYield             = model.ProposedPortfolio.GrossAverageYield;
                illustrationDetail.NetAverageYield               = model.ProposedPortfolio.GrossAverageYield;
                illustrationDetail.IllustrationProposedPortfolio = new List <ProposedPortfolio>();
                foreach (var item in model.ProposedPortfolio.ProposedInvestments)
                {
                    ProposedPortfolio folio = new ProposedPortfolio();
                    folio.InvestmentTerm       = item.InvestmentTerm.TermText;
                    folio.AnnualInterest       = item.AnnualInterest;
                    folio.DepositSize          = item.DepositSize;
                    folio.IllustrationID       = model.Id;
                    folio.InstitutionID        = item.InstitutionID;
                    folio.InstitutionName      = item.InstitutionName;
                    folio.InstitutionShortName = item.InstitutionShortName;
                    folio.Rate = item.Rate;
                    illustrationDetail.IllustrationProposedPortfolio.Add(folio);
                }
            }
            else
            {
                illustrationDetail        = _mapper.Map <IllustrationDetailViewModel, IllustrationDetail>(model);
                illustrationDetail.Status = "Created";
                //Save interests
                illustrationDetail.AnnualGrossInterestEarned     = model.ProposedPortfolio.AnnualGrossInterestEarned;
                illustrationDetail.AnnualNetInterestEarned       = model.ProposedPortfolio.AnnualNetInterestEarned;
                illustrationDetail.GrossAverageYield             = model.ProposedPortfolio.GrossAverageYield;
                illustrationDetail.NetAverageYield               = model.ProposedPortfolio.GrossAverageYield;
                illustrationDetail.IllustrationProposedPortfolio = new List <ProposedPortfolio>();
                foreach (var item in model.ProposedPortfolio.ProposedInvestments)
                {
                    ProposedPortfolio folio = new ProposedPortfolio();
                    folio.InvestmentTerm       = item.InvestmentTerm.TermText;
                    folio.AnnualInterest       = item.AnnualInterest;
                    folio.DepositSize          = item.DepositSize;
                    folio.IllustrationID       = model.Id;
                    folio.InstitutionID        = item.InstitutionID;
                    folio.InstitutionName      = item.InstitutionName;
                    folio.InstitutionShortName = item.InstitutionShortName;
                    folio.Rate = item.Rate;
                    illustrationDetail.IllustrationProposedPortfolio.Add(folio);
                }
                _context.IllustrationDetails.Add(illustrationDetail);
            }

            _context.SaveChanges();
            var IsSave = true;

            return(IsSave);
        }