public ActionResult AddVehicle(DetailsVM addVehicle)
 {
     if (addVehicle.Vehicle.Year < 2000 || addVehicle.Vehicle.Year > 2021)
     {
         ModelState.AddModelError("Vehicle.Year", "Please enter a year between 2000 and 2021.");
     }
     if (addVehicle.Vehicle.MSRP < 0)
     {
         ModelState.AddModelError("Vehicle.MSRP", "MSRP must be a positive number.");
     }
     if (addVehicle.Vehicle.SalePrice < 0)
     {
         ModelState.AddModelError("Vehicle.SalePrice", "SalePrice must be a positive number.");
     }
     if (addVehicle.Vehicle.MSRP < addVehicle.Vehicle.SalePrice)
     {
         ModelState.AddModelError("Vehicle.MSRP", "SalePrice must be lower than MSRP.");
     }
     if (ModelState.IsValid)
     {
         addVehicle.Vehicle.Picture = "placeholder.png";
         VehicleRepositoryFactory.GetVehicleRepository().Add(addVehicle.Vehicle);
         return(RedirectToAction("EditVehicle", new { vinNumber = addVehicle.Vehicle.VinNumber }));
     }
     else
     {
         var viewModel = addVehicle;
         viewModel.BodyStyles   = BodyStyleRepositoryFactory.GetBodyStyleRepository().GetBodyStyles();
         viewModel.Colors       = ColorRepositoryFactory.GetColorRepository().GetColors();
         viewModel.Makes        = MakeRepositoryFactory.GetMakeRepository().GetMakes();
         viewModel.Models       = ModelRepositoryFactory.GetModelRepository().GetModels();
         viewModel.Transmission = TransmissionTypeRepositoryFactory.GetTransmissionTypeRepository().GetTransmissions();
         return(View(viewModel));
     }
 }
        public ActionResult Purchase(SaleVM sold)
        {
            var details = new DetailsVM();

            details.Vehicle      = VehicleRepositoryFactory.GetVehicleRepository().GetVehicleByVIN(sold.Sale.VinNumber);
            details.BodyStyles   = BodyStyleRepositoryFactory.GetBodyStyleRepository().GetBodyStyles();
            details.Colors       = ColorRepositoryFactory.GetColorRepository().GetColors();
            details.Makes        = MakeRepositoryFactory.GetMakeRepository().GetMakes();
            details.Models       = ModelRepositoryFactory.GetModelRepository().GetModels();
            details.Transmission = TransmissionTypeRepositoryFactory.GetTransmissionTypeRepository().GetTransmissions();

            sold.Details = details;

            if (sold.Sale.Phone == null && sold.Sale.Email == null)
            {
                ModelState.AddModelError("", "You must enter a phone number or an email address.");
            }
            if (sold.Sale.Zipcode.Length > 5)
            {
                ModelState.AddModelError("", "Zipcode should only be 5 digits long, please.");
            }
            if (sold.Sale.PurchasePrice < ((sold.Details.Vehicle.MSRP * 95) * .01m))
            {
                ModelState.AddModelError("", "Purchase price should not be lower than 95% vehicle MSRP, please recalculate.");
            }
            if (sold.Sale.PurchasePrice > sold.Details.Vehicle.MSRP)
            {
                ModelState.AddModelError("", "Purchase price should not be greater than MSRP, please recalculate.");
            }


            var repo = SaleRepositoryFactory.GetSaleRepository();

            if (ModelState.IsValid)
            {
                sold.Sale.UserID       = User.Identity.GetUserId();
                sold.Sale.PurchaseDate = DateTime.Now;
            }

            var result = repo.Add(sold.Sale);

            if (result != null)
            {
                var vehicleToUpdate = VehicleRepositoryFactory.GetVehicleRepository().GetVehicleByVIN(sold.Sale.VinNumber);
                vehicleToUpdate.IsPurchased = true;
                vehicleToUpdate.IsFeatured  = false;
                VehicleRepositoryFactory.GetVehicleRepository().Edit(vehicleToUpdate);

                return(View("Index"));
            }


            // assign sold.UserID to currently logged in user
            // assign sold.PurchaseDate to DateTime.Now
            // if saving the purchase is a success, update vehicle.IsPurchased to true and vehicle.IsFeatured to false;
            return(View());
        }
        public ActionResult AddVehicle()
        {
            var viewModel = new DetailsVM();

            viewModel.BodyStyles   = BodyStyleRepositoryFactory.GetBodyStyleRepository().GetBodyStyles();
            viewModel.Colors       = ColorRepositoryFactory.GetColorRepository().GetColors();
            viewModel.Makes        = MakeRepositoryFactory.GetMakeRepository().GetMakes();
            viewModel.Models       = ModelRepositoryFactory.GetModelRepository().GetModels();
            viewModel.Transmission = TransmissionTypeRepositoryFactory.GetTransmissionTypeRepository().GetTransmissions();
            return(View(viewModel));
        }
        public ActionResult EditVehicle(string vinNumber)
        {
            var viewModel = new DetailsVM();

            viewModel.Vehicle      = VehicleRepositoryFactory.GetVehicleRepository().GetVehicleByVIN(vinNumber);
            viewModel.BodyStyles   = BodyStyleRepositoryFactory.GetBodyStyleRepository().GetBodyStyles();
            viewModel.Colors       = ColorRepositoryFactory.GetColorRepository().GetColors();
            viewModel.Makes        = MakeRepositoryFactory.GetMakeRepository().GetMakes();
            viewModel.Models       = ModelRepositoryFactory.GetModelRepository().GetModels();
            viewModel.Transmission = TransmissionTypeRepositoryFactory.GetTransmissionTypeRepository().GetTransmissions();
            return(View(viewModel));
        }
        public ActionResult Inventory()
        {
            var bodystyles   = BodyStyleRepositoryFactory.GetBodyStyleRepository().GetBodyStyles();
            var colors       = ColorRepositoryFactory.GetColorRepository().GetColors();
            var makes        = MakeRepositoryFactory.GetMakeRepository().GetMakes();
            var models       = ModelRepositoryFactory.GetModelRepository().GetModels();
            var transmission = TransmissionTypeRepositoryFactory.GetTransmissionTypeRepository().GetTransmissions();
            var vehicles     = VehicleRepositoryFactory.GetVehicleRepository().GetVehicles();

            var vehiclesToReturn = new List <VehicleDisplayVM>();

            foreach (var vehicle in vehicles)
            {
                var model = new VehicleDisplayVM();

                model.Vehicle       = vehicle;
                model.Model         = models.FirstOrDefault(m => m.ModelID == vehicle.ModelID);
                model.Make          = makes.FirstOrDefault(m => m.MakeID == vehicle.MakeID);
                model.BodyStyle     = bodystyles.FirstOrDefault(b => b.BodyStyleID == vehicle.BodyStyleID);
                model.ExteriorColor = colors.FirstOrDefault(c => c.ColorID == vehicle.ExteriorColor);
                model.InteriorColor = colors.FirstOrDefault(c => c.ColorID == vehicle.InteriorColor);
                model.Transmission  = transmission.FirstOrDefault(t => t.TransmissionID == vehicle.TransmissionID);

                vehiclesToReturn.Add(model);
            }

            //vehiclesToReturn = vehiclesToReturn.OrderBy(vehicle => vehicle.Vehicle.Year).ThenBy(vehicle => vehicle.Make.MakeName).ThenBy(vehicle => vehicle.Model.ModelName).ToList();

            var usedVehicles = vehiclesToReturn.Where(v => v.Vehicle.Mileage > 1000).ToList();
            var newVehicles  = vehiclesToReturn.Where(v => v.Vehicle.Mileage <= 1000).ToList();


            List <InventoryReportVehicleGroup> usedVehicleGroupList = VehicleList(usedVehicles);
            List <InventoryReportVehicleGroup> newVehicleGroupList  = VehicleList(newVehicles);

            InventoryReportViewModel viewModel = new InventoryReportViewModel()
            {
                NewVehicles  = newVehicleGroupList,
                UsedVehicles = usedVehicleGroupList
            };

            return(View(viewModel));
        }
        public ActionResult Purchase(string vinNumber)
        {
            var details = new DetailsVM();

            details.Vehicle      = VehicleRepositoryFactory.GetVehicleRepository().GetVehicleByVIN(vinNumber);
            details.BodyStyles   = BodyStyleRepositoryFactory.GetBodyStyleRepository().GetBodyStyles();
            details.Colors       = ColorRepositoryFactory.GetColorRepository().GetColors();
            details.Makes        = MakeRepositoryFactory.GetMakeRepository().GetMakes();
            details.Models       = ModelRepositoryFactory.GetModelRepository().GetModels();
            details.Transmission = TransmissionTypeRepositoryFactory.GetTransmissionTypeRepository().GetTransmissions();

            var viewModel = new SaleVM();

            viewModel.Sale           = new GuildCars.Models.Tables.Sale();
            viewModel.Sale.VinNumber = vinNumber;
            viewModel.Details        = details;

            return(View(viewModel));
        }
        public IHttpActionResult SearchAdminVehicles(string searchTerm, decimal priceMin, decimal priceMax, int yearMin, int yearMax, int mileage)
        {
            var vehicleRepo  = VehicleRepositoryFactory.GetVehicleRepository();
            var bodystyles   = BodyStyleRepositoryFactory.GetBodyStyleRepository().GetBodyStyles();
            var colors       = ColorRepositoryFactory.GetColorRepository().GetColors();
            var makes        = MakeRepositoryFactory.GetMakeRepository().GetMakes();
            var models       = ModelRepositoryFactory.GetModelRepository().GetModels();
            var transmission = TransmissionTypeRepositoryFactory.GetTransmissionTypeRepository().GetTransmissions();

            if (searchTerm == "chipmunk")
            {
                searchTerm = "";
            }

            try
            {
                var vehicles         = vehicleRepo.GetVehiclesBySearchParameters(searchTerm, priceMin, priceMax, yearMin, yearMax, mileage);
                var vehiclesToReturn = new List <VehicleDisplayVM>();
                foreach (var vehicle in vehicles)
                {
                    var model = new VehicleDisplayVM();

                    model.Vehicle       = vehicle;
                    model.Model         = models.FirstOrDefault(m => m.ModelID == vehicle.ModelID);
                    model.Make          = makes.FirstOrDefault(m => m.MakeID == vehicle.MakeID);
                    model.BodyStyle     = bodystyles.FirstOrDefault(b => b.BodyStyleID == vehicle.BodyStyleID);
                    model.ExteriorColor = colors.FirstOrDefault(c => c.ColorID == vehicle.ExteriorColor);
                    model.InteriorColor = colors.FirstOrDefault(c => c.ColorID == vehicle.InteriorColor);
                    model.Transmission  = transmission.FirstOrDefault(t => t.TransmissionID == vehicle.TransmissionID);

                    vehiclesToReturn.Add(model);
                }
                vehiclesToReturn.RemoveAll(v => v.Vehicle.IsPurchased == true);
                return(Ok(vehiclesToReturn));
            }
            catch
            {
                return(BadRequest());
            }
        }
        public ActionResult EditVehicle(DetailsVM editVehicle)
        {
            if (editVehicle.Vehicle.Year < 2000 || editVehicle.Vehicle.Year > 2021)
            {
                ModelState.AddModelError("Vehicle.Year", "Please enter a year between 2000 and 2021.");
            }
            if (editVehicle.Vehicle.MSRP <= 0)
            {
                ModelState.AddModelError("Vehicle.MSRP", "MSRP must be greater than $0.");
            }
            if (editVehicle.Vehicle.SalePrice < 0)
            {
                ModelState.AddModelError("Vehicle.SalePrice", "SalePrice must be greater than $0.");
            }
            if (editVehicle.Vehicle.MSRP < editVehicle.Vehicle.SalePrice)
            {
                ModelState.AddModelError("Vehicle.MSRP", "SalePrice must be lower than MSRP.");
            }
            if (ModelState.IsValid)
            {
                editVehicle.Vehicle.Picture = "placeholder.png";
                VehicleRepositoryFactory.GetVehicleRepository().Edit(editVehicle.Vehicle);
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                var viewModel = new DetailsVM();
                viewModel.Vehicle      = editVehicle.Vehicle;
                viewModel.BodyStyles   = BodyStyleRepositoryFactory.GetBodyStyleRepository().GetBodyStyles();
                viewModel.Colors       = ColorRepositoryFactory.GetColorRepository().GetColors();
                viewModel.Makes        = MakeRepositoryFactory.GetMakeRepository().GetMakes();
                viewModel.Models       = ModelRepositoryFactory.GetModelRepository().GetModels();
                viewModel.Transmission = TransmissionTypeRepositoryFactory.GetTransmissionTypeRepository().GetTransmissions();

                return(View(viewModel));
            }
        }