Exemplo n.º 1
0
        /// <summary>
        /// Remove any holidays form the date passed
        /// </summary>
        /// <param name="pCustomerID">which customer</param>
        /// <param name="pDT">the date to remvoe from</param>
        /// <param name="pServiceTypeID">for which service type Id</param>
        /// <returns></returns>
        public DateTime RemoveHolidayPeriodFromDate(long pCustomerID, DateTime pDT, int pServiceTypeID)
        {
            // select all the usage lines order DESC so that the last record is the latest
            ClientUsageLinesTbl        _UsageDAL   = new ClientUsageLinesTbl();
            List <ClientUsageLinesTbl> _UsageLines = _UsageDAL.GetLast10UsageLines(pCustomerID);
            int _NumValues = 0;

            // For every holiday period int he last 6 remove the period from the data difference
            while ((_UsageLines.Count > _NumValues) && (_NumValues < 6))
            {
                if (_UsageLines[_NumValues].ServiceTypeID != 0)
                {
                    if ((_UsageLines[_NumValues].ServiceTypeID >= TrackerTools.CONST_SERVTYPE1WKHOLI) || (_UsageLines[_NumValues].ServiceTypeID <= TrackerTools.CONST_SERVTYPE2MTHHOLI))
                    {
                        pDT = pDT.AddMonths(DaysToRemoveFromDate(_UsageLines[_NumValues].ServiceTypeID));
                    }
                    else if (_UsageLines[_NumValues].ServiceTypeID == pServiceTypeID)
                    {
                        _NumValues = 6;
                    }
                }
                _NumValues++;
            }

            return(pDT);
        }
Exemplo n.º 2
0
        /// <summary>x
        /// Calculates the average consumption of an item, either perday or per cups consumed
        /// </summary>
        /// <param name="pCustomerID">for wich customer id</param>
        /// <param name="pServiceTypeID">for what type of service type default is coffee</param>
        /// <param name="pTypicalAverageConsumption">What is the typical average consumption to set as default</param>
        /// <param name="pPerDayCalc">Is this a per day or per cup calculation</param>
        /// <returns>average consumption of the type passed</returns>
        public double CalcAveConsumption(long pCustomerID, int pServiceTypeID, double pTypicalAverageConsumption, bool pPerDayCalc)
        {
            double        _AveConsumption = pTypicalAverageConsumption; // average cups divided by cups used
            List <double> _AverageConsumptions = new List <double>();
            double        _ThisAverage = 0, _TotalAverages = 0;
            int           _NumValues = 0, _DaysSincePrev = 0;
            int           _ThisServiceTypeID = 0, _UsageLineNo = 1; // start at one since we need at leqast 2 dates to calc
            DateTime      _PrevDate = DateTime.MinValue;            // NULLDATE
            long          _PrevCupCount = 0, _CupsSincePrev = 0;


            ClientUsageLinesTbl        _ClientUsageLinesTbl = new ClientUsageLinesTbl();
            List <ClientUsageLinesTbl> _UsageData           = _ClientUsageLinesTbl.GetLast10UsageLines(pCustomerID, pServiceTypeID);

            // sort the data so that the first record is the oldest date is priority
            _UsageData.Sort((x, y) => x.LineDate.CompareTo(y.LineDate));
            /// Go through the records starting at the top in descending order and calculate the average cup count excluding SwopStart and SwopStop
            /// and catering for holiday periods

            if (_UsageData.Count > 1) // must have at least to values to calcualte an average
            {
                _PrevDate     = _UsageData[0].LineDate;
                _PrevCupCount = _UsageData[0].CupCount;

                while ((_UsageData.Count > _UsageLineNo) && (_NumValues < CONST_MAXROLLINGAVEVALUES))
                {
                    _ThisServiceTypeID = _UsageData[_UsageLineNo].ServiceTypeID;

                    // Only process correctly sequenced entries - otherwise we get impossible results (remember we are reading backwards)
                    if (_PrevCupCount < _UsageData[_UsageLineNo].CupCount) // is this bogus data so ignore it
                    {
                        if ((_ThisServiceTypeID == pServiceTypeID) && (_UsageData[_UsageLineNo].LineDate > _PrevDate))
                        {
                            // we are at the next record so cacl difference
                            _CupsSincePrev = (_UsageData[_UsageLineNo].CupCount - _PrevCupCount); // number of cups consumed
                            if (pPerDayCalc)
                            {
                                _DaysSincePrev = (_UsageData[_UsageLineNo].LineDate - _PrevDate).Days; // number of days between types
                                _ThisAverage   = Math.Round((double)(_CupsSincePrev / _DaysSincePrev), 5);
                                _AverageConsumptions.Add(_ThisAverage);                                // round to the nearest 5th decimal
                            }
                            else
                            {
                                _AverageConsumptions.Add(_CupsSincePrev); // round to the nearest 5th decimal
                            }
                            // now add an average per day
                            _NumValues++;
                            _PrevDate     = _UsageData[_UsageLineNo].LineDate;
                            _PrevCupCount = _UsageData[_UsageLineNo].CupCount;
                        }
                        // Check if they were on holiday and deduct that from total days
                        else if ((_ThisServiceTypeID >= TrackerTools.CONST_SERVTYPE1WKHOLI) && (_ThisServiceTypeID <= TrackerTools.CONST_SERVTYPE2MTHHOLI))
                        {
                            _PrevDate = _PrevDate.AddDays(DaysToAddToDate(_ThisServiceTypeID));
                        }
                    }
                    _UsageLineNo++; // move to next record
                } // while

                // now if there are more than 5 values kill the smallest
                if (_AverageConsumptions.Count >= CONST_MAXROLLINGAVEVALUES)
                {
                    double _MaxAve = _AverageConsumptions[0];
                    for (int i = 1; i < _AverageConsumptions.Count; i++)
                    {
                        if (_AverageConsumptions[i] > _MaxAve)
                        {
                            _MaxAve = _AverageConsumptions[i];
                        }
                    }
                    _AverageConsumptions.Remove(_MaxAve);
                }
                // now calc the Average
                _TotalAverages = 0;
                for (int i = 0; i < _AverageConsumptions.Count; i++)
                {
                    _TotalAverages += _AverageConsumptions[i];
                }

                if (_TotalAverages > 0)
                {
                    _AveConsumption = Math.Round(_TotalAverages / _AverageConsumptions.Count, 3);
                }
            }
            else
            {
                if (pPerDayCalc)
                {
                    _AveConsumption = TrackerTools.CONST_TYPICALAVECONSUMPTION;
                }
                else
                {
                    _AveConsumption = pTypicalAverageConsumption;
                }
            }


            // check for negative or 0
            if (_AveConsumption <= 0)
            {
                _AveConsumption = pTypicalAverageConsumption;
            }

            return(_AveConsumption);
        }