public ActionResult Create(POAFormModel model)
        {
            try
            {
                bool isExist;
                var  poa = AutoMapper.Mapper.Map <BusinessObject.POA>(model.Detail);
                poa.POA_ID       = model.Detail.UserId;
                poa.CREATED_BY   = CurrentUser.USER_ID;
                poa.CREATED_DATE = DateTime.Now;
                poa.IS_ACTIVE    = true;
                if (model.Detail.PoaSKFile != null)
                {
                    int counter = 0;
                    foreach (var sk in model.Detail.PoaSKFile)
                    {
                        if (sk != null)
                        {
                            var poa_sk        = new POA_SK();
                            var filenamecheck = sk.FileName;
                            if (filenamecheck.Contains("\\"))
                            {
                                poa_sk.FILE_NAME = filenamecheck.Split('\\')[filenamecheck.Split('\\').Length - 1];
                            }
                            else
                            {
                                poa_sk.FILE_NAME = sk.FileName;
                            }

                            poa_sk.FILE_PATH = SaveUploadedFile(sk, poa.ID_CARD, counter);
                            poa.POA_SK.Add(poa_sk);
                            counter += 1;
                        }
                    }
                }
                MASTER_DATA_APPROVAL approvalData;
                _masterDataAprovalBLL.MasterDataApprovalValidation((int)Enums.MenuList.POA, CurrentUser.USER_ID,
                                                                   new BusinessObject.POA(), poa, out isExist, out approvalData, true);
                //_poaBll.Save(poa);
                _masterDataAprovalBLL.SendEmailWorkflow(approvalData.APPROVAL_ID);
                AddMessageInfo(Constans.SubmitMessage.Saved, Enums.MessageInfoType.Success);
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                AddMessageInfo(ex.Message, Enums.MessageInfoType.Error);
                return(RedirectToAction("Index"));
            }
            //return RedirectToAction("Create");
        }
        //public ActionResult Create()
        //{
        //    if (CurrentUser.UserRole == Enums.UserRole.Viewer || CurrentUser.UserRole == Enums.UserRole.Controller)
        //    {
        //        AddMessageInfo("Operation not allow", Enums.MessageInfoType.Error);
        //        return RedirectToAction("Index");
        //    }

        //    var poa = new POAFormModel();
        //    poa.MainMenu = _mainMenu;
        //    poa.CurrentMenu = PageInfo;
        //    poa.Users = GlobalFunctions.GetCreatorList();
        //    return View(poa);
        //}

        //[HttpPost]
        //public ActionResult Create(POAFormModel model)
        //{
        //    try
        //    {
        //        bool isExist;
        //        var poa = AutoMapper.Mapper.Map<BusinessObject.POA>(model.Detail);
        //        poa.POA_ID = model.Detail.UserId;
        //        poa.CREATED_BY = CurrentUser.USER_ID;
        //        poa.CREATED_DATE = DateTime.Now;
        //        poa.MODIFIED_BY = CurrentUser.USER_ID;
        //        poa.MODIFIED_DATE = DateTime.Now;
        //        poa.IS_ACTIVE = true;
        //        if (model.Detail.PoaSKFile != null)
        //        {
        //            int counter = 0;
        //            foreach (var sk in model.Detail.PoaSKFile)
        //            {
        //                if (sk != null)
        //                {
        //                    var poa_sk = new POA_SK();
        //                    var filenamecheck = sk.FileName;
        //                    if (filenamecheck.Contains("\\"))
        //                    {
        //                        poa_sk.FILE_NAME = filenamecheck.Split('\\')[filenamecheck.Split('\\').Length - 1];
        //                    }
        //                    else
        //                    {
        //                        poa_sk.FILE_NAME = sk.FileName;
        //                    }

        //                    poa_sk.FILE_PATH = SaveUploadedFile(sk, poa.ID_CARD, counter);
        //                    poa.POA_SK.Add(poa_sk);
        //                    counter += 1;
        //                }
        //            }
        //        }

        //        //var exciser = new POA_EXCISER();

        //        //exciser.POA_ID = poa.POA_ID;
        //        //exciser.IS_ACTIVE_EXCISER = model.Detail.IsExciser;

        //        // Added: Set Exciser Flag: True or False

        //        _poaBll.Save(poa); // Insert only poa
        //        _poaBll.InsertChangesLog(poa, (int)Enums.MenuList.POA, (int)Enums.ActionType.Created, (int)CurrentUser.UserRole, CurrentUser.USER_ID);

        //        var modelExciser = new PoaExciserViewModel();
        //        var data = new POA_EXCISER();
        //        modelExciser.POA_ID = poa.POA_ID;

        //        var ExciserData = _poaExciserService.GetByPoaID(poa.POA_ID);

        //        if (model.Detail.IsExciser == true) // if checked true
        //        {
        //            if (ExciserData == null) // if no data with current POA ID
        //            {
        //                data.POA_ID = poa.POA_ID;
        //                data.IS_ACTIVE_EXCISER = true;
        //                _poaExciserService.CreateExciser(data, (int)Enums.MenuList.POA, (int)Enums.ActionType.Created, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
        //            }
        //            if (ExciserData != null) // if data exist
        //            {
        //                if (ExciserData.IS_ACTIVE_EXCISER == false)
        //                {
        //                    data.EXCISER_ID = ExciserData.EXCISER_ID;
        //                    data.POA_ID = modelExciser.POA_ID;
        //                    data.IS_ACTIVE_EXCISER = true;
        //                    _poaExciserService.SetActive(data, (int)Enums.MenuList.POA, (int)Enums.ActionType.Modified, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
        //                }
        //            }
        //        }
        //        else // if checked false
        //        {
        //            if (ExciserData == null) // if no data with current POA ID, keep create row
        //            {
        //                data.POA_ID = poa.POA_ID;
        //                data.IS_ACTIVE_EXCISER = false;
        //                _poaExciserService.CreateExciser(data, (int)Enums.MenuList.POA, (int)Enums.ActionType.Created, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
        //            }
        //            if (ExciserData != null) // if data exist
        //            {
        //                if (ExciserData.IS_ACTIVE_EXCISER == true)
        //                {
        //                    data.EXCISER_ID = ExciserData.EXCISER_ID;
        //                    data.POA_ID = modelExciser.POA_ID;
        //                    data.IS_ACTIVE_EXCISER = false;
        //                    _poaExciserService.SetActive(data, (int)Enums.MenuList.POA, (int)Enums.ActionType.Modified, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
        //                }
        //            }
        //        }

        //        //    _poaExciserService.CreateExciser(exciser, (int)Enums.MenuList.POA, (int)Enums.ActionType.Created, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
        //        AddMessageInfo(Constans.SubmitMessage.Saved, Enums.MessageInfoType.Success);

        //        return RedirectToAction("Index");
        //    }
        //    catch (Exception ex)
        //    {
        //        AddMessageInfo(ex.Message, Enums.MessageInfoType.Error);

        //        return RedirectToAction("Index");
        //    }
        //    // return RedirectToAction("Create");

        //}
        #endregion

        #region Create by EMS
        public ActionResult Create()
        {
            if (CurrentUser.UserRole == Enums.UserRole.Viewer || CurrentUser.UserRole == Enums.UserRole.Controller)
            {
                AddMessageInfo("Operation not allow", Enums.MessageInfoType.Error);
                return(RedirectToAction("Index"));
            }

            var poa = new POAFormModel();

            poa.MainMenu    = _mainMenu;
            poa.CurrentMenu = PageInfo;
            poa.Users       = GlobalFunctions.GetCreatorList();
            return(View(poa));
        }
        public ActionResult Edit(string id)
        {
            if (CurrentUser.UserRole == Enums.UserRole.Viewer || CurrentUser.UserRole == Enums.UserRole.Controller)
            {
                return(RedirectToAction("Detail", new { id }));
            }

            var poa = _poaBll.GetById(id);

            if (poa == null)
            {
                return(HttpNotFound());
            }
            var changeHistoryList = _changesHistoryBll.GetByFormTypeAndFormId(Enums.MenuList.POA, id);
            var model             = new POAFormModel();

            model.MainMenu           = _mainMenu;
            model.CurrentMenu        = PageInfo;
            model.ChangesHistoryList = Mapper.Map <List <ChangesHistoryItemModel> >(changeHistoryList);

            var detail = AutoMapper.Mapper.Map <POAViewDetailModel>(poa);

            //Added : Get Exciser Flag : True or False
            var ExciserData = _poaExciserService.GetByPoaID(detail.PoaId);

            if (ExciserData == null)
            {
                detail.IsExciser = false;
            }
            else
            {
                detail.IsExciser = ExciserData.IS_ACTIVE_EXCISER == true ? true : false;
            }

            model.Managers = detail.Manager == null?GlobalFunctions.GetCreatorList() : GlobalFunctions.GetCreatorList(detail.Manager.USER_ID);

            model.Users = detail.User == null?GlobalFunctions.GetCreatorList() : GlobalFunctions.GetCreatorList(detail.User.USER_ID);

            model.Detail = detail;

            return(View(model));
        }
        public ActionResult Edit(POAFormModel model)
        {
            try
            {
                bool isExist;
                var  poaId = model.Detail.PoaId;
                var  poa   = _poaBll.GetById(poaId);
                if (model.Detail.PoaSKFile != null)
                {
                    int counter = 0;
                    foreach (var sk in model.Detail.PoaSKFile)
                    {
                        if (sk != null)
                        {
                            var poa_sk        = new POA_SK();
                            var filenamecheck = sk.FileName;
                            if (filenamecheck.Contains("\\"))
                            {
                                poa_sk.FILE_NAME = filenamecheck.Split('\\')[filenamecheck.Split('\\').Length - 1];
                            }
                            else
                            {
                                poa_sk.FILE_NAME = sk.FileName;
                            }
                            poa_sk.FILE_PATH = SaveUploadedFile(sk, poa.ID_CARD, counter);
                            poa_sk.POA_ID    = poaId;
                            _poaskbll.Save(poa_sk);
                            counter += 1;
                        }
                    }
                }
                poa.MODIFIED_BY   = CurrentUser.USER_ID;
                poa.MODIFIED_DATE = DateTime.Now;

                // Added: Set Exciser Flag: True or False
                var modelExciser = new PoaExciserViewModel();
                var data         = new Sampoerna.EMS.CustomService.Data.POA_EXCISER();
                modelExciser.POA_ID = poa.POA_ID;

                var ExciserData = _poaExciserService.GetByPoaID(poa.POA_ID);

                if (model.Detail.IsExciser == true) // if checked true
                {
                    if (ExciserData == null)        // if no data with current POA ID
                    {
                        data.POA_ID            = poa.POA_ID;
                        data.IS_ACTIVE_EXCISER = true;
                        _poaExciserService.CreateExciser(data, (int)Enums.MenuList.POA, (int)Enums.ActionType.Created, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
                    }
                    if (ExciserData != null) // if data exist
                    {
                        if (ExciserData.IS_ACTIVE_EXCISER == false)
                        {
                            data.EXCISER_ID        = ExciserData.EXCISER_ID;
                            data.POA_ID            = modelExciser.POA_ID;
                            data.IS_ACTIVE_EXCISER = true;
                            _poaExciserService.SetActive(data, (int)Enums.MenuList.POA, (int)Enums.ActionType.Modified, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
                        }
                    }
                }
                else // if checked false
                {
                    if (ExciserData == null) // if no data with current POA ID, keep create row
                    {
                        data.POA_ID            = poa.POA_ID;
                        data.IS_ACTIVE_EXCISER = false;
                        _poaExciserService.CreateExciser(data, (int)Enums.MenuList.POA, (int)Enums.ActionType.Created, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
                    }
                    if (ExciserData != null) // if data exist
                    {
                        if (ExciserData.IS_ACTIVE_EXCISER == true)
                        {
                            data.EXCISER_ID        = ExciserData.EXCISER_ID;
                            data.POA_ID            = modelExciser.POA_ID;
                            data.IS_ACTIVE_EXCISER = false;
                            _poaExciserService.SetActive(data, (int)Enums.MenuList.POA, (int)Enums.ActionType.Modified, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
                        }
                    }
                }

                var origin = AutoMapper.Mapper.Map <POAViewDetailModel>(poa);
                var newpoa = AutoMapper.Mapper.Map <BusinessObject.POA>(model.Detail);
                newpoa.CREATED_BY    = poa.CREATED_BY;
                newpoa.CREATED_DATE  = poa.CREATED_DATE;
                newpoa.MODIFIED_BY   = CurrentUser.USER_ID;
                newpoa.MODIFIED_DATE = DateTime.Today;
                newpoa.IS_ACTIVE     = poa.IS_ACTIVE;
                MASTER_DATA_APPROVAL approvalData;
                newpoa = _masterDataAprovalBLL.MasterDataApprovalValidation((int)Enums.MenuList.POA, CurrentUser.USER_ID, poa,
                                                                            newpoa, out isExist, out approvalData);
                SetChanges(origin, newpoa);

                _poaBll.Save(newpoa);
                _masterDataAprovalBLL.SendEmailWorkflow(approvalData.APPROVAL_ID);
                AddMessageInfo(Constans.SubmitMessage.Updated, Enums.MessageInfoType.Success);

                return(RedirectToAction("Index"));
            }

            catch (Exception ex)
            {
                AddMessageInfo(ex.Message, Enums.MessageInfoType.Error);
                model.MainMenu    = _mainMenu;
                model.CurrentMenu = PageInfo;
                model.Managers    = model.Detail.Manager == null?GlobalFunctions.GetCreatorList() : GlobalFunctions.GetCreatorList(model.Detail.Manager.USER_ID);

                model.Users = model.Detail.User == null?GlobalFunctions.GetCreatorList() : GlobalFunctions.GetCreatorList(model.Detail.User.USER_ID);

                return(View(model));
            }
        }