public IEnumerable<ModelViewModel> RetrieveModelByItemByBrand(ModelViewModel item)
        {
            var records = from models in db.Models
                          join brands in db.Brands on models.BrandId equals brands.BrandId
                          where models.BrandId == item.BrandId
                          && models.ItemId == item.ItemId
                          select models;

            return records;
        }
Exemple #2
0
        public ModelViewModel Create(ModelViewModel item)
        {
            var newRecord = new ModelViewModel
            {
                BrandId = item.BrandId,
                ModelNumber = item.ModelNumber,
                ItemId = item.ItemId,
            };
            db.Models.Add(newRecord);
            db.SaveChanges();

            return newRecord;
        }
Exemple #3
0
        public ModelViewModel Update(int id, ModelViewModel item)
        {
            var currentrecord = db.Models
               .Where(x => x.ModelId == id)
               .FirstOrDefault();

            if (!(String.IsNullOrWhiteSpace(item.ModelNumber)))
            {
                currentrecord.ModelNumber = item.ModelNumber;
                currentrecord.Price = item.Price;
                db.SaveChanges();
                return currentrecord;
            }

            return currentrecord;
        }
        public ActionResult RevertArchive(ModelViewModel item)
        {
            try
            {
                string userId = User.Identity.GetUserId();
                if (userId == null)
                {
                    return RedirectToAction("Login", "Account");
                }

                //Check if the "Admin" role exists if not it returns a null value
                var role = db.Roles.SingleOrDefault(m => m.Name == "Admin");

                if (User.IsInRole("Admin"))
                {
                    //Runs a query to determine if the user is actually an "Admin" if not it returns a null value
                    //var userInRole = db.Users.Where(m => m.Roles.Any(r => r.UserId == userId)).FirstOrDefault();
                    //if (userInRole != null)
                    //{
                    var specId = item.ModelId;
                    if (specId < 1)
                    {
                        RedirectToAction("RetrieveArchives");
                    }
                    if (item == null)
                    {
                        RedirectToAction("RetrieveArchives", new { message = ManageMessageId.Error });
                    }

                    _repo.RevertArchive(specId);

                    //   return RedirectToAction("Retrieve");
                    return RedirectToAction("RetrieveArchives", new { message = ManageMessageId.RestoreSuccess });
                }
                return RedirectToAction("Login", "Account");
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString());
            }
        }
        public ActionResult Create(ModelViewModel item)
        {
            try
            {
                string userId = User.Identity.GetUserId();
                if (userId == null)
                {
                    return RedirectToAction("Login", "Account");
                }

                //Check if the "Admin" role exists if not it returns a null value
                //var role = db.roles.singleordefault(m => m.name == "admin");

                if (User.IsInRole("Admin"))
                {
                    if (item == null)
                    {
                        return RedirectToAction("Retrieve", new { message = ManageMessageId.Error });
                    }

                    // var newItem = _repository.Create(item);
                    if (ModelState.IsValid)
                    {
                        ViewBag.BrandId = new SelectList(db.Brands.Where(x => x.Status == "Active"), "BrandId", "Name", item.BrandId);
                        ViewBag.ItemId = new SelectList(db.Items.Where(x => x.AdminStatus == "Active"), "ItemId", "Description", item.ItemId);

                        item.Status = "Active";
                        db.Models.Add(item);
                        db.SaveChanges();
                        return RedirectToAction("Retrieve", new { message = ManageMessageId.AddSuccess });
                    }

                    return RedirectToAction("Retrieve", new { message = ManageMessageId.Error });
                }
                return RedirectToAction("Login", "Account");
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString());
            }
        }
        // GET: Model/Create
        public ActionResult Create()
        {
            try
            {
                string userId = User.Identity.GetUserId();
                if (userId == null)
                {
                    return RedirectToAction("Login", "Account");
                }

                //Check if the "Admin" role exists if not it returns a null value
                //var role = db.roles.singleordefault(m => m.name == "admin");

                if (User.IsInRole("Admin"))
                {
                    var newItem = new ModelViewModel();
                    ViewBag.BrandId = new SelectList(db.Brands.Where(x => x.Status == "Active"), "BrandId", "Name");
                    ViewBag.ItemId = new SelectList(db.Items.Where(x => x.AdminStatus == "Active"), "ItemId", "Description");

                    GetNewSupplierActivation();
                    GetNewModelsActivation();
                    return View(newItem);
                }
                return RedirectToAction("Login", "Account");
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString());
            }
        }
        public ActionResult ModelCreation(SpecProductViewModel item, HttpPostedFileBase modelPhoto)
        {
            try
            {
                string userId = User.Identity.GetUserId();
                if (userId == null)
                {
                    return RedirectToAction("Login", "Account");
                }

                //Check if the "Supplier" role exists if not it returns a null value
                var role = db.Roles.SingleOrDefault(m => m.Name == "Supplier");

                if (User.IsInRole("Supplier"))
                {
                    var photo = UploadPhoto(modelPhoto, item);
                    if (photo != null)
                    {
                        SpecProductViewModel specProd = Session["SpecProd"] as SpecProductViewModel;
                        int brandId = 0;

                        //This object will allow us to use the convert to ConvertToTitleCase method
                        var helper = new Helper();

                        //Convert to Title Case
                        item.BrandName = helper.ConvertToTitleCase(item.BrandName);

                        //Check if brand name exists
                        var brand = (from b in db.Brands
                                     where b.Name == item.BrandName
                                     select b).FirstOrDefault();

                        //If the brand does not exist create a new one
                        if (brand == null)
                        {
                            var newBrand = new BrandViewModel()
                            {
                                Name = item.BrandName
                            };
                            db.Brands.Add(newBrand);
                            db.SaveChanges();
                            brandId = newBrand.BrandId;
                        }

                        //if the brand does exist assign the existing brandId to the brandId of the new model
                        if (brand != null)
                        {
                            brandId = brand.BrandId;
                        }

                        //Create a new instance of ModelViewModel and assign it to the model variable
                        var model = new ModelViewModel();

                        model.Price = item.Price;
                        model.Status = specProd.Status;
                        model.UserId = userId;
                        model.SupplierId = specProd.SupplierId;
                        model.ItemId = specProd.ItemId;
                        model.BrandId = brandId;
                        model.ModelNumber = item.ModelNumber;
                        model.DtCreated = DateTime.UtcNow;
                        model.NumberDaysToAdvert = item.NumberDaysToAdvert;
                        model.DeliveryInDays = item.DeliveryInDays;

                        //Add a new model to the table model and save changes into the database
                        db.Models.Add(model);
                        db.SaveChanges();

                        var modelCommission = new ModelCommissionViewModel()
                        {
                            ModelId = model.ModelId
                        };
                        string commissionName = "Tier1";
                        if (model.NumberDaysToAdvert >= 1 && model.NumberDaysToAdvert < 6)
                            commissionName = "Tier1";
                        if (model.NumberDaysToAdvert >= 6 && model.NumberDaysToAdvert < 20)
                            commissionName = "Tier2";
                        if (model.NumberDaysToAdvert >= 20)
                            commissionName = "Tier3";

                        int commissionId = (from c in db.Commissions
                                            where c.Name == commissionName
                                            select c.CommissionId).FirstOrDefault();

                        modelCommission.CommissionId = commissionId;

                        db.ModelCommissions.Add(modelCommission);
                        db.SaveChanges();

                        var photoModel = new PhotoModelViewModel()
                        {
                            PhotoId = photo.PhotoId,
                            ModelId = model.ModelId
                        };
                        db.PhotoModels.Add(photoModel);
                        db.SaveChanges();

                        Session["ModelSpecData"] = model;
                        GetSupplierNotification();
                        return RedirectToAction("ModelSpecCreation");

                        //return RedirectToAction("ModelSpecCreation");on("ModelSpecCreation");
                    }
                }
                return RedirectToAction("Login", "Account");
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString());
            }
        }
        public ActionResult ItemSelection(ItemViewModel item)
        {
            try
            {
                string userId = User.Identity.GetUserId();
                if (userId == null)
                {
                    return RedirectToAction("Login", "Account");
                }

                //Check if the "Supplier" role exists if not it returns a null value
                var role = db.Roles.SingleOrDefault(m => m.Name == "Supplier");

                if (User.IsInRole("Supplier"))
                {
                    //Runs a query to determine if the user is actually an "Supplier" if not it returns a null value
                    //var userInRole = db.Users.Where(m => m.Roles.Any(r => r.UserId == userId)).FirstOrDefault();
                    //if (userInRole != null)
                    //{
                    if (item == null)
                    {
                        return HttpNotFound();
                    }
                    records = _repository.RetrieveItemByProduct(item);
                    ViewBag.ItemId = new SelectList(records, "ItemId", "Description", item.ItemId);
                    TempData["ModelData"] = new ModelViewModel()
                    {
                        ItemId = item.ItemId
                    };
                    GetSupplierNotification();
                    return RedirectToAction("ModelCreation");
                }
                return RedirectToAction("Login", "Account");
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString());
            }
        }