Ejemplo n.º 1
0
        public async Task <ActionResult> Create(AddEditOrganisationModel model)
        {
            if (ModelState.IsValid)
            {
                Ukrlp ukrlp = db.Ukrlps.Find(model.UKPRN);
                if (ukrlp == null)
                {
                    // Don't tie error message to field as this will happen anyway when the script looks it up
                    ModelState.AddModelError("", AppGlobal.Language.GetText(this, "UKRLPNotFound", "UKRLP Not Found"));
                }
                else if (ukrlp.UkrlpStatus.HasValue && ukrlp.UkrlpStatus.Value == (Int32)Constants.RecordStatus.Deleted)
                {
                    // Don't tie error message to field as this will happen anyway when the script looks it up
                    ModelState.AddModelError("",
                                             AppGlobal.Language.GetText(this, "UKRLPDeactived", "UKRLP Has Been Deactivated"));
                }

                if (ProvisionUtilities.IsNameInUse(db, model.OrganisationName, null, null))
                {
                    ModelState.AddModelError("OrganisationName",
                                             AppGlobal.Language.GetText(this, "NameInUse",
                                                                        "This Organisation name already exists in the database, please contact the Course Directory Support Team on 0844 811 5073 or [email protected] if you need any help."));
                }

                if (ModelState.IsValid)
                {
                    Organisation organisation = model.ToEntity(db);
                    organisation.RecordStatu = db.RecordStatus.Find((int)Constants.RecordStatus.Live);
                    Address providerAddress = model.Address.ToEntity(db);
                    organisation.Address = providerAddress;
                    organisation.Address.ProviderRegionId = null;
                    if (userContext.IsProvider())
                    {
                        organisation.OrganisationProviders.Add(new OrganisationProvider
                        {
                            ProviderId = userContext.ItemId.Value,
                            IsAccepted = false,
                            IsRejected = false,
                        });
                    }
                    db.Addresses.Add(providerAddress);
                    db.Organisations.Add(organisation);
                    await db.SaveChangesAsync();

                    ShowGenericSavedMessage();
                    ProvisionUtilities.SendNewOrganisationEmail(organisation);
                    return(RedirectToAction("Index", "Home"));
                }
            }

            // Populate the drop downs
            GetLookups(model);

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Edit(AddEditOrganisationModel model)
        {
            if (Permission.HasPermission(false, true, Permission.PermissionName.CanEditProviderSpecialFields))
            {
                Ukrlp ukrlp = db.Ukrlps.Find(model.UKPRN);
                if (ukrlp == null)
                {
                    // Don't tie error message to field as this will happen anyway when the script looks it up
                    ModelState.AddModelError("", AppGlobal.Language.GetText(this, "UKRLPNotFound", "UKRLP Not Found"));
                }
                else if (ukrlp.UkrlpStatus.HasValue && ukrlp.UkrlpStatus.Value == (Int32)Constants.RecordStatus.Deleted &&
                         model.RecordStatusId.HasValue &&
                         model.RecordStatusId.Value != (Int32)Constants.RecordStatus.Deleted)
                {
                    // Don't tie error message to field as this will happen anyway when the script looks it up
                    ModelState.AddModelError("",
                                             AppGlobal.Language.GetText(this, "UKRLPDeactived", "UKRLP Has Been Deactivated"));
                }
            }

            if (ProvisionUtilities.IsNameInUse(db, model.OrganisationName, null, model.OrganisationId))
            {
                ModelState.AddModelError("OrganisationName",
                                         AppGlobal.Language.GetText(this, "NameInUse",
                                                                    "This Organisation name already exists in the database, please contact the Course Directory Support Team on 0844 811 5073 or [email protected] if you need any help."));
            }

            if (ModelState.IsValid)
            {
                Organisation organisation = model.ToEntity(db);
                if (organisation == null || organisation.OrganisationId != userContext.ItemId)
                {
                    return(HttpNotFound());
                }

                bool recordStatuschanged = model.RecordStatusId != organisation.RecordStatusId;
                if (
                    !(model.RecordStatusId == (int)Constants.RecordStatus.Live ||
                      model.RecordStatusId == (int)Constants.RecordStatus.Deleted))
                {
                    ModelState.AddModelError(
                        "RecordStatusId",
                        AppGlobal.Language.GetText("RecordStatusNotFound", "Status Is Invalid"));
                }

                if (recordStatuschanged &&
                    model.RecordStatusId == (int)Constants.RecordStatus.Deleted &&
                    organisation.OrganisationProviders.Any(x => x.IsAccepted && !x.IsRejected))
                {
                    ModelState.AddModelError(
                        "RecordStatusId",
                        AppGlobal.Language.GetText(this, "CannotDeleteActiveOrganisations",
                                                   "You may not delete this Organisation until all Providers have left or been removed."));
                }

                if (ModelState.IsValid)
                {
                    if (Permission.HasPermission(false, true, Permission.PermissionName.CanEditProviderSpecialFields))
                    {
                        organisation.UKPRN = model.UKPRN.HasValue ? model.UKPRN.Value : 0;
                        organisation.QualityEmailsPaused  = model.QualityEmailsPaused;
                        organisation.QualityEmailStatusId = model.QualityEmailStatusId;
                    }

                    organisation.ModifiedByUserId    = Permission.GetCurrentUserId();
                    organisation.ModifiedDateTimeUtc = DateTime.UtcNow;
                    // ReSharper disable once PossibleInvalidOperationException
                    organisation.RecordStatusId = model.RecordStatusId.Value;
                    Address organisationAddress = model.Address.ToEntity(db);

                    db.Entry(organisationAddress).State = EntityState.Modified;
                    db.Entry(organisation).State        = EntityState.Modified;
                    await db.SaveChangesAsync();

                    // Reset the user context if the status has changed
                    if (recordStatuschanged)
                    {
                        UserContext.SetUserContext(db, model.RecordStatusId == (int)Constants.RecordStatus.Deleted
                            ? UserContext.UserContextName.DeletedOrganisation
                            : UserContext.UserContextName.Organisation, userContext.ItemId, true);
                    }
                    ShowGenericSavedMessage();
                    return(RedirectToAction("Index", "Organisation"));
                }
            }

            // Populate the dropdowns
            GetLookups(model);

            return(View(model));
        }