public async Task <IActionResult> FilterConsumables(FilterConsumablesBindingModel bindModel)
        {
            if (!ModelState.IsValid)
            {
                TempData.AddErrorMessage(WrongInput);
                return(RedirectToAction("IndexConsumables"));
            }

            var model = await this.payments.FilterConsumablesAsync(bindModel);

            TempData.AddSuccessMessage(model.FilterDetails);
            return(View(model));
        }
Пример #2
0
        public async Task <FilterConsumablesViewModel> FilterConsumablesAsync(FilterConsumablesBindingModel bindModel)
        {
            var filterDetails = "Аргументите на филтъра са: ";
            var payments      = new List <Payment>();

            //Filter by deadline or payment date
            if (bindModel.FilterByDeadline)
            {
                payments = await this.Db.Payments
                           .Where(x => x.MonthlyPaymentConsumableId != null &&
                                  x.MonthlyPaymentConsumable.DeadLine >= bindModel.StartDate &&
                                  x.MonthlyPaymentConsumable.DeadLine <= bindModel.EndDate)
                           .OrderBy(x => x.MonthlyPaymentConsumable.DeadLine)
                           .ToListAsync();

                filterDetails += "по крайна дата, ";
            }
            else
            {
                payments = await this.Db.Payments
                           .Where(x => x.MonthlyPaymentConsumableId != null && x.PaidOn >= bindModel.StartDate && x.PaidOn <= bindModel.EndDate)
                           .OrderBy(x => x.PaidOn)
                           .ToListAsync();

                filterDetails += "по дата на плащане, ";
            }

            if (payments == null)
            {
                return(null);
            }

            //Filter By clinet
            if (bindModel.Client != 0)
            {
                payments = payments
                           .Where(x => x.MonthlyPaymentConsumable.RentAgreement.ClientId == bindModel.Client)
                           .ToList();
                var client = await this.Db.Clients.FirstOrDefaultAsync(x => x.Id == bindModel.Client);

                if (client != null)
                {
                    filterDetails += $"само за клиент {client.Name}, ";
                }
                else
                {
                    filterDetails += "търсеният клиента няма такива плащания, ";
                }
            }
            else
            {
                filterDetails += "всички клиенти, ";
            }

            //Filter only money given in cash
            if (bindModel.OnlyCash)
            {
                payments = payments
                           .Where(x => x.CashPayment == true)
                           .ToList();
                filterDetails += "пари платени само в борй, ";
            }
            else
            {
                filterDetails += "всякакви плащания,  ";
            }
            var paymentsListingModel = Mapper.Map <IEnumerable <PaymentConsumablesListingModel> >(payments);

            var model = new FilterConsumablesViewModel()
            {
                StartDate     = bindModel.StartDate,
                EndDate       = bindModel.EndDate,
                Payments      = paymentsListingModel,
                FilterDetails = filterDetails
            };

            return(model);
        }
Пример #3
0
        public async Task FilterConsumablesAsync_ShouldReturn_CorrectQueryResult_DependingOnInput()
        {
            var db     = GetDatabase();
            var mapper = GetMapper();

            var firstMonthConsumable = new MonthlyPaymentConsumable
            {
                Id            = 1,
                RentAgreement = new RentAgreement
                {
                    Id       = 1,
                    ClientId = 1
                }
            };
            var secondMonthConsumable = new MonthlyPaymentConsumable
            {
                Id            = 2,
                RentAgreement = new RentAgreement
                {
                    Id       = 1,
                    ClientId = 1
                }
            };
            var thirdMonthConsumable = new MonthlyPaymentConsumable
            {
                Id            = 3,
                RentAgreement = new RentAgreement
                {
                    Id       = 2,
                    ClientId = 2
                }
            };
            var forthMonthConsumable = new MonthlyPaymentConsumable
            {
                Id            = 4,
                RentAgreement = new RentAgreement
                {
                    Id       = 1,
                    ClientId = 1
                }
            };
            await db.MonthlyPaymentConsumables.AddRangeAsync(firstMonthConsumable, secondMonthConsumable, thirdMonthConsumable, forthMonthConsumable);

            var firstPayment = new Payment
            {
                Id          = 1,
                PaidOn      = new DateTime(2018, 12, 12),
                Amount      = 10,
                CashPayment = false,
                MonthlyPaymentConsumableId = 1,
            };
            var secndPayment = new Payment
            {
                Id          = 2,
                PaidOn      = new DateTime(2017, 11, 6),
                Amount      = 20,
                CashPayment = true,
                MonthlyPaymentConsumableId = 2,
            };
            var thirdPayment = new Payment
            {
                Id          = 3,
                PaidOn      = new DateTime(2015, 11, 12),
                Amount      = 30,
                CashPayment = false,
                MonthlyPaymentConsumableId = 3
            };
            var forthPayment = new Payment
            {
                Id          = 4,
                PaidOn      = new DateTime(2015, 12, 15),
                Amount      = 40,
                CashPayment = false,
                MonthlyPaymentConsumableId = 4
            };
            var fifthPayment = new Payment
            {
                Id     = 5,
                PaidOn = new DateTime(2017, 12, 15),
                Amount = 50,
                MonthlyPaymentConsumableId = null,
                CashPayment = false
            };
            await db.Payments.AddRangeAsync(firstPayment, secndPayment, thirdPayment, forthPayment, fifthPayment);

            await db.SaveChangesAsync();

            var filterModel = new FilterConsumablesBindingModel
            {
                Client    = 1,
                OnlyCash  = true,
                StartDate = new DateTime(2017, 1, 1),
                EndDate   = new DateTime(2018, 12, 30)
            };
            var paymentService = new PaymentsService(mapper, db, null);
            //Act
            var result = await paymentService.FilterConsumablesAsync(filterModel);

            //Assert
            result
            .Should()
            .BeOfType <FilterConsumablesViewModel>()
            .And
            .Match <FilterConsumablesViewModel>(x => x.TotalIncome == 20 &&
                                                x.Payments.Any(y => y.PaidOn == new DateTime(2017, 11, 6)));
        }