public ActionResult Create(int patientId)
        {
            var patient = db.Patients.Find(patientId);

            var model = new CreatePrescriptionModel
            {
                PatientID           = patientId,
                Patient             = patient,
                PrescriptionDetails = db.PrescriptionDetails.Where(d => d.PatientId == patientId &&
                                                                   d.PrescriptionId == null),
                ProductsDropdown = new SelectList(db.Products.ToList(), "Id", "Name"),
                MedicineName     = "",
                Instructions     = "",
                PackSize         = ""
            };

            return(View(model));
        }
        public async Task <ActionResult> AddDetail(CreatePrescriptionModel model)
        {
            if (!ModelState.IsValid)
            {
                model.PatientID           = model.PatientID;
                model.Patient             = db.Patients.Where(p => p.UserID == model.PatientID).FirstOrDefault();
                model.PrescriptionDetails = db.PrescriptionDetails.Where(d => d.PatientId == model.PatientID &&
                                                                         d.PrescriptionId == null);
                model.ProductsDropdown = new SelectList(db.Products.ToList(), "Id", "Name");

                return(View("Create", model));
            }

            db.PrescriptionDetails.Add(new PrescriptionDetail
            {
                Instructions = model.Instructions,
                MedicineName = model.MedicineName,
                PackSize     = model.PackSize,
                Patient      = db.Patients.Where(p => p.UserID == model.PatientID).FirstOrDefault(),
                PatientId    = model.PatientID
            });

            await db.SaveChangesAsync();

            model.PatientID           = model.PatientID;
            model.Patient             = db.Patients.Where(p => p.UserID == model.PatientID).FirstOrDefault();
            model.PrescriptionDetails = db.PrescriptionDetails.Where(d => d.PatientId == model.PatientID &&
                                                                     d.PrescriptionId == null);
            model.ProductsDropdown = new SelectList(db.Products.ToList(), "Id", "Name");

            model.Instructions = "";
            model.MedicineName = "";
            model.PackSize     = "";

            return(View("Create", model));
        }
        public async Task <string> Complete(CreatePrescriptionModel model)
        {
            if (!ModelState.IsValid || model.ExpiryDate < DateTime.Today)
            {
                model.PatientID           = model.PatientID;
                model.Patient             = db.Patients.Where(p => p.UserID == model.PatientID).FirstOrDefault();
                model.PrescriptionDetails = db.PrescriptionDetails.Where(d => d.PatientId == model.PatientID &&
                                                                         d.PrescriptionId == null);
                model.ProductsDropdown = new SelectList(db.Products.ToList(), "Id", "Name");

                return("Failed");
            }

            // Barcode
            // Generate a new random GUID using System.Guid class
            Guid   barcodeGuid = Guid.NewGuid();
            string barcode     = barcodeGuid.ToString();

            // Saving the signature as image
            //string signaturePath;
            //MemoryStream ms = new MemoryStream(model.Signature);
            //Image returnImage = Image.FromStream(ms);
            //returnImage.Save(Server.MapPath("~/Files/Signatures/" + barcode + ".png"), System.Drawing.Imaging.ImageFormat.Png);
            //signaturePath = "/Files/Signatures/" + barcode + ".png";

            /*
             * Generate Barcode
             */

            // Save Barcode to that path
            //string _barcodePath = Path.Combine(Server
            //    .MapPath("~/Files/Barcodes/Prescriptions"), barcode + ".png");

            //// Generate a Simple BarCode image and save as PNG
            ////using IronBarCode;
            //GeneratedBarcode MyBarCode = BarcodeWriter
            //    .CreateBarcode(barcode,
            //    BarcodeWriterEncoding.Code128);

            //MyBarCode.SetMargins(25);

            //MyBarCode.SaveAsPng(_barcodePath);

            //string _barcodeUrl = "/Files/Barcodes/Prescriptions/" + barcode + ".png";
            string _barcodeUrl = "/";


            // Get list of prescription details
            var details = db.PrescriptionDetails.Where(d => d.PatientId == model.PatientID &&
                                                       d.PrescriptionId == null).ToList();

            // Patient
            var patient = db.Patients.Find(model.PatientID);

            db.Prescriptions.Add(new Prescription
            {
                Barcode             = barcode,
                BarcodeUrl          = _barcodeUrl,
                DateIssued          = DateTime.Now,
                Patient             = patient,
                PatientID           = model.PatientID,
                PrescriptionDetails = details,
                PrescriptionValid   = model.ExpiryDate,
                SignatureUrl        = "/",
                Signature           = model.Signature
            });

            await db.SaveChangesAsync();

            //var lastPrescription = await db.Prescriptions.OrderByDescending(s => s.Id)
            //    .FirstOrDefaultAsync();

            return(barcode);

            //return RedirectToAction("PrescriptionToPdf", new { id = lastPrescription.Id });
        }