public ActionResult Delete(string id = "")
        {
            Logger.Debug("Inside Organization Controller- Delete");
            try
            {
                if (Session["OrganizationGUID"] != null)
                {
                    OrganizationView organization = new OrganizationView();
                    organization.OrganizationGUID = new Guid(id);
                    OrganizationSubscription orgSubscription = _IOrganizationSubscriptionRepository.GetOrganizationSubscriptionByOrgID(organization.OrganizationGUID);
                    if (orgSubscription != null)
                    {
                        _IUserSubscriptionRepository.DeleteUserSubscriptionByOrgSubID(orgSubscription.OrganizationSubscriptionGUID);

                        _IOrganizationSubscriptionRepository.DeleteOrganizationSubscription(orgSubscription.OrganizationSubscriptionGUID);
                        // _IOrganizationSubscriptionRepository.Save();
                    }

                    _IMarketRepository.DeleteMarketByOrganizationGUID(organization.OrganizationGUID);

                    List<OrganizationUsersMap> OrganizationuserList = _IOrganizationRepository.GetOrganizationUserMapByOrgGUID(new Guid(id));

                    _IOrganizationRepository.DeleteOrganizationUserMapByOrganizationGUID(organization.OrganizationGUID);

                    foreach (OrganizationUsersMap item in OrganizationuserList)
                    {

                        if (_IGlobalUserRepository.DeleteGlobalUser(item.UserGUID) > 0)
                        {
                            _IUserProfileRepository.DeleteUserProfileByUserGUID(item.UserGUID);
                            //_IUserProfileRepository.Save();
                        }
                    }

                    // _IUserProfileRepository.DeleteUserProfileByOrganizationGUID(organization.OrganizationGUID);
                    // _IGroupRepository.DeleteGroupByOrganizationGUID(organization.OrganizationGUID);
                    _ITerritoryRepository.DeleteTerritoryByOrganizationGUID(organization.OrganizationGUID);
                    _IRegionRepository.DeleteRegionByOrganizationGUID(organization.OrganizationGUID);
                    _IUserRepository.DeleteByOrganizationGUID(organization.OrganizationGUID);
                    int result = _IOrganizationRepository.DeleteOrganization(organization.OrganizationGUID);
                    //int result = _IOrganizationRepository.Save();
                    _IUserRepository.DeleteByOrganizationGUID(organization.OrganizationGUID);

                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("SessionTimeOut", "User");
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return RedirectToAction("Index");
            }
        }
        public ActionResult Create(OrganizationView organization)
        {
            Logger.Debug("Inside Organization Controller- Create HttpPost");
            try
            {
                if (ModelState.IsValid)
                {
                    if (_IOrganizationRepository.GetOrganizationByID(organization.OrganizationGUID) == null)
                    {
                        Organization Organization = new Organization();
                        Organization.OrganizationFullName = organization.OrganizationFullName;
                        Organization.OrganizationName = organization.OrganizationFullName.Trim();
                        Organization.OrganizationGUID = Guid.NewGuid();
                        Organization.Website = organization.Website;
                        Organization.Phone = organization.Phone;
                        Organization.AddressLine1 = organization.AddressLine1;
                        Organization.AddressLine2 = organization.AddressLine2;
                        Organization.City = organization.City;
                        Organization.Country = organization.Country;
                        Organization.ZipCode = organization.ZipCode;
                        LatLong latLong = new LatLong();
                        latLong = GetLatLngCode(Organization.AddressLine1, Organization.AddressLine2, Organization.City, Organization.State, Organization.Country, Organization.ZipCode);
                        Organization.TimeZone = Convert.ToDouble(getTimeZone(latLong.Latitude, latLong.Longitude));
                        Organization.Latitude = latLong.Latitude;
                        Organization.Longitude = latLong.Longitude;
                        Organization.EmailID = organization.EmailID;
                        Organization.State = organization.State;
                        Organization.IsActive = false;
                        Organization.IsDeleted = false;
                        Organization.AllowContractors = true;
                        Organization.ImageURL = organization.ImageURL;
                        Organization.CreateDate = DateTime.UtcNow;
                        if (Session["UserGUID"] != null)
                            Organization.CreateBy = new Guid(Session["UserGUID"].ToString());
                        Organization.LastModifiedDate = DateTime.UtcNow;
                        if (Session["UserGUID"] != null)
                            Organization.LastModifiedBy = new Guid(Session["UserGUID"].ToString());
                        //Group Group = new Group();
                        //Group.GroupGUID = Guid.NewGuid();
                        //Group.Name = "Default";
                        //Group.Description = "Default";
                        //Group.OrganizationGUID = Organization.OrganizationGUID;
                        //Group.IsDefault = false;

                        Region Region = new Region();
                        Region.RegionGUID = Guid.NewGuid();
                        Region.Name = "Default";
                        Region.Description = "Default";
                        if (Organization.OrganizationGUID != Guid.Empty)
                        {
                            Region.OrganizationGUID = Organization.OrganizationGUID;
                        }
                        else
                        {
                            Region.OrganizationGUID = null;
                        }
                        Region.IsDefault = false;

                        Territory Territory = new Territory();
                        Territory.TerritoryGUID = Guid.NewGuid();
                        if (Territory.RegionGUID != Guid.Empty)
                        {
                            Territory.RegionGUID = Territory.RegionGUID;
                        }
                        else
                        {
                            Territory.RegionGUID = null;
                        }
                        Territory.Name = "Default";
                        Territory.Description = "Default";
                        if (Organization.OrganizationGUID != Guid.Empty)
                        {
                            Territory.OrganizationGUID = Organization.OrganizationGUID;
                        }
                        else
                        {
                            Territory.OrganizationGUID = null;
                        }
                        Territory.IsDefault = false;




                        GlobalUser globalUser = new GlobalUser();
                        globalUser.UserGUID = Guid.NewGuid();
                        globalUser.USERID = organization.UserID;
                        globalUser.UserName = organization.UserName;
                        globalUser.Password = _IUserRepository.EncodeTo64(organization.Password);
                        globalUser.IsActive = true;
                        globalUser.IsDelete = false;
                        globalUser.Latitude = latLong.Latitude;
                        globalUser.Longitude = latLong.Longitude;
                        globalUser.Role_Id = _IGlobalUserRepository.GetOrganizationAdminRoleID();
                        globalUser.CreateDate = DateTime.UtcNow;
                        if (Session["UserGUID"] != null)
                            globalUser.CreateBy = new Guid(Session["UserGUID"].ToString());
                        globalUser.LastModifiedDate = DateTime.UtcNow;
                        if (Session["UserGUID"] != null)
                            globalUser.LastModifiedBy = new Guid(Session["UserGUID"].ToString());

                        UserProfile userprofile = new UserProfile();
                        userprofile.ProfileGUID = Guid.NewGuid();
                        userprofile.UserGUID = globalUser.UserGUID;
                        userprofile.CompanyName = Organization.OrganizationFullName;
                        userprofile.FirstName = organization.FirstName;
                        userprofile.LastName = organization.LastName;
                        userprofile.MobilePhone = "";
                        userprofile.BusinessPhone = organization.Phone;
                        userprofile.HomePhone = "";
                        userprofile.EmailID = organization.EmailID;
                        userprofile.AddressLine1 = organization.AddressLine1;
                        userprofile.AddressLine2 = organization.AddressLine2;
                        userprofile.City = organization.City;
                        userprofile.State = organization.State;
                        userprofile.Country = organization.Country;
                        userprofile.Latitude = latLong.Latitude;
                        userprofile.Longitude = latLong.Longitude;
                        userprofile.ZipCode = organization.ZipCode;
                        userprofile.IsDeleted = false;
                        userprofile.PicFileURL = Organization.ImageURL;
                        userprofile.LastModifiedDate = DateTime.UtcNow;
                        if (Session["UserGUID"] != null)
                            userprofile.LastModifiedBy = new Guid(Session["UserGUID"].ToString());


                        userprofile.CompanyName = Organization.OrganizationName;

                        AspNetUser aspnetuser = new AspNetUser();
                        aspnetuser.Id = globalUser.UserGUID.ToString();
                        aspnetuser.UserName = organization.UserName;
                        aspnetuser.FirstName = organization.FirstName;
                        aspnetuser.LastName = organization.LastName;
                        aspnetuser.PasswordHash = _IUserRepository.EncodeTo64(organization.Password);
                        aspnetuser.PhoneNumber = organization.Phone;
                        aspnetuser.EmailID = organization.EmailID;
                        if (Organization.OrganizationGUID != Guid.Empty)
                        {
                            aspnetuser.OrganizationGUID = Organization.OrganizationGUID;
                        }
                        else
                        {
                            aspnetuser.OrganizationGUID = null;
                        }
                        aspnetuser.SecurityStamp = "";
                        aspnetuser.Discriminator = "";

                        OrganizationSubscription organizationSubscription = new OrganizationSubscription();
                        organizationSubscription.OrganizationSubscriptionGUID = Guid.NewGuid();
                        organizationSubscription.OrganizationGUID = Organization.OrganizationGUID;
                        organizationSubscription.IsActive = true;
                        organizationSubscription.Version = 1;
                        organizationSubscription.SubscriptionPurchased = 100;
                        organizationSubscription.SubscriptionConsumed = 1;
                        organizationSubscription.StartDate = DateTime.UtcNow;
                        organizationSubscription.ExpiryDate = DateTime.UtcNow.AddDays(30);
                        organizationSubscription.CreatedDate = DateTime.UtcNow;


                        UserSubscription userSubscription = new UserSubscription();
                        userSubscription.UserSubscriptionGUID = Guid.NewGuid();
                        userSubscription.UserGUID = globalUser.UserGUID;
                        if (organizationSubscription.OrganizationSubscriptionGUID != Guid.Empty)
                        {
                            userSubscription.OrganizationSubscriptionGUID = organizationSubscription.OrganizationSubscriptionGUID;
                        }
                        else
                        {
                            userSubscription.OrganizationSubscriptionGUID = null;
                        }

                        userSubscription.IsActive = true;
                        userSubscription.CreatedDate = DateTime.UtcNow;

                        Market Market = new Market();
                        Market.MarketGUID = Guid.NewGuid();
                        Market.IsDefault = true;
                        if (globalUser.UserGUID != Guid.Empty)
                        {
                            Market.UserGUID = globalUser.UserGUID;
                        }
                        else
                        {
                            Market.UserGUID = null;
                        }
                        Market.EntityType = 0;
                        if (Organization.OrganizationGUID != Guid.Empty)
                        {
                            Market.OrganizationGUID = Organization.OrganizationGUID;
                        }
                        else
                        {
                            Market.OrganizationGUID = null;
                        }
                        if (Organization.OrganizationGUID != Guid.Empty)
                        {
                            Market.OwnerGUID = Organization.OrganizationGUID;
                        }
                        else
                        {
                            Market.OwnerGUID = null;
                        }
                        Market.MarketName = Organization.OrganizationFullName;
                        if (Region.RegionGUID != Guid.Empty)
                        {
                            Market.RegionGUID = Region.RegionGUID;
                        }
                        else
                        {
                            Market.RegionGUID = null;
                        }
                        if (Territory.TerritoryGUID != Guid.Empty)
                        {
                            Market.TerritoryGUID = Territory.TerritoryGUID;
                        }
                        else
                        {
                            Market.TerritoryGUID = null;
                        }
                        if (globalUser.UserGUID != Guid.Empty)
                        {
                            Market.PrimaryContactGUID = globalUser.UserGUID;
                        }
                        else
                        {
                            Market.PrimaryContactGUID = null;
                        }
                        Market.FirstName = organization.FirstName;
                        Market.LastName = organization.LastName;
                        Market.MobilePhone = "";
                        Market.MarketPhone = organization.Phone;
                        Market.HomePhone = "";
                        Market.Emails = organization.EmailID;
                        Market.TimeZone = Organization.TimeZone.ToString();
                        Market.AddressLine1 = organization.AddressLine1;
                        Market.AddressLine2 = organization.AddressLine2;
                        Market.City = organization.City;
                        Market.State = organization.State;
                        Market.Country = organization.Country;
                        Market.ZipCode = organization.ZipCode;
                        Market.Latitude = latLong.Latitude;
                        Market.Longitude = latLong.Longitude;
                        Market.ImageURL = organization.ImageURL;
                        Market.IsDeleted = false;
                        Market.CreateDate = DateTime.UtcNow;
                        Market.UpdatedDate = DateTime.UtcNow;

                        OrganizationUsersMap organizationUserMap = new OrganizationUsersMap();
                        organizationUserMap.OrganizationUserMapGUID = Guid.NewGuid();
                        organizationUserMap.OrganizationGUID = Organization.OrganizationGUID;
                        organizationUserMap.UserGUID = globalUser.UserGUID;
                        organizationUserMap.IsContractor = false;
                        organizationUserMap.IsActive = true;
                        organizationUserMap.Status = 0;
                        if (Region.RegionGUID != Guid.Empty)
                        {
                            organizationUserMap.RegionGUID = Region.RegionGUID;
                        }
                        else
                        {
                            organizationUserMap.RegionGUID = null;
                        }
                        if (Territory.TerritoryGUID != Guid.Empty)
                        {
                            organizationUserMap.TerritoryGUID = Territory.TerritoryGUID;
                        }
                        else
                        {
                            organizationUserMap.TerritoryGUID = null;
                        }
                        organizationUserMap.UserType = "ENT_A";
                        organizationUserMap.CreateDate = DateTime.UtcNow;
                        if (Session["UserGUID"] != null)
                            organizationUserMap.CreateBy = new Guid(Session["UserGUID"].ToString());
                        organizationUserMap.LastModifiedDate = DateTime.UtcNow;
                        if (Session["UserGUID"] != null)
                            organizationUserMap.LastModifiedBy = new Guid(Session["UserGUID"].ToString());



                        int result = _IOrganizationRepository.InsertOrganization(Organization);
                        // int result = _IOrganizationRepository.Save();
                        if (result > 0)
                        {
                            _IOrganizationSubscriptionRepository.InsertOrganizationSubscription(organizationSubscription);
                            //_IOrganizationSubscriptionRepository.Save();
                            //_IGroupRepository.InsertGroup(Group);
                            //_IGroupRepository.Save();
                            _IRegionRepository.InsertRegion(Region);
                            //_IRegionRepository.Save();
                            _ITerritoryRepository.InsertTerritory(Territory);
                            // _ITerritoryRepository.Save();

                            int userresult = _IUserRepository.InsertUser(aspnetuser);
                            // int userresult = _IUserRepository.Save();
                            if (userresult > 0)
                            {
                                int guresult = _IGlobalUserRepository.InsertGlobalUser(globalUser);
                                //int guresult = _IGlobalUserRepository.Save();

                                if (guresult > 0)
                                {
                                    int OrgUserMap = _IOrganizationRepository.InsertOrganizationUserMap(organizationUserMap);
                                    //int OrgUserMap = _IOrganizationRepository.Save();
                                    if (OrgUserMap > 0)
                                    {
                                        int usrresult = _IUserProfileRepository.InsertUserProfile(userprofile);
                                        //int usrresult = _IUserProfileRepository.Save();
                                        if (usrresult > 0)
                                        {
                                            int usubresult = _IUserSubscriptionRepository.InsertUserSubscription(userSubscription);
                                            //int usubresult = _IUserSubscriptionRepository.Save();
                                            if (usubresult > 0)
                                            {
                                                int marketresult = _IMarketRepository.InsertMarket(Market);
                                                //int marketresult = _IMarketRepository.Save();
                                                if (marketresult > 0)
                                                {
                                                    if (Session["UserType"] != null && Session["UserType"].ToString() == "WIM_A")
                                                    {
                                                        TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion','Organization Created Successfully');</script>";
                                                        return RedirectToAction("Index", "Organization");
                                                    }
                                                    else
                                                    {
                                                        TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion','Organization Created Successfully');</script>";
                                                        return RedirectToAction("../User/Login");
                                                    }
                                                }
                                                else
                                                {
                                                    _IUserSubscriptionRepository.DeleteUserSubscription(userSubscription.UserSubscriptionGUID);
                                                    //_IUserSubscriptionRepository.Save();
                                                    //_IGroupRepository.DeleteGroup(Group.GroupGUID);
                                                    //_IGroupRepository.Save();
                                                    _ITerritoryRepository.DeleteTerritoryByRegionGUID(Region.RegionGUID);
                                                    //_ITerritoryRepository.Save();
                                                    _IRegionRepository.DeleteRegion(Region.RegionGUID);
                                                    //_IRegionRepository.Save();


                                                    _IUserSubscriptionRepository.DeleteUserSubscription(userSubscription.UserSubscriptionGUID);
                                                    //_IUserSubscriptionRepository.Save();
                                                    _IUserProfileRepository.DeleteUserProfile(userprofile.ProfileGUID);
                                                    //_IUserProfileRepository.Save();
                                                    _IGlobalUserRepository.DeleteGlobalUser(globalUser.UserGUID);
                                                    //_IGlobalUserRepository.Save();
                                                    _IUserRepository.DeleteUser(aspnetuser.Id);
                                                    // _IUserRepository.Save();
                                                }
                                            }
                                            else
                                            {
                                                //_IGroupRepository.DeleteGroup(Group.GroupGUID);
                                                //_IGroupRepository.Save();
                                                _ITerritoryRepository.DeleteTerritoryByRegionGUID(Region.RegionGUID);
                                                //_ITerritoryRepository.Save();
                                                _IRegionRepository.DeleteRegion(Region.RegionGUID);
                                                // _IRegionRepository.Save();


                                                _IUserSubscriptionRepository.DeleteUserSubscription(userSubscription.UserSubscriptionGUID);
                                                //_IUserSubscriptionRepository.Save();
                                                _IUserProfileRepository.DeleteUserProfile(userprofile.ProfileGUID);
                                                //_IUserProfileRepository.Save();
                                                _IGlobalUserRepository.DeleteGlobalUser(globalUser.UserGUID);
                                                //_IGlobalUserRepository.Save();
                                                _IUserRepository.DeleteUser(aspnetuser.Id);
                                                //_IUserRepository.Save();

                                                _IOrganizationSubscriptionRepository.DeleteOrganizationSubscription(organizationSubscription.OrganizationSubscriptionGUID);
                                                //_IOrganizationSubscriptionRepository.Save();
                                                //_IUserSubscriptionRepository.Save();
                                                _IOrganizationRepository.DeleteOrganization(Organization.OrganizationGUID);
                                                //_IOrganizationRepository.Save();
                                            }
                                        }
                                        else
                                        {
                                            _IOrganizationRepository.DeleteOrganizationUserMap(organizationUserMap.OrganizationUserMapGUID);
                                            //_IOrganizationRepository.Save();
                                            _ITerritoryRepository.DeleteTerritoryByRegionGUID(Region.RegionGUID);
                                            //_ITerritoryRepository.Save();
                                            _IRegionRepository.DeleteRegion(Region.RegionGUID);
                                            //_IRegionRepository.Save();


                                            _IUserSubscriptionRepository.DeleteUserSubscription(userSubscription.UserSubscriptionGUID);
                                            //_IUserSubscriptionRepository.Save();
                                            _IUserProfileRepository.DeleteUserProfile(userprofile.ProfileGUID);
                                            //_IUserProfileRepository.Save();
                                            _IGlobalUserRepository.DeleteGlobalUser(globalUser.UserGUID);
                                            //_IGlobalUserRepository.Save();
                                            _IUserRepository.DeleteUser(aspnetuser.Id);
                                            //_IUserRepository.Save();

                                            _IOrganizationSubscriptionRepository.DeleteOrganizationSubscription(organizationSubscription.OrganizationSubscriptionGUID);
                                            //_IOrganizationSubscriptionRepository.Save();
                                            //_IUserSubscriptionRepository.Save();
                                            _IOrganizationRepository.DeleteOrganization(Organization.OrganizationGUID);
                                            //_IOrganizationRepository.Save();
                                        }
                                    }
                                    else
                                    {
                                        //_IGroupRepository.DeleteGroup(Group.GroupGUID);
                                        //_IGroupRepository.Save();
                                        _ITerritoryRepository.DeleteTerritoryByRegionGUID(Region.RegionGUID);
                                        //_ITerritoryRepository.Save();
                                        _IRegionRepository.DeleteRegion(Region.RegionGUID);
                                        // _IRegionRepository.Save();


                                        _IUserSubscriptionRepository.DeleteUserSubscription(userSubscription.UserSubscriptionGUID);
                                        //_IUserSubscriptionRepository.Save();
                                        _IUserProfileRepository.DeleteUserProfile(userprofile.ProfileGUID);
                                        // _IUserProfileRepository.Save();
                                        _IGlobalUserRepository.DeleteGlobalUser(globalUser.UserGUID);
                                        //_IGlobalUserRepository.Save();
                                        _IUserRepository.DeleteUser(aspnetuser.Id);
                                        //_IUserRepository.Save();

                                        _IOrganizationSubscriptionRepository.DeleteOrganizationSubscription(organizationSubscription.OrganizationSubscriptionGUID);
                                        //_IOrganizationSubscriptionRepository.Save();
                                        //_IUserSubscriptionRepository.Save();
                                        _IOrganizationRepository.DeleteOrganization(Organization.OrganizationGUID);
                                        //_IOrganizationRepository.Save();
                                    }
                                }
                                else
                                {
                                    _IGlobalUserRepository.DeleteGlobalUser(globalUser.UserGUID);
                                    //_IGlobalUserRepository.Save();
                                    _IUserRepository.DeleteUser(aspnetuser.Id);
                                    // _IUserRepository.Save();
                                    _IOrganizationRepository.DeleteOrganization(Organization.OrganizationGUID);
                                    //_IOrganizationRepository.Save();
                                }
                            }
                            else
                            {
                                _IUserRepository.DeleteUser(aspnetuser.Id);
                                // _IUserRepository.Save();
                                _IOrganizationSubscriptionRepository.DeleteOrganizationSubscription(organizationSubscription.OrganizationSubscriptionGUID);
                                //_IOrganizationSubscriptionRepository.Save();
                                _IOrganizationRepository.DeleteOrganization(Organization.OrganizationGUID);
                                // _IOrganizationRepository.Save();
                            }
                        }
                        else
                        {
                            _IOrganizationRepository.DeleteOrganization(Organization.OrganizationGUID);
                            //_IOrganizationRepository.Save();
                        }
                    }
                    else
                    {
                        //UserName already exists
                        TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion','Organization Name is Aleady Exists');</script>";
                    }
                }
                return View(organization);

            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return View(organization);
            }
        }