/// <summary> /// Merupakan fungsi yang digunakan untuk mengubah/memperbaharui data dari sebuah MPS /// </summary> /// <param name="newDataMPS">sebuah object bertipe MasterPlanSchedule yang menyimpan informasi data MPS yang baru</param> /// <param name="currenDataMPS">sebuah object bertipe MasterPlanSchedule yang menyimpan informasi data MPS yang saat ini di database</param> /// <param name="username">sebuah string yang berisikan nama username dari user yang sedang login</param> /// <param name="now">sebuh data bertipe datetime yang bersisikan tanggal sekarang</param> /// <returns>sebuah object MasterPlanSchedule yang berisikan data MPS yang terbaru</returns> public MasterPlanSchedule UpdateMPSData(MasterPlanSchedule newDataMPS, MasterPlanSchedule currenDataMPS, string username, DateTime now) { currenDataMPS.PlannedQuantity = newDataMPS.PlannedQuantity; currenDataMPS.EndWorkingDate = newDataMPS.EndWorkingDate; currenDataMPS.LastModified = now; currenDataMPS.LastModifiedBy = username; return(currenDataMPS); }
/// <summary> /// Merupakan fungsi yang digunakan untuk menambahkan data MPS ke table MasterPlanSchedule /// </summary> /// <param name="dataMPS">Sebuah object MasterPlanSchedule yang berisikan data-data MasterPlanSchedule yang akan ditambahkan</param> /// <param name="username">sebuah string yang berisikan nama username dari user yang sedang login</param> /// <param name="now">sebuh data bertipe datetime yang bersisikan tanggal sekarang</param> public void AddMPSData(MasterPlanSchedule dataMPS, string username, DateTime now) { MasterPlanSchedule newMPSData = new MasterPlanSchedule(); newMPSData.PROID = dataMPS.PROID; newMPSData.Week = dataMPS.Week; newMPSData.Month = dataMPS.Month; newMPSData.Year = dataMPS.Year; newMPSData.PlannedQuantity = dataMPS.PlannedQuantity; newMPSData.EndWorkingDate = dataMPS.EndWorkingDate; newMPSData.CreatedBy = newMPSData.LastModifiedBy = username; newMPSData.Created = newMPSData.LastModified = now; db.MasterPlanSchedules.Add(newMPSData); }
/// <summary> /// Merupakan fungsi yang digunakan untuk menghapus data dari sebuah MPS /// </summary> /// <param name="currenDataMPS">sebuah object bertipe MasterPlanSchedule yang menyimpan informasi data MPS yang akan dihapus</param> /// <param name="username">sebuah string yang berisikan nama username dari user yang sedang login</param> /// <param name="now">sebuh data bertipe datetime yang bersisikan tanggal sekarang</param> public void DeleteMPSData(MasterPlanSchedule currenDataMPS, string username, DateTime now) { List <Process> processWillbeDeleted = new List <Process>(); foreach (Unit unitItem in currenDataMPS.Units) { unitItem.MPSID = null; unitItem.MPSDueDate = null; unitItem.SFSDueDate = null; unitItem.LastModified = now; unitItem.LastModifiedBy = username; processWillbeDeleted.AddRange(unitItem.Processes); } db.Processes.RemoveRange(processWillbeDeleted); db.MasterPlanSchedules.Remove(currenDataMPS); }
public ActionResult UpdateMPSValue(List <MasterPlanSchedule> masterPlanScheduleList) { try { var username = User.Identity.Name; DateTime now = DateTime.Now; db = new PowerAppsCMSEntities(); if (masterPlanScheduleList != null) { int proid = 0; MasterPlanSchedule firstMPSData = masterPlanScheduleList.FirstOrDefault(); MasterPlanSchedule lastMPSData = masterPlanScheduleList.LastOrDefault(); foreach (MasterPlanSchedule item in masterPlanScheduleList) { proid = item.PROID; MasterPlanSchedule currenDataMPS = new MasterPlanSchedule(); if (item.ID != 0)//if mps ID has value (not equal zero), find the mps data base on ID in Database { currenDataMPS = db.MasterPlanSchedules.Find(item.ID); } if (item.ID == 0 || currenDataMPS == null) //if mps ID has zero value or currentMPSData is Null, find the mps data base on proID, week, month and year data in Database { currenDataMPS = db.MasterPlanSchedules.Where(x => x.PROID == item.PROID && x.Week == item.Week && x.Month == item.Month && x.Year == item.Year).OrderByDescending(x => x.Created).FirstOrDefault(); } if (currenDataMPS == null) { if (item.PlannedQuantity > 0) //if currentDataMPS is not exist but newData have planQuantity, so we add new mps data in database { //AddMPSData(item, username, now); MasterPlanSchedule newMPSData = new MasterPlanSchedule(); newMPSData.PROID = item.PROID; newMPSData.Week = item.Week; newMPSData.Month = item.Month; newMPSData.Year = item.Year; newMPSData.PlannedQuantity = item.PlannedQuantity; newMPSData.EndWorkingDate = item.EndWorkingDate; newMPSData.CreatedBy = newMPSData.LastModifiedBy = username; newMPSData.Created = newMPSData.LastModified = now; db.MasterPlanSchedules.Add(newMPSData); } } else { if (item.PlannedQuantity > 0) //if currentDataMPS is exist and newData have planQuantity, so we update the mps data in database { //currenDataMPS = UpdateMPSData(item, currenDataMPS,username,now); currenDataMPS.PlannedQuantity = item.PlannedQuantity; currenDataMPS.EndWorkingDate = item.EndWorkingDate; currenDataMPS.LastModified = now; currenDataMPS.LastModifiedBy = username; } if (item.PlannedQuantity == 0) //if currentDataMPS is exist but new plannedQuantity is zero, so we delete the mps data in database { //DeleteMPSData(currenDataMPS, username, now); List <Process> processWillbeDeleted = new List <Process>(); foreach (Unit unitItem in currenDataMPS.Units) { unitItem.MPSID = null; unitItem.MPSDueDate = null; unitItem.SFSDueDate = null; unitItem.LastModified = now; unitItem.LastModifiedBy = username; processWillbeDeleted.AddRange(unitItem.Processes); } db.Processes.RemoveRange(processWillbeDeleted); db.MasterPlanSchedules.Remove(currenDataMPS); } } #region Old Code //if (item.ID == 0 && item.PlannedQuantity > 0) //{ // MasterPlanSchedule newMPSData = new MasterPlanSchedule(); // newMPSData = db.MasterPlanSchedules.Where(x => x.PROID == item.PROID && x.Week == item.Week && x.Month == item.Month && x.Year == item.Year).SingleOrDefault(); // if (newMPSData == null) // { // newMPSData = new MasterPlanSchedule(); // newMPSData.PROID = item.PROID; // newMPSData.Week = item.Week; // newMPSData.Month = item.Month; // newMPSData.Year = item.Year; // newMPSData.PlannedQuantity = item.PlannedQuantity; // newMPSData.EndWorkingDate = item.EndWorkingDate; // newMPSData.CreatedBy = newMPSData.LastModifiedBy = username; // newMPSData.Created = newMPSData.LastModified = now; // db.MasterPlanSchedules.Add(newMPSData); // } // else // { // newMPSData.PlannedQuantity = item.PlannedQuantity; // newMPSData.EndWorkingDate = item.EndWorkingDate; // newMPSData.LastModified = now; // newMPSData.LastModifiedBy = username; // } //} //else if (item.ID == 0 && item.PlannedQuantity == 0) //{ // MasterPlanSchedule newMPSData = new MasterPlanSchedule(); // newMPSData = db.MasterPlanSchedules.Where(x => x.PROID == item.PROID && x.Week == item.Week && x.Month == item.Month && x.Year == item.Year).SingleOrDefault(); // if (newMPSData != null) // { // List<Process> processWillbeDeleted = new List<Process>(); // foreach (Unit unitItem in newMPSData.Units) // { // unitItem.MPSID = null; // unitItem.MPSDueDate = null; // unitItem.SFSDueDate = null; // unitItem.LastModified = now; // unitItem.LastModifiedBy = username; // processWillbeDeleted.AddRange(unitItem.Processes); // } // db.Processes.RemoveRange(processWillbeDeleted); // db.MasterPlanSchedules.Remove(newMPSData); // } //} //else //{ // MasterPlanSchedule currentMPSData = db.MasterPlanSchedules.Find(item.ID); // if (currentMPSData == null) // { // currentMPSData = db.MasterPlanSchedules.Where(x => x.PROID == item.PROID && x.Week == item.Week && x.Month == item.Month && x.Year == item.Year).SingleOrDefault(); // } // if (currentMPSData != null) // { // if (item.PlannedQuantity > 0) // { // currentMPSData.PlannedQuantity = item.PlannedQuantity; // currentMPSData.EndWorkingDate = item.EndWorkingDate; // currentMPSData.LastModified = now; // currentMPSData.LastModifiedBy = username; // } // if (item.PlannedQuantity == 0) // { // List<Process> processWillbeDeleted = new List<Process>(); // foreach (Unit unitItem in currentMPSData.Units) // { // unitItem.MPSID = null; // unitItem.MPSDueDate = null; // unitItem.SFSDueDate = null; // unitItem.LastModified = now; // unitItem.LastModifiedBy = username; // processWillbeDeleted.AddRange(unitItem.Processes); // } // db.Processes.RemoveRange(processWillbeDeleted); // db.MasterPlanSchedules.Remove(currentMPSData); // } // } // else // { // MasterPlanSchedule newMPSData = new MasterPlanSchedule(); // newMPSData.PROID = item.PROID; // newMPSData.Week = item.Week; // newMPSData.Month = item.Month; // newMPSData.Year = item.Year; // newMPSData.PlannedQuantity = item.PlannedQuantity; // newMPSData.EndWorkingDate = item.EndWorkingDate; // newMPSData.CreatedBy = newMPSData.LastModifiedBy = username; // newMPSData.Created = newMPSData.LastModified = now; // db.MasterPlanSchedules.Add(newMPSData); // } //} #endregion } if (db.SaveChanges() > 0)//if database update is happened { List <MasterPlanSchedule> listCurrentMPS = db.MasterPlanSchedules.Where(x => x.PROID == proid).ToList(); List <MasterPlanSchedule> listNotUpdateMPS = listCurrentMPS.Where(x => x.EndWorkingDate < firstMPSData.EndWorkingDate || x.EndWorkingDate >= lastMPSData.EndWorkingDate).ToList(); listCurrentMPS = listCurrentMPS.Where(x => x.EndWorkingDate.Date >= firstMPSData.EndWorkingDate.Date && x.EndWorkingDate.Date <= lastMPSData.EndWorkingDate.Date).OrderBy(o => o.EndWorkingDate).ToList(); List <Unit> listUnit = db.Units.Where(x => x.PROID == proid && x.IsHold == false).ToList(); listUnit = listUnit.Where(x => !listNotUpdateMPS.Contains(x.MasterPlanSchedule)).ToList(); listUnit = listUnit.Where(x => x.IsHaveProcessAssign == false).OrderBy(o => o.SerialNumber).ToList(); int indexData = 0; foreach (MasterPlanSchedule item in listCurrentMPS) { int unAssignedQuantity = 0; unAssignedQuantity = item.PlannedQuantity - item.AssignedPlanCount; if (unAssignedQuantity > 0) { DateTime mpsDueDate = Convert.ToDateTime(item.EndWorkingDate); if (item.PRO != null) { if (mpsDueDate.Date > item.PRO.DueDate.Date) { mpsDueDate = item.PRO.DueDate; } } else { PRO currentPRO = db.Pros.Find(item.PROID); if (currentPRO != null && mpsDueDate.Date > currentPRO.DueDate.Date) { mpsDueDate = currentPRO.DueDate; } } Holiday holidayDate = db.Holidays.Where(x => x.StartDate <= mpsDueDate && x.EndDate >= mpsDueDate).OrderBy(o => o.StartDate).FirstOrDefault(); if (holidayDate != null) { mpsDueDate = holidayDate.StartDate.AddDays(-1); if (mpsDueDate.DayOfWeek == DayOfWeek.Sunday) { mpsDueDate = mpsDueDate.AddDays(-2); } else if (mpsDueDate.DayOfWeek == DayOfWeek.Saturday) { mpsDueDate = mpsDueDate.AddDays(-1); } } for (int i = 0; i < unAssignedQuantity; i++) { Unit dataUnit = listUnit.ElementAtOrDefault(indexData); if (dataUnit != null) { dataUnit.MPSID = item.ID; dataUnit.MPSDueDate = mpsDueDate; } indexData++; } } } if (listUnit.ElementAtOrDefault(indexData) != null) { for (int i = indexData; i < listUnit.Count(); i++) { Unit dataUnit = listUnit.ElementAt(i); dataUnit.MPSDueDate = null; } } db.SaveChanges(); } return(Json(new { success = true, responseText = "Value successfully updated" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = true, responseText = "No Value updated" }, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { ViewBag.Exception(ex); } return(View("Error")); }