public ActionResult Edit(VirtualMappingPlantEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var dbVirtual = _virtualMappingPlanBll.GetById(model.VirtualMapId);

                if (dbVirtual == null)
                {
                    ModelState.AddModelError("Details", "Data Not Found");
                    InitEditModel(model);

                    return(View("Edit", model));
                }
                var origin = AutoMapper.Mapper.Map <VIRTUAL_PLANT_MAP>(dbVirtual);
                SetChanges(origin, model, CurrentUser.USER_ID);
                dbVirtual.COMPANY_ID      = model.CompanyId;
                dbVirtual.IMPORT_PLANT_ID = model.ImportPlantId;
                dbVirtual.EXPORT_PLANT_ID = model.ExportPlantId;

                if (_virtualMappingPlanBll.Save(dbVirtual))
                {
                    TempData[Constans.SubmitType.Update] = Constans.SubmitMessage.Updated;
                    return(RedirectToAction("Index"));
                }
                else
                {
                    AddMessageInfo("Same Virtual plant mapping already exist", Enums.MessageInfoType.Warning);
                    //InitEditModel(model);
                    //return View("Edit", model);
                }
            }

            InitEditModel(model);
            return(View("Edit", model));
        }
        private VirtualMappingPlantEditViewModel InitEditModel(VirtualMappingPlantEditViewModel model)
        {
            model.MainMenu    = _mainMenu;
            model.CurrentMenu = PageInfo;

            model.CompanyNameList    = GlobalFunctions.GetCompanyList(_companyBll);
            model.ImportPlanNameList = GlobalFunctions.GetVirtualPlantList();
            model.ExportPlanNameList = GlobalFunctions.GetVirtualPlantList();

            return(model);
        }
        private void SetChanges(VIRTUAL_PLANT_MAP origin, VirtualMappingPlantEditViewModel data, string userId)
        {
            var changesData = new Dictionary <string, bool>();

            changesData.Add("COMPANY_ID", origin.COMPANY_ID.Equals(data.CompanyId));
            changesData.Add("EXPORT_PLANT", origin.EXPORT_PLANT_ID.Equals(data.ExportPlantId));
            changesData.Add("IMPORT_PLANT", origin.IMPORT_PLANT_ID.Equals(data.ImportPlantId));

            foreach (var listChange in changesData)
            {
                if (listChange.Value == false)
                {
                    var changes = new CHANGES_HISTORY
                    {
                        FORM_TYPE_ID  = Core.Enums.MenuList.VirtualMappingPlant,
                        FORM_ID       = data.VirtualMapId.ToString(),
                        FIELD_NAME    = listChange.Key,
                        MODIFIED_BY   = userId,
                        MODIFIED_DATE = DateTime.Now
                    };
                    switch (listChange.Key)
                    {
                    case "COMPANY_ID":
                        changes.OLD_VALUE = origin.COMPANY_ID;
                        changes.NEW_VALUE = data.CompanyId;
                        break;

                    case "EXPORT_PLANT":
                        changes.OLD_VALUE = origin.EXPORT_PLANT_ID;
                        changes.NEW_VALUE = data.ExportPlantId;
                        break;

                    case "IMPORT_PLANT":
                        changes.OLD_VALUE = origin.IMPORT_PLANT_ID;
                        changes.NEW_VALUE = data.ImportPlantId;
                        break;
                    }
                    _changesHistoryBLL.AddHistory(changes);
                }
            }
        }
        public ActionResult Edit(int id)
        {
            if (CurrentUser.UserRole == Enums.UserRole.Viewer || CurrentUser.UserRole == Enums.UserRole.Controller)
            {
                return(RedirectToAction("Details", new { id }));
            }

            var model = new VirtualMappingPlantEditViewModel();

            InitEditModel(model);

            var dbVirtual = _virtualMappingPlanBll.GetByIdIncludeChild(id);

            if (dbVirtual != null)
            {
                model.VirtualMapId = dbVirtual.VIRTUAL_PLANT_MAP_ID;

                if (!string.IsNullOrEmpty(dbVirtual.COMPANY_ID))
                {
                    model.CompanyId = dbVirtual.COMPANY_ID;
                }

                model.ImportPlantId = dbVirtual.T001W1.WERKS;
                model.ExportPlantId = dbVirtual.T001W.WERKS;
                model.IsDeleted     = dbVirtual.IS_DELETED;
                model.IsAllowDelete = !model.IsDeleted.HasValue || !model.IsDeleted.Value;
                return(View(model));
            }
            else
            {
                //model.VirtualMapId = 0;
                //model.ImportPlantId = 0;
                //model.ExportPlantId = 0;
                //ModelState.AddModelError("Exception", "Data Not Found");
                throw new HttpException(403, "Data not found");
            }
        }