public async Task <IActionResult> UpdateStock(string newStock, int drugId) { int numNewDrugs = 0; if (!string.IsNullOrEmpty(newStock)) { try { numNewDrugs = int.Parse(newStock); } catch { HttpContext.Session.SetString(HomeController.DrugCountValidation, "Must be a positive integer"); return(RedirectToAction("Details", new { id = drugId })); } } if (numNewDrugs <= 0) { HttpContext.Session.SetString(HomeController.DrugCountValidation, "Must be a positive integer"); return(RedirectToAction("Details", new { id = drugId })); } HttpContext.Session.SetString(HomeController.DrugCountValidation, ""); Drug drug = await _context.Drugs.FirstAsync(d => d.Id == drugId); drug.Stock += numNewDrugs; _context.Drugs.Update(drug); _context.SaveChanges(); return(RedirectToAction("Details", new { id = drugId })); }
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)); }
private void SaveCompensationRecord(CompensationRecord record) { _ctx.CompensationRecords.Add(record); _ctx.SaveChanges(); }