public ActionResult DeleteConfirmed(int id) { CT_PhieuKhamBenh cT_PhieuKhamBenh = db.CT_PhieuKhamBenh.Find(id); db.CT_PhieuKhamBenh.Remove(cT_PhieuKhamBenh); db.SaveChanges(); return(RedirectToAction("Index")); }
public static void UpdateAfterDeleteChiTietPhieu(CT_PhieuKhamBenh ct) { QLPMContext db = new QLPMContext(); Thuoc thuoc = db.Thuocs.Find(ct.ID_Thuoc); //Xu ly //Update so luong ton thuoc.SoLuong += ct.SoLuongThuocLay; //Save db.SaveChanges(); }
public ActionResult Edit([Bind(Include = "ID_PhieuKham,ID_Thuoc,ID_CachDung,SoLuongThuocLay")] CT_PhieuKhamBenh cT_PhieuKhamBenh) { if (ModelState.IsValid) { db.Entry(cT_PhieuKhamBenh).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ID_CachDung = new SelectList(db.CachDungs, "ID_CachDung", "TenCachDung", cT_PhieuKhamBenh.ID_CachDung); ViewBag.ID_PhieuKham = new SelectList(db.PhieuKhamBenhs, "ID_PhieuKham", "TrieuChung", cT_PhieuKhamBenh.ID_PhieuKham); ViewBag.ID_Thuoc = new SelectList(db.Thuocs, "ID_Thuoc", "TenThuoc", cT_PhieuKhamBenh.ID_Thuoc); return(View(cT_PhieuKhamBenh)); }
// GET: CT_PhieuKhamBenh/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } CT_PhieuKhamBenh cT_PhieuKhamBenh = db.CT_PhieuKhamBenh.Find(id); if (cT_PhieuKhamBenh == null) { return(HttpNotFound()); } return(View(cT_PhieuKhamBenh)); }
// GET: CT_PhieuKhamBenh/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } CT_PhieuKhamBenh cT_PhieuKhamBenh = db.CT_PhieuKhamBenh.Find(id); if (cT_PhieuKhamBenh == null) { return(HttpNotFound()); } ViewBag.ID_CachDung = new SelectList(db.CachDungs, "ID_CachDung", "TenCachDung", cT_PhieuKhamBenh.ID_CachDung); ViewBag.ID_PhieuKham = new SelectList(db.PhieuKhamBenhs, "ID_PhieuKham", "TrieuChung", cT_PhieuKhamBenh.ID_PhieuKham); ViewBag.ID_Thuoc = new SelectList(db.Thuocs, "ID_Thuoc", "TenThuoc", cT_PhieuKhamBenh.ID_Thuoc); return(View(cT_PhieuKhamBenh)); }
public ActionResult Save(int?IDPhieu, int idPhieuTN, DateTime date, int IDBenh, string TrieuChung, CT_PhieuKhamBenh[] chitietphieukham) { string result = "Error! Thêm chi tiết không thể hoàn tất!"; if (IDPhieu == null && idPhieuTN != 0 && date != null && chitietphieukham != null && IDBenh != 0) { PhieuKhamBenh phieuKham = new PhieuKhamBenh(); int tienThuoc = 0; phieuKham.IDPhieuTN = idPhieuTN; phieuKham.ID_Benh = IDBenh; phieuKham.NgayKham = date; phieuKham.TrieuChung = TrieuChung; db.PhieuKhamBenhs.Add(phieuKham); db.SaveChanges(); int idPhieuKham = phieuKham.ID_PhieuKham; HoaDon hoaDon = new HoaDon(); hoaDon.ID_PhieuKham = idPhieuKham; int tienKham = Convert.ToInt32(db.ThamSoes.Where(x => x.GhiChu.Contains("Tien kham")).SingleOrDefault().GiaTri);; hoaDon.TienKham = tienKham; foreach (var ct in chitietphieukham) { CT_PhieuKhamBenh C = new CT_PhieuKhamBenh(); C.ID_PhieuKham = idPhieuKham; C.ID_Thuoc = ct.ID_Thuoc; C.DonGiaBan = ct.DonGiaBan; C.SoLuongThuocLay = ct.SoLuongThuocLay; C.ThanhTien = ct.ThanhTien; db.CT_PhieuKhamBenh.Add(C); tienThuoc += ct.ThanhTien; } hoaDon.TienThuoc = tienThuoc; hoaDon.TongTien = tienKham + tienThuoc; db.HoaDons.Add(hoaDon); db.SaveChanges(); Session["ID_PhieuKham"] = idPhieuKham; result = "Thành công! Thêm chi tiết hoàn tất!"; } else { try { int tienThuoc = 0; //Trường hợp đã remove 1 hay nhiều ChiTietKhamBenh nào đó ... CT_PhieuKhamBenh[] C = db.CT_PhieuKhamBenh.Where(x => x.ID_PhieuKham == IDPhieu && x.Deleted == false).ToList().ToArray(); List <int> listC = Helper.GhiIDThuocThanhMang(C); List <int> listChiTiet = Helper.GhiIDThuocThanhMang(chitietphieukham); //Tìm phần tử có trong C mà ko có trong chitietphieusua var excepts = listC.Except(listChiTiet).ToArray(); if (excepts.Count() != 0) { foreach (var e in excepts) { CT_PhieuKhamBenh chiTietPhieuKham = db.CT_PhieuKhamBenh.Find(IDPhieu, e); chiTietPhieuKham.Deleted = true; //Update lại thành Số lượng tồn Helper.UpdateAfterDeleteChiTietPhieu(chiTietPhieuKham); //Flag db.Entry(chiTietPhieuKham).State = EntityState.Modified; } db.SaveChanges(); } foreach (CT_PhieuKhamBenh chiTiet in chitietphieukham) { int idThuoc = chiTiet.ID_Thuoc; //Nếu đã có ChiTietPhieu này thi chỉ thực hiện edit if (db.CT_PhieuKhamBenh.Any(x => x.ID_PhieuKham == IDPhieu && x.ID_Thuoc == idThuoc)) { CT_PhieuKhamBenh ct = db.CT_PhieuKhamBenh.Find(IDPhieu, idThuoc); if (ct.Deleted == false) //Chi Edit vs ChiTiet chua xoa { ct.DonGiaBan = chiTiet.DonGiaBan; ct.SoLuongThuocLay = chiTiet.SoLuongThuocLay; ct.ThanhTien = chiTiet.ThanhTien; db.Entry(ct).State = EntityState.Modified; } else //Neu xoa roi (Deleted = true) thi xoa hẳn giá trị đó, va them lai => To fire trigger, update Soluongton, ... { //Xóa db.CT_PhieuKhamBenh.Remove(ct); db.SaveChanges(); //Gán & Add lại ct.DonGiaBan = chiTiet.DonGiaBan; ct.SoLuongThuocLay = chiTiet.SoLuongThuocLay; ct.ThanhTien = chiTiet.ThanhTien; ct.Deleted = false; db.CT_PhieuKhamBenh.Add(ct); db.SaveChanges(); } } else //Nếu không có thì thêm mới { CT_PhieuKhamBenh ChiTiet = new CT_PhieuKhamBenh(); ChiTiet.ID_PhieuKham = (int)IDPhieu; ChiTiet.ID_Thuoc = chiTiet.ID_Thuoc; ChiTiet.DonGiaBan = chiTiet.DonGiaBan; ChiTiet.SoLuongThuocLay = chiTiet.SoLuongThuocLay; ChiTiet.ThanhTien = chiTiet.ThanhTien; db.CT_PhieuKhamBenh.Add(ChiTiet); } db.SaveChanges(); } HoaDon phieuThuTien = db.HoaDons.Where(x => x.ID_PhieuKham == IDPhieu).SingleOrDefault(); var chiTietKham = db.CT_PhieuKhamBenh.Where(x => x.ID_PhieuKham == IDPhieu && x.Deleted == false); foreach (var ele in chiTietKham) { tienThuoc += ele.ThanhTien; } phieuThuTien.TienThuoc = tienThuoc; phieuThuTien.TongTien = tienThuoc + phieuThuTien.TienKham; db.SaveChanges(); Session["ID_PhieuKham"] = IDPhieu; result = "Sửa thành công!!"; } catch (Exception ex) { ViewBag.Errors = ex.Message; throw ex; } } return(Json(result, JsonRequestBehavior.AllowGet)); }