public static ReceiptViewModel GetViewModel(Receipt receipt, Func<DateTime, DateTime> toLocal)
        {
            if (receipt == null)
                return new ReceiptViewModel();

            return new ReceiptViewModel
                {
                    Id = receipt.Id,
                    PatientId = receipt.PatientId, // expected to be null here
                    IssuanceDate = toLocal(receipt.IssuanceDate),
                    PrescriptionMedicines = (from rm in receipt.ReceiptMedicines
                                        select new ReceiptMedicineViewModel()
                                            {
                                                Id = rm.Id,
                                                MedicineId = rm.Medicine.Id,
                                                MedicineText = rm.Medicine.Name,
                                                Observations = rm.Observations,
                                                Prescription = rm.Prescription,
                                                Quantity = rm.Quantity
                                            }).ToList()
                };
        }
        public void Delete_WhenTheresAReceipt()
        {
            PatientsController controller;
            int patientId;
            Patient patient;

            try
            {
                var doctor = Firestarter.Create_CrmMg_Psiquiatria_DrHouse_Andre(this.db);
                var mr = new MockRepository(true);
                controller = mr.CreateController<PatientsController>();
                Firestarter.CreateFakePatients(doctor, this.db, 1);

                // we now have 1 patient
                patient = this.db.Patients.FirstOrDefault();
                Assert.IsNotNull(patient);

                patientId = patient.Id;

                var medicine = new Medicine()
                    {
                        Laboratory = new Laboratory()
                        {
                            Name = "Lab1",
                            Doctor = doctor
                        },
                        Name = "Med1",
                        Doctor = doctor,
                        PracticeId = doctor.PracticeId,
                    };

                medicine.ActiveIngredients.Add(new MedicineActiveIngredient()
                    {
                        Name = "AI1",
                        PracticeId = doctor.PracticeId,
                    });

                this.db.Medicines.AddObject(medicine);

                // now, let's add an receipt
                var receipt = new Receipt()
                {
                    PatientId = patientId,
                    CreatedOn = DateTime.UtcNow,
                    PracticeId = doctor.PracticeId,
                };

                receipt.ReceiptMedicines.Add(new ReceiptMedicine()
                    {
                        Medicine = medicine,
                        Quantity = "1 caixa",
                        Prescription = "toma 1 de manha",
                        PracticeId = doctor.PracticeId,
                    });

                this.db.Receipts.AddObject(receipt);

                this.db.SaveChanges();
            }
            catch
            {
                Assert.Inconclusive("Test initialization has failed.");
                return;
            }

            controller.Delete(patientId);

            // this patient must have been deleted
            patient = this.db.Patients.FirstOrDefault(p => p.Id == patientId);
            Assert.IsNull(patient);
        }
 /// <summary>
 /// Create a new Receipt object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="patientId">Initial value of the PatientId property.</param>
 /// <param name="createdOn">Initial value of the CreatedOn property.</param>
 /// <param name="practiceId">Initial value of the PracticeId property.</param>
 /// <param name="issuanceDate">Initial value of the IssuanceDate property.</param>
 public static Receipt CreateReceipt(global::System.Int32 id, global::System.Int32 patientId, global::System.DateTime createdOn, global::System.Int32 practiceId, global::System.DateTime issuanceDate)
 {
     Receipt receipt = new Receipt();
     receipt.Id = id;
     receipt.PatientId = patientId;
     receipt.CreatedOn = createdOn;
     receipt.PracticeId = practiceId;
     receipt.IssuanceDate = issuanceDate;
     return receipt;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Receipts EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToReceipts(Receipt receipt)
 {
     base.AddObject("Receipts", receipt);
 }
        public ActionResult Edit(ReceiptViewModel[] receipts)
        {
            var formModel = receipts.Single();

            Receipt receipt;

            if (formModel.PrescriptionMedicines == null)
                this.ModelState.AddModelError("Medicines", "A receita deve ter pelo menos um medicamento");

            // we cannot trust that the autocomplete has removed incorrect
            // value from the client.
            for (var i = 0; i < formModel.PrescriptionMedicines.Count; i++)
            {
                var medication = formModel.PrescriptionMedicines[i];
                if (medication.MedicineId != null)
                    continue;
                // it's necessary to remove this value from the ModelState to
                // prevent it from "reappearing"
                // http://stackoverflow.com/questions/9163445/my-html-editors-are-ignoring-any-value-i-set-and-are-always-taking-their-values
                this.ModelState.Remove(string.Format("ReceiptMedicines[{0}].MedicineId", i));
                medication.MedicineText = string.Empty;
            }

            if (formModel.Id == null)
            {
                Debug.Assert(formModel.PatientId != null, "formModel.PatientId != null");
                receipt = new Receipt()
                {
                    CreatedOn = this.GetUtcNow(),
                    PatientId = formModel.PatientId.Value,
                    PracticeId = this.DbUser.PracticeId,
                };
                this.db.Receipts.AddObject(receipt);
            }
            else
                receipt = this.db.Receipts.FirstOrDefault(r => r.Id == formModel.Id);

            if (formModel.PrescriptionMedicines.Count == 0)
            {
                this.ModelState.AddModelError(
                    () => formModel.PrescriptionMedicines,
                    "Não é possível criar uma receita sem medicamentos.");
            }

            if (this.ModelState.IsValid)
            {
                // Updating receipt medicines. This is only possible when view-model is valid,
                // otherwise this is going to throw exceptions.
                Debug.Assert(receipt != null, "receipt != null");
                receipt.ReceiptMedicines.Update(
                        formModel.PrescriptionMedicines,
                        (vm, m) => vm.Id == m.Id,
                        (vm, m) =>
                        {
                            m.PracticeId = this.DbPractice.Id;
                            m.MedicineId = vm.MedicineId.Value;
                            m.Quantity = vm.Quantity;
                            m.Observations = vm.Observations;
                            m.Prescription = vm.Prescription;
                        },
                        m => this.db.ReceiptMedicines.DeleteObject(m)
                    );

                receipt.Patient.IsBackedUp = false;
                receipt.IssuanceDate = this.ConvertToUtcDateTime(formModel.IssuanceDate.Value);
                this.db.SaveChanges();

                return this.View("Details", GetViewModel(receipt, this.GetToLocalDateTimeConverter()));
            }

            return this.View("Edit", formModel);
        }