Example #1
0
        public ActionResult QuoteSummary(Quote quote)
        {
            var monthlyRepayments = _financePlanCalculator.GenerateFinancePlanRepayments(quote);
            var financePlan       = new FinancePlan(quote, monthlyRepayments);

            return(View("FinancePlan", financePlan));
        }
Example #2
0
        public void SetupTest()
        {
            var controller = new QuoteMeController(new InterestFreeFinancePlanCalculator());

            _quote                      = GetQuote();
            _result                     = controller.QuoteSummary(_quote) as ViewResult;
            _financePlan                = (FinancePlan)_result.ViewData.Model;
            _expectedNumOfRepayments    = _quote.NumOfRepaymentYears * 12;
            _expectedOutstandingPayment = _quote.VehiclePrice - _quote.DepositAmount;
            _expectedAmountDueEachMonth = _expectedOutstandingPayment / _expectedNumOfRepayments;
        }
        public async Task <IActionResult> Post([FromBody] FinancePlan plan)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            // Check
            if (!plan.IsValid(this._context))
            {
                throw new BadRequestException("Check IsValid failed");
            }

            // User
            String usrName = String.Empty;

            try
            {
                usrName = HIHAPIUtility.GetUserID(this);
                if (String.IsNullOrEmpty(usrName))
                {
                    throw new UnauthorizedAccessException();
                }
            }
            catch
            {
                throw new UnauthorizedAccessException();
            }

            // Check whether User assigned with specified Home ID
            var hms = _context.HomeMembers.Where(p => p.HomeID == plan.HomeID && p.User == usrName).Count();

            if (hms <= 0)
            {
                throw new UnauthorizedAccessException();
            }

            plan.Createdby = usrName;
            plan.CreatedAt = DateTime.Now;
            _context.FinancePlan.Add(plan);
            await _context.SaveChangesAsync();

            return(Created(plan));
        }
        public async Task <IActionResult> Put([FromODataUri] int key, [FromBody] FinancePlan update)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            if (key != update.ID)
            {
                throw new BadRequestException("Inputted ID mismatched");
            }

            // User
            String usrName = String.Empty;

            try
            {
                usrName = HIHAPIUtility.GetUserID(this);
                if (String.IsNullOrEmpty(usrName))
                {
                    throw new UnauthorizedAccessException();
                }
            }
            catch
            {
                throw new UnauthorizedAccessException();
            }

            // Check whether User assigned with specified Home ID
            var hms = _context.HomeMembers.Where(p => p.HomeID == update.HomeID && p.User == usrName).Count();

            if (hms <= 0)
            {
                throw new UnauthorizedAccessException();
            }

            if (!update.IsValid(this._context))
            {
                throw new BadRequestException("Inputted Object IsValid failed");
            }

            update.UpdatedAt             = DateTime.Now;
            update.Updatedby             = usrName;
            _context.Entry(update).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException exp)
            {
                if (!_context.FinancePlan.Any(p => p.ID == key))
                {
                    throw new NotFoundException("Inputted ID not found");
                }
                else
                {
                    throw new DBOperationException(exp.Message);
                }
            }

            return(Updated(update));
        }
Example #5
0
        public void TestModel(int hid, string curr, FinancePlanTypeEnum pty)
        {
            var context = this.fixture.GetCurrentDataContext();

            if (hid == DataSetupUtility.Home1ID)
            {
                fixture.InitHome1TestData(context);
            }
            if (hid == DataSetupUtility.Home2ID)
            {
                fixture.InitHome2TestData(context);
            }
            if (hid == DataSetupUtility.Home3ID)
            {
                fixture.InitHome3TestData(context);
            }
            if (hid == DataSetupUtility.Home4ID)
            {
                fixture.InitHome4TestData(context);
            }
            if (hid == DataSetupUtility.Home5ID)
            {
                fixture.InitHome5TestData(context);
            }

            var plan = new FinancePlan();

            Assert.False(plan.IsValid(context));
            plan.HomeID = hid;
            Assert.False(plan.IsValid(context));
            plan.TranCurr = curr;
            Assert.False(plan.IsValid(context));
            plan.Description = "test";
            Assert.False(plan.IsValid(context));
            plan.StartDate  = DateTime.Today;
            plan.TargetDate = DateTime.Today;
            Assert.False(plan.IsValid(context));
            plan.TargetDate = DateTime.Today.AddDays(100);
            Assert.False(plan.IsValid(context));
            plan.PlanType = pty;
            switch (pty)
            {
            case FinancePlanTypeEnum.Account:
                plan.AccountID = context.FinanceAccount.Where(p => p.HomeID == hid).FirstOrDefault().ID;
                Assert.True(plan.IsValid(context));
                break;

            case FinancePlanTypeEnum.AccountCategory:
                plan.AccountCategoryID = context.FinAccountCategories.FirstOrDefault().ID;
                Assert.True(plan.IsValid(context));
                break;

            case FinancePlanTypeEnum.ControlCenter:
                plan.ControlCenterID = context.FinanceControlCenter.Where(p => p.HomeID == hid).FirstOrDefault().ID;
                Assert.True(plan.IsValid(context));
                break;

            case FinancePlanTypeEnum.TranType:
                plan.TranTypeID = context.FinTransactionType.FirstOrDefault().ID;
                Assert.True(plan.IsValid(context));
                break;

            default:
                break;
            }
        }
