public ActionResult Cancel()
        {
            var mainContract = this.DbPractice.AccountContract;
            var viewModel    = new CancelAccountViewModel
            {
                CurrentContract = new ConfigAccountViewModel.Contract
                {
                    PlanTitle     = mainContract.SYS_ContractType.Name,
                    UrlIdentifier = mainContract.SYS_ContractType.UrlIdentifier.Trim(),
                }
            };

            // Get plan informations of this practice.
            // Contract text, title, description, and other things to display.

            this.ViewBag.IsTrial = mainContract.SYS_ContractType.IsTrial;

            return(View(viewModel));
        }
        public ActionResult Cancel(CancelAccountViewModel viewModel)
        {
            if (!viewModel.Confirm)
            {
                var mainContract = this.DbPractice.AccountContract;

                // Get plan informations of this practice.
                // Contract text, title, description, and other things to display.
                viewModel.CurrentContract = new ConfigAccountViewModel.Contract
                {
                    PlanTitle     = mainContract.SYS_ContractType.Name,
                    UrlIdentifier = mainContract.SYS_ContractType.UrlIdentifier.Trim(),
                };

                this.ModelState.AddModelError(() => viewModel.Confirm, "A caixa de checagem de confirmação precisa ser marcada para prosseguir.");

                return(this.View(viewModel));
            }

            // Payd accounts are canceled manually.
            if (!this.DbPractice.AccountContract.SYS_ContractType.IsTrial)
            {
                this.DbPractice.AccountCancelRequest = true;

                this.db.SaveChanges();

                this.ViewBag.CancelRequested = true;

                return(this.View(viewModel));
            }

            // Sending e-mail with data.
            if (viewModel.SendDataByEmail)
            {
                foreach (var eachDoctorUser in this.DbPractice.Users.Where(u => u.DoctorId != null))
                {
                    // Rendering message bodies from partial view.
                    var emailViewModel = new UserEmailViewModel(eachDoctorUser);
                    var toAddress      = new MailAddress(eachDoctorUser.Person.Email, eachDoctorUser.Person.FullName);
                    var mailMessage    = this.CreateEmailMessagePartial("AccountDataEmail", toAddress, emailViewModel);

                    // attaching pdf
                    var pdf = ReportController.ExportPatientsPdf(null, this.db, this.DbPractice, eachDoctorUser.Doctor);

                    mailMessage.Attachments.Add(
                        new Attachment(new MemoryStream(pdf), "Prontuários.pdf", "application/pdf"));

                    // attaching xml
                    var xml = ReportController.ExportDoctorXml(this.db, this.DbPractice, eachDoctorUser.Doctor);

                    mailMessage.Attachments.Add(
                        new Attachment(
                            new MemoryStream(Encoding.UTF8.GetBytes(xml)), "Prontuários.xml", "text/xml"));

                    // sending message
                    using (mailMessage)
                    {
                        this.TrySendEmail(mailMessage);
                    }
                }
            }

            var userReason = string.IsNullOrWhiteSpace(viewModel.Reason) ? "No reason provided by user." : viewModel.Reason;

            // sending self e-mail with user reason for canceling
            using (var message = EmailHelper.CreateEmailMessage(
                       new MailAddress("*****@*****.**"),
                       string.Format("Conta cancelada pelo usuário: {0}", this.DbPractice.UrlIdentifier),
                       userReason))
            {
                this.TrySendEmail(message);
            }

            // logging off
            FormsAuthentication.SignOut();

            // disabling account
            this.DbPractice.AccountDisabled = true;
            this.DbPractice.UrlIdentifier  += " !disabled"; // change this, so that a new practice with this name can be created.
            this.db.SaveChanges();

            // redirecting user to success message (outside of the app, because the account was canceled)
            return(this.RedirectToAction("AccountCanceled", "Home", new { area = "", practice = this.DbPractice.UrlIdentifier }));
        }