/// <summary>
 /// Deprecated Method for adding a new object to the Patients EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToPatients(Patient patient)
 {
     base.AddObject("Patients", patient);
 }
 /// <summary>
 /// Create a new Patient object.
 /// </summary>
 /// <param name="patientId">Initial value of the PatientId property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 /// <param name="dateOfBirth">Initial value of the DateOfBirth property.</param>
 /// <param name="address">Initial value of the Address property.</param>
 /// <param name="telephoneNumber">Initial value of the TelephoneNumber property.</param>
 /// <param name="userId">Initial value of the UserId property.</param>
 public static Patient CreatePatient(global::System.Int32 patientId, global::System.String name, global::System.DateTime dateOfBirth, global::System.String address, global::System.String telephoneNumber, global::System.Guid userId)
 {
     Patient patient = new Patient();
     patient.PatientId = patientId;
     patient.Name = name;
     patient.DateOfBirth = dateOfBirth;
     patient.Address = address;
     patient.TelephoneNumber = telephoneNumber;
     patient.UserId = userId;
     return patient;
 }
        //
        // GET: /RepeatPrescription/
        public ActionResult SaveRepeatPrescription(FormCollection collection)
        {
            var model = new RepeatPrescriptionViewModel();
            this.TryUpdateModel<RepeatPrescriptionModel>(model.model, "model", collection);

            if (!model.model.AuthoriseFlag)
            {
                ModelState.AddModelError("AuthoriseFlag", "Please indicate whether you agree with the t&c's before continuing");
            }

            var count = 0;
            foreach (var item in model.model.RepeatItems)
            {
                if (!string.IsNullOrEmpty(item))
                    count++;
            }

            if (count == 0)
            {
                ModelState.AddModelError("RepeatItems", "Please add at least one repeat item before continuing");
            }
            MembershipUser user = Membership.GetUser(HttpContext.User.Identity.Name);
            Guid userId = (Guid)user.ProviderUserKey;

            var saveModel = model.model;
            if (ModelState.IsValid)
            {
                try
                {


                    var context = new LangleyPharmacyEntities();

                    if (saveModel.PatientId <= 0)
                    {
                        var patient = new Patient();
                        patient.Name = saveModel.Name;
                        patient.DateOfBirth = saveModel.DateOfBirth.Value;
                        patient.Address = saveModel.Address;
                        patient.EmailAddress = saveModel.Email;
                        patient.TelephoneNumber = saveModel.TelephoneNumber;
                        patient.UserId = userId;

                        var patientDetail = new PatientPrescriptionDetail();
                        patientDetail.PatientId = patient.PatientId;
                        patientDetail.SurgeryId = 1;
                        patientDetail.PayForPrescription = saveModel.PayForPerscriptions.Value;
                        patientDetail.ExcemptionReason = saveModel.ExcemptionReason;
                        patientDetail.ExcemptionNumber = saveModel.ExcemptionNumber;
                        patientDetail.FurtherComments = saveModel.FurtherComments;
                        patientDetail.DeliveryMethodId = 1;
                        patientDetail.DeliveryAddress = saveModel.DeliveryAddress;
                        patientDetail.CreatedDate = DateTime.Now;
                        foreach (var item in saveModel.RepeatItems)
                        {
                            if (!string.IsNullOrEmpty(item))
                                patient.PatientPerscriptionDetailsRepeatItems.Add(new PatientPerscriptionDetailsRepeatItem { Item = item });
                        }

                        patient.PatientPrescriptionDetail = patientDetail;
                        context.Patients.AddObject(patient);
                        context.SaveChanges();
                        model.model.PatientId = patient.PatientId;
                        TempData["Action"] = "Create";


                        var errorEmailAddress = ConfigurationManager.AppSettings["RepeatCCEmailAddresses"];
                        var emailBody = this.RenderPartialViewToString("Email", model);
                        Email.SendEmail("*****@*****.**", errorEmailAddress, emailBody, string.Format("Repeat prescription for patient #{0} created", patient.PatientId));
                        return RedirectToAction("Index");

                    }
                    else
                    {
                        var patient = context.Patients.Where(i => i.PatientId == saveModel.PatientId).First();
                        patient.Name = saveModel.Name;
                        patient.DateOfBirth = saveModel.DateOfBirth.Value;
                        patient.Address = saveModel.Address;
                        patient.EmailAddress = saveModel.Email;
                        patient.TelephoneNumber = saveModel.TelephoneNumber;

                        var patientPerscription = patient.PatientPrescriptionDetail;
                        patientPerscription.SurgeryId = 1;
                        patientPerscription.PayForPrescription = saveModel.PayForPerscriptions.Value;
                        patientPerscription.ExcemptionReason = saveModel.ExcemptionReason;
                        patientPerscription.ExcemptionNumber = saveModel.ExcemptionNumber;
                        patientPerscription.FurtherComments = saveModel.FurtherComments;
                        patientPerscription.DeliveryMethodId = 1;
                        patientPerscription.DeliveryAddress = saveModel.DeliveryAddress;
                        patientPerscription.ModifiedDate = DateTime.Now;
                        patient.PatientPerscriptionDetailsRepeatItems.ToList().ForEach(x => context.PatientPerscriptionDetailsRepeatItems.DeleteObject(x));
                        foreach (var item in saveModel.RepeatItems)
                        {
                            if (!string.IsNullOrEmpty(item))
                                patient.PatientPerscriptionDetailsRepeatItems.Add(new PatientPerscriptionDetailsRepeatItem { Item = item });
                        }
                        context.SaveChanges();
                        TempData["Action"] = "Edit";

                        var errorEmailAddress = ConfigurationManager.AppSettings["RepeatCCEmailAddresses"];
                        var emailBody = this.RenderPartialViewToString("Email", model);
                        Email.SendEmail("*****@*****.**", errorEmailAddress, emailBody, string.Format("Repeat prescription for patient #{0} updated", patient.PatientId));
                    }
                }
                catch (Exception ex)
                {
                    var s = ex.Message;
                    ExceptionHelper.SendExceptionEmail(ex);
                }
            }
            return View("Index", model);
        }