/// <summary> /// Actualiza las categorias de los usuarios activos/suspendidos. /// </summary> /// <returns></returns> public virtual async Task <ActionResult> UpdateCategories() { int update = 0; var lCategories = await this.repository.Categories.AsNoTracking().ToListAsync(); if (lCategories.Count == 0) { return(Json(new { success = true, msg = Validation.ThereAreNoCategories })); } var lUsers = await this.repository.AspNetUsers.AsNoTracking() .Where(u => u.UserNumber.HasValue && u.Birthdate.HasValue && u.CategoryId.HasValue && u.State != AspNetUserState.Disabled).ToListAsync(); foreach (var item in lUsers) { Int32 age = SiteMethods.GetAge(item.Birthdate, DateTime.Now); Category category = lCategories.Where(x => x.YearFrom <= age && x.YearTo >= age).FirstOrDefault(); if (category?.Price > 0) { AspNetUser user = await this.repository.AspNetUsers.Where(u => u.Id == item.Id).FirstOrDefaultAsync(); if (user.CategoryId != category.Id) { user.CategoryId = category.Id; repository.Entry(user).State = EntityState.Modified; await this.repository.SaveChangesAsync(); update++; } } } return(Json(new { success = true, msg = String.Format(Validation.TheyWereUpdated, update, lUsers.Count()) })); }
/// <summary> /// Obtiene las Categorias de una Persona (la idea es que siempre retorne una categoria) /// </summary> /// <param name="birthdate"></param> /// <returns></returns> public virtual JsonResult GetCategories(string id) { DateTime birthdate; List <Category> lCategory = new List <Category>(); if (DateTime.TryParse(id, out birthdate)) { Int32 age = SiteMethods.GetAge(birthdate, DateTime.Now); lCategory = repository.Categories.AsNoTracking() .Where(x => x.YearFrom <= age && x.YearTo >= age).ToList(); } return(Json(new SelectList(lCategory, "Id", "Name"))); }
public virtual async Task <ActionResult> Edit(AspNetUser aspNetUser, HttpPostedFileBase image) { if (ModelState.IsValid) { #region Photo /* if you save out as JPEG you will have only RGB: width * height * 3 = size in bytes ==> 97200 * 155000 lo puse por poner una medida no significa nada */ if (image != null && image.ContentLength > 0 && image.ContentLength < 155000) { var content = new byte[image.ContentLength]; image.InputStream.Read(content, 0, image.ContentLength); int indexOfLastDot = image.FileName.LastIndexOf('.'); string extension = image.FileName.Substring(indexOfLastDot + 1, image.FileName.Length - indexOfLastDot - 1); if (extension == "jpg" || extension == "jpeg") { #region New Photo Random r = new Random(); string newPhoto = String.Format("{0}_{1}", aspNetUser.UserName, r.Next(0, 99)); string newPath = System.IO.Path.Combine(aspNetUser.FullPath, String.Format("{0}.{1}", newPhoto, extension)); #endregion #region Delete Old Photo if (!String.IsNullOrEmpty(aspNetUser.Photo)) { string oldPhoto = System.IO.Path.Combine(aspNetUser.FullPath, aspNetUser.Photo); if (System.IO.File.Exists(oldPhoto)) { System.IO.File.Delete(oldPhoto); } } #endregion aspNetUser.Photo = String.Format("{0}.{1}", newPhoto, extension); image.SaveAs(newPath); } } #endregion if (Validate(aspNetUser)) { int countFees = 0; #region Socio Disable //Si no se encuentra Habilitado, genera cuotas con monto 0, serían las cuotas que le falto pagar var isDisabled = await repository.AspNetUsers.AsNoTracking().Where(d => d.Id == aspNetUser.Id && d.State == AspNetUserState.Disabled).AnyAsync(); if (!isDisabled && aspNetUser.State == AspNetUserState.Disabled && aspNetUser.UserNumber != null) { DateTime currentPeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); DateTime lastPeriod = SiteMethods.GetFirstDayOfMonth(Convert.ToDateTime(aspNetUser.Modified)); if (lastPeriod != currentPeriod) { while (lastPeriod <= currentPeriod) { await repository.SaveChangesAsync(); lastPeriod = lastPeriod.AddMonths(1); countFees++; } } } #endregion if (!aspNetUser.UserNumber.HasValue) { aspNetUser.Birthdate = null; aspNetUser.CategoryId = null; } aspNetUser.Modified = DateTime.Now; aspNetUser.ModifiedUser = User.Identity.Name; repository.Entry(aspNetUser).State = EntityState.Modified; await repository.SaveChangesAsync(); return(RedirectToAction(MVC.AspNetUsers.Index(aspNetUser.Id, countFees))); } } ViewBag.CategoryId = new SelectList(repository.Categories, "Id", "Name", aspNetUser.CategoryId); ViewBag.LocalityId = new SelectList(repository.Localities, "Id", "Name", aspNetUser.LocalityId); ViewBag.ProvinceId = new SelectList(repository.Provinces, "Id", "Name", aspNetUser.ProvinceId); return(View(aspNetUser)); }