Example #6
0
        public async Task TestController1(int hid, string user)
        {
            var context = this.fixture.GetCurrentDataContext();
            var curr    = "";

            if (hid == DataSetupUtility.Home1ID)
            {
                fixture.InitHome1TestData(context);
                curr = DataSetupUtility.Home1BaseCurrency;
            }
            if (hid == DataSetupUtility.Home2ID)
            {
                fixture.InitHome2TestData(context);
                curr = DataSetupUtility.Home2BaseCurrency;
            }
            if (hid == DataSetupUtility.Home3ID)
            {
                fixture.InitHome3TestData(context);
                curr = DataSetupUtility.Home3BaseCurrency;
            }
            if (hid == DataSetupUtility.Home4ID)
            {
                fixture.InitHome4TestData(context);
                curr = DataSetupUtility.Home4BaseCurrency;
            }
            if (hid == DataSetupUtility.Home5ID)
            {
                fixture.InitHome5TestData(context);
                curr = DataSetupUtility.Home5BaseCurrency;
            }

            // 1. Prepare dta
            var userclaim = DataSetupUtility.GetClaimForUser(user);
            var httpctx   = UnitTestUtility.GetDefaultHttpContext(provider, userclaim);

            var existamt = (from homemem in context.HomeMembers
                            join finplan in context.FinancePlan
                            on new { homemem.HomeID, homemem.User } equals new { finplan.HomeID, User = user }
                            select finplan.ID).ToList().Count();
            var existamt_curhome = context.FinancePlan.Where(p => p.HomeID == hid).Count();

            // 2. Create plan
            var control = new FinancePlansController(context);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };
            var plan = new FinancePlan()
            {
                HomeID        = hid,
                PlanType      = FinancePlanTypeEnum.Account,
                TranCurr      = curr,
                AccountID     = context.FinanceAccount.Where(p => p.HomeID == hid).FirstOrDefault().ID,
                StartDate     = DateTime.Today,
                TargetBalance = 10000,
                TargetDate    = DateTime.Today.AddDays(100),
                Description   = "Test",
            };
            var rst = await control.Post(plan);

            Assert.NotNull(rst);
            var rst2 = Assert.IsType <CreatedODataResult <FinancePlan> >(rst);

            Assert.Equal(rst2.Entity.TranCurr, plan.TranCurr);
            Assert.Equal(rst2.Entity.TargetBalance, plan.TargetBalance);
            var oid = rst2.Entity.ID;

            Assert.True(oid > 0);
            plansCreated.Add(oid);

            // 3. Read the plan out (without Home ID)
            var queryUrl     = "http://localhost/api/FinancePlans";
            var req          = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl);
            var odatacontext = UnitTestUtility.GetODataQueryContext <FinancePlan>(this.model);
            var options      = UnitTestUtility.GetODataQueryOptions <FinancePlan>(odatacontext, req);
            var rst3         = control.Get(options);

            Assert.NotNull(rst3);
            Assert.Equal(existamt + 1, rst3.Cast <FinancePlan>().Count());

            // 3a. Read the plan out (with Home ID)
            queryUrl = "http://localhost/api/FinancePlans?$filter=HomeID eq " + hid.ToString();
            req      = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl);
            //var odatacontext = UnitTestUtility.GetODataQueryContext<FinancePlan>(this.model);
            options = UnitTestUtility.GetODataQueryOptions <FinancePlan>(odatacontext, req);
            rst3    = control.Get(options);
            Assert.NotNull(rst3);
            Assert.Equal(existamt_curhome + 1, rst3.Cast <FinancePlan>().Count());

            // 4. Change one plan
            var nplan = rst2.Entity;

            nplan.Description = "Test > 2";
            rst = await control.Put(nplan.ID, nplan);

            Assert.NotNull(rst);
            var rst4 = Assert.IsType <UpdatedODataResult <FinancePlan> >(rst);

            Assert.Equal(nplan.Description, rst4.Entity.Description);

            // 5. Delete a plan
            var rst5 = await control.Delete(oid);

            Assert.NotNull(rst4);
            var rst6 = Assert.IsType <StatusCodeResult>(rst5);

            Assert.Equal(204, rst6.StatusCode);
            plansCreated.Clear();

            // 6. Read the plan again
            rst3 = control.Get(options);
            Assert.NotNull(rst3);
            Assert.Equal(existamt_curhome, rst3.Cast <FinancePlan>().Count());

            await context.DisposeAsync();
        }