public bool AddRefuling(NewRefulingVm model, NewCarHistoryVm carHistoryVm)
        {
            if (model is null)
            {
                return(false);
            }
            else
            {
                var refuelingModelToAdd  = _mapper.Map <Refueling>(model);
                var carHistoryModelToAdd = _mapper.Map <CarHistory>(carHistoryVm);

                refuelingModelToAdd.IsActive = true;
                refuelingModelToAdd.BurningFuelPerOneHundredKilometers =
                    VehicleServiceHelpers.ReturnBurningPerOneHoundredKilometers(model);

                refuelingModelToAdd.FuelPrice = decimal.Multiply(refuelingModelToAdd.AmountOfFuel, refuelingModelToAdd.PriceForOneUnit);
                string userId = GetUserIdByVehicleId(refuelingModelToAdd.VehicleId);
                bool   refuelingSucessfullyAdded = _vehicleRepository.AddRefueling(refuelingModelToAdd, userId, carHistoryModelToAdd);

                if (refuelingSucessfullyAdded == true)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        public IActionResult EditRefueling(NewRefulingVm refueling)
        {
            if (refueling is null)
            {
                TempData["InCorrectOperation"] = "Nie udało się edytować tankowania";
                return(RedirectToAction("VehicleHistory", "Vehicle", new { id = refueling.VehicleId }));
            }

            if (refueling.VehicleId != 0)
            {
                refueling.LastMeters = _vehicleService.GetLastRefuelingMileage(refueling.VehicleId);
            }

            if (refueling.MeterStatus < refueling.LastMeters)
            {
                ModelState.AddModelError("MeterStatus", $"Aktualny przebieg, nie może być niższy od poprzedniego - {refueling.LastMeters} km, edytuj przebieg pojazdu, lub wpisz inną wartość!");
            }
            if (!ModelState.IsValid)
            {
                refueling.VehiclesList      = _vehicleService.GetUserCars(_userManager.GetUserId(User));
                refueling.VehicleFuelTypes  = _vehicleService.GetAllFuelsTypesForRefuling();
                refueling.UnitOfFuelForList = _vehicleService.GetUnitsOfFuels();
                return(View(refueling));
            }
            _vehicleService.EditRefueling(refueling);
            TempData["CorrectOperation"] = "Udało się edytować tankowanie";

            return(RedirectToAction("VehicleHistory", "Vehicle", new { id = refueling.VehicleId }));
        }
        public IActionResult EditRefueling(string id)
        {
            NewRefulingVm refueling = _vehicleService.GetRefulingForEditById(id);

            refueling.VehiclesList      = _vehicleService.GetUserCars(_userManager.GetUserId(User));
            refueling.VehicleFuelTypes  = _vehicleService.GetAllFuelsTypesForRefuling();
            refueling.UnitOfFuelForList = _vehicleService.GetUnitsOfFuels();
            return(View(refueling));
        }
        public void ShouldBeReturnBurnigFuelPerOneHounderKilometers()
        {
            //Arrange
            //Correct data
            var firstModel = new NewRefulingVm()
            {
                LastMeters     = 100,
                MeterStatus    = 550,
                AmountOfFuel   = 30,
                IsRefulingFull = true
            };
            //Kilometers before refueling is greather than actually meter status
            var secondModel = new NewRefulingVm()
            {
                LastMeters     = 1200,
                MeterStatus    = 1190,
                IsRefulingFull = true
            };
            //Kilometers before refueling is equal with actually meter status
            var thirdModel = new NewRefulingVm()
            {
                LastMeters     = 1200,
                MeterStatus    = 1200,
                IsRefulingFull = true
            };
            var fourthModel = new NewRefulingVm()
            {
                LastMeters     = 1200,
                MeterStatus    = 1390,
                IsRefulingFull = true
            };
            //Act
            decimal burningFuelPerOneHoundredKilometersIsRefuelingFullTrue = VehicleServiceHelpers
                                                                             .ReturnBurningPerOneHoundredKilometers(firstModel);

            decimal burningFuelPerOneHoundredKilometersIsRefuelingFullFalse = VehicleServiceHelpers
                                                                              .ReturnBurningPerOneHoundredKilometers(secondModel);

            decimal burningFuelPerOneHoundredKilometersIsRefuelingAfterRefuelingIsLessThanBefore = VehicleServiceHelpers
                                                                                                   .ReturnBurningPerOneHoundredKilometers(thirdModel);

            decimal burningFuelPerOneHoundredKilometersIsRefuelingAfterRefuelingIsEqualThanBefore = VehicleServiceHelpers
                                                                                                    .ReturnBurningPerOneHoundredKilometers(fourthModel);

            //Assert
            burningFuelPerOneHoundredKilometersIsRefuelingFullTrue.Should().Be(6.6667M);
            burningFuelPerOneHoundredKilometersIsRefuelingFullTrue.Should().BePositive();
            burningFuelPerOneHoundredKilometersIsRefuelingFullTrue.Should().NotBe(0M);

            burningFuelPerOneHoundredKilometersIsRefuelingFullFalse.Should().Be(0M);

            burningFuelPerOneHoundredKilometersIsRefuelingAfterRefuelingIsLessThanBefore.Should().Be(0);

            burningFuelPerOneHoundredKilometersIsRefuelingAfterRefuelingIsEqualThanBefore.Should().Be(0);
        }
        public IActionResult AddRefueling()
        {
            var model = new NewRefulingVm()
            {
                VehiclesList      = _vehicleService.GetUserCars(_userManager.GetUserId(User)),
                VehicleFuelTypes  = _vehicleService.GetAllFuelsTypesForRefuling(),
                UnitOfFuelForList = _vehicleService.GetUnitsOfFuels()
            };

            return(View(model));
        }
        public static decimal ReturnBurningPerOneHoundredKilometers(NewRefulingVm newRefulingVm)
        {
            if (newRefulingVm.IsRefulingFull == false)
            {
                return(0M);
            }
            else if (newRefulingVm.LastMeters >= newRefulingVm.MeterStatus)
            {
                return(0M);
            }

            int differenceKilometersBetweenRefuling = newRefulingVm.MeterStatus - newRefulingVm.LastMeters;

            decimal burningFuelPerOneHoundredKilometers = decimal.Multiply(decimal.Divide(newRefulingVm.AmountOfFuel, differenceKilometersBetweenRefuling), 100);

            burningFuelPerOneHoundredKilometers = decimal.Round(burningFuelPerOneHoundredKilometers, 4);
            return(burningFuelPerOneHoundredKilometers);
        }
        public IActionResult AddRefueling(NewRefulingVm model)
        {
            if (model is null)
            {
                TempData["InCorrectOperation"] = "Tankowanie nie dodane, skontaktuj się z pomocą techniczną aby zgłosić błąd, lub spróbuj ponownie!";
                return(RedirectToAction("VehicleHistory", "Vehicle", new { id = model.VehicleId }));
            }
            if (model.VehicleId != 0)
            {
                model.LastMeters = _vehicleService.GetLastRefuelingMileage(model.VehicleId);
            }

            if (model.MeterStatus < model.LastMeters)
            {
                ModelState.AddModelError("MeterStatus", $"Aktualny przebieg, nie może być niższy od poprzedniego - {model.LastMeters} km, edytuj przebieg pojazdu, lub wpisz inną wartość!");
            }
            if (!ModelState.IsValid)
            {
                model.VehicleFuelTypes  = _vehicleService.GetAllFuelsTypesForRefuling();
                model.UnitOfFuelForList = _vehicleService.GetUnitsOfFuels();
                model.VehiclesList      = _vehicleService.GetUserCars(_userManager.GetUserId(User));
                return(View(model));
            }
            var carHistory = _vehicleService.ReturnCarHistoryToAdd("Tankowanie", _userManager.GetUserId(User));
            var isAddedRefuelingCorrectly = _vehicleService.AddRefuling(model, carHistory);

            if (isAddedRefuelingCorrectly == true)
            {
                TempData["CorrectOperation"] = "Pomyślnie dodano tankowanie!";
            }
            else
            {
                TempData["InCorrectOperation"] = "Tankowanie nie dodane, skontaktuj się z pomocą techniczną aby zgłosić błąd, lub spróbuj ponownie!";
            }

            return(RedirectToAction("VehicleHistory", "Vehicle", new { id = model.VehicleId }));
        }
        public bool EditRefueling(NewRefulingVm refueling)
        {
            if (refueling is null)
            {
                return(false);
            }

            var refuelingForUpdate = _mapper.Map <Refueling>(refueling);

            refuelingForUpdate.BurningFuelPerOneHundredKilometers = VehicleServiceHelpers.ReturnBurningPerOneHoundredKilometers(refueling);

            refuelingForUpdate.FuelPrice        = decimal.Multiply(refuelingForUpdate.AmountOfFuel, refuelingForUpdate.PriceForOneUnit);
            refuelingForUpdate.ModifiedDateTime = DateTime.UtcNow;
            bool isCorrectEdit = _vehicleRepository.EditRefueling(refuelingForUpdate);

            if (isCorrectEdit == true)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }