public void TestExtendSuccess()
        {
            // Arrange
            AdminController ac = new AdminController();
            bool expected = true;

            Prescription prescription = new Prescription();
            prescription.PrescriptionID = 2;
            prescription.PatientID = 1;
            prescription.StaffID = 1;
            prescription.StartDate = new DateTime(2013, 01, 01);
            prescription.EndDate = new DateTime(2013, 02, 01);
            prescription.Extended = false;
            prescription.Medicines = new List<Medicine>();
            prescription.Medicines.Add(new Medicine());
            prescription.Medicines[0].MedicineID = 2;
            prescription.Medicines[0].MedicineName = "Thyroxine";
            prescription.Medicines[0].Dosage = "50mg";
            prescription.Medicines[0].Extendable = true;

            // Act
            bool actual = ac.ExtendPrescription(prescription);

            // Assert
            Assert.AreEqual(expected, actual);
        }
 private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
 {
     if (e.RowIndex > -1)
     {
         _prescription = _patient.Prescriptions[e.RowIndex];
         RefreshForm();
     }
 }
        // Extend an existing prescription by 30 days if possible
        public bool ExtendPrescription(Prescription prescription)
        {
            if (!prescription.Extended)
            {
                return BusinessMetaLayer.ExtendPrescription(prescription);
            }

            return false;
        }
        /* Remove a test - not used
        public bool RemoveTest(Test test)
        {
            return BusinessMetaLayer.RemoveTest(test);
        }*/
        // Add a new prescription for a patient based on user entered data
        public bool AddPrescription(int patientID, int staffID, string startDate, string endDate, List<Medicine> medicines)
        {
            DateTime start, end;

            if (DateTime.TryParse(startDate, out start) && DateTime.TryParse(endDate, out end))
            {
                Prescription prescription = new Prescription();
                prescription.PatientID = patientID;
                prescription.StaffID = staffID;
                prescription.StartDate = start;
                prescription.EndDate = end;
                prescription.Medicines = medicines;
                prescription.Extended = false;

                return BusinessMetaLayer.AddPrescription(prescription);
            }
            return false;
        }
        private void prescriptionGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                _prescription = _patient.Prescriptions[e.RowIndex];

                startDateTxt.Text = _prescription.StartDate.ToShortDateString();
                endDateTxt.Text = _prescription.EndDate.ToShortDateString();

                BindingSource medSource = new BindingSource();
                medSource.DataSource = _prescription.Medicines;

                medicineLstBx.DisplayMember = "MedicineDetails";
                medicineLstBx.DataSource = medSource;

            }
        }
        // Return all prescriptions for a specific patient
        public static List<Prescription> GetPatientPrescriptions(int patientID)
        {
            Database db = Database.Instance();
            List<Prescription> _prescriptions = new List<Prescription>();

            if (db.OpenConnection())
            {
                String query;

                query = "SELECT * FROM prescriptions p " +
                        "WHERE p.patientID=" + patientID + ";";

                DbDataReader dr = db.Select(query);

                // Create the staff data
                // Read the data and store them in the list
                while (dr.Read())
                {
                    Prescription newPrescription = new Prescription();
                    newPrescription.PrescriptionID = dr.GetInt32(0);
                    newPrescription.PatientID = dr.GetInt32(1);
                    newPrescription.StaffID = dr.GetInt32(2);
                    newPrescription.StartDate = dr.GetDateTime(3);
                    newPrescription.EndDate = dr.GetDateTime(4);
                    newPrescription.Extended = Convert.ToBoolean(dr.GetInt32(5));
                    _prescriptions.Add(newPrescription);
                }
                dr.Close();

                foreach (Prescription p in _prescriptions)
                {
                    p.Medicines = GetPrescriptionMedicines(p.PrescriptionID);
                }

                db.CloseConnection();
            }

            return _prescriptions;
        }
        // Extend the duration of a current prescription
        public static bool ExtendPrescription(Prescription prescription)
        {
            Database db = Database.Instance();

            if (db.OpenConnection())
            {
                // Check if the test doesn't already exist
                if (PrescriptionExists(prescription))
                {
                    String newPrescriptionQuery;

                    // Format the test date
                    String sqlFormattedEndDate = prescription.EndDate.AddDays(30).ToString("yyyy-MM-dd HH:mm:ss");

                    // Create the query string to be inserted
                    newPrescriptionQuery = "UPDATE prescriptions SET endDate='" + sqlFormattedEndDate +"', extended=1 " +
                    "WHERE prescriptionID=" + prescription.PrescriptionID + ";";

                    // Insert the entry into the database
                    db.Update(newPrescriptionQuery);

                    db.CloseConnection();
                    return true;
                }
                db.CloseConnection();
            }
            return false;
        }
        // Attempt to add a new test to the database
        //public static bool RemoveTest(Test test)
        //{
        //    Database db = Database.Instance();
        //    if (db.OpenConnection())
        //    {
        //        // Check if the test doesn't already exist
        //        if (TestExists(test))
        //        {
        //            String removeTestQuery;
        //            String sqlFormattedDate = test.TestDate.Date.ToString("yyyy-MM-dd HH:mm:ss");
        //            // Create the query string to be inserted
        //            removeTestQuery = "DELETE FROM testresults WHERE patientID='" + test.PatientID +
        //            "' and staffID='" + test.StaffID +
        //            "' and testDate='" + sqlFormattedDate +
        //            "' and testType='" + (int) test.TestType +
        //            "' and testResult='" + test.TestResult + "';";
        //            // Insert the entry into the database
        //            db.Delete(removeTestQuery);
        //            db.CloseConnection();
        //            return true;
        //        }
        //        db.CloseConnection();
        //    }
        //    return false;
        //}
        // Add a new prescription to a patient
        public static bool AddPrescription(Prescription prescription)
        {
            Database db = Database.Instance();

            if (db.OpenConnection())
            {
                // Check if the test doesn't already exist
                if (!PrescriptionExists(prescription))
                {
                    String newPrescriptionQuery;

                    // Format the test date
                    String sqlFormattedStartDate = prescription.StartDate.ToString("yyyy-MM-dd HH:mm:ss");
                    String sqlFormattedEndDate = prescription.EndDate.ToString("yyyy-MM-dd HH:mm:ss");

                    // Create the query string to be inserted
                    newPrescriptionQuery = "INSERT INTO prescriptions VALUES(NULL, '" + prescription.PatientID + "'," +
                    "'" + prescription.StaffID + "'," +
                    "'" + sqlFormattedStartDate + "'," +
                    "'" + sqlFormattedEndDate + "'," +
                    "'" + Convert.ToInt32(prescription.Extended) + "'); SELECT @@IDENTITY;";

                    // Insert the entry into the database
                    int prescriptionID = db.InsertScalar(newPrescriptionQuery);

                    foreach (Medicine m in prescription.Medicines)
                    {
                        // Insert the entry into the login table with the new staffID
                        String addMedicinesQuery = "INSERT INTO prescriptions_medicine VALUES(NULL, " + prescriptionID + ",'" + m.MedicineID + "');";
                        db.Insert(addMedicinesQuery);
                    }

                    db.CloseConnection();
                    return true;
                }
                db.CloseConnection();
            }
            return false;
        }
        // Check if a prescription already exists
        public static bool PrescriptionExists(Prescription prescription)
        {
            Database db = Database.Instance();

            // Format the test date
            String sqlFormattedStartDate = prescription.StartDate.ToString("yyyy-MM-dd HH:mm:ss");
            String sqlFormattedEndDate = prescription.EndDate.ToString("yyyy-MM-dd HH:mm:ss");

            // Check the number of rows that are returned
            int numRows = db.Count("SELECT COUNT(*) FROM prescriptions p " +
                "WHERE p.patientID='" + prescription.PatientID +
                "' and p.staffID='" + prescription.StaffID +
                "' and p.startDate='" + sqlFormattedStartDate +
                "' and p.endDate='" + sqlFormattedEndDate +
                "' and p.extended=" + Convert.ToInt32(prescription.Extended) + ";");

            // If a test exists and there's one row
            if (numRows > 0)
            {
                // The test exists already
                return true;
            }

            return false;
        }