/// <summary> /// Process employees. Some come, some go. /// </summary> /// <param name="connection">DB Connection</param> private Boolean ProcessEmployees(SqlConnection connection, Random r, TextBox txtResults, String description, int amount) { txtResults.AppendText(Environment.NewLine + description); SqlDataReader reader = null; SqlCommand cmd = new SqlCommand(); cmd.CommandText = "Select * from vStoresNotClosedForever"; cmd.CommandType = CommandType.Text; cmd.Connection = connection; int storeID; // Add a random employee for each store that is not closed forever try { reader = cmd.ExecuteReader(); while (reader.Read()) { storeID = Convert.ToInt32(reader.GetValue(0)); Empl.AddRandomEmpl(storeID, r, connection); } reader.Close(); Empl.RandomizeWorkStatusForAllEmployees(connection, r, Write); } catch (Exception ex) { Console.WriteLine("AddTransactions.ProcessEmployees(): " + ex.Message); } finally { try { reader.Close(); } catch (Exception ex) { Utils.Log(ex.Message); } } return(true); }
/// <summary> /// Compute random record IDs for the random transaction we will add /// </summary> /// <param name="tp">The set of transaction parameters upon which to operate</param> private void computeRandomValuesForTransaction(TransactionParameters tp, Random r) { try { // Date/time of transaction if (Config.useCurrentDateStampForTransaction) { tp.dateOfTransaction = DateTime.Now.ToShortDateString(); tp.timeOftransaction = DateTime.Now.ToLongTimeString(); } else { tp.dateOfTransaction = Utils.GetRandomDate(r); tp.timeOftransaction = Utils.GetRandomTime(r); } tp.emplID = 0; tp.storeID = Utils.GetRandomOpenStoreID(r, DefaultValues.storeID_count, tp.dateOfTransaction + " " + tp.timeOftransaction); // Choose from only stores that are open for business while (true) { int emplID; // Pick a random employee that is available for work at the storeID try { Object tmp; tmp = Utils.MyDLookup("EmplID", "SELECT TOP 1 EmplID FROM [fEmplWhoCanWorkOnASpecificDateAtASpecificStore](" + Utils.QuoteMeForSQL(tp.dateOfTransaction) + " ," + tp.storeID + ") ORDER BY NEWID()", "", ""); if (tmp != null) { emplID = (int)tmp; } else { emplID = 0; } // SELECT top 1 * FROM [fEmplWhoCanWorkOnASpecificDateAtASpecificStore]('1/1/2016', 2) order by newID() } catch (Exception ex) { emplID = 0; Utils.Log("AddTransactions.computeRandomValuesForTransaction(): " + ex.Message); } // emplID = (int)Utils.MyDLookup("EmplID", "(SELECT ROW_NUMBER() OVER (ORDER BY emplID) AS RowNum, * FROM tEmpl) sub ", " RowNum = " + (r.Next(DefaultValues.emplID_count) + 1), ""); if (emplID > 0) { tp.emplID = emplID; break; } else { Write("### It appears that no employees are available for work. Adding one... ###"); emplID = Empl.AddRandomEmplAndMakeThemAvailableForWork(tp.storeID, DateTime.Parse(tp.dateOfTransaction), connection, r); tp.emplID = emplID; break; } } if (tp.emplID == 0) // Uh oh. No employees available for work at this store... // todo: something here { } tp.loyaltyID = (int)Utils.MyDLookup("loyaltyID", "(SELECT ROW_NUMBER() OVER (ORDER BY loyaltyID) AS RowNum, * FROM tloyalty) sub ", " RowNum = " + (r.Next(DefaultValues.loyaltyID_count) + 1), ""); // A high percentage of transactions should be purchases. int selector = r.Next(100) + 1; // 1 to 100 if (selector >= Config.percentageOfPurchaseTransactions) { // It can be another type of transaction so we will look it up randomly tp.transactionTypeID = (int)Utils.MyDLookup("transactionTypeID", "(SELECT ROW_NUMBER() OVER (ORDER BY transactionTypeID) AS RowNum, * FROM tTransactionType) sub ", " RowNum = " + (r.Next(DefaultValues.transactionTypeID_count) + 1), ""); } else { // It has to be a purchase transaction tp.transactionTypeID = DefaultValues.transactionTypeID_Purchase; } } catch (Exception ex) { // Something went wrong. txtResults.AppendText(Environment.NewLine + "AddTransactions.computeRandomValuesForTransaction: " + ex.Message); } }