void CheckMedicationPackageStatus(MedicationPackage package, PackageStatus status) { if (package.Status != status) { throw new ENETCareException(Properties.Resources.IncorrectPackageStatus); } }
/// <summary> /// Retrieves medication packages of given medication type at a given distribution centre. /// </summary> /// <param name="distributionCentreId">distribution centre id</param> /// <param name="medicationTypeId">medication type id</param> /// <returns>a list of the medication packages</returns> public List <MedicationPackage> FindInStockPackagesInDistributionCentre(int distributionCentreId, int?medicationTypeId = null) { List <MedicationPackage> packageList = new List <MedicationPackage>(); using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); string whereClause = "where Status = @status and StockDC = @stockdc"; if (medicationTypeId != null) { whereClause += " and Type = @type"; } string query = string.Format("{0} {1} {2}", selectStatement, fromClause, whereClause); SqlCommand command = new SqlCommand(query, conn); command.Parameters.Add(new SqlParameter("status", PackageStatus.InStock)); command.Parameters.Add(new SqlParameter("stockdc", distributionCentreId)); if (medicationTypeId != null) { command.Parameters.Add(new SqlParameter("type", medicationTypeId)); } using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { MedicationPackage package = GetMedicationPackageFromDataReader(reader); packageList.Add(package); } } } return(packageList); }
void CheckMedicationPackageIsValid(MedicationPackage package) { if (package == null) { throw new ENETCareException(Properties.Resources.MedicationPackageNotFound); } }
void CheckDestinationDistributionCentre(MedicationPackage package) { if (package.DestinationDCId != User.DistributionCentreId) { throw new ENETCareException(Properties.Resources.IncorrectDistributionCentreDestination); } }
/// <summary> /// Manipulate package updating its status and related distribution centres. /// </summary> /// <param name="package">medication package</param> /// <param name="status">package status </param> /// <param name="stockDC">stock distribution centre</param> /// <param name="sourceDC">source distribution centre</param> /// <param name="destinationDC">destination distribution centre</param> void ManipulatePackage(MedicationPackage package, PackageStatus status, int?stockDC, int?sourceDC, int?destinationDC) { package.Status = status; package.StockDCId = stockDC; package.SourceDCId = sourceDC; package.DestinationDCId = destinationDC; package.Operator = User.Username; package.UpdateTime = TimeProvider.Current.Now; }
/// <summary> /// Updates a medication package record in the database. /// </summary> /// <param name="package">medication package</param> public void UpdatePackage(MedicationPackage package) { var currentPackage = context.MedicationPackage.SingleOrDefault(x => x.ID == package.ID); if (currentPackage != null) { context.Entry(currentPackage).CurrentValues.SetValues(package); context.SaveChanges(); } }
/// <summary> /// Deletes a medication package record from the database corresponding to the package id. /// </summary> /// <param name="package">medication package</param> public void DeletePackage(MedicationPackage package) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); string query = @"delete from MedicationPackage where ID = @id" ; SqlCommand command = new SqlCommand(query, conn); command.Parameters.Add(new SqlParameter("id", package.ID)); command.ExecuteNonQuery(); } }
/// <summary> /// Discards a medication package. /// </summary> /// <param name="barcode">package barcode</param> /// <param name="isTrusted">whether to trust the source of the package</param> public void DiscardPackage(string barcode, bool isTrusted = true) { MedicationPackage package = ScanPackage(barcode); CheckMedicationPackageIsValid(package); if (!isTrusted) { CheckMedicationPackageStatus(package, PackageStatus.InStock); CheckStockDistributionCentre(package); } ManipulatePackage(package, PackageStatus.Discarded, User.DistributionCentreId, null, null); MedicationPackageDAO.UpdatePackage(package); }
/// <summary> /// Receives a medication package. /// </summary> /// <param name="barcode">package barcode</param> /// <param name="isTrusted">whether to trust the source of the package</param> public void ReceivePackage(string barcode, bool isTrusted = true) { MedicationPackage package = ScanPackage(barcode); CheckMedicationPackageIsValid(package); if (!isTrusted) { CheckMedicationPackageStatus(package, PackageStatus.InTransit); CheckDestinationDistributionCentre(package); } ManipulatePackage(package, PackageStatus.InStock, User.DistributionCentreId, null, null); MedicationPackageDAO.UpdatePackage(package); }
string RegisterPackage(MedicationType medicationType, DateTime expireDate, string barcode) { MedicationPackage package = new MedicationPackage(); if (string.IsNullOrEmpty(barcode)) { barcode = BarcodeHelper.GenerateBarcode(); } package.Barcode = barcode; package.TypeId = medicationType.ID; package.ExpireDate = expireDate; ManipulatePackage(package, PackageStatus.InStock, User.DistributionCentreId, null, null); MedicationPackageDAO.InsertPackage(package); return(barcode); }
/// <summary> /// Inserts a medication package record into the database. /// </summary> /// <param name="package">medication package</param> public void InsertPackage(MedicationPackage package) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); string query = @"insert into MedicationPackage (Barcode, Type, ExpireDate, Status, StockDC, Operator, UpdateTime) values (@barcode, @type, @expiredate, @status, @stockdc, @operator, getdate())" ; SqlCommand command = new SqlCommand(query, conn); command.Parameters.Add(new SqlParameter("barcode", package.Barcode)); command.Parameters.Add(new SqlParameter("type", package.Type != null ? package.Type.ID : package.TypeId)); command.Parameters.Add(new SqlParameter("expiredate", package.ExpireDate)); command.Parameters.Add(new SqlParameter("status", package.Status)); command.Parameters.Add(new SqlParameter("stockdc", package.StockDCId ?? package.StockDC.ID)); command.Parameters.Add(new SqlParameter("operator", package.Operator)); command.ExecuteNonQuery(); } }
/// <summary> /// Helper-method to create a medication package for a row of the database. /// </summary> /// <param name="reader"></param> /// <returns></returns> MedicationPackage GetMedicationPackageFromDataReader(SqlDataReader reader) { MedicationPackage package = new MedicationPackage(); package.ID = reader.GetInt32(0); package.Barcode = reader.GetString(1); package.TypeId = reader.GetInt32(2); package.Type = GetMedicationTypeByID(reader.GetInt32(2)); package.ExpireDate = reader.GetDateTime(3); package.Status = (PackageStatus)reader.GetInt16(4); if (reader.IsDBNull(5)) { package.StockDCId = null; package.StockDC = null; } else { package.StockDCId = reader.GetInt32(5); package.StockDC = GetDistributionCentreByID(reader.GetInt32(5)); } if (reader.IsDBNull(6)) { package.SourceDCId = null; package.SourceDC = null; } else { package.SourceDCId = reader.GetInt32(6); package.SourceDC = GetDistributionCentreByID(reader.GetInt32(6)); } if (reader.IsDBNull(7)) { package.DestinationDCId = null; package.DestinationDC = null; } else { package.DestinationDCId = reader.GetInt32(7); package.DestinationDC = GetDistributionCentreByID(reader.GetInt32(7)); } package.Operator = reader.GetString(8); return(package); }
/// <summary> /// Updates a medication package record in the database corresponding to the package id. /// </summary> /// <param name="package">medication package</param> public void UpdatePackage(MedicationPackage package) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); string query = @"update MedicationPackage set Status = @status, StockDC = @stockdc, SourceDC = @sourcedc, DestinationDC = @destinationdc, Operator = @operator, UpdateTime = getdate() where ID = @id" ; SqlCommand command = new SqlCommand(query, conn); command.Parameters.Add(new SqlParameter("id", package.ID)); command.Parameters.Add(new SqlParameter("status", package.Status)); command.Parameters.Add(new SqlParameter("stockdc", package.StockDCId ?? (package.StockDC != null ? (object)package.StockDC.ID : DBNull.Value))); command.Parameters.Add(new SqlParameter("sourcedc", package.SourceDCId ?? (package.SourceDC != null ? (object)package.SourceDC.ID : DBNull.Value))); command.Parameters.Add(new SqlParameter("destinationdc", package.DestinationDCId ?? (package.DestinationDC != null ? (object)package.DestinationDC.ID : DBNull.Value))); command.Parameters.Add(new SqlParameter("operator", package.Operator)); command.ExecuteNonQuery(); } }
/// <summary> /// Sends a medication package. /// </summary> /// <param name="barcode">package barcode</param> /// <param name="distributionCentreId">distribution centre id</param> /// <param name="isTrusted">whether to trust the source of the package</param> public void SendPackage(string barcode, int distributionCentreId, bool isTrusted = true) { DistributionCentre distributionCentre = DistributionCentreDAO.GetDistributionCentreById(distributionCentreId); CheckDistributionCentreIsValid(distributionCentre); MedicationPackage package = ScanPackage(barcode); CheckMedicationPackageIsValid(package); if (User.DistributionCentreId == distributionCentreId) { throw new ENETCareException(Properties.Resources.AnotherDistributionCentre); } if (!isTrusted) { CheckMedicationPackageStatus(package, PackageStatus.InStock); CheckStockDistributionCentre(package); } ManipulatePackage(package, PackageStatus.InTransit, null, User.DistributionCentreId, distributionCentreId); MedicationPackageDAO.UpdatePackage(package); }
/// <summary> /// Retrieves all medication packages in the database. /// </summary> /// <returns>a list of all the medication packages</returns> public List <MedicationPackage> FindAllPackages() { List <MedicationPackage> packageList = new List <MedicationPackage>(); using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); string query = string.Format("{0} {1}", selectStatement, fromClause); SqlCommand command = new SqlCommand(query, conn); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { MedicationPackage package = GetMedicationPackageFromDataReader(reader); packageList.Add(package); } } } return(packageList); }
/// <summary> /// Check whether a package is unexpected and has its status/location updated /// </summary> /// <param name="medicationTypeId">medication type id</param> /// <param name="barcode">package barcode</param> /// <returns>true if status/location of the package has been updated, or false if not</returns> public bool CheckAndUpdatePackage(int medicationTypeId, string barcode) { bool updated = false; MedicationPackage package = MedicationPackageDAO.FindPackageByBarcode(barcode); if (package == null) { MedicationType medicationType = MedicationTypeDAO.GetMedicationTypeById(medicationTypeId); DateTime expireDate = medicationType.DefaultExpireDate; RegisterPackage(medicationType, expireDate, barcode); updated = true; } else if (package.TypeId != medicationTypeId) { throw new ENETCareException(Properties.Resources.MedicationTypeNotMatched); } else if (package.Status != PackageStatus.InStock || package.StockDCId != User.DistributionCentreId) { ManipulatePackage(package, PackageStatus.InStock, User.DistributionCentreId, null, null); MedicationPackageDAO.UpdatePackage(package); updated = true; } return(updated); }
/// <summary> /// Inserts a medication package record into the database. /// </summary> /// <param name="package">medication package</param> public void InsertPackage(MedicationPackage package) { context.MedicationPackage.Add(package); context.SaveChanges(); }
/// <summary> /// Deletes a medication package record from the database. /// </summary> /// <param name="package">medication package</param> public void DeletePackage(MedicationPackage package) { context.MedicationPackage.Attach(package); context.MedicationPackage.Remove(package); context.SaveChanges(); }