public ActionResult Delete(string id = "", string id1 = "")
        {
            Logger.Debug("Inside Territory Controller- Delete");
            try
            {
                if (Session["OrganizationGUID"] != null)
                {
                    int countCustomerStop = _IMarketRepository.GetMarketByTerritoryGUID(new Guid(id1), 1).Count();
                    int countgobalUser = _IGlobalUserRepository.GetGlobalUserByRegionandTerritory(new Guid(id), new Guid(id1)).Count();
                    if (countCustomerStop == 0 && countgobalUser == 0)
                    {
                        TerritoryModel territory = new TerritoryModel();
                        territory.RegionGUID = id;
                        territory.TerritoryGUID = id1;
                        _IMarketRepository.DeleteMarketByTerritoryGUID(new Guid(territory.TerritoryGUID));
                        _ITerritoryRepository.DeleteTerritory(new Guid(territory.TerritoryGUID));
                        //_ITerritoryRepository.Save();
                        return RedirectToAction("Index", "MyCompany", new { id = "Markets", regionguid = id });
                    }
                    else
                    {
                        if (countCustomerStop > 0 && countgobalUser > 0)
                        {
                            TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion','Territory is associate with Users and Store.');</script>";
                        }
                        else if (countCustomerStop > 0)
                        {
                            TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion','Territory is associate with Store.');</script>";
                        }
                        else if (countgobalUser > 0)
                        {
                            TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion','Territory is associate with Users.');</script>";
                        }
                        return RedirectToAction("Index", "MyCompany", new { id = "Markets", territoryguid = id1 });
                    }


                }
                else
                {
                    return RedirectToAction("SessionTimeOut", "User");
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return RedirectToAction("../MyCompany/Index/Markets");
            }
        }
 public ActionResult Edit(string id = "")
 {
     Logger.Debug("Inside User Controller- Edit");
     try
     {
         if (Session["OrganizationGUID"] != null)
         {
             TerritoryModel territory = new TerritoryModel();
             territory.TerritoryGUID = id;
             Territory Territory = _ITerritoryRepository.GetTerritoryByID(new Guid(territory.TerritoryGUID));
             if (Territory == null)
             {
                 return HttpNotFound();
             }
             else
             {
                 territory.TerritoryGUID = Territory.TerritoryGUID.ToString();
                 territory.RegionGUID = Territory.RegionGUID != null ? Territory.RegionGUID.ToString() : Guid.Empty.ToString();
                 //   region.RegionGUID = Region.RegionGUID.ToString();
                 territory.Name = Territory.Name;
                 ViewBag.TerritoryName = Territory.Name;
                 territory.Description = Territory.Description;
                 territory.TerritoryID = Territory.TerritoryID;
                 territory.OrganizationGUID = Territory.OrganizationGUID != null ? Territory.OrganizationGUID.ToString() : Guid.Empty.ToString();
             }
             return View(territory);
         }
         else
         {
             return RedirectToAction("SessionTimeOut", "User");
         }
     }
     catch (Exception ex)
     {
         Logger.Error(ex.Message);
         return View();
     }
 }
        public ActionResult Edit(TerritoryModel territory)
        {
            Logger.Debug("Inside Region Controller- Edit HttpPost");
            try
            {
                if (Session["OrganizationGUID"] != null)
                {
                    if (ModelState.IsValid)
                    {
                        //Territory _territory = _ITerritoryRepository.GetTerritoryByTerritoryID(territory.TerritoryID, new Guid(Session["OrganizationGUID"].ToString()));
                        //if (_territory != null)
                        //{
                        Territory Territory = new Territory();
                        Territory.TerritoryGUID = new Guid(territory.TerritoryGUID);
                        if (!string.IsNullOrEmpty(territory.RegionGUID) && territory.RegionGUID != Guid.Empty.ToString())
                        {
                            Territory.RegionGUID = new Guid(territory.RegionGUID);
                        }
                        else
                        {
                            Territory.RegionGUID = null;
                        }
                        Territory.Name = territory.Name;
                        Territory.Description = territory.Description;
                        if (!string.IsNullOrEmpty(territory.OrganizationGUID) && territory.OrganizationGUID != Guid.Empty.ToString())
                        {
                            Territory.OrganizationGUID = new Guid(territory.OrganizationGUID);
                        }
                        else
                        {
                            Territory.OrganizationGUID = null;
                        }
                        Territory.TerritoryID = territory.TerritoryID;
                        Territory.IsDefault = false;
                        _ITerritoryRepository.UpdateTerritory(Territory);
                        // _ITerritoryRepository.Save();
                        return RedirectToAction("Index", "MyCompany", new { id = "Markets", territoryguid = territory.TerritoryGUID });
                    }
                    //}

                    return View(territory);
                }
                else
                {
                    return RedirectToAction("SessionTimeOut", "User");
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return View(territory);
            }
        }
 public ActionResult Create(TerritoryModel territory, string id = "")
 {
     Logger.Debug("Inside Territory Controller- Create HttpPost");
     try
     {
         if (Session["OrganizationGUID"] != null)
         {
             if (ModelState.IsValid)
             {
                 Territory _territory = _ITerritoryRepository.GetTerritoryByTerritoryID(territory.TerritoryID, new Guid(Session["OrganizationGUID"].ToString()));
                 if (_territory == null)
                 {
                     Territory Territory = new Territory();
                     Territory.TerritoryGUID = Guid.NewGuid();
                     if (!string.IsNullOrEmpty(id) && id != Guid.Empty.ToString())
                     {
                         Territory.RegionGUID = new Guid(id);
                     }
                     else
                     {
                         Territory.RegionGUID = null;
                     }
                     Territory.TerritoryID = territory.TerritoryID;
                     Territory.Name = territory.Name;
                     Territory.Description = territory.Description;
                     Territory.OrganizationGUID = new Guid(Session["OrganizationGUID"].ToString());
                     Territory.IsDefault = false;
                     _ITerritoryRepository.InsertTerritory(Territory);
                     // _ITerritoryRepository.Save();
                     return RedirectToAction("Index", "MyCompany", new { id = "Markets", regionguid = Territory.RegionGUID });
                 }
                 else
                 {
                     TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion','MarketID already exists');</script>";
                     if (!string.IsNullOrEmpty(id))
                         ViewBag.RegionGUID = id;
                 }
             }
             else
             {
                 if (!string.IsNullOrEmpty(id))
                     ViewBag.RegionGUID = id;
             }
             return View(territory);
         }
         else
         {
             return RedirectToAction("SessionTimeOut", "User");
         }
     }
     catch (Exception ex)
     {
         Logger.Error(ex.Message);
         return View(territory);
     }
 }