public void loadDefaultData(string fixAssetId) { ViewBag.typeCode = "Generated"; ViewBag.codeManual = ""; ViewBag.employeeList = db.employees.AsNoTracking().ToList(); var popUp = db.employees.AsNoTracking() .Join(db.employeePositions, x => x.employeeID, y => y.employeeID, (x, y) => new { x, y }) .Join(db.departments, q => q.y.deptID, r => r.deptID, (q, r) => new { q, r }) .Select(c => new { c.q.x.employeeID, c.q.x.employeeNIK, c.q.x.employeeName, c.q.y.deptID, c.r.deptName }).ToList(); var model = new employeeLoan(); for (int i = 0; i < popUp.Count; i++) { var editor = new employeeLoan.employeePOPUp() { employeeID = popUp[i].employeeID, employeeNIK = popUp[i].employeeNIK, employeeName = popUp[i].employeeName, deptID = popUp[i].deptID, deptName = popUp[i].deptName }; model.popUpEmployee.Add(editor); } ViewBag.EmpPopUp = model.popUpEmployee.ToList(); ViewBag.StatusfixedAsset = ccm.ddlStatusFixedAsset(string.Empty); ViewBag.ddlPeriodDepreciation = ccm.ddlPeriodDepreciation(string.Empty); if (fixAssetId != string.Empty) { var id = int.Parse(fixAssetId); var dataDetailSaved = db.fixedAssetPersons.AsNoTracking().Where(x => x.fixedAssetID == id) .Join(db.employees, c => c.employeeID, d => d.employeeID, (c, d) => new { c, d }) .Join(db.employeePositions, f => f.d.employeeID, g => g.employeeID, (f, g) => new { f, g }) .Join(db.departments, e => e.g.deptID, h => h.deptID, (e, h) => new { e, h }) .Select(z => new { z.e.f.c.startDate, z.e.f.c.endDate, z.e.f.c.employeeID, z.e.f.d.employeeName, z.h.deptName, z.e.f.c.remarks }).ToList(); var headerFix = new fixedAsset(); for (int i = 0; i < dataDetailSaved.Count; i++ ) { var editor = new fixedAsset.detailSavedFixedAssetPerson() { startDate = dataDetailSaved[i].startDate, endDate = dataDetailSaved[i].endDate, employeeID = dataDetailSaved[i].employeeID, employeeName = dataDetailSaved[i].employeeName, deptName = dataDetailSaved[i].deptName, remarks = dataDetailSaved[i].remarks }; headerFix.fixAssetPersonDetail.Add(editor); } ViewBag.dataDetail = headerFix.fixAssetPersonDetail.ToList(); ViewBag.dataDetailMaint = db.fixedAssetMaintenances.AsNoTracking().Where(x => x.fixedAssetID == id).ToList(); var headerSaved = db.fixedAssets.AsNoTracking().Where(x => x.fixedAssetID == id).ToList(); var userID = headerSaved[0].createdUser; var s = db.Users.Where(x => x.userID == userID).Select(x => new { x.userName }).ToList(); if (s.Count > 0) ViewBag.UserName = s[0].userName; else { var modifuserID = headerSaved[0].modifiedUser; var mod = db.Users.Where(x => x.userID == modifuserID).Select(x => new { x.userName }).ToList(); if (mod.Count > 0) ViewBag.UserName = mod[0].userName; else ViewBag.UserName = ""; } ViewBag.UserID = userID; ViewBag.StatusfixedAsset = ccm.ddlStatusFixedAsset(headerSaved[0].fixedAssetStatus); ViewBag.ddlPeriodDepreciation = ccm.ddlPeriodDepreciation(headerSaved[0].depreciationPeriod); } }
public ActionResult Edit( fixedAsset fixedasset) { if (acm.cekSession() == false) return RedirectToAction("Logout", "Account"); lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin; if (acm.cekValidation(Url.Action().ToString()) == false && lvm.isAdmin == false) return RedirectToAction("NotAuthorized", "Account", new { menu = Url.Action().ToString() }); loadDefaultData(string.Empty); #region collect code n detail string userID = string.Empty; string typeCreated = string.Empty; int countDetail = 0; string rbTypeCode = string.Empty; DateTime maintDate = DateTime.Now; string maintRemark = string.Empty; int maintAmount = 0; string codeManual = string.Empty; for (int i = 0; i < Request.Form.Count; i++) { if (Request.Form.AllKeys.ToList()[i].Contains("rbTypeCode")) { rbTypeCode = Request.Form["rbTypeCode"].ToString(); ViewBag.typeCode = Request.Form["rbTypeCode"].ToString(); } else if (Request.Form.AllKeys.ToList()[i].Contains("txtCode")) { if (rbTypeCode == "Manual") { fixedasset.fixedAssetCode = Request.Form["txtCode"].ToString(); ViewBag.codeManual = Request.Form["txtCode"].ToString(); } } else if (Request.Form.AllKeys.ToList()[i].Contains("persStart_")) { countDetail++; } else if (Request.Form.AllKeys.ToList()[i].Contains("maintDate_")) { maintDate = Convert.ToDateTime(Request.Form["maintDate_0"].ToString()); } else if (Request.Form.AllKeys.ToList()[i].Contains("maintRemark_")) { maintRemark = Request.Form["maintRemark_0"].ToString(); } else if (Request.Form.AllKeys.ToList()[i].Contains("maintAmount_")) { maintAmount = int.Parse(Request.Form["maintAmount_0"].ToString()); } } #endregion if (ModelState.IsValid) { try { using (TransactionScope ts = new TransactionScope()) { lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin; fixedasset.modifiedUser = lvm.userID; fixedasset.modifiedDate = DateTime.Now; db.Entry(fixedasset).State = EntityState.Modified; db.fixedAssetPersons.RemoveRange(db.fixedAssetPersons.Where(x => x.fixedAssetID == fixedasset.fixedAssetID)); db.fixedAssetMaintenances.RemoveRange(db.fixedAssetMaintenances.Where(x => x.fixedAssetID == fixedasset.fixedAssetID)); #region insertDetailPerson for (int i = 0; i < countDetail; i++) { var colVal = Request.Form["persStart_" + i]; if (colVal == "") break; DateTime t_persStart = Convert.ToDateTime(Request.Form["persStart_" + i].ToString()); DateTime t_persEnd = Convert.ToDateTime(Request.Form["persEnd_" + i].ToString()); int t_persEmpId = int.Parse(Request.Form["persEmpId_" + i].ToString()); string t_persEmpName = Request.Form["persEmpName_" + i].ToString(); string t_persRemark = Request.Form["persRemark_" + i].ToString(); var editorx = new fixedAsset.fixedAssetPerson() { fixedAssetID = fixedasset.fixedAssetID, startDate = t_persStart, endDate = t_persEnd, employeeID = t_persEmpId, remarks = t_persRemark }; db.fixedAssetPersons.Add(editorx); } #endregion fixedAsset.fixedAssetMaintenance assetMaint = new fixedAsset.fixedAssetMaintenance(); assetMaint.fixedAssetID = fixedasset.fixedAssetID; assetMaint.maintenanceDate = maintDate; assetMaint.remarks = maintRemark; assetMaint.amount = maintAmount; db.fixedAssetMaintenances.Add(assetMaint); db.SaveChanges(); ts.Complete(); } return RedirectToAction("Index"); } catch (Exception exc) { string a = exc.Message; } } return View(fixedasset); }