// GET: /UnDelete
        public ActionResult UnDelete(int id)
        {
            //Get Item From Database
            ClientFeeGroup clientFeeGroup = new ClientFeeGroup();

            clientFeeGroup = clientFeeGroupRepository.GetGroup(id);

            //Check Exists
            if (clientFeeGroup == null || clientFeeGroup.DeletedFlag == false)
            {
                ViewData["ActionMethod"] = "DeleteGet";
                return(View("RecordDoesNotExistError"));
            }
            //Check Access
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToClientFeeGroup(id))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            ClientFeeGroupVM clientFeeGroupVM = new ClientFeeGroupVM();

            clientFeeGroupRepository.EditGroupForDisplay(clientFeeGroup);
            clientFeeGroupVM.ClientFeeGroup          = clientFeeGroup;
            clientFeeGroupVM.FeeTypeId               = (int)clientFeeGroup.FeeTypeId;
            clientFeeGroupVM.FeeTypeDisplayName      = clientFeeGroupRepository.FeeTypeDisplayName(clientFeeGroupVM.FeeTypeId);
            clientFeeGroupVM.FeeTypeDisplayNameShort = clientFeeGroupRepository.FeeTypeDisplayNameShort(clientFeeGroupVM.FeeTypeId);

            return(View(clientFeeGroupVM));
        }
        // GET: /Create
        public ActionResult Create(int ft)
        {
            //Check Access Rights to Domain
            if (!hierarchyRepository.AdminHasDomainWriteAccess(groupName))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            ClientFeeGroupVM clientFeeGroupVM = new ClientFeeGroupVM();

            clientFeeGroupVM.FeeTypeId               = ft;
            clientFeeGroupVM.FeeTypeDisplayName      = clientFeeGroupRepository.FeeTypeDisplayName(ft);
            clientFeeGroupVM.FeeTypeDisplayNameShort = clientFeeGroupRepository.FeeTypeDisplayNameShort(ft);

            TripTypeRepository tripTypeRepository = new TripTypeRepository();
            SelectList         tripTypesList      = new SelectList(tripTypeRepository.GetAllTripTypes().ToList(), "TripTypeId", "TripTypeDescription");

            clientFeeGroupVM.TripTypes = tripTypesList;

            TablesDomainHierarchyLevelRepository tablesDomainHierarchyLevelRepository = new TablesDomainHierarchyLevelRepository();
            SelectList hierarchyTypesList = new SelectList(tablesDomainHierarchyLevelRepository.GetDomainHierarchies(groupName).ToList(), "HierarchyLevelTableName", "HierarchyLevelTableName");

            clientFeeGroupVM.HierarchyTypes = hierarchyTypesList;

            ClientFeeGroup clientFeeGroup = new ClientFeeGroup();

            clientFeeGroup.FeeTypeId        = ft;
            clientFeeGroup.Mandatory        = (clientFeeGroup.FeeTypeId == 1 || clientFeeGroup.FeeTypeId == 2);
            clientFeeGroup.EnabledFlag      = true;
            clientFeeGroupVM.ClientFeeGroup = clientFeeGroup;

            return(View(clientFeeGroupVM));
        }
        public ActionResult UnDelete(ClientFeeGroupVM clientFeeGroupVM)
        {
            //Check Valid Item passed in Form
            if (clientFeeGroupVM.ClientFeeGroup == null)
            {
                ViewData["ActionMethod"] = "DeletePost";
                return(View("RecordDoesNotExistError"));
            }

            //Get Item From Database
            ClientFeeGroup clientFeeGroup = new ClientFeeGroup();

            clientFeeGroup = clientFeeGroupRepository.GetGroup(clientFeeGroupVM.ClientFeeGroup.ClientFeeGroupId);

            //Check Exists in Databsase
            if (clientFeeGroup == null || clientFeeGroup.DeletedFlag == false)
            {
                ViewData["ActionMethod"] = "DeletePost";
                return(View("RecordDoesNotExistError"));
            }
            //Check Access
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToClientFeeGroup(clientFeeGroup.ClientFeeGroupId))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }
            //Delete Form Item
            try
            {
                clientFeeGroupVM.ClientFeeGroup.DeletedFlag = false;
                clientFeeGroupRepository.UpdateGroupDeletedStatus(clientFeeGroupVM.ClientFeeGroup);
            }
            catch (SqlException ex)
            {
                //Versioning Error - go to standard versionError page
                if (ex.Message == "SQLVersioningError")
                {
                    ViewData["ReturnURL"] = "/ClientFeeGroup.mvc/UnDelete/" + clientFeeGroup.ClientFeeGroupId;
                    return(View("VersionError"));
                }

                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                ViewData["Message"] = "There was a problem with your request, please see the log file or contact an administrator for details";
                return(View("Error"));
            }
            return(RedirectToAction("ListUnDeleted", new { ft = clientFeeGroup.FeeTypeId }));
        }
        // GET: /Edit
        public ActionResult Edit(int id)
        {
            //Get Item From Database
            ClientFeeGroup clientFeeGroup = new ClientFeeGroup();

            clientFeeGroup = clientFeeGroupRepository.GetGroup(id);

            //Check Exists
            if (clientFeeGroup == null)
            {
                ViewData["ActionMethod"] = "EditGet";
                return(View("RecordDoesNotExistError"));
            }
            //Check Access
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToClientFeeGroup(id))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            ClientFeeGroupVM clientFeeGroupVM = new ClientFeeGroupVM();

            clientFeeGroupRepository.EditGroupForDisplay(clientFeeGroup);
            clientFeeGroupVM.ClientFeeGroup          = clientFeeGroup;
            clientFeeGroupVM.FeeTypeId               = (int)clientFeeGroup.FeeTypeId;
            clientFeeGroupVM.FeeTypeDisplayName      = clientFeeGroupRepository.FeeTypeDisplayName(clientFeeGroupVM.FeeTypeId);
            clientFeeGroupVM.FeeTypeDisplayNameShort = clientFeeGroupRepository.FeeTypeDisplayNameShort(clientFeeGroupVM.FeeTypeId);

            TripTypeRepository tripTypeRepository = new TripTypeRepository();
            SelectList         tripTypesList      = new SelectList(tripTypeRepository.GetAllTripTypes().ToList(), "TripTypeId", "TripTypeDescription");

            clientFeeGroupVM.TripTypes = tripTypesList;

            TablesDomainHierarchyLevelRepository tablesDomainHierarchyLevelRepository = new TablesDomainHierarchyLevelRepository();
            SelectList hierarchyTypesList = new SelectList(tablesDomainHierarchyLevelRepository.GetDomainHierarchies(groupName).ToList(), "HierarchyLevelTableName", "HierarchyLevelTableName");

            clientFeeGroupVM.HierarchyTypes = hierarchyTypesList;

            return(View(clientFeeGroupVM));
        }
        // GET: /View
        public ActionResult View(int id)
        {
            ClientFeeGroup clientFeeGroup = new ClientFeeGroup();

            clientFeeGroup = clientFeeGroupRepository.GetGroup(id);

            //Check Exists
            if (clientFeeGroup == null)
            {
                ViewData["ActionMethod"] = "ViewGet";
                return(View("RecordDoesNotExistError"));
            }
            ClientFeeGroupVM clientFeeGroupVM = new ClientFeeGroupVM();

            clientFeeGroupRepository.EditGroupForDisplay(clientFeeGroup);
            clientFeeGroupVM.ClientFeeGroup          = clientFeeGroup;
            clientFeeGroupVM.FeeTypeId               = (int)clientFeeGroup.FeeTypeId;
            clientFeeGroupVM.FeeTypeDisplayName      = clientFeeGroupRepository.FeeTypeDisplayName(clientFeeGroupVM.FeeTypeId);
            clientFeeGroupVM.FeeTypeDisplayNameShort = clientFeeGroupRepository.FeeTypeDisplayNameShort(clientFeeGroupVM.FeeTypeId);

            return(View(clientFeeGroupVM));
        }
        public ActionResult Edit(ClientFeeGroupVM clientFeeGroupVM)
        {
            //Get Item
            ClientFeeGroup clientFeeGroup = new ClientFeeGroup();

            clientFeeGroup = clientFeeGroupRepository.GetGroup(clientFeeGroupVM.ClientFeeGroup.ClientFeeGroupId);

            //Check Exists
            if (clientFeeGroup == null)
            {
                ViewData["ActionMethod"] = "EditPost";
                return(View("RecordDoesNotExistError"));
            }
            //Check Access Rights
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToClientFeeGroup(clientFeeGroup.ClientFeeGroupId))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }
            //Update Model From Form + Validate against DB
            try
            {
                UpdateModel <ClientFeeGroup>(clientFeeGroup, "ClientFeeGroup");
            }
            catch
            {
                string n = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        n += error.ErrorMessage;
                    }
                }
                ViewData["Message"] = "ValidationError : " + n;
                return(View("Error"));
            }

            //Dont check for multiple as We are not editing Hierarchy, we have alrady checked access to the item itself
            if (clientFeeGroup.HierarchyType != "Multiple")
            {
                //ClientSubUnitTravelerType has extra field
                string hierarchyCode = clientFeeGroup.HierarchyCode;
                if (clientFeeGroup.HierarchyType == "ClientSubUnitTravelerType")
                {
                    clientFeeGroup.ClientSubUnitGuid = hierarchyCode;  //ClientSubUnitTravelerType has 2 primarykeys
                }
                //Check Access Rights to PolicyGroup
                HierarchyRepository hierarchyRepository = new HierarchyRepository();
                if (!hierarchyRepository.AdminHasDomainHierarchyWriteAccess(clientFeeGroup.HierarchyType, hierarchyCode, clientFeeGroup.SourceSystemCode, groupName))
                {
                    ViewData["Message"] = "You cannot add to this hierarchy item";
                    return(View("Error"));
                }
            }


            //Database Update
            try
            {
                clientFeeGroupRepository.Edit(clientFeeGroup);
            }
            catch (SqlException ex)
            {
                //Versioning Error
                if (ex.Message == "SQLVersioningError")
                {
                    ViewData["ReturnURL"] = "/ClientFeeGroup.mvc/Edit/" + clientFeeGroup.ClientFeeGroupId;
                    return(View("VersionError"));
                }
                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                ViewData["Message"] = "There was a problem with your request, please see the log file or contact an administrator for details";
                return(View("Error"));
            }
            return(RedirectToAction("ListUnDeleted", new { ft = clientFeeGroup.FeeTypeId }));
        }
        public ActionResult Create(ClientFeeGroupVM clientFeeGroupVM)
        {
            //Check Access Rights to Domain
            if (!hierarchyRepository.AdminHasDomainWriteAccess(groupName))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            //We need to extract group from groupVM
            ClientFeeGroup clientFeeGroup = new ClientFeeGroup();

            clientFeeGroup = clientFeeGroupVM.ClientFeeGroup;
            if (clientFeeGroup == null)
            {
                ViewData["Message"] = "ValidationError : missing item";;
                return(View("Error"));
            }

            //Check Access Rights to Domain Hierarchy
            if (!hierarchyRepository.AdminHasDomainHierarchyWriteAccess(clientFeeGroup.HierarchyType, clientFeeGroup.HierarchyCode, clientFeeGroup.SourceSystemCode, groupName))
            {
                ViewData["Message"] = "You cannot add to this hierarchy item";
                return(View("Error"));
            }

            //Update Model From Form + Validate against DB
            try
            {
                UpdateModel <ClientFeeGroup>(clientFeeGroup, "ClientFeeGroup");
            }
            catch
            {
                string n = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        n += error.ErrorMessage;
                    }
                }
                ViewData["Message"] = "ValidationError : " + n;
                return(View("Error"));
            }

            //ClientSubUnitTravelerType has extra field
            string hierarchyCode = clientFeeGroup.HierarchyCode;

            if (clientFeeGroup.HierarchyType == "ClientSubUnitTravelerType")
            {
                clientFeeGroup.ClientSubUnitGuid = hierarchyCode;  //ClientSubUnitTravelerType has 2 primarykeys
            }

            //Database Update
            try
            {
                clientFeeGroupRepository.Add(clientFeeGroup);
            }
            catch (SqlException ex)
            {
                //Non-Unique Name
                if (ex.Message == "NonUniqueName")
                {
                    return(View("NonUniqueNameError"));
                }
                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                ViewData["Message"] = "There was a problem with your request, please see the log file or contact an administrator for details";
                return(View("Error"));
            }

            ViewData["NewSortOrder"] = 0;
            return(RedirectToAction("ListUnDeleted", new { ft = clientFeeGroup.FeeTypeId }));
        }