///NOTE: depricated
        //static List<string> getCDRs(string pWhereFilter, CdrExportMap pCdrExportMap, int pStartTimokDate, int pEndTimokDate, string pDecimalFormatString, BackgroundWorker pBackgroundWorker) {
        //  pBackgroundWorker.ReportStatus("Retrieving CDRs...");
        //  List<string> _records = new List<string>();
        //  if (Cdr_Db.Exists(TimokDate.ToDateTime(pStartTimokDate))) {
        //    using (Cdr_Db _db = new Cdr_Db(TimokDate.ToDateTime(pStartTimokDate))) {
        //      IDbCommand _cmd = _db.Connection.CreateCommand();
        //      _cmd.CommandText = getSQLForCDRViewExport(_db.Connection.Database, pCdrExportMap.CdrExportMapDetails, pStartTimokDate, pEndTimokDate, pWhereFilter);
        //      IDataReader _reader = _cmd.ExecuteReader();
        //      while (_reader.Read()) {
        //        if (pBackgroundWorker.CancellationPending) {
        //          throw new Exception("CDR Export canceled");
        //        }
        //        StringBuilder _record = new StringBuilder();
        //        foreach (CdrExportMapDetail _field in pCdrExportMap.CdrExportMapDetails) {
        //          object _value = _reader.GetValue(_field.Sequence - 1);
        //          _record.Append(_value);
        //          //NOTE: if need to format prices, here is the place to do that
        //          //if (_value is Decimal) {
        //          //  _record.Append(((decimal) _value).ToString(pDecimalFormatString));
        //          //}
        //          //else {
        //          //  _record.Append(_value);
        //          //}
        //          _record.Append((char) pCdrExportMap.CdrExportDelimeter);
        //        }
        //        _records.Add(_record.ToString().TrimEnd((char) pCdrExportMap.CdrExportDelimeter));
        //      }
        //    }
        //  }
        //  pBackgroundWorker.ReportStatus(string.Format("Retrieved {0} CDRs", _records.Count));
        //  return _records;
        //}
        static List <string> getCDRs(string pWhereFilter, ICdrExportInfo pCdrExportInfo, int pTimokDate, IBackgroundWorker pBackgroundWorker)
        {
            var _records = new List <string>();

            if (Cdr_Db.Exists(TimokDate.ToDateTime(pTimokDate)))
            {
                CDRViewRow[] _cdrViewRows;
                try {
                    using (var _db = new Cdr_Db(TimokDate.ToDateTime(pTimokDate))) {
                        _cdrViewRows = _db.CDRViewCollection.Get(pTimokDate, pWhereFilter);
                    }
                }
                catch (Exception _ex) {
                    TimokLogger.Instance.LogRbr(LogSeverity.Critical, GetcdrsLabel, string.Format("Exception:\r\n{0}", _ex));
                    pBackgroundWorker.ReportStatus(string.Format("Exception! Exported {0} CDRs", _records.Count));
                    return(_records);
                }
                if (_cdrViewRows == null || _cdrViewRows.Length == 0)
                {
                    TimokLogger.Instance.LogRbr(LogSeverity.Error, GetcdrsLabel, "No CDR Records found");
                    pBackgroundWorker.ReportStatus("No CDR Records found");
                    return(_records);
                }

                pBackgroundWorker.ReportStatus(string.Format("Retrieved {0} CDRs for: {1}", _cdrViewRows.Length, TimokDate.ToDateTime(pTimokDate).ToString("yyyy-MM-dd HH:mm")));

                var _recordCount = 0;
                foreach (var _cdrViewRow in _cdrViewRows)
                {
                    if (pBackgroundWorker.CancellationPending)
                    {
                        throw new Exception("CDR Export canceled");
                    }

                    if (pCdrExportInfo.WithRerating && _cdrViewRow.Duration > 0)
                    {
                        if (!rerateCDR(_cdrViewRow, pBackgroundWorker))
                        {
                            continue;
                        }
                    }

                    _records.Add(mapToExportedRecord(_cdrViewRow, pCdrExportInfo.CdrExportMap));
                    pBackgroundWorker.ReportProgress(++_recordCount * 100 / _cdrViewRows.Length);
                }

                if (_records.Count != _cdrViewRows.Length)
                {
                    pBackgroundWorker.ReportStatus(string.Format("ERROR: Exported {0} out of {1} retreived CDRs", _records.Count, _cdrViewRows.Length));
                }
                else
                {
                    pBackgroundWorker.ReportStatus(string.Format("Exported {0} CDRs for: {1}", _records.Count, TimokDate.ToDateTime(pTimokDate).ToString("yyyy-MM-dd HH:mm")));
                }
            }
            return(_records);
        }
        static bool rerateCDR(CDRViewRow_Base pCdrViewRow, IBackgroundWorker pBackgroundWorker)
        {
            try {
                var _customerAcct = CustomerAcct.Get(pCdrViewRow.Customer_acct_id);
                if (_customerAcct == null)
                {
                    pBackgroundWorker.ReportStatus(string.Format("ERROR! CustomerAcct NOT FOUND, {0}", pCdrViewRow.Customer_acct_id));
                    return(false);
                }

                var _customerRoute = CustomerRoute.Get(_customerAcct.ServiceId, _customerAcct.RoutingPlanId, pCdrViewRow.Customer_route_id);
                if (_customerRoute == null)
                {
                    pBackgroundWorker.ReportStatus(string.Format("ERROR! CustomerRoute NOT FOUND, {0}", pCdrViewRow.Customer_route_id));
                    return(false);
                }

                var _carrierAcct = CarrierAcct.Get(pCdrViewRow.Carrier_acct_id);
                if (_carrierAcct == null)
                {
                    pBackgroundWorker.ReportStatus(string.Format("ERROR! CarrierAcct NOT FOUND, {0}", pCdrViewRow.Carrier_acct_id));
                    return(false);
                }

                var _carrierRoute = CarrierRoute.Get(_carrierAcct.Id, pCdrViewRow.Carrier_route_id);
                if (_carrierRoute == null)
                {
                    pBackgroundWorker.ReportStatus(string.Format("ERROR! CarrierRoute NOT FOUND, {0}", pCdrViewRow.Carrier_route_id));
                    return(false);
                }

                var _cdr = new Cdr(pCdrViewRow);
                _customerAcct.RateCall(_customerRoute, ref _cdr);
                pCdrViewRow.Customer_duration = _cdr.CustomerDuration;
                pCdrViewRow.Price             = _cdr.CustomerPrice;
                _carrierAcct.RateCall(_carrierRoute, ref _cdr);
                pCdrViewRow.Carrier_duration = _cdr.CarrierDuration;
                pCdrViewRow.Cost             = _cdr.CarrierCost;
            }
            catch (Exception _ex) {
                pBackgroundWorker.ReportStatus(string.Format("Exception! {0}", _ex.Message));
                TimokLogger.Instance.LogRbr(LogSeverity.Critical, "CdrExportController.rerate", string.Format("Exception:\r\n{0}", _ex));
                return(false);
            }
            return(true);
        }