/// <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 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>
        /// 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>
 /// 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;
     }
 }