Beispiel #1
0
        // GET: PROSynchronize/Details/5

        /// <summary>
        /// method ini berfungsi untuk menampilkan halaman yang berisi data-data dari tabel DataZemesFails
        /// yang berhasil disinkronisasikan (data yang berhasil ditambahkan ke tabel PRO)
        /// dan kemudian data pada tabel DataZemesFails dihapus apabaila data tersebut berhasil ditambahkan
        /// </summary>
        /// <param name="page">merupakan parameter bertipe integer yang berisi data halaman saat ini</param>
        /// <returns></returns>
        public ActionResult SynchronizePRO(int?page)
        {
            string               userName       = User.Identity.Name;
            DateTime             now            = DateTime.Now;
            List <DataZemesFail> dataZemesFails = db.DataZemesFails.ToList();
            List <Unit>          unitDataList   = new List <Unit>();

            foreach (DataZemesFail item in dataZemesFails)
            {
                Products dataProduct = db.Products.Where(x => x.PN == item.Material).SingleOrDefault();

                if (dataProduct != null)
                {
                    PRO dataPRO = db.Pros.Where(x => x.Number == item.ProNo).SingleOrDefault();

                    if (dataPRO == null)
                    {
                        PRO pro = new PRO();
                        pro.Number         = item.ProNo;
                        pro.ProductID      = dataProduct.ID;
                        pro.Quantity       = item.Quantity;
                        pro.DueDate        = item.DeliveryDate;
                        pro.Created        = now;
                        pro.CreatedBy      = userName;
                        pro.LastModified   = now;
                        pro.LastModifiedBy = userName;

                        Unit unitData = new Unit();
                        unitData.SerialNumber = item.SerialNumber;
                        unitData.ProductID    = dataProduct.ID;
                        unitData.DueDate      = item.DeliveryDate;
                        unitData.IsHold       = false;
                        unitData.Created      = unitData.LastModified = now;
                        unitData.CreatedBy    = unitData.LastModifiedBy = userName;

                        pro.Units.Add(unitData);

                        db.Pros.Add(pro);

                        if (db.SaveChanges() > 0)
                        {
                            unitDataList.Add(unitData);
                            db.DataZemesFails.Remove(item);
                            db.SaveChanges();
                        }
                    }
                    else
                    {
                        Unit existUnitData = new Unit();
                        existUnitData.PROID        = dataPRO.ID;
                        existUnitData.ProductID    = dataProduct.ID;
                        existUnitData.SerialNumber = item.SerialNumber;
                        existUnitData.DueDate      = item.DeliveryDate;
                        existUnitData.IsHold       = false;
                        existUnitData.Created      = existUnitData.LastModified = now;
                        existUnitData.CreatedBy    = existUnitData.LastModifiedBy = userName;

                        db.Units.Add(existUnitData);

                        if (db.SaveChanges() > 0)
                        {
                            unitDataList.Add(existUnitData);
                            db.DataZemesFails.Remove(item);
                            db.SaveChanges();
                        }
                    }
                }
            }
            int pageSize   = 10;
            int pageNumber = (page ?? 1);

            return(View(unitDataList.OrderBy(x => x.PROID).ToPagedList(pageNumber, pageSize)));
        }
Beispiel #2
0
        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"));
        }