public LevelTransferViewModel GetByID(int? id) { LevelTransferViewModel model = new LevelTransferViewModel(); try { using (SATEntities db = new SATEntities()) { if (id != null) { var data = db.tb_Move_Level_Head.Where(x => x.MlID == id).FirstOrDefault(); LevelTransferViewModel head = new LevelTransferViewModel(); model.MlID = data.MlID; model.MlYear = data.MlYear; model.MlBookCmd = data.MlBookCmd; model.MlDateCmd = data.MlDateCmd; model.MlDateCmdText = data.MlDateCmd.HasValue ? data.MlDateCmd.Value.ToString("dd/MM/yyyy") : string.Empty; model.MlSignatory = data.MlSignatory; model.MIPathFile = data.MIPathFile; model.MlStatus = data.MlStatus; var detail = GetDetail(id); model.ListDetail = detail; } } } catch (Exception) { throw; } return model; }
public JsonResult SaveLevelTransfer(LevelTransferViewModel data) { ResponseData result = new Models.ResponseData(); if (data.MlID != 0) { result = new LevelTransferRepository().UpdateByEntity(data); } else { result = new LevelTransferRepository().AddByEntity(data); } return(Json(result, JsonRequestBehavior.AllowGet)); }
public ResponseData UpdateByEntity(LevelTransferViewModel newdata) { using (SATEntities db = new SATEntities()) { using (var transection = db.Database.BeginTransaction()) { ResponseData result = new Models.ResponseData(); try { if (newdata.fileUpload != null) { HttpPostedFileBase fileUpload = newdata.fileUpload; if (fileUpload != null && fileUpload.ContentLength > 0) { var fileName = Path.GetFileName(fileUpload.FileName); var fileExt = System.IO.Path.GetExtension(fileUpload.FileName).Substring(1); string directory = SysConfig.PathUploadLevelTransfer; bool isExists = System.IO.Directory.Exists(directory); if (!isExists) System.IO.Directory.CreateDirectory(directory); string newFileName = "คส.ที่ " + newdata.MlBookCmd + " เรื่องการแต่งตั้งเลื่อนระดับพนักงาน" + "." + fileExt; string fileLocation = Path.Combine(directory, newFileName); fileUpload.SaveAs(fileLocation); newdata.MIPathFile = newFileName; } } var head = db.tb_Move_Level_Head.Single(x => x.MlID == newdata.MlID); head.MlID = newdata.MlID; head.MlYear = newdata.MlYear; head.MlBookCmd = newdata.MlBookCmd; if (!string.IsNullOrEmpty(newdata.MlDateCmdText)) head.MlDateCmd = Convert.ToDateTime(newdata.MlDateCmdText); head.MlSignatory = newdata.MlSignatory; head.MIPathFile = newdata.MIPathFile; head.MlStatus = newdata.MlStatus; head.ModifyBy = UtilityService.User.UserID; head.ModifyDate = DateTime.Now; db.SaveChanges(); var listdelete = db.tb_Move_Level_Detail.Where(x => x.MlID == newdata.MlID).ToList(); db.tb_Move_Level_Detail.RemoveRange(listdelete); db.SaveChanges(); foreach (var item in newdata.ListDetail) { tb_Move_Level_Detail detail = new tb_Move_Level_Detail(); detail.MlID = head.MlID; detail.UserID = item.UserID; detail.MlLevelOld = item.MlLevelOld; detail.MlStepOld = item.MlStepOld; detail.MlLevelNew = item.MlLevelNew; detail.MlStepNew = item.MlStepNew; detail.CreateBy = UtilityService.User.UserID; detail.CreateDate = DateTime.Now; detail.ModifyBy = UtilityService.User.UserID; detail.ModifyDate = DateTime.Now; db.tb_Move_Level_Detail.Add(detail); db.SaveChanges(); } transection.Commit(); } catch (Exception ex) { transection.Rollback(); result.MessageCode = ""; result.MessageText = ex.Message; } return result; } } }
public LevelTransferResult GetPage(string filter, int? draw, int? initialPage, int? pageSize, string sortDir, string sortBy) { LevelTransferResult result = new LevelTransferResult(); List<LevelTransferViewModel> list = new List<LevelTransferViewModel>(); try { using (SATEntities db = new SATEntities()) { var data = db.vw_Move_Level_Head.ToList(); int recordsTotal = data.Count(); if (!string.IsNullOrEmpty(filter)) { data = data.Where(x => x.MlBookCmd.Contains(filter)).ToList(); } int recordsFiltered = data.Count(); switch (sortBy) { case "MlYear": data = (sortDir == "asc") ? data.OrderBy(x => x.MlYear).ToList() : data.OrderByDescending(x => x.MlYear).ToList(); break; case "MlBookCmd": data = (sortDir == "asc") ? data.OrderBy(x => x.MlBookCmd).ToList() : data.OrderByDescending(x => x.MlBookCmd).ToList(); break; case "MlDateCmdText": data = (sortDir == "asc") ? data.OrderBy(x => x.MlDateCmd).ToList() : data.OrderByDescending(x => x.MlDateCmd).ToList(); break; } int start = initialPage.HasValue ? (int)initialPage / (int)pageSize : 0; int length = pageSize ?? 10; int i = 1; foreach (var item in data) { LevelTransferViewModel model = new LevelTransferViewModel(); model.RowNumber = ++i; model.MlID = item.MlID; model.MlYear = item.MlYear; model.MlBookCmd = item.MlBookCmd; model.MlTotal = item.MlTotal; model.MlDateCmd = item.MlDateCmd; model.MlDateCmdText = (item.MlDateCmd.HasValue) ? item.MlDateCmd.Value.ToString("dd/MM/yyyy") : string.Empty; model.CreateDateText = item.CreateDate.Value.ToString("dd/MM/yyy"); model.MlStatusName = (item.MlStatus.HasValue) ? "ยืนยันแล้ว" : ""; model.recordsTotal = recordsTotal; model.recordsFiltered = recordsFiltered; list.Add(model); } } } catch (Exception) { throw; } result.draw = draw ?? 0; result.recordsTotal = list.Count() > 0 ? list[0].recordsTotal : 0; result.recordsFiltered = list.Count() > 0 ? list[0].recordsFiltered : 0; result.data = list; return result; }