public async Task <IActionResult> CreateWarrantyDetail(WarrantyCardViewModel warrantyCardVm) { var warrantyCard = await _context.WarrantyCard.Include(m => m.Item).ThenInclude(m => m.Model).SingleOrDefaultAsync(m => m.WarrantyCardID == warrantyCardVm.WarrantyDetail.WarrantyCardID); if (warrantyCard == null) { return(NotFound()); } if (ModelState.IsValid) { var returnDeadline = await _context.Constant.SingleAsync(m => m.ConstantID == 1); var anyReturnItem = await _context.ReturnItem.Where(m => m.OldItemID == warrantyCard.ItemID).AnyAsync(); var anyWatingWarrant = await _context.WarrantyDetail.Where(m => ( (m.WarrantyCardID == warrantyCard.WarrantyCardID) && ( m.Status == WarrantyDetailStatus.Fixing || m.Status == WarrantyDetailStatus.Fixed))).AnyAsync(); if (warrantyCard.CanWarrant() && !anyReturnItem && !anyWatingWarrant && warrantyCard.IsPrinted) { var warrantyDetail = new WarrantyDetail(); warrantyDetail.Date = DateTime.Now; warrantyDetail.DefectInfo = warrantyCardVm.WarrantyDetail.DefectInfo; warrantyDetail.ExpectedDate = warrantyCardVm.WarrantyDetail.ExpectedDate; warrantyDetail.ReturnedDate = null; warrantyDetail.Status = WarrantyDetailStatus.Fixing; warrantyDetail.WarrantyCardID = warrantyCardVm.WarrantyDetail.WarrantyCardID; warrantyDetail.WarrantyDate = null; warrantyDetail.IsPrinted = false; warrantyDetail.ApplicationUserID = _userManager.GetUserId(User); warrantyCard.NumberOfWarranty += 1; _context.Update(warrantyCard); _context.Update(warrantyDetail); await _context.SaveChangesAsync(); } else { ViewData["ErrorText"] = "Khong the them chi tiet bao hanh"; return(View("ErrorPage")); } } return(RedirectToAction(nameof(Detail), "WarrantyCards", new { id = warrantyCardVm.WarrantyDetail.WarrantyCardID })); }
// GET: WarrantyCards/Details/5 #region Get Edit // GET: Orders/Edit/5 public async Task <IActionResult> Detail(int?id) { if (id == null) { return(NotFound()); } var warrantyCard = await _context.WarrantyCard.Include(m => m.Item).ThenInclude(m => m.Model).SingleOrDefaultAsync(m => m.WarrantyCardID == id); if (warrantyCard == null) { return(NotFound()); } // NGƯỜI KHÁC CÓ THỂ XEM BẢO HẢNH KHÔNG //var isAuthorized = await _authorizationService.AuthorizeAsync(User, warrantyCard, // OrderOperations.Update); //if (!isAuthorized.Succeeded) //{ // return new ChallengeResult(); //} var warrantyCardVm = new WarrantyCardViewModel(); warrantyCardVm.WarrantyCard = warrantyCard; var returnDeadline = await _context.Constant.SingleAsync(m => m.ConstantID == 1); // Kiểm tra xem sản phẩm này đã bị đổi chưa var isReturnItem = await _context.ReturnItem.Where(m => m.OldItemID == warrantyCard.ItemID).AnyAsync(); // Nếu đã bị đổi thì gửi thông tin đổi qua view if (isReturnItem) { var returnItem = await _context.ReturnItem.Include(m => m.OldItem).Include(m => m.NewItem) .SingleAsync(m => m.OldItemID == warrantyCard.ItemID); var newWarrantyCard = await _context.WarrantyCard.SingleAsync(m => m.ItemID == returnItem.NewItemID); ViewData["WarrantyCardID"] = newWarrantyCard.WarrantyCardID; warrantyCardVm.ReturnItem = returnItem; } // Xem warrantyCarrd này còn có thể đổi trả ko? warrantyCardVm.CanReturn = DateTime.Now <= warrantyCard.StartDate.AddDays(returnDeadline.Parameter) && !isReturnItem; //Return Item, WarrantyDetail dung de nhan Post var warrantyDetails = await _context.WarrantyDetail.Where(m => m.WarrantyCardID == id).OrderByDescending(m => m.Date).ToListAsync(); warrantyCardVm.WarrantyDetails = warrantyDetails; var items = await _context.Item .Where(m => m.ModelID == warrantyCard.Item.ModelID && m.Status == ItemStatus.InStock).ToListAsync(); warrantyCardVm.Items = items; if (isReturnItem) { warrantyCardVm.WarrantyCardStatus = WarrantyCardStatus.Returned; } else { if (await _context.WarrantyDetail.Where(m => ( (m.WarrantyCardID == warrantyCard.WarrantyCardID) && ( m.Status == WarrantyDetailStatus.Fixing || m.Status == WarrantyDetailStatus.Fixed))).AnyAsync()) { warrantyCardVm.WarrantyCardStatus = WarrantyCardStatus.Waiting; } else { if (warrantyCard.CanWarrant()) { warrantyCardVm.WarrantyCardStatus = WarrantyCardStatus.CanWarrant; } else { warrantyCardVm.WarrantyCardStatus = WarrantyCardStatus.Expired; } } } return(View(warrantyCardVm)); }
public async Task <IActionResult> CreateReturnItem(WarrantyCardViewModel warrantyCardVm) { var warrantyCard = await _context.WarrantyCard.Include(m => m.Item).ThenInclude(m => m.Model).SingleOrDefaultAsync(m => m.ItemID == warrantyCardVm.ReturnItem.OldItemID); if (ModelState.IsValid) { var returnDeadline = await _context.Constant.SingleAsync(m => m.ConstantID == 1); var anyReturnItem = await _context.ReturnItem.Where(m => m.OldItemID == warrantyCard.ItemID).AnyAsync(); if (DateTime.Now <= warrantyCard.StartDate.AddDays(returnDeadline.Parameter) && !anyReturnItem && warrantyCard.IsPrinted) { // Tạo thông tin return item var returnItem = new ReturnItem(); returnItem.OldItemID = warrantyCardVm.ReturnItem.OldItemID; returnItem.DefectInfo = warrantyCardVm.ReturnItem.DefectInfo; returnItem.NewItemID = warrantyCardVm.ReturnItem.NewItemID; returnItem.ReturnDate = DateTime.Now; returnItem.ApplicationUserID = _userManager.GetUserId(User); // Tạo warrantycard cho sản phẩm đổi var newWarrantyCard = new WarrantyCard(); newWarrantyCard.NumberOfWarranty = 0; newWarrantyCard.StartDate = DateTime.Now; var itemInfo = await _context.Item.Where(m => m.ItemID == warrantyCardVm.ReturnItem.NewItemID) .Include(m => m.ModelFromSupplier).SingleOrDefaultAsync(); newWarrantyCard.EndDate = DateTime.Now.AddMonths(itemInfo.ModelFromSupplier.Period); newWarrantyCard.IsPrinted = false; newWarrantyCard.IsDisabled = false; newWarrantyCard.ItemID = warrantyCardVm.ReturnItem.NewItemID; newWarrantyCard.ApplicationUserID = _userManager.GetUserId(User); // Disable warrantyCard cũ warrantyCard.IsDisabled = true; // Cap nhat tinh trang san pham cu var oldItem = await _context.Item.SingleAsync(m => m.ItemID == warrantyCardVm.ReturnItem.OldItemID); oldItem.Status = ItemStatus.Returned; // Cap nhat tinh trang san pham moi var newItem = await _context.Item.SingleAsync(m => m.ItemID == warrantyCardVm.ReturnItem.NewItemID); newItem.Status = ItemStatus.Sold; _context.UpdateRange(oldItem, newItem); _context.Add(newWarrantyCard); _context.Update(warrantyCard); _context.Add(returnItem); await _context.SaveChangesAsync(); } else { ViewData["ErrorText"] = "Khong the Doi tra"; return(View("ErrorPage")); } } return(RedirectToAction(nameof(Detail), "WarrantyCards", new { id = warrantyCard.WarrantyCardID })); }