private DataTable CreateOrderTransactionDatesPreviewTable()
        {
            DataTable     dt                          = new DataTable();
            RandomNumber  rnd                         = new RandomNumber();
            int           numPreviewDates             = 10;
            DateTime      earliestDate                = DateTime.MinValue;
            DateTime      latestDate                  = DateTime.MinValue;
            DateTime      currDate                    = DateTime.MinValue;
            bool          includeWeekendDays          = this.chkIncludeWeekendDays.Checked;
            int           minNumSalesOrdersPerDate    = 1;
            int           maxNumSalesOrdersPerDate    = 1;
            int           minNumPurchaseOrdersPerDate = 1;
            int           maxNumPurchaseOrdersPerDate = 1;
            TimeSpan      minTimePerDate              = TimeSpan.MinValue;
            TimeSpan      maxTimePerDate              = TimeSpan.MinValue;
            int           minSecondsPerDate           = 1;
            int           maxSecondsPerDate           = 1;
            int           numSalesOrdersForDate       = 0;
            int           numPurchaseOrdersForDate    = 0;
            int           numSecondsForTx             = 0;
            TimeSpan      txTime                      = TimeSpan.MinValue;
            StringBuilder timeList                    = new StringBuilder();

            string errMsg = VerifyFormValues();

            if (errMsg.Length > 0)
            {
                WriteToMessageLog(errMsg);
                AppMessages.DisplayErrorMessage(errMsg);
                return(null);
            }


            DataColumn dc0 = new DataColumn();

            dc0.ColumnName        = "DayNum";
            dc0.DataType          = Type.GetType("System.Int32");
            dc0.AutoIncrement     = true;
            dc0.AutoIncrementSeed = 1;
            dc0.AutoIncrementStep = 1;

            DataColumn dc1 = new DataColumn();

            dc1.ColumnName = "DayOfWeek";
            dc1.DataType   = Type.GetType("System.String");
            dc1.MaxLength  = 255;

            DataColumn dc2 = new DataColumn();

            dc2.ColumnName = "DateAndTime";
            dc2.DataType   = Type.GetType("System.DateTime");

            DataColumn dc3 = new DataColumn();

            dc3.ColumnName = "NumSalesOrders";
            dc3.DataType   = Type.GetType("System.Int32");

            DataColumn dc4 = new DataColumn();

            dc4.ColumnName = "NumPurchaseOrders";
            dc4.DataType   = Type.GetType("System.Int32");

            DataColumn dc5 = new DataColumn();

            dc5.ColumnName = "Tx Times";
            dc5.DataType   = Type.GetType("System.String");
            dc5.MaxLength  = Int16.MaxValue;

            dt.Columns.Add(dc0);
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            dt.Columns.Add(dc5);

            numPreviewDates             = AppTextGlobals.ConvertStringToInt(this.txtNumPreviewDates.Text, -1);
            earliestDate                = AppTextGlobals.ConvertStringToDateTime(this.txtEarliestTransactionDate.Text, DateTime.MinValue);
            latestDate                  = AppTextGlobals.ConvertStringToDateTime(this.txtLatestTransactionDate.Text, DateTime.MinValue);
            currDate                    = earliestDate;
            minNumSalesOrdersPerDate    = AppTextGlobals.ConvertStringToInt(this.txtMinNumSalesOrdersPerDate.Text, 1);
            maxNumSalesOrdersPerDate    = AppTextGlobals.ConvertStringToInt(this.txtMaxNumSalesOrdersPerDate.Text, 1);
            minNumPurchaseOrdersPerDate = AppTextGlobals.ConvertStringToInt(this.txtMinNumPurchaseOrdersPerDate.Text, 1);
            maxNumPurchaseOrdersPerDate = AppTextGlobals.ConvertStringToInt(this.txtMaxNumPurchaseOrdersPerDate.Text, 1);
            minTimePerDate              = AppTextGlobals.ConvertStringToTimeSpan(this.txtMinTimePerDate.Text, "00:00:00");
            maxTimePerDate              = AppTextGlobals.ConvertStringToTimeSpan(this.txtMaxTimePerDate.Text, "00:00:00");
            minSecondsPerDate           = (int)minTimePerDate.TotalSeconds;
            maxSecondsPerDate           = (int)maxTimePerDate.TotalSeconds;

            for (int r = 0; r < numPreviewDates; r++)
            {
                numSalesOrdersForDate    = rnd.GenerateRandomInt(minNumSalesOrdersPerDate, maxNumSalesOrdersPerDate);
                numPurchaseOrdersForDate = rnd.GenerateRandomInt(minNumPurchaseOrdersPerDate, maxNumPurchaseOrdersPerDate);

                DataRow dr = dt.NewRow();
                dr[1] = currDate.DayOfWeek.ToString();
                dr[2] = currDate;
                dr[3] = numSalesOrdersForDate;
                dr[4] = numPurchaseOrdersForDate;

                timeList.Length = 0;

                for (int so = 0; so < numSalesOrdersForDate; so++)
                {
                    numSecondsForTx = rnd.GenerateRandomInt(minSecondsPerDate, maxSecondsPerDate);
                    DateTime txDate = currDate.AddSeconds(numSecondsForTx);
                    timeList.Append("SO at ");
                    timeList.Append(txDate.ToString("HH:mm:ss"));
                    timeList.Append(", ");
                }

                for (int po = 0; po < numPurchaseOrdersForDate; po++)
                {
                    numSecondsForTx = rnd.GenerateRandomInt(minSecondsPerDate, maxSecondsPerDate);
                    DateTime txDate = currDate.AddSeconds(numSecondsForTx);
                    timeList.Append("PO at ");
                    timeList.Append(txDate.ToString("HH:mm:ss"));
                    timeList.Append(", ");
                }

                dr[5] = timeList.ToString().TrimEnd(' ').TrimEnd(',');

                dt.Rows.Add(dr);

                //increment counters and dates to continue loop
                currDate = currDate.AddDays(1);
                if (currDate > latestDate)
                {
                    break;
                }
                if (currDate.DayOfWeek == DayOfWeek.Sunday &&
                    includeWeekendDays == false)
                {
                    currDate = currDate.AddDays(1);
                }
                else if (currDate.DayOfWeek == DayOfWeek.Saturday &&
                         includeWeekendDays == false)
                {
                    currDate = currDate.AddDays(2);
                }
                else
                {
                    //keep the previously calculated date
                    ;
                }
            }


            return(dt);
        }
        /// <summary>
        /// Creates a set of sequential numbers that start from a user specified start number and are incremented by a user specified number. This is a testing routine.
        /// </summary>
        /// <param name="incrementType">Time slice to increment with (seconds, minutes, hours, days, years).</param>
        /// <param name="numRows">Number of random values to generate.</param>
        /// <param name="incrementSize">Number to increment by.</param>
        /// <param name="startDateForSequence">First date in the sequence.</param>
        /// <param name="endDateForSequence">Maximum date in the sequence.</param>
        /// <param name="generateRandomTime">If true, a randomized time value will be generated for each sequential date.</param>
        /// <param name="fromTime">Minimum time value to generate.</param>
        /// <param name="toTime">Maximum time value to generate.</param>
        /// <param name="minNumDatesPerIncrement">For each date increment, the minimum number of dates that can be generated with same date into the sequence.</param>
        /// <param name="maxNumDatesPerIncrement">For each date increment, the maximum number of dates that can be generated with same date into the sequence.</param>
        /// <param name="initStartDateForSequence">If maximum date is passed during sequence generation, the sequence will be restarted with this date and new values will be appended to the existing sequence.</param>
        /// <param name="dateConversionType">Determines whether or not to convert the DateTime value to an integer. Useful for data warehousing scenarios.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        /// <remarks>This routine is used for testing.</remarks>
        public DataTable CreateDateSequencePreviewDataTable(enRandomIncrementType incrementType, int numRows, string incrementSize, string startDateForSequence, string endDateForSequence, bool generateRandomTime, string fromTime, string toTime, string minNumDatesPerIncrement, string maxNumDatesPerIncrement, string initStartDateForSequence, enDateConversionType dateConversionType)
        {
            DataTable dt = new DataTable();

            int      sizeOfIncrement   = 1;
            DateTime startDate         = DateTime.MinValue;
            DateTime endDate           = DateTime.MaxValue;
            DateTime currDate          = DateTime.Now;
            DateTime initDate          = DateTime.MinValue;
            int      minIncrementDates = 1;
            int      maxIncrementDates = 1;
            TimeSpan fromTimeTs        = Convert.ToDateTime(fromTime).TimeOfDay;
            TimeSpan toTimeTs          = Convert.ToDateTime(toTime).TimeOfDay;
            double   fromSeconds       = fromTimeTs.TotalSeconds;
            double   toSeconds         = toTimeTs.TotalSeconds;
            double   randNum           = 0.0;

            try
            {
                sizeOfIncrement = AppTextGlobals.ConvertStringToInt(incrementSize, 1);
                startDate       = AppTextGlobals.ConvertStringToDateTime(startDateForSequence, new DateTime(1000, 1, 1));
                endDate         = AppTextGlobals.ConvertStringToDateTime(endDateForSequence, new DateTime(5999, 12, 31));
                initDate        = AppTextGlobals.ConvertStringToDateTime(initStartDateForSequence, new DateTime(1000, 1, 1));
                currDate        = IncrementDateTime(incrementType, -sizeOfIncrement, startDate, startDate);

                minIncrementDates = AppTextGlobals.ConvertStringToInt(minNumDatesPerIncrement, 1);
                maxIncrementDates = AppTextGlobals.ConvertStringToInt(maxNumDatesPerIncrement, 1);

                DataColumn dc = new DataColumn("RandomValue");
                //dc.DataType = Type.GetType("System.DateTime");
                switch (dateConversionType)
                {
                case enDateConversionType.DoNotConvert:
                    dc.DataType = Type.GetType("System.DateTime");
                    break;

                case enDateConversionType.ConvertDateTo32bitInt:
                    dc.DataType = Type.GetType("System.Int32");
                    break;

                case enDateConversionType.ConvertTimeTo32bitInt:
                    dc.DataType = Type.GetType("System.Int32");
                    break;

                case enDateConversionType.ConvertDateTimeTo64bitInt:
                    dc.DataType = Type.GetType("System.Int64");
                    break;

                default:
                    dc.DataType = Type.GetType("System.DateTime");
                    break;
                }

                dt.Columns.Add(dc);


                for (int i = 0; i < numRows; i++)
                {
                    currDate = IncrementDateTime(incrementType, sizeOfIncrement, currDate, startDate);

                    if (currDate > endDate)
                    {
                        currDate = initDate;
                    }

                    int      numDatesToGenerate = _rn.GenerateRandomInt(minIncrementDates, maxIncrementDates);
                    DateTime dtm = currDate;

                    for (int n = 0; n < numDatesToGenerate; n++)
                    {
                        DataRow dr = dt.NewRow();
                        if (generateRandomTime)
                        {
                            randNum = _rn.GenerateRandomNumber(fromSeconds, toSeconds);
                            TimeSpan ts2 = new TimeSpan(0, 0, 0, (int)randNum, 0);
                            try
                            {
                                dtm = currDate.AddSeconds(ts2.TotalSeconds);
                            }
                            catch (System.ArgumentOutOfRangeException)
                            {
                                dtm      = startDate;
                                currDate = startDate;
                            }
                        }
                        //dr[0] = dtm;
                        switch (dateConversionType)
                        {
                        case enDateConversionType.DoNotConvert:
                            dr[0] = dtm;
                            break;

                        case enDateConversionType.ConvertDateTo32bitInt:
                            dr[0] = Convert.ToInt32(dtm.ToString("yyyyMMdd"));
                            break;

                        case enDateConversionType.ConvertTimeTo32bitInt:
                            dr[0] = Convert.ToInt32(dtm.ToString("HHmmss"));
                            break;

                        case enDateConversionType.ConvertDateTimeTo64bitInt:
                            dr[0] = Convert.ToInt64(dtm.ToString("yyyyMMddHHmmss"));
                            break;

                        default:
                            dr[0] = dtm;
                            break;
                        }
                        dt.Rows.Add(dr);
                    }
                }
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateSequencePreviewDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }



            return(dt);
        }
        private void EstimateTransactionRowCounts()
        {
            string errMsg = VerifyFormValues();

            if (errMsg.Length > 0)
            {
                WriteToMessageLog(errMsg);
                AppMessages.DisplayErrorMessage(errMsg);
                return;
            }


            try
            {
                _msg.Length = 0;

                DateTime earliestTxDate = AppTextGlobals.ConvertStringToDateTime(this.txtEarliestTransactionDate.Text, DateTime.MinValue);
                DateTime latestTxDate   = AppTextGlobals.ConvertStringToDateTime(this.txtLatestTransactionDate.Text, DateTime.MinValue);
                TimeSpan dateDiff       = latestTxDate.Subtract(earliestTxDate);
                double   numDays        = dateDiff.TotalDays;
                double   numWeeks       = numDays / 7.0;
                if (this.chkIncludeWeekendDays.Checked == false)
                {
                    numDays = numDays - (numWeeks * 2.0);
                }
                _msg.Append("Estimated number of days:                ");
                _msg.Append(numDays.ToString("#,##0"));
                _msg.Append(Environment.NewLine);

                double minNum                  = AppTextGlobals.ConvertStringToDouble(this.txtMinNumSalesOrdersPerDate.Text, 0.0);
                double maxNum                  = AppTextGlobals.ConvertStringToDouble(this.txtMaxNumSalesOrdersPerDate.Text, 0.0);
                double numSalesTxs             = numDays * ((minNum + maxNum) / 2.0);
                double numSalesDetails         = numSalesTxs * 3.0;
                double numInternetSalesDetails = numSalesTxs * 2.0;
                double numResellerSalesDetails = numSalesTxs * 16.0;

                _msg.Append("Estimated number of sales order headers:    ");
                _msg.Append(numSalesTxs.ToString("#,##0"));
                _msg.Append(Environment.NewLine);
                _msg.Append("Estimated number of sales order details:    ");
                _msg.Append(numSalesDetails.ToString("#,##0"));
                _msg.Append(Environment.NewLine);
                _msg.Append("Estimated number of internet order details: ");
                _msg.Append(numInternetSalesDetails.ToString("#,##0"));
                _msg.Append(Environment.NewLine);
                _msg.Append("Estimated number of reseller order details: ");
                _msg.Append(numResellerSalesDetails.ToString("#,##0"));
                _msg.Append(Environment.NewLine);

                minNum = AppTextGlobals.ConvertStringToDouble(this.txtMinNumPurchaseOrdersPerDate.Text, 0.0);
                maxNum = AppTextGlobals.ConvertStringToDouble(this.txtMaxNumPurchaseOrdersPerDate.Text, 0.0);
                double numPurchaseTxs     = numDays * ((minNum + maxNum) / 2.0);
                double numPurchaseDetails = numPurchaseTxs * 2.0;

                _msg.Append("Estimated number of purchase order headers: ");
                _msg.Append(numPurchaseTxs.ToString("#,##0"));
                _msg.Append(Environment.NewLine);
                _msg.Append("Estimated number of purchase order details: ");
                _msg.Append(numPurchaseDetails.ToString("#,##0"));
                _msg.Append(Environment.NewLine);


                WriteToMessageLog(_msg.ToString());
                AppMessages.DisplayInfoMessage(_msg.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                ;
            }
        }