// GET: CtphieuNhap/Create public async Task <IActionResult> Create(int phieuNhapId) { CtphieuNhap chitietphieunhap = new CtphieuNhap(); chitietphieunhap.PhieuNhap = phieuNhapId; var list_sach = await _apiService.GetAsync("api/sach").Result.Content.ReadAsAsync <List <Sach> >(); Sach sach = list_sach.Where(x => x.TrangThai == true).FirstOrDefault(); TempData["sltmacdinh"] = Convert.ToInt32(sach.SoLuong); ViewData["Book"] = new SelectList(list_sach, "Id", "TenSach"); return(View(chitietphieunhap)); }
public async Task <IActionResult> Create([Bind("Id,PhieuNhap,Book,SoLuong,TinhTrangSach")] CtphieuNhap ctphieuNhap) { var list_sach = await _apiService.GetAsync("api/sach").Result.Content.ReadAsAsync <List <Sach> >(); if (ModelState.IsValid) { ctphieuNhap.TinhTrangSach = "Tốt"; try { _context.CtphieuNhap.Add(ctphieuNhap); await _context.SaveChangesAsync(); Sach up_sltsp = list_sach.SingleOrDefault(m => m.Id == ctphieuNhap.Book); up_sltsp.SoLuong += ctphieuNhap.SoLuong; HttpResponseMessage respond = await _apiService.PutAsJsonAsync($"api/sach/{up_sltsp.Id}", up_sltsp); respond.EnsureSuccessStatusCode(); PhieuNhap uppn_soluong = _context.PhieuNhap.SingleOrDefault(m => m.Id == ctphieuNhap.PhieuNhap); uppn_soluong.SoLuong += ctphieuNhap.SoLuong; _context.Entry(uppn_soluong).State = EntityState.Modified; _context.SaveChanges(); TempData["notice"] = "Successfully create"; TempData["masp"] = ctphieuNhap.Book; Sach giveten = _context.Sach.Where(s => s.Id == ctphieuNhap.Book).FirstOrDefault(); TempData["tensp"] = giveten.TenSach; ViewBag.MaSP = new SelectList(list_sach.Where(s => s.TrangThai == true), "Id", "TenSach"); return(RedirectToAction("Index", new { phieuNhapId = ctphieuNhap.PhieuNhap })); } catch (Exception) { TempData["masperror"] = ctphieuNhap.Book; TempData["trungmasp"] = "trungmasp"; ViewBag.MaSP = new SelectList(list_sach.Where(s => s.TrangThai == true), "Id", "TenSach"); return(View(ctphieuNhap)); } } ViewData["Book"] = new SelectList(list_sach.Where(s => s.TrangThai == true), "Id", "TenSach"); return(View(ctphieuNhap)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,PhieuNhap,Book,SoLuong,TinhTrangSach")] CtphieuNhap ctphieuNhap) { var list_sach = await _apiService.GetAsync("api/sach").Result.Content.ReadAsAsync <List <Sach> >(); int slpn_cu = (_context.CtphieuNhap.Find(id)).SoLuong; ViewBag.SoLuongSPCu = slpn_cu; if (id != ctphieuNhap.Id) { return(NotFound()); } if (ModelState.IsValid) { var entity = _context.CtphieuNhap.Where(c => c.Id == ctphieuNhap.Id).AsQueryable().FirstOrDefault(); _context.Entry(entity).CurrentValues.SetValues(ctphieuNhap); _context.SaveChanges(); @TempData["notice"] = "Successfully edit"; @TempData["ctpn"] = ctphieuNhap.PhieuNhap; @TempData["masp"] = ctphieuNhap.Book; Sach sach = list_sach.SingleOrDefault(m => m.Id == ctphieuNhap.Book); sach.SoLuong = sach.SoLuong + (ctphieuNhap.SoLuong - slpn_cu); HttpResponseMessage respond = await _apiService.PutAsJsonAsync($"api/sach/{sach.Id}", sach); respond.EnsureSuccessStatusCode(); PhieuNhap pn = _context.PhieuNhap.SingleOrDefault(m => m.Id == ctphieuNhap.PhieuNhap); pn.SoLuong = pn.SoLuong + (ctphieuNhap.SoLuong - slpn_cu); _context.Entry(pn).State = EntityState.Modified; _context.SaveChanges(); return(RedirectToAction("Index", new { phieuNhapId = ctphieuNhap.PhieuNhap })); } ViewData["Book"] = new SelectList(list_sach, "Id", "TenSach", ctphieuNhap.Book); return(View(ctphieuNhap)); }