/// <summary>
        /// Gets the return visits of a given quantity and sorted.
        /// </summary>
        /// <param name="so">The sort order.</param>
        /// <param name="maxReturnCount">The max return count. -1 for all.</param>
        /// <returns><c>ReturnVisitData</c> Array</returns>
        public static ReturnVisitData[] GetReturnVisits(SortOrder so, int maxReturnCount = 25)
        {
            var rvs = new List<ReturnVisitData>();
            using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) {
                if (maxReturnCount == -1) maxReturnCount = db.ReturnVisitItems.Count();
                IQueryable<ReturnVisitDataItem> q;
                IEnumerable<ReturnVisitDataItem> demRVs = null;
                if (so == SortOrder.DateNewestToOldest || so == SortOrder.DateOldestToNewest) {
                    q = from x in db.ReturnVisitItems
                        orderby x.DateCreated
                        select x;
                    q = q.Take(maxReturnCount);
                    demRVs = so == SortOrder.DateNewestToOldest ? q.ToArray().Reverse() : q.ToArray();
                } else if (so == SortOrder.CityAToZ || so == SortOrder.CityZToA) {
                    q = from x in db.ReturnVisitItems
                        orderby x.City
                        select x;
                    q = q.Take(maxReturnCount);
                    demRVs = so == SortOrder.CityZToA ? q.ToArray().Reverse() : q.ToArray();
                }

                if (demRVs != null)
                    foreach (ReturnVisitDataItem r in demRVs) {
                        var rr = new ReturnVisitData {
                            ItemId = r.ItemId,
                            DateCreated = r.DateCreated,
                            AddressOne = r.AddressOne,
                            AddressTwo = r.AddressTwo,
                            Age = r.Age,
                            City = r.City,
                            Country = r.Country,
                            FullName = r.FullName,
                            Gender = r.Gender,
                            OtherNotes = r.OtherNotes,
                            PhysicalDescription = r.PhysicalDescription,
                            PostalCode = r.PostalCode,
                            StateProvince = r.StateProvince,
                            ImageSrc = r.ImageSrc,
                            PhoneNumber = r.PhoneNumber,
                            Latitude = r.Latitude ?? 0,
                            Longitude = r.Longitude ?? 0,
                            LastVisitDate = r.LastVisitDate ?? GetLastVisitDate(r)
                        };
                        rvs.Add(rr);
                    }
            }
            return rvs.ToArray();
        }
        /// <summary>
        /// Updates the return visit.
        /// </summary>
        /// <param name="newRv">The new rv.</param>
        public static bool UpdateReturnVisit(ref ReturnVisitData newRv)
        {
            using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) {
                try {
                    int itemId = newRv.ItemId;
                    ReturnVisitDataItem rv = db.ReturnVisitItems.Single(s => s.ItemId == itemId);

                    rv.AddressOne = newRv.AddressOne;
                    rv.AddressTwo = newRv.AddressTwo;
                    rv.Age = newRv.Age;
                    rv.City = newRv.City;
                    rv.Country = newRv.Country;
                    rv.FullName = newRv.FullName;
                    rv.ImageSrc = newRv.ImageSrc;
                    rv.OtherNotes = newRv.OtherNotes;
                    rv.PhoneNumber = newRv.PhoneNumber;
                    rv.PhysicalDescription = newRv.PhysicalDescription;
                    rv.PostalCode = newRv.PostalCode;
                    rv.StateProvince = newRv.StateProvince;
                    rv.Gender = newRv.Gender;
                    rv.Latitude = newRv.Latitude;
                    rv.Longitude = newRv.Longitude;
                    rv.LastVisitDate = newRv.LastVisitDate;

                    db.SubmitChanges();
                    return true;
                } catch (InvalidOperationException) {
                    return AddNewReturnVisit(ref newRv); //rv not found, lets create it.
                }
            }
        }
 public static bool UpdateLastVisitDate(int rvItemId, DateTime date)
 {
     try {
         using (var db = new ReturnVisitDataContext()) {
             var rv = db.ReturnVisitItems.Single(s => s.ItemId == rvItemId);
             if (rv != null && date > rv.LastVisitDate) { //Don't update if the new rv visit date is prior to the current last visit date
                 rv.LastVisitDate = date;
                 db.SubmitChanges();
                 return true;
             }
             if (rv != null) {
                 return true;
             }
         }
         return false;
     }
     catch (InvalidOperationException) {
         return false;
     }
     return false;
 }
        public static bool IdExists(int rv)
        {
            //throw new NotImplementedException();
            using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) {
                try {
                    var b = db.ReturnVisitItems.Single(s => s.ItemId == rv);
                    if (b == null) return false;
                    return true;
                } catch {
                    return false;
                }

            }
        }
        public static List<ReturnVisitData> GetReturnVisitsByLastVisitDate(int maxReturnCount = 8)
        {
            try {
                using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) {
                    var qry = from x in db.ReturnVisitItems
                        orderby x.LastVisitDate
                        //where x.LastVisitDate != SqlCeConstants.DateTimeMinValue
                        select x;

                    if (!qry.Any()) return null;
                    bool save = false;
                    foreach (var r in qry) {
                        if (r.LastVisitDate == null) {
                            r.LastVisitDate = GetLastVisitDate(r);
                            save = true;
                        }
                    }
                    if(save) db.SubmitChanges();
                    return qry.Take(maxReturnCount == -1 ? qry.Count() : maxReturnCount).Select(rv => ReturnVisitData.Copy(rv)).ToList();
                }
            }
            catch {
                return null;
            }
        }
 /// <summary>
 /// Gets the return visit.
 /// </summary>
 /// <param name="itemID">The item ID.</param>
 /// <returns>ReturnVisitData.</returns>
 public static ReturnVisitData GetReturnVisit(int itemID)
 {
     using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) {
         try {
             ReturnVisitDataItem r = db.ReturnVisitItems.Single(s => s.ItemId == itemID);
             var rr = new ReturnVisitData {
                 ItemId = r.ItemId,
                 DateCreated = r.DateCreated,
                 AddressOne = r.AddressOne,
                 AddressTwo = r.AddressTwo,
                 Age = r.Age,
                 City = r.City,
                 Country = r.Country,
                 FullName = r.FullName,
                 Gender = r.Gender,
                 OtherNotes = r.OtherNotes,
                 PhysicalDescription = r.PhysicalDescription,
                 PostalCode = r.PostalCode,
                 StateProvince = r.StateProvince,
                 ImageSrc = r.ImageSrc,
                 PhoneNumber = r.PhoneNumber,
                 Latitude = r.Latitude ?? 0,
                 Longitude = r.Longitude ?? 0,
                 LastVisitDate = r.LastVisitDate ?? GetLastVisitDate(r)
             };
             return rr;
         } catch {
             return new ReturnVisitData();
         }
     }
 }
        /// <summary>
        /// Deletes the return visit.
        /// </summary>
        /// <param name="itemId">The item id.</param>
        public static bool DeleteReturnVisit(int itemId, bool deleteCalls)
        {
            using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) {
                try {
                    ReturnVisitDataItem rv = db.ReturnVisitItems.Single(s => s.ItemId == itemId);

                    db.ReturnVisitItems.DeleteOnSubmit(rv);
                    db.SubmitChanges();
                    if (deleteCalls) return RvPreviousVisitsDataInterface.DeleteAllCallsFromRv(itemId);
                    return true;
                } catch (InvalidOperationException) { return false; }
            }
        }
 public static bool DeleteCallFromRv(int rvItemId, DateTime date)
 {
     try {
         using (var db = new ReturnVisitDataContext()) {
             var rv = db.ReturnVisitItems.Single(s => s.ItemId == rvItemId);
             if (date >= rv.LastVisitDate) {
                 //Just to be safe checking '>='
                 rv.LastVisitDate = GetLastVisitDate(rv);
                 db.SubmitChanges();
             }
             return true;
         }
     }
     catch {
         return false;
     }
 }
 /// <summary>
 /// Checks the database.
 /// </summary>
 public static void CheckDatabase()
 {
     using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString))
     {
         if (db.DatabaseExists() == false)
         {
             db.CreateDatabase();
             DatabaseSchemaUpdater dbUpdater = db.CreateDatabaseSchemaUpdater();
             dbUpdater.DatabaseSchemaVersion = APP_VERSION;
             dbUpdater.Execute();
         }
         else
         {
             var dbUpdater = db.CreateDatabaseSchemaUpdater();
             if (dbUpdater.DatabaseSchemaVersion < 2)
             {
                 //update from 1.0 to 2.0 db version
                 dbUpdater.AddColumn<ReturnVisitDataItem>("Longitude");
                 dbUpdater.AddColumn<ReturnVisitDataItem>("Latitude");
                 dbUpdater.AddColumn<ReturnVisitDataItem>("LastVisitDate");
                 dbUpdater.DatabaseSchemaVersion = APP_VERSION;
                 dbUpdater.Execute();
                 var rvList = GetReturnVisits(SortOrder.DateNewestToOldest, -1);
                 foreach (var r in rvList)
                 {
                     var x = r;
                     if (r.LastVisitDate > SqlCeConstants.DateTimeMinValue) {
                         r.LastVisitDate = GetLastVisitDate(ReturnVisitData.Copy(x));
                     }
                     UpdateReturnVisit(ref x);
                 }
             }
         }
     }
 }
 /// <summary>
 /// Adds the new return visit.
 /// </summary>
 /// <param name="newRv">The new rv.</param>
 /// <returns><c>True</c> if successful and <c>False</c> if unsuccessful.</returns>
 /// <exception cref="MyTimeDatabaseLib.ReturnVisitAlreadyExistsException">The Return Visit already exists.</exception>
 public static bool AddNewReturnVisit(ref ReturnVisitData newRv)
 {
     using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) {
         var r = ReturnVisitData.Copy(newRv);
         IQueryable<ReturnVisitDataItem> qry = from x in db.ReturnVisitItems
                                               where x.AddressOne.Equals(r.AddressOne) &&
                                                     x.AddressTwo.Equals(r.AddressTwo) &&
                                                     x.City.Equals(r.City) &&
                                                     x.Country.Equals(r.Country) &&
                                                     x.StateProvince.Equals(r.StateProvince) &&
                                                     x.PostalCode.Equals(r.PostalCode) &&
                                                     x.FullName.Equals(r.FullName)
                                               select x;
         if (qry.Any())
             throw new ReturnVisitAlreadyExistsException("The Return Visit already exists.", qry.First().ItemId);
         db.ReturnVisitItems.InsertOnSubmit(r);
         db.SubmitChanges();
         newRv.ItemId = r.ItemId;
         return newRv.ItemId >= 0;
     }
 }