/// <summary>
 /// Deletes the specified entity to delete.
 /// </summary>
 /// <param name="entityToDelete">The entity to delete.</param>
 public virtual void Delete(TEntity entityToDelete)
 {
     if (context.Entry(entityToDelete).State == EntityState.Detached)
     {
         dbSet.Attach(entityToDelete);
     }
     dbSet.Remove(entityToDelete);
 }
        public IHttpActionResult PutVehicleType(long id, VehicleType vehicleType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != vehicleType.VehicleTypeID)
            {
                return(BadRequest());
            }

            db.Entry(vehicleType).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VehicleTypeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#3
0
        public IHttpActionResult PutUser(long id, User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != user.UserID)
            {
                return(BadRequest());
            }

            db.Entry(user).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#4
0
        public ActionResult Edit([Bind(Include = "PostingFieldID,PostingSiteID,FieldName,IncludeInPosting,IncludeOrder,CreatedBy,UpdatedBy,CreatedDate,UpdatedDate")] PostingField postingfield)
        {
            var user = db.Users.Where(d => d.Email == User.Identity.Name).Select(d => new { d.UserID, d.UserRole.Role }).FirstOrDefault();

            if (user.Role != "Admin")
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }
            if (ModelState.IsValid)
            {
                db.Entry(postingfield).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.PostingSiteID = new SelectList(db.PostingSite, "PostingSiteID", "PostingSiteName", postingfield.PostingSiteID);
            return(View(postingfield));
        }
        public ActionResult UploadExcel(FormCollection formCollection)
        {
            if (Request != null)
            {
                _userId = 3;
                try
                {
                    var user = db.Users.Where(d => d.Email == User.Identity.Name).Select(d => new { d.UserID, d.UserRole.Role }).FirstOrDefault();
                    _userId = user.UserID;
                }
                catch (Exception ex)
                {
                }

                HttpPostedFileBase file = Request.Files["UploadedFile"];
                if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                {
                    string fileName        = file.FileName;
                    string fileContentType = file.ContentType;
                    byte[] fileBytes       = new byte[file.ContentLength];
                    var    data            = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
                    var    vehicleList     = new List <VehicleModel>();
                    try
                    {
                        using (var package = new ExcelPackage(file.InputStream))
                        {
                            var currentSheet = package.Workbook.Worksheets;
                            var workSheet    = currentSheet.First();
                            var noOfCol      = workSheet.Dimension.End.Column;
                            var noOfRow      = workSheet.Dimension.End.Row - 3;
                            var lstMessages  = new List <string>();

                            _makerList          = db.Makers.ToList();
                            _modelList          = db.AutoModels.ToList();
                            _subModelList       = db.SubModels.ToList();
                            _transmissionList   = db.AutoTransmissions.ToList();
                            _colorList          = db.AutoExteriorColors.ToList();
                            _yearList           = db.Years.ToList();
                            _titlesList         = db.VehclieTitles.ToList();
                            _vehicleAddressList = db.VehicleAddress.ToList();
                            var lstStatus = db.InventoryStatus.ToList();

                            _vehicleStatusInventory  = lstStatus.FirstOrDefault(a => a.Status.ToUpper() == "INVENTORY").InventoryStatusID;
                            _vehicleStatusPending    = lstStatus.FirstOrDefault(a => a.Status.ToUpper() == "PENDING").InventoryStatusID;
                            _vehicleStatusSold       = lstStatus.FirstOrDefault(a => a.Status.ToUpper() == "SOLD").InventoryStatusID;
                            _vehicleStatusWholeSale  = lstStatus.FirstOrDefault(a => a.Status.ToUpper() == "WHOLESALE").InventoryStatusID;
                            _vehicleStatusTaqeemDone = lstStatus.FirstOrDefault(a => a.Status.ToUpper() == "TAQEEM DONE").InventoryStatusID;

                            // Getting indexes
                            int indexPrice        = GetHeaderIndex(workSheet, "Price", noOfCol);
                            int indexStatus       = GetHeaderIndex(workSheet, "Status", noOfCol);
                            int indexSNo          = GetHeaderIndex(workSheet, "SNo", noOfCol);
                            int indexMake         = GetHeaderIndex(workSheet, "Make", noOfCol);
                            int indexLocation     = GetHeaderIndex(workSheet, "Location", noOfCol);
                            int indexBranch       = GetHeaderIndex(workSheet, "Branch", noOfCol);
                            int indexBalance      = GetHeaderIndex(workSheet, "Balance", noOfCol);
                            int indexYearOfModel  = GetHeaderIndex(workSheet, "Year Of Model", noOfCol);
                            int indexColour       = GetHeaderIndex(workSheet, "Colour", noOfCol);
                            int indexMileage      = GetHeaderIndex(workSheet, "Mileage", noOfCol);
                            int indexPlateNo      = GetHeaderIndex(workSheet, "Plate No", noOfCol);
                            int indexVehicle      = GetHeaderIndex(workSheet, "Vehicle", noOfCol);
                            int indexVINNumber    = GetHeaderIndex(workSheet, "VIN Number", noOfCol);
                            int indexTransmission = GetHeaderIndex(workSheet, "Transmission", noOfCol);
                            int indexSubModel     = GetHeaderIndex(workSheet, "Sub Model", noOfCol);
                            int indexModel        = GetHeaderIndex(workSheet, "Model", noOfCol);



                            for (int rowIterator = 5; rowIterator <= noOfRow; rowIterator++)
                            {
                                var fileVehicleObj = new VehicleModel();
                                if (indexSNo > 0)
                                {
                                    fileVehicleObj.SNo = Convert.ToInt32(workSheet.Cells[rowIterator, indexSNo].Value);
                                }
                                if (indexMake > 0)
                                {
                                    fileVehicleObj.Make = Convert.ToString(workSheet.Cells[rowIterator, indexMake].Value);
                                }
                                if (indexModel > 0)
                                {
                                    fileVehicleObj.Model = Convert.ToString(workSheet.Cells[rowIterator, indexModel].Value);
                                }
                                if (indexSubModel > 0)
                                {
                                    fileVehicleObj.SubModel = Convert.ToString(workSheet.Cells[rowIterator, indexSubModel].Value);
                                }
                                if (indexTransmission > 0)
                                {
                                    fileVehicleObj.Transmission = Convert.ToString(workSheet.Cells[rowIterator, indexTransmission].Value);
                                }
                                if (indexVINNumber > 0)
                                {
                                    fileVehicleObj.VINNumber = Convert.ToString(workSheet.Cells[rowIterator, indexVINNumber].Value);
                                }
                                if (indexVehicle > 0)
                                {
                                    fileVehicleObj.Vehicle = Convert.ToString(workSheet.Cells[rowIterator, indexVehicle].Value);
                                }
                                if (indexPlateNo > 0)
                                {
                                    fileVehicleObj.PlateNo = Convert.ToString(workSheet.Cells[rowIterator, indexPlateNo].Value);
                                }
                                if (indexMileage > 0)
                                {
                                    fileVehicleObj.Mileage = Convert.ToString(workSheet.Cells[rowIterator, indexMileage].Value);
                                }
                                if (indexColour > 0)
                                {
                                    fileVehicleObj.Colour = Convert.ToString(workSheet.Cells[rowIterator, indexColour].Value);
                                }
                                if (indexYearOfModel > 0)
                                {
                                    fileVehicleObj.YearOfModel = Convert.ToString(workSheet.Cells[rowIterator, indexYearOfModel].Value);
                                }
                                // Please ignore "Istemara Expiry" and "Balance" columns.
                                // fileVehicleObj.IstemaraExp = Convert.ToString(workSheet.Cells[rowIterator, ++columnNumber].Value);
                                if (indexBalance > 0)
                                {
                                    fileVehicleObj.Balance = Convert.ToString(workSheet.Cells[rowIterator, indexBalance].Value);
                                }
                                if (indexBranch > 0)
                                {
                                    fileVehicleObj.Branch = Convert.ToString(workSheet.Cells[rowIterator, indexBranch].Value);
                                }
                                if (indexLocation > 0)
                                {
                                    fileVehicleObj.Location = Convert.ToString(workSheet.Cells[rowIterator, indexLocation].Value);
                                }

                                // If we have Price
                                try
                                {
                                    if (indexPrice > 0)
                                    {
                                        fileVehicleObj.Price = Convert.ToDecimal(workSheet.Cells[rowIterator, indexPrice].Value);
                                    }
                                    else
                                    {
                                        fileVehicleObj.Price = -1;
                                    }
                                }
                                catch (Exception)
                                {
                                    fileVehicleObj.Price = -1;
                                }

                                try
                                {
                                    if (indexStatus > 0)
                                    {
                                        fileVehicleObj.Status = (Convert.ToString(workSheet.Cells[rowIterator, indexStatus].Value)).ToUpper().Trim();
                                    }
                                    else
                                    {
                                        fileVehicleObj.Status = "";
                                    }
                                }
                                catch (Exception)
                                {
                                    fileVehicleObj.Status = "";
                                }

                                if (string.IsNullOrEmpty(fileVehicleObj.VINNumber))
                                {
                                    continue;
                                }

                                // Check if there is any existing vehicle with that VIN
                                var existingVehicleObj = db.VehicleWizards.Where(a => a.VIN == fileVehicleObj.VINNumber && (a.IsDeleted == false || a.IsDeleted == null)).OrderByDescending(a => a.CreatedDate).FirstOrDefault();
                                if (existingVehicleObj == null)
                                {
                                    AddNewVehicle(fileVehicleObj);
                                }
                                else
                                {
                                    try
                                    {
                                        // If the status of existing vehicle in DMS with same VIN # is "INVENTORY" and the status provided in EXCEL file is "SOLD",
                                        // THEN we will change the status of the vehicle in DMS to "SOLD".

                                        // Removing this case because the vehicles with sold status will not available in file
                                        //else if (fileVehicleObj.Status.ToUpper() == "SOLD" && existingVehicleObj.InventoryStatusID == _vehicleStatusInventory)
                                        //{
                                        //    // add status change history
                                        //    new UtilityRepository().AddInventoryStatusHistory(1, existingVehicleObj.VehicleID, existingVehicleObj.InventoryStatusID, _vehicleStatusSold);

                                        //    existingVehicleObj.InventoryStatusID = _vehicleStatusSold;
                                        //    existingVehicleObj.UpdateDate = DateTime.Now;
                                        //    existingVehicleObj.UserID = _userId;
                                        //    db.Entry<VehicleWizard>(existingVehicleObj).State = System.Data.Entity.EntityState.Modified;
                                        //    db.SaveChanges();
                                        //}
                                        //If the status of existing vehicle in DMS with same VIN # is "INVENTORY" and the status provided in EXCEL file is any value other than "SOLD"
                                        //THEN our application should reject
                                        //that record and print out a clear message saying that VIN # 123ABCXYZ already exists in the system with status INVENTORY.
                                        if (fileVehicleObj.Status.ToUpper() != "SOLD" && existingVehicleObj.InventoryStatusID == _vehicleStatusInventory)
                                        {
                                            lstMessages.Add(string.Format("VIN # {0} already exists in the system with status INVENTORY.", existingVehicleObj.VIN));
                                        }
                                        // If the status of existing vehicle in DMS with same VIN # is "SOLD"  and the status provided in EXCEL file is any value other than "SOLD",
                                        // then we will add a new record for the vehicle while keeping the existing AS IS... This means, we will have two vehicles with same VIN number
                                        // in the system now,
                                        // with status of first one as SOLD, while the other one with status as provided in Excel.
                                        else if (fileVehicleObj.Status.ToUpper() != "SOLD" && existingVehicleObj.InventoryStatusID == _vehicleStatusSold)
                                        {
                                            fileVehicleObj.InventoryStatus = GetStatusIdFromString(fileVehicleObj.Status);
                                            AddNewVehicle(fileVehicleObj);
                                        }
                                        // If the status of existing vehicle in DMS with same VIN # is NOT "SOLD" AND NOT "INVENTORY",
                                        // then we will just update the status of the existing vehicle to match the status provided in excel file.
                                        else if (existingVehicleObj.InventoryStatusID != _vehicleStatusInventory && existingVehicleObj.InventoryStatusID != _vehicleStatusSold)
                                        {
                                            if (existingVehicleObj.InventoryStatusID != _vehicleStatusWholeSale)
                                            {
                                                var newStatus = GetStatusIdFromString(fileVehicleObj.Status);
                                                // add status change history
                                                new UtilityRepository().AddInventoryStatusHistory(1, existingVehicleObj.VehicleID, existingVehicleObj.InventoryStatusID, newStatus);
                                                existingVehicleObj.InventoryStatusID = newStatus;
                                            }

                                            existingVehicleObj.UpdateDate = DateTime.Now;
                                            existingVehicleObj.UserID     = _userId;


                                            var makerObj = _makerList.FirstOrDefault(a => a.Name == fileVehicleObj.Make);
                                            if (makerObj != null)
                                            {
                                                existingVehicleObj.MakerID = makerObj.MakerID;
                                            }
                                            else
                                            {
                                                existingVehicleObj.MakerID = AddNewMaker(fileVehicleObj.Make);
                                                _makerList = db.Makers.ToList();
                                            }

                                            var modelObj = _modelList.FirstOrDefault(a => a.ModelName == fileVehicleObj.Model && a.MakerID == existingVehicleObj.MakerID);
                                            if (modelObj != null)
                                            {
                                                existingVehicleObj.AutoModelID = modelObj.AutoModelID;
                                            }
                                            else
                                            {
                                                existingVehicleObj.AutoModelID = AddNewModel(fileVehicleObj.Model, existingVehicleObj.MakerID);
                                                _modelList = db.AutoModels.ToList();
                                            }

                                            var subModelObj = _subModelList.FirstOrDefault(a => a.ModelName == fileVehicleObj.SubModel && a.AutoModelID == existingVehicleObj.AutoModelID);
                                            if (subModelObj != null)
                                            {
                                                existingVehicleObj.SubModelID = subModelObj.SubModelID;
                                            }
                                            else
                                            {
                                                existingVehicleObj.SubModelID = GetSubModelID(fileVehicleObj.SubModel, existingVehicleObj.AutoModelID);
                                                _subModelList = db.SubModels.ToList();
                                            }

                                            db.Entry <VehicleWizard>(existingVehicleObj).State = System.Data.Entity.EntityState.Modified;

                                            db.SaveChanges();
                                        }
                                        else if (string.IsNullOrEmpty(fileVehicleObj.Status))
                                        {
                                            fileVehicleObj.InventoryStatus = -1;
                                            UpdateVehicle(fileVehicleObj, existingVehicleObj);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                }

                                vehicleList.Add(fileVehicleObj);
                            }

                            db.SaveChanges();


                            UpdateSoldStatus(vehicleList, _vehicleStatusSold);
                            //Remove any extra vehicles
                            // This is a temporary call, we need to fix the root cause why vehicles get duplicated
                            RemoveExtraVehicles();
                            ViewBag.DisplayMessages = lstMessages;
                            ViewBag.Message         = true;
                            return(View("Index"));
                        }
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Message = false;
                        return(View("Index"));
                    }
                }
            }

            return(View("Index"));
        }