Ejemplo n.º 1
0
        public async Task RebuildSeriesAsync(BillInstance instance)
        {
            List <BillInstance> instances =
                await FetchEditableBillInstancesAsync(instance);

            _context.RemoveRange(instances);
        }
Ejemplo n.º 2
0
        public async Task CreateSeriesAsync(BillInstance instance)
        {
            //Make sure there are enough bills to last from now until
            //thirty years from now. If that ends up not being enough,
            //we can argue about it thirty years from now.

            DateTime thirtyYearsFromNow = DateTime.Today.AddYears(30);

            int instancesToAdd = Convert.ToInt16(
                (
                    (thirtyYearsFromNow - instance.DueDate).TotalDays / 365.25 * 12
                ) / instance.BillTemplate.FrequencyInMonths);

            for (int i = 1; i <= instancesToAdd; i++)
            {
                DateTime dueDate = instance.DueDate.AddMonths(i * instance.BillTemplate.FrequencyInMonths);

                BillInstance instanceToAdd = new BillInstance();

                instanceToAdd.BillTemplate = instance.BillTemplate;
                instanceToAdd.DueDate      = dueDate;
                instanceToAdd.Amount       = instance.Amount;
                instanceToAdd.Name         = instance.Name;

                await _context.BillInstance.AddAsync(instanceToAdd);
            }
        }
        public IActionResult OnPostTest()
        {
            BillInstance instance = new BillInstance();

            instance.Name = HttpContext.User.Identity.Name;

            return(new JsonResult(instance));
        }
Ejemplo n.º 4
0
        public async Task <BillInstance> GetBillInstanceAsync(int id)
        {
            BillInstance instance = await _context.BillInstance
                                    .Include(b => b.BillTemplate)
                                    .Include(p => p.Payments)
                                    .Include(b => b.BillTemplate.User)
                                    .Where(b => b.BillTemplate.User.UserName ==
                                           _httpContextAccessor.HttpContext.User.Identity.Name)
                                    .FirstOrDefaultAsync(m => m.ID == id);

            return(instance);
        }
Ejemplo n.º 5
0
        public async Task <List <BillInstance> > FetchEditableBillInstancesAsync(BillInstance instance)
        {
            List <BillInstance> instances = await _context.BillInstance
                                            .Include(b => b.BillTemplate)
                                            .Include(p => p.Payments)
                                            .Include(b => b.BillTemplate.User)
                                            .Where(b =>
                                                   b.BillTemplate.User.UserName == _httpContextAccessor.HttpContext.User.Identity.Name &&
                                                   b.Payments.Count == 0 &&
                                                   b.BillTemplateID == instance.BillTemplateID &&
                                                   b.DueDate > instance.DueDate)
                                            .ToListAsync();

            return(instances);
        }
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            BillInstance = await _billService.GetBillInstanceAsync((int)id);

            if (BillInstance == null)
            {
                return(NotFound());
            }
            return(Page());
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            BillInstance = await _billService.GetBillInstanceAsync((int)id);

            if (BillInstance != null)
            {
                await _billService.RemoveAsync(BillInstance);
            }

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAddPayment(int?billInstanceID, int?billPayID)
        {
            if (billPayID == null || billInstanceID == null)
            {
                return(NotFound());
            }

            //Ensure parameters belong to current user
            BillInstance instance = await _billService.GetBillInstanceAsync((int)billInstanceID);

            if (instance == null)
            {
                return(NotFound());
            }

            SimpleBillPay.Models.BillPay billPay = await _billPayService.GetByIdAsync((int)billPayID);

            if (billPay == null)
            {
                return(NotFound());
            }

            //Add new payment with this bill instance and attached to this bill pay
            Payment payment = new Payment();

            payment.BillInstance = instance;

            payment.Amount        = (instance.Amount - instance.Payments.Sum(p => p.Amount));
            payment.PaymentDate   = billPay.BillPayDate;
            payment.DateConfirmed = null;
            payment.BillPay       = billPay;

            await _paymentService.AddAsync(payment);

            //Call get method to rebuild page
            return(Redirect("./Edit?id=" + billPayID.ToString()));
        }
Ejemplo n.º 9
0
 public async Task RemoveAsync(BillInstance billInstance)
 {
     _context.BillInstance.Remove(billInstance);
     await _context.SaveChangesAsync();
 }
Ejemplo n.º 10
0
 public async Task AddAsync(BillInstance billInstance)
 {
     _context.BillInstance.Add(billInstance);
     await _context.SaveChangesAsync();
 }
Ejemplo n.º 11
0
 public async Task UpdateAsync(BillInstance billInstance)
 {
     _context.Attach(billInstance).State = EntityState.Modified;
     await _context.SaveChangesAsync();
 }
        public async Task <IActionResult> OnPostAsync(int?id, int?returnBillPayID)
        {
            if (!ModelState.IsValid || id == null)
            {
                return(Page());
            }

            BillInstance instance = await _billService.GetBillInstanceAsync((int)id);

            if (instance == null)
            {
                return(NotFound());                 //Make sure this bill is in the DB and accessible to user
            }
            //Overwrite properties from form
            instance.Amount  = BillInstance.Amount;
            instance.DueDate = BillInstance.DueDate;
            instance.Name    = BillInstance.BillTemplate.Name;

            bool isTemplated =
                ((ChangeEntireSeries && BillInstance.BillTemplate.FrequencyInMonths > 0) ||
                 (instance.BillTemplate.FrequencyInMonths == 0 &&
                  BillInstance.BillTemplate.FrequencyInMonths > 0));

            if (isTemplated)
            {
                instance.BillTemplate.FrequencyInMonths = BillInstance.BillTemplate.FrequencyInMonths;
            }

            instance.BillTemplate.Name = BillInstance.BillTemplate.Name;


            if (ChangeEntireSeries)
            {
                await _billService.RebuildSeriesAsync(instance);

                if (BillInstance.BillTemplate.FrequencyInMonths > 0)
                {
                    await _billService.CreateSeriesAsync(instance);
                }
            }

            try
            {
                await _billService.UpdateAsync(instance);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!await _billService.BillInstanceExistsAsync(BillInstance.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            if (returnBillPayID != null && returnBillPayID > 0)
            {
                return(RedirectToPage("/BillPay/Edit", new { id = returnBillPayID }));
            }

            return(RedirectToPage("./Index"));
        }