public static void AssignCurrentTerritory(string assignedTo, string note, DateTime assignDate, DateTime returndate, bool onlineOnly)
        {
            var db = new Linq.TerritoryDBDataContext();
                var newAssignment = new Linq.TerritoryAssignment();
                newAssignment.TerritoryID = CurrentUser.TerritoryId;
                newAssignment.AssignedOn = assignDate;
                newAssignment.ExpiresOn = returndate;
                newAssignment.Note = note;
                newAssignment.AssignedTo = assignedTo;
                newAssignment.Guid = Guid.NewGuid();

                db.TerritoryAssignments.InsertOnSubmit(newAssignment);
                var territory = (from t in db.Territories
                                 where t.TerritoryId == CurrentUser.TerritoryId
                                 select t).First();
                territory.AssignmentGUID = newAssignment.Guid;
                territory.IsAssigned = true;
                territory.AssignedDate = assignDate;
                territory.AssignmentNotes = assignedTo;
                db.SubmitChanges();
                Territory.UpdateOnlineOnly(territory.TerritoryId);

                AssignPinsCurrentTerritory();
        }
 public static void UpdateOnlineOnly(int territoryID)
 {
     var db = new Linq.TerritoryDBDataContext();
     var outOfSyncCount = (from ta in db.TerritoryAssignments
                           where ta.TerritoryID == territoryID
                           & ta.OnlineOnly == true
                           select ta).Count();
     bool onlineOnly = outOfSyncCount > 0;
     var territory = (from t in db.Territories
              where t.TerritoryId == territoryID
              select t).First();
     territory.OnlineOnly = onlineOnly;
     db.SubmitChanges();
 }
 public static void ReturnTerritory(int territoryId, string note, DateTime returnDate)
 {
     var db = new Linq.TerritoryDBDataContext();
     var territory = (from t in db.Territories
                      where t.TerritoryId == territoryId
                      select t).First();
     var assignment = (from a in db.TerritoryAssignments
                       where a.Guid == territory.AssignmentGUID
                       select a).First();
     territory.IsAssigned = false;
     territory.AssignedDate = null;
     territory.AssignmentNotes = string.Empty;
     territory.IsBeingReturned = true;
     assignment.Guid = Guid.NewGuid();
     territory.AssignmentGUID = assignment.Guid;
     assignment.Note = note;
     assignment.ReturnedOn = returnDate;
     db.SubmitChanges();
 }
        public static void ReturnCurrentTerritory(string feedbackAssignedTo, string note, DateTime returnDate)
        {
            var db = new Linq.TerritoryDBDataContext();
            var territory = (from t in db.Territories
                             where t.TerritoryId == CurrentUser.TerritoryId
                             select t).First();
            var assignment = (from a in db.TerritoryAssignments
                              where a.Guid == territory.AssignmentGUID
                              select a).First();
            territory.IsAssigned = false;
            territory.AssignedDate = null;
            territory.AssignmentNotes = feedbackAssignedTo;
            territory.IsBeingReturned = true;

            assignment.Guid = Guid.NewGuid();
            territory.AssignmentGUID = assignment.Guid;
            assignment.Note = note;
            assignment.CleanupAssignedTo = feedbackAssignedTo;
            assignment.ReturnedOn = returnDate;

            db.SubmitChanges();
        }
 public static bool IsCurrentTerritoryReturned()
 {
     var db = new Linq.TerritoryDBDataContext();
     var territory = (from t in db.Territories
                      where t.TerritoryId == CurrentUser.TerritoryId
                      select t).First();
     if (territory.AssignmentGUID != null)
     {
         var assignment = (from a in db.TerritoryAssignments
                           where a.Guid == territory.AssignmentGUID
                           select a).FirstOrDefault();
         if (assignment != null)
         {
             if (assignment.ReturnedOn.HasValue)
             {
                 return true;
             }
         }
         else
         {
             territory.AssignmentGUID = null;
             db.SubmitChanges();
         }
     }
     return false;
 }
        public static void FeedBackCompleteTerritory(int territoryId)
        {
            var db = new Linq.TerritoryDBDataContext();

            var territory = (from t in db.Territories
                             where t.TerritoryId == territoryId
                             select t).First();
            territory.IsBeingReturned = false;
            territory.AssignmentGUID = null;
            db.SubmitChanges();

            var addressList = (from a in db.Addresses
                               where a.TerritoryId == territoryId
                               orderby a.StreetName, a.HouseNumber
                               select a
                               ).ToList<Linq.Address>();
            int count = 1;
            foreach (var address in addressList)
            {
                address.AssignmentPinNumber = 0;
                count++;
            }
            db.SubmitChanges();
        }