public void ShouldSendRentalViewModelMessageWithContinueRentalPropertyIfDriverHasActiveRental()
        {
            //assign
            var driverId        = Guid.NewGuid();
            var driverViewModel = new DriverViewModel()
            {
                Id = driverId
            };
            var activeRentalDto = new RentalDTO();

            _rentalServiceMock.GetActiveRentalForDriver(driverId).Returns(activeRentalDto);

            var activeRentalViewModel = new RentalViewModel();

            _rentalViewModelMapperMock.Map(activeRentalDto).Returns(activeRentalViewModel);

            //act
            var sut = new RentCarViewModel(_carServiceMock, _rentalServiceMock, _carViewModelMapperMock, _rentalViewModelMapperMock, _messengerServiceMock);

            sut.AssignLoggedInDriver(driverViewModel);

            //assert
            _messengerServiceMock.Received().Send(Arg.Is <RentalViewModelMessage>(message => message.RentalViewModel == activeRentalViewModel && message.MessageType == RentalViewModelMessageType.ContinueRental));
            _messengerServiceMock.Received().Send(Arg.Is <NotificationMessage>(message => message.Notification == "Start Car Rental"));
        }
        public void ShouldNotPopulateAvailableCarListIfCarsAreNull()
        {
            var sut = new RentCarViewModel(_carServiceMock, _rentalServiceMock, _carViewModelMapperMock,
                                           _rentalViewModelMapperMock, _messengerServiceMock);

            Assert.IsEmpty(sut.AvailableCars);
        }
        public void ShouldRentCarIfRequirementsAreFulfilled()
        {
            var driverId        = Guid.NewGuid();
            var driverViewModel = new DriverViewModel
            {
                Id           = driverId,
                DriverStatus = DriverStatus.Active
            };
            var carId = Guid.NewGuid();
            var car   = new CarViewModel()
            {
                Id = carId
            };
            var sut = new RentCarViewModel(_carServiceMock, _rentalServiceMock, _carViewModelMapperMock,
                                           _rentalViewModelMapperMock, _messengerServiceMock)
            {
                CurrentDriver = driverViewModel, SelectedCar = car
            };

            sut.RentCarCommand.Execute(null);

            _rentalServiceMock.Received().TakeCar(Arg.Any <Guid>(), carId, driverId, Arg.Any <DateTime>());
            _rentalViewModelMapperMock.Received().Map(Arg.Any <RentalDTO>());
            _messengerServiceMock.Received().Send(Arg.Is <RentalViewModelMessage>(message => message.MessageType == RentalViewModelMessageType.StartRental));
            _messengerServiceMock.Received().Send(Arg.Is <NotificationMessage>(message => message.Notification == "Start Car Rental"));
            Assert.Null(sut.ErrorString);
        }
        // GET: Rent
        public ActionResult RentCar()
        {
            ApplicationUser user  = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
            var             osoba = osobaRepository.GetOsobaByHash(user.UserHash);

            if (osoba.Adres == null || osoba.DataUrodzenia == null || osoba.KodPocztowy == null || osoba.Kraj == null || osoba.NrTelefonu == null || osoba.Miasto == null)
            {
                return(View("AddYourPersonalData"));
            }

            var wypTemp      = wypozyczenieTempRepository.FindWypozyczenieTempForOsobaId(osoba.OsobaId);
            var lengthOfRent = wypTemp.FirstOrDefault().DataZwrotu.Date.Day - wypTemp.FirstOrDefault().DataWypozyczenia.Date.Day;


            List <SelectListItem> wydarzenieList = new SelectList(wydarzenieRepository.FindAllWydarzenie(), "WydarzenieId", "NazwaWydarzenia").ToList();

            ViewData["wydarzenieList"] = wydarzenieList;

            RentCarViewModel rentCar = new RentCarViewModel();

            rentCar.CarInCartList = wypTemp;

            foreach (var item in rentCar.CarInCartList)
            {
                if (item.Samochod.Cennik.Count() > 0)
                {
                    if (lengthOfRent < 7)
                    {
                        if (item.Samochod.Cennik.Where(c => c.Opis == "dzien").Count() > 0)
                        {
                            item.PricePerDay    = (item.Samochod.Cennik.Where(c => c.Opis == "dzien").SingleOrDefault().Cena);
                            rentCar.TotalPrice += item.PricePerDay * lengthOfRent;
                        }
                    }
                    else if (lengthOfRent >= 7 && lengthOfRent < 30)
                    {
                        if (item.Samochod.Cennik.Where(c => c.Opis == "tydzien").Count() > 0)
                        {
                            item.PricePerDay    = (item.Samochod.Cennik.Where(c => c.Opis == "tydzien").SingleOrDefault().Cena);
                            rentCar.TotalPrice += item.PricePerDay * lengthOfRent;
                        }
                    }
                    else if (lengthOfRent >= 30)
                    {
                        if (item.Samochod.Cennik.Where(c => c.Opis == "miesiac").Count() > 0)
                        {
                            item.PricePerDay    = (item.Samochod.Cennik.Where(c => c.Opis == "miesiac").SingleOrDefault().Cena);
                            rentCar.TotalPrice += item.PricePerDay * lengthOfRent;
                        }
                    }
                }
            }

            rentCar.DataWypozyczenia = wypTemp.FirstOrDefault().DataWypozyczenia;
            rentCar.DataZwrotu       = wypTemp.FirstOrDefault().DataZwrotu;
            rentCar.OsobaId          = osoba.OsobaId;

            return(View(rentCar));
        }
        public ActionResult Summary()
        {
            ApplicationUser user    = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
            var             osoba   = osobaRepository.GetOsobaByHash(user.UserHash);
            var             wypTemp = wypozyczenieTempRepository.FindWypozyczenieTempForOsobaId(osoba.OsobaId);

            if (wypTemp.Count() == 0)
            {
                return(RedirectToAction("SearchCar", "Cars"));
            }

            var lengthOfRent = wypTemp.FirstOrDefault().DataZwrotu.Date.Day - wypTemp.FirstOrDefault().DataWypozyczenia.Date.Day;



            RentCarViewModel rentCar = new RentCarViewModel();

            rentCar.CarInCartList = wypTemp;
            foreach (var item in rentCar.CarInCartList)
            {
                if (item.Samochod.Cennik.Count() > 0)
                {
                    if (lengthOfRent < 7)
                    {
                        if (item.Samochod.Cennik.Where(c => c.Opis == "dzien").Count() > 0)
                        {
                            item.PricePerDay    = (item.Samochod.Cennik.Where(c => c.Opis == "dzien").SingleOrDefault().Cena);
                            rentCar.TotalPrice += item.PricePerDay * lengthOfRent;
                        }
                    }
                    else if (lengthOfRent >= 7 && lengthOfRent < 30)
                    {
                        if (item.Samochod.Cennik.Where(c => c.Opis == "tydzien").Count() > 0)
                        {
                            item.PricePerDay    = (item.Samochod.Cennik.Where(c => c.Opis == "tydzien").SingleOrDefault().Cena);
                            rentCar.TotalPrice += item.PricePerDay * lengthOfRent;
                        }
                    }
                    else if (lengthOfRent >= 30)
                    {
                        if (item.Samochod.Cennik.Where(c => c.Opis == "miesiac").Count() > 0)
                        {
                            item.PricePerDay    = (item.Samochod.Cennik.Where(c => c.Opis == "miesiac").SingleOrDefault().Cena);
                            rentCar.TotalPrice += item.PricePerDay * lengthOfRent;
                        }
                    }
                }
            }

            rentCar.OsobaId          = osoba.OsobaId;
            rentCar.DataWypozyczenia = wypTemp.FirstOrDefault().DataWypozyczenia;
            rentCar.DataZwrotu       = wypTemp.FirstOrDefault().DataZwrotu;

            return(View(rentCar));
        }
 public DriverMainViewModel(IMessengerService messengerService, RentCarViewModel rentCarViewModel,
                            ActiveRentalSessionViewModel activeRentalSessionViewModel, DriverAccountViewModel driverAccountViewModel,
                            DriverRentalsViewModel driverRentalsViewModel) : base(messengerService)
 {
     Messenger.Default.Register <NotificationMessage>(this, SwitchRentalView);
     _rentCarViewModel             = rentCarViewModel ?? throw new ArgumentNullException();
     _activeRentalSessionViewModel = activeRentalSessionViewModel ?? throw new ArgumentNullException();
     DriverAccountViewModel        = driverAccountViewModel ?? throw new ArgumentNullException();
     DriverRentalsViewModel        = driverRentalsViewModel ?? throw new ArgumentNullException();
     CurrentRentCarViewModel       = _rentCarViewModel;
 }
        public void ShouldUpdatePropertiesAccordinglyToDriverStatusWhenAssigningLoggedInDriver(DriverStatus driverStatus, string driverBannedError, bool isCarListEnabled)
        {
            var driverViewModel = new DriverViewModel()
            {
                DriverStatus = driverStatus
            };
            var sut = new RentCarViewModel(_carServiceMock, _rentalServiceMock, _carViewModelMapperMock,
                                           _rentalViewModelMapperMock, _messengerServiceMock);

            sut.AssignLoggedInDriver(driverViewModel);
            Assert.AreEqual(driverBannedError, sut.DriverBannedError);
            Assert.AreEqual(sut.IsCarListEnabled, isCarListEnabled);
        }
        public void ShouldNotSendRentalViewModelMessageWithContinueRentalPropertyIfDriverDoesntHaveActiveRental()
        {
            var driverId        = Guid.NewGuid();
            var driverViewModel = new DriverViewModel
            {
                Id = driverId
            };
            var sut = new RentCarViewModel(_carServiceMock, _rentalServiceMock, _carViewModelMapperMock,
                                           _rentalViewModelMapperMock, _messengerServiceMock);

            sut.AssignLoggedInDriver(driverViewModel);
            _messengerServiceMock.DidNotReceive().Send(Arg.Any <RentalViewModelMessage>());
            _messengerServiceMock.DidNotReceive().Send(Arg.Is <NotificationMessage>(message => message.Notification == "Start Car Rental"));
        }
        public void ShouldNotRentCarIfRequirementsAreNotFulfilled()
        {
            var driverId        = Guid.NewGuid();
            var driverViewModel = new DriverViewModel
            {
                Id           = driverId,
                DriverStatus = DriverStatus.Banned
            };
            var sut = new RentCarViewModel(_carServiceMock, _rentalServiceMock, _carViewModelMapperMock,
                                           _rentalViewModelMapperMock, _messengerServiceMock);

            sut.CurrentDriver = driverViewModel;
            sut.SelectedCar   = null;
            sut.RentCarCommand.Execute(null);
            _rentalServiceMock.DidNotReceive().TakeCar(Arg.Any <Guid>(), Arg.Any <Guid>(), Arg.Any <Guid>(), Arg.Any <DateTime>());
        }
        public void ShouldPopulateAvailableCarList()
        {
            var carList = new List <CarDTO>()
            {
                new CarDTO()
            };
            var expectedCarViewModel = new CarViewModel();

            _carServiceMock.GetFreeCars().Returns(carList);
            _carViewModelMapperMock.Map(carList[0]).Returns(expectedCarViewModel);
            var sut = new RentCarViewModel(_carServiceMock, _rentalServiceMock, _carViewModelMapperMock,
                                           _rentalViewModelMapperMock, _messengerServiceMock);
            var availableCars = sut.AvailableCars;

            Assert.AreEqual(1, availableCars.Count);
            Assert.AreEqual(expectedCarViewModel, availableCars[0]);
        }
 public void Setup()
 {
     _messengerServiceMock             = Substitute.For <IMessengerService>();
     _activeRentalSessionViewModelMock = Substitute.For <ActiveRentalSessionViewModel>(
         Substitute.For <ITimerFactory>(), Substitute.For <IRentalService>(), Substitute.For <IDialogService>(),
         Substitute.For <IMessengerService>());
     _rentCarViewModelMock = Substitute.For <RentCarViewModel>(Substitute.For <ICarService>(),
                                                               Substitute.For <IRentalService>(), Substitute.For <ICarViewModelMapper>(),
                                                               Substitute.For <IRentalViewModelMapper>(), Substitute.For <IMessengerService>());
     _driverAccountViewModelMock = Substitute.For <DriverAccountViewModel>(Substitute.For <IDriverService>(),
                                                                           Substitute.For <IDriverViewModelMapper>());
     _driverRentalsViewModelMock = Substitute.For <DriverRentalsViewModel>(Substitute.For <IRentalService>(),
                                                                           Substitute.For <IRentalViewModelMapper>());
     _driverRentalsViewModelMock.CurrentDriver = new DriverViewModel()
     {
         Id = Guid.NewGuid()
     };
 }
        public void ShouldSetErrorStringIfFailedToRentCar()
        {
            var driverId        = Guid.NewGuid();
            var driverViewModel = new DriverViewModel
            {
                Id           = driverId,
                DriverStatus = DriverStatus.Active
            };
            var carId = Guid.NewGuid();
            var car   = new CarViewModel()
            {
                Id = carId
            };

            _rentalServiceMock.When(x => x.TakeCar(Arg.Any <Guid>(), carId, driverId, Arg.Any <DateTime>())).Do(x => throw new Exception());
            var sut = new RentCarViewModel(_carServiceMock, _rentalServiceMock, _carViewModelMapperMock,
                                           _rentalViewModelMapperMock, _messengerServiceMock)
            {
                CurrentDriver = driverViewModel, SelectedCar = car
            };

            sut.RentCarCommand.Execute(null);
            Assert.AreEqual("Could not rent car.", sut.ErrorString);
        }
        public ActionResult RentCar(RentCarViewModel rcv, FormCollection collection)
        {
            ApplicationUser user  = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
            var             osoba = osobaRepository.GetOsobaByHash(user.UserHash);


            var wypTemp      = wypozyczenieTempRepository.FindWypozyczenieTempForOsobaId(osoba.OsobaId);
            var lengthOfRent = wypTemp.FirstOrDefault().DataZwrotu - wypTemp.FirstOrDefault().DataWypozyczenia;

            Wypozyczenie wypozyczenie = new Wypozyczenie();

            rcv.DataWypozyczenia = wypTemp.FirstOrDefault().DataWypozyczenia;
            rcv.DataZwrotu       = wypTemp.FirstOrDefault().DataZwrotu;
            rcv.OsobaId          = osoba.OsobaId;
            rcv.CarInCartList    = wypTemp;



            if (ModelState.IsValid)
            {
                wypozyczenie.OsobaId          = rcv.OsobaId;
                wypozyczenie.DataWypozyczenia = rcv.DataWypozyczenia;
                wypozyczenie.DataZwrotu       = rcv.DataZwrotu;
                wypozyczenie.CzyDostarczany   = rcv.CzyDostarczany;
                wypozyczenie.CzyKierowca      = rcv.CzyKierowca;
                wypozyczenie.AdresDojazdu     = rcv.AdresDojazdu;
                wypozyczenie.WydarzenieId     = rcv.WydarzenieId;
                wypozyczenie.Uwagi            = rcv.Uwagi;
                wypozyczenie.Status           = "nowe";

                wypozyczenieRepository.Add(wypozyczenie);
                wypozyczenieRepository.Save();


                //wypozyczenie.MetodaPlatnosci = rcv.PaymentMethod; <<-- Nie ma takiego pola w bazie

                foreach (var item in rcv.CarInCartList)
                {
                    Dostepnosc dostepnosc = new Dostepnosc();
                    dostepnosc.NiedostepnyOd = rcv.DataWypozyczenia;
                    dostepnosc.NiedostepnyDo = rcv.DataZwrotu;
                    dostepnosc.SamochodId    = item.SamochodId;

                    dostepnoscRepository.Add(dostepnosc);
                }

                Rozliczenie rozliczenie = new Rozliczenie();
                rozliczenie.WypozyczenieId = wypozyczenie.WypozyczenieId;
                rozliczenie.Zaliczka       = 0;
                rozliczenie.CalkowityKoszt = rcv.TotalPrice;

                if (rcv.CzyKierowca == true)
                {
                    rozliczenie.CalkowityKoszt = rozliczenie.CalkowityKoszt + (100 * lengthOfRent.Days);
                }

                rozliczenieRepository.Add(rozliczenie);
                dostepnoscRepository.Save();
                rozliczenieRepository.Save();

                foreach (var item in wypTemp)
                {
                    WypSam wypSam = new WypSam();
                    wypSam.SamochodId     = item.SamochodId;
                    wypSam.WypozyczenieId = wypozyczenie.WypozyczenieId;

                    wypSamRepository.Add(wypSam);
                }
                wypSamRepository.Save();



                var wypTempForDate = wypozyczenieTempRepository.FindAllWypozyczenieTempForDate(rcv.DataWypozyczenia, rcv.DataZwrotu);

                foreach (var item in wypTempForDate)
                {
                    wypozyczenieTempRepository.Delete(item);
                }

                //foreach (var item in wypTemp)
                //{
                //    wypozyczenieTempRepository.Delete(item);
                //}

                wypozyczenieTempRepository.Save();


                return(RedirectToAction("RentDetails", new { id = wypozyczenie.WypozyczenieId }));
            }
            else
            {
                return(View());
            }
        }