private bool CheckExists(PhuTungModel PhuTung, HMSEntities db) { H_PhuTung obj = null; if (!string.IsNullOrEmpty(PhuTung.Ma)) { obj = db.H_PhuTung.FirstOrDefault(x => !x.IsDeleted && x.Id != PhuTung.Id && x.Code.Trim().ToUpper().Equals(PhuTung.Ma.Trim().ToUpper())); } return(obj != null ? true : false); }
public ResponseModel InsertOrUpdate(string connectString, PhuTungModel model) { var result = new ResponseModel(); result.IsSuccess = true; using (var db = new HMSEntities(connectString)) { if (!CheckExists(model, db)) { H_PhuTung phutung = null; if (model.Id == 0) { phutung = new H_PhuTung() { Code = model.Ma, Name = model.Ten, Quantities = model.SoLuong, Price_Out = model.GiaBan, Price_In = model.GiaMua, Note = model.Note }; db.H_PhuTung.Add(phutung); } else { var found = db.H_PhuTung.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id); if (found != null) { found.Code = model.Ma; found.Name = model.Ten; found.Quantities = model.SoLuong; found.Price_In = model.GiaMua; found.Price_Out = model.GiaBan; found.Note = model.Note; } else { result.IsSuccess = false; result.sms = "Phụ tùnng xe đã bị xóa hoặc không tồn tại trong hệ thống.!"; } } if (result.IsSuccess) { db.SaveChanges(); } } else { result.IsSuccess = false; result.sms = "Mã này đã tồn tại trong hệ thống. Vui lòng chọn lại mã khác."; } return(result); } }
public int InsertFromExcel(Stream fileStream, string path, string connectionString) { try { using (var excel = new ExcelPackage(fileStream)) { var ws = excel.Workbook.Worksheets.First(); using (var db = new HMSEntities(connectionString)) { var dsPTs = (from x in db.H_PhuTung where !x.IsDeleted select new { Id = x.Id, Code = x.Code, Name = x.Name, sl = x.Quantities }).ToList(); H_PhuTung phuTung; H_NhapPT nhapPT; string malo = "", code = "", name = "", soluong = "", giamua = "", giaban = "", query = ""; double _giaban = 0, _giamua = 0; int _soluong = 0; var now = DateTime.Now; for (int ii = 2; ii <= ws.Dimension.End.Row; ii++) { _soluong = 0; _giaban = 0; _giamua = 0; malo = ws.Cells[ii, 1].Text.ToString(); code = ws.Cells[ii, 2].Text.ToString(); name = ws.Cells[ii, 3].Text.ToString(); soluong = ws.Cells[ii, 4].Text.ToString(); giamua = ws.Cells[ii, 5].Text.ToString(); giaban = ws.Cells[ii, 6].Text.ToString(); int.TryParse(soluong, out _soluong); double.TryParse(giamua, out _giamua); double.TryParse(giaban, out _giaban); var found = dsPTs.FirstOrDefault(x => x.Code.Trim().ToUpper() == code.Trim().ToUpper()); if (found == null) { found = dsPTs.FirstOrDefault(x => x.Name.Trim().ToUpper() == name.Trim().ToUpper()); if (found == null) { phuTung = new H_PhuTung() { Code = code, Name = name, Quantities = _soluong, Price_In = _giamua, Price_Out = _giaban }; nhapPT = new H_NhapPT() { Code = code, Name = name, Price_In = _giamua, Price_Out = _giaban, MaLo = malo, H_PhuTung = phuTung, CreatedDate = now, Quantities = _soluong }; phuTung.H_NhapPT = new List <H_NhapPT>(); phuTung.H_NhapPT.Add(nhapPT); db.H_PhuTung.Add(phuTung); } else { nhapPT = new H_NhapPT() { Code = code, Name = name, Price_In = _giamua, Price_Out = _giaban, MaLo = malo, PTId = found.Id, CreatedDate = now, Quantities = _soluong }; db.H_NhapPT.Add(nhapPT); query += " UPDATE [dbo].[H_PhuTung] set [Quantities] =" + (_soluong + found.sl) + " where id=" + found.Id; } } else { nhapPT = new H_NhapPT() { Code = code, Name = name, Price_In = _giamua, Price_Out = _giaban, MaLo = malo, PTId = found.Id, CreatedDate = now, Quantities = _soluong }; db.H_NhapPT.Add(nhapPT); query += " UPDATE [dbo].[H_PhuTung] set [Quantities] =" + (_soluong + found.sl) + " where id=" + found.Id; } } if (!string.IsNullOrEmpty(query)) { db.Database.ExecuteSqlCommand(query); } db.SaveChanges(); return(1); } } } catch (Exception ex) { return(0); } }