// GET: Prescriptions/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var prescription = await _prescriptionContext.Prescriptions.FirstAsync(m => m.Id == id);

            if (prescription == null)
            {
                return(NotFound());
            }

            List <PrescribedDrug> prescribedDrugs = _prescriptionContext.PrescribedDrugs.Where(p => p.PrescriptionId == id).ToList();

            foreach (PrescribedDrug pd in prescribedDrugs)
            {
                pd.CurrentDrug = _drugContext.Drugs.First(d => d.Id == pd.DrugId);
            }

            PrescriptionDetailsViewModel vm = new PrescriptionDetailsViewModel {
                CurrentPrescription = prescription, PrescribedDrugs = prescribedDrugs
            };

            return(View(vm));
        }
        public ActionResult ViewBill(int id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var prescription = _prescriptionContext.Prescriptions.First(m => m.Id == id);

            if (prescription == null)
            {
                return(NotFound());
            }

            List <PrescribedDrug> prescribedDrugs = _prescriptionContext.PrescribedDrugs.Where(p => p.PrescriptionId == id).ToList();

            if (prescription.BillCreated == null)
            {
                foreach (PrescribedDrug pd in prescribedDrugs)
                {
                    Drug currentDrug = _drugContext.Drugs.First(d => d.Id == pd.DrugId);
                    if (currentDrug.Stock < pd.Count)
                    {
                        HttpContext.Session.SetString(HomeController.PrescriptionFillValidation, "Not enough " + currentDrug.MedicalName);
                        return(RedirectToAction("Details", new { id = id }));
                    }
                }
                // If here, must have enough of all drugs
                foreach (PrescribedDrug pd in prescribedDrugs)
                {
                    Drug currentDrug = _drugContext.Drugs.First(d => d.Id == pd.DrugId);
                    currentDrug.Stock -= pd.Count;
                    _drugContext.Drugs.Update(currentDrug);
                    _drugContext.SaveChanges();
                }
                prescription.BillCreated = DateTime.Now;
                _prescriptionContext.Prescriptions.Update(prescription);
                _prescriptionContext.SaveChanges();
            }
            double costs = 0;

            foreach (PrescribedDrug pd in prescribedDrugs)
            {
                pd.CurrentDrug = _drugContext.Drugs.First(d => d.Id == pd.DrugId);
                pd.TotalCost   = pd.CurrentDrug.CostPer * pd.Count;
                pd.Remaining   = pd.TotalCost - pd.CoveredAmount;
                costs         += pd.Remaining;
            }
            prescription.SubtotalCost = costs;
            prescription.TaxCost      = Math.Round(costs * 0.055f, 2);
            prescription.TotalCost    = prescription.SubtotalCost + prescription.TaxCost;

            PrescriptionDetailsViewModel vm = new PrescriptionDetailsViewModel {
                CurrentPrescription = prescription, PrescribedDrugs = prescribedDrugs
            };

            return(View(vm));
        }
Example #3
0
        // GET: Prescriptions/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var prescription = await _context.Prescription
                               .Include(p => p.Dependent)
                               .Include(p => p.Doctor)
                               .SingleOrDefaultAsync(m => m.PrescriptionId == id);

            if (prescription == null)
            {
                return(NotFound());
            }
            var model = new PrescriptionDetailsViewModel();

            model.Prescription = prescription;
            return(View(model));
        }