public ActionResult Login(sdtoUser u)
        {
            FormsAuthentication.SignOut();
            // this action is for handle post (login)
            //if (ModelState.IsValid) // this is check validity
            {
                if ((u.FinancialYearId != null && u.FinancialYearId > 0))
                {
                    using (LoanDBContext dc = new LoanDBContext())
                    {
                        var v = dc.User.Where(a => a.UserName.Equals(u.UserName) && a.Password.Equals(u.Password)).FirstOrDefault();
                        if (v != null)
                        {
                            var userSession = new sdtoUserSession() { UserId = v.UserID, CompanyId = v.CompanyId, StartTime = DateTime.Now, FinancialYearId = u.FinancialYearId, Browser = UtilityHelper.UtilityHelper.GetBrowser(), SessionKey = System.Guid.NewGuid().ToString("N"), IPAddress = Request.ServerVariables["REMOTE_ADDR"] };
                            db.UserSessions.Add(userSession);
                            db.SaveChanges();
                            v.UserSession = userSession;
                            FormsAuthentication.SetAuthCookie(u.UserName, false);
                            UtilityHelper.UserSession.SetSession(UtilityHelper.UserSession.LoggedInUser, v);
                            //Session["LogedUserFullname"] = v.FirstName.ToString() + " " + v.LastName.ToString();
                            return RedirectToAction("Index", "Home");
                        }
                        else
                            ModelState.AddModelError("", "Invalid login attempt.");
                    }
                }
                else
                    ModelState.AddModelError("", "Please select a financial period.");

            }
            var accFY = db.FinancialPeriod.Where(x => x.IsDeleted == false).ToList().Select(x => new SelectListItem() { Value = x.FinancialPeriodId.ToString(), Text = x.PeriodName }).ToList();
            accFY.Insert(0, new SelectListItem() { Value = "0", Text = "Select a Period Name" });
            ViewBag.FinancialYears = new SelectList(accFY, "Value", "Text", 0);
            return View();
        }
        public ActionResult Create(sdtoUser sdtouser)
        {
            if (ModelState.IsValid)
            {
                sdtouser.UserType = UserType.Executive;
                if (sdtouser.UserAddress != null)
                {
                    sdtouser.UserAddress.CreatedOn = DateTime.Now;
                    sdtouser.UserAddress.ModifiedOn = DateTime.Now;
                }

                if (sdtouser.Contacts != null)
                {
                    sdtouser.Contacts.CreatedOn = DateTime.Now;
                    sdtouser.Contacts.ModifiedOn = DateTime.Now;
                }

                if (sdtouser.UserAddress != null)
                {
                    sdtouser.UserAddress.CountryId = sdtouser.UserAddress.CountryId == 0 ? null : sdtouser.UserAddress.CountryId;
                    sdtouser.UserAddress.StateId = sdtouser.UserAddress.StateId == 0 ? null : sdtouser.UserAddress.StateId;
                    sdtouser.UserAddress.DistrictId = sdtouser.UserAddress.DistrictId == 0 ? null : sdtouser.UserAddress.DistrictId;
                    sdtouser.UserAddress.TalukId = sdtouser.UserAddress.TalukId == 0 ? null : sdtouser.UserAddress.TalukId;
                    sdtouser.UserAddress.VillageId = sdtouser.UserAddress.VillageId == 0 ? null : sdtouser.UserAddress.VillageId;
                }

                sdtouser.GuaranterAddress = null;
                sdtouser.PermanentAddress = null;
                sdtouser.PermanentAddressId = null;
                sdtouser.GuaranterAddressId = null;

                sdtouser.Contacts = db.Contacts.Add(sdtouser.Contacts);
                sdtouser.UserAddress = db.Address.Add(sdtouser.UserAddress);
                sdtouser.UserType = UserType.Executive;
                db.User.Add(sdtouser);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.AddressId = new SelectList(db.Address, "AddressId", "Address1", sdtouser.UserAddressId);
            ViewBag.ContactId = new SelectList(db.Contacts, "ContactId", "ContactName", sdtouser.UserContactId);
            //ViewBag.UserGroupId = new SelectList(db.Usergroup, "UserGroupId", "Name", sdtouser.UserGroupId);
            var countries = db.Countries.ToList();
            countries.Insert(0, new sdtoCountry() { CountryId = 0, CountryName = "Select Country" });
            ViewBag.UserAddressCountryList = new SelectList(countries, "CountryId", "CountryName", 0);
            ViewBag.StateList = new SelectList(db.States, "StateId", "StateName", 0);
            return View(sdtouser);
        }
        // GET: Settings/Create
        public ActionResult Create()
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();

            var items = new SelectList(db.Companies, "CompanyId", "CompanyName");
            ViewBag.CompanyList = items;
            if (items == null || items.Count() == 0)
            {
                TempData["ERR_PREMATURE_MESSAGE"] = "Please register a company for configuring settings";
                return RedirectToAction("Create", "Company");
            }

            var settings = db.GeneralSettings.Where(x => x.CompanyId == userDetails.CompanyId);
            if (settings != null && settings.Count() > 0)
                return EditOnCreate(settings.FirstOrDefault(x => x.CompanyId == userDetails.CompanyId) as sdtoSettings);
            else
                return View(new sdtoSettings() { CompanyId = userDetails.CompanyId, BankCharges = 0, BankInterest = 0 });
        }
        public ActionResult Edit(sdtoUser user, HttpPostedFileBase ProfileImage)
        {
            if (ModelState.IsValid)
            {
                user.UserType = UserType.User;
                user.ModifiedBy = CurrentUserSession.UserId;
                user.ModifiedOn = DateTime.Now;

                user.GuaranterAddressId = null;
                user.PermanentAddressId = null;
                user.UserAddressId = null;
                user.UserAddress = null;
                user.PermanentAddress = null;
                user.GuaranterAddress = null;

                if (user.UserAddress != null)
                {
                    user.UserAddress.CountryId = user.UserAddress.CountryId == 0 ? null : user.UserAddress.CountryId;
                    user.UserAddress.StateId = user.UserAddress.StateId == 0 ? null : user.UserAddress.StateId;
                    user.UserAddress.DistrictId = user.UserAddress.DistrictId == 0 ? null : user.UserAddress.DistrictId;
                    user.UserAddress.TalukId = user.UserAddress.TalukId == 0 ? null : user.UserAddress.TalukId;
                    user.UserAddress.VillageId = user.UserAddress.VillageId == 0 ? null : user.UserAddress.VillageId;
                }

                if (user.PermanentAddress != null)
                {
                    user.PermanentAddress.CountryId = user.PermanentAddress.CountryId == 0 ? null : user.PermanentAddress.CountryId;
                    user.PermanentAddress.StateId = user.PermanentAddress.StateId == 0 ? null : user.PermanentAddress.StateId;
                    user.PermanentAddress.DistrictId = user.PermanentAddress.DistrictId == 0 ? null : user.PermanentAddress.DistrictId;
                    user.PermanentAddress.TalukId = user.PermanentAddress.TalukId == 0 ? null : user.PermanentAddress.TalukId;
                    user.PermanentAddress.VillageId = user.PermanentAddress.VillageId == 0 ? null : user.PermanentAddress.VillageId;
                }

                if (user.GuaranterAddress != null)
                {
                    user.GuaranterAddress.CountryId = user.GuaranterAddress.CountryId == 0 ? null : user.GuaranterAddress.CountryId;
                    user.GuaranterAddress.StateId = user.GuaranterAddress.StateId == 0 ? null : user.GuaranterAddress.StateId;
                    user.GuaranterAddress.DistrictId = user.GuaranterAddress.DistrictId == 0 ? null : user.GuaranterAddress.DistrictId;
                    user.GuaranterAddress.TalukId = user.GuaranterAddress.TalukId == 0 ? null : user.GuaranterAddress.TalukId;
                    user.GuaranterAddress.VillageId = user.GuaranterAddress.VillageId == 0 ? null : user.GuaranterAddress.VillageId;
                }

                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();

                if (ProfileImage != null)
                {
                    FileUpload(user.UserID, ProfileImage);

                    System.IO.FileInfo fInfo = new FileInfo(ViewBag.UserProfileAvatar);
                    fInfo.CopyTo(Path.Combine(fInfo.Directory.FullName, user.UserID + ".logo"), true);
                    fInfo.Delete();
                }

                SetDisplayMessage("User is saved successfully");
                return RedirectToAction("Index");
            }
            ViewBag.UserGroupList = new SelectList(db.Usergroup, "UserGroupId", "Code", user.UserGroupId);
            return View(user);
        }
        public ActionResult Create(sdtoUser sdtouser, HttpPostedFileBase ProfileImage)
        {
            sdtouser.ConfirmPassword = sdtouser.Password;
            if (string.IsNullOrEmpty(sdtouser.Code))
                ModelState.AddModelError("", "Code cannot be empty");
            else if (string.IsNullOrEmpty(sdtouser.FirstName))
                ModelState.AddModelError("", "FirstName cannot be empty");
            else if (string.IsNullOrEmpty(sdtouser.LastName))
                ModelState.AddModelError("", "LastName cannot be empty");
            //else if (string.IsNullOrEmpty(sdtouser.UserAddress.Address1))
            //    ModelState.AddModelError("", "Communication Address cannot be empty");
            else if (string.IsNullOrWhiteSpace(sdtouser.PermanentAddress.Address1)
                || string.IsNullOrWhiteSpace(sdtouser.PermanentAddress.Address2)
                || sdtouser.PermanentAddress.CountryId == 0
                || sdtouser.PermanentAddress.StateId == 0
                || sdtouser.PermanentAddress.DistrictId == 0
                || sdtouser.PermanentAddress.TalukId == 0
                || sdtouser.PermanentAddress.VillageId == 0)
                ModelState.AddModelError("", "Permanent Address cannot be empty");
            else if (ModelState.IsValid)
            {
                var validImageTypes = new string[]
                                        {
                                            "image/gif",
                                            "image/jpeg",
                                            "image/pjpeg",
                                            "image/png"
                                        };

                if (ProfileImage != null && !validImageTypes.Contains(ProfileImage.ContentType))
                {
                    ModelState.AddModelError("", "Please choose either a GIF, JPG or PNG image for ProfileImage");
                }
                else
                {
                    sdtouser.UserType = UserType.Member;
                    sdtouser.IsActive = true;
                    if (sdtouser.UserAddress != null)
                    {
                        sdtouser.UserAddress.CreatedOn = DateTime.Now;
                    }

                    if (sdtouser.Contacts != null)
                    {
                        sdtouser.Contacts.CreatedOn = DateTime.Now;
                    }
                    if (sdtouser.PermanentAddress != null)
                    {
                        sdtouser.PermanentAddress.CreatedOn = DateTime.Now;
                    }

                    if (sdtouser.PermanentContacts != null)
                    {
                        sdtouser.PermanentContacts.CreatedOn = DateTime.Now;
                    }
                    if (sdtouser.GuaranterAddress != null)
                    {
                        sdtouser.GuaranterAddress.CreatedOn = DateTime.Now;
                    }

                    if (sdtouser.GuaranterContacts != null)
                    {
                        sdtouser.GuaranterContacts.CreatedOn = DateTime.Now;
                        sdtouser.GuaranterContacts.ModifiedOn = DateTime.Now;
                    }

                    sdtouser.CreatedOn = DateTime.Now;
                    sdtouser.CreatedBy = CurrentUserSession.UserId;

                    if (sdtouser.UserAddress != null)
                    {
                        sdtouser.UserAddress.CountryId = sdtouser.UserAddress.CountryId == 0 ? null : sdtouser.UserAddress.CountryId;
                        sdtouser.UserAddress.StateId = sdtouser.UserAddress.StateId == 0 ? null : sdtouser.UserAddress.StateId;
                        sdtouser.UserAddress.DistrictId = sdtouser.UserAddress.DistrictId == 0 ? null : sdtouser.UserAddress.DistrictId;
                        sdtouser.UserAddress.TalukId = sdtouser.UserAddress.TalukId == 0 ? null : sdtouser.UserAddress.TalukId;
                        sdtouser.UserAddress.VillageId = sdtouser.UserAddress.VillageId == 0 ? null : sdtouser.UserAddress.VillageId;
                    }

                    if (sdtouser.PermanentAddress != null)
                    {
                        sdtouser.PermanentAddress.CountryId = sdtouser.PermanentAddress.CountryId == 0 ? null : sdtouser.PermanentAddress.CountryId;
                        sdtouser.PermanentAddress.StateId = sdtouser.PermanentAddress.StateId == 0 ? null : sdtouser.PermanentAddress.StateId;
                        sdtouser.PermanentAddress.DistrictId = sdtouser.PermanentAddress.DistrictId == 0 ? null : sdtouser.PermanentAddress.DistrictId;
                        sdtouser.PermanentAddress.TalukId = sdtouser.PermanentAddress.TalukId == 0 ? null : sdtouser.PermanentAddress.TalukId;
                        sdtouser.PermanentAddress.VillageId = sdtouser.PermanentAddress.VillageId == 0 ? null : sdtouser.PermanentAddress.VillageId;
                    }

                    if (sdtouser.GuaranterAddress != null)
                    {
                        sdtouser.GuaranterAddress.CountryId = sdtouser.GuaranterAddress.CountryId == 0 ? null : sdtouser.GuaranterAddress.CountryId;
                        sdtouser.GuaranterAddress.StateId = sdtouser.GuaranterAddress.StateId == 0 ? null : sdtouser.GuaranterAddress.StateId;
                        sdtouser.GuaranterAddress.DistrictId = sdtouser.GuaranterAddress.DistrictId == 0 ? null : sdtouser.GuaranterAddress.DistrictId;
                        sdtouser.GuaranterAddress.TalukId = sdtouser.GuaranterAddress.TalukId == 0 ? null : sdtouser.GuaranterAddress.TalukId;
                        sdtouser.GuaranterAddress.VillageId = sdtouser.GuaranterAddress.VillageId == 0 ? null : sdtouser.GuaranterAddress.VillageId;
                    }

                    sdtouser.UserAddress = db.Address.Add(sdtouser.UserAddress);
                    sdtouser.Contacts = db.Contacts.Add(sdtouser.Contacts);

                    sdtouser.PermanentAddress = db.Address.Add(sdtouser.PermanentAddress);
                    sdtouser.PermanentContacts = db.Contacts.Add(sdtouser.PermanentContacts);

                    sdtouser.GuaranterAddress = db.Address.Add(sdtouser.GuaranterAddress);
                    sdtouser.GuaranterContacts = db.Contacts.Add(sdtouser.GuaranterContacts);

                    db.User.Add(sdtouser);
                    db.SaveChanges();

                    if (ProfileImage != null)
                    {
                        FileUpload(sdtouser.UserID, ProfileImage);

                        System.IO.FileInfo fInfo = new FileInfo(ViewBag.UserProfileAvatar);
                        fInfo.CopyTo(Path.Combine(fInfo.Directory.FullName, sdtouser.UserID + ".logo"), true);
                        fInfo.Delete();
                    }
                    else
                    {
                        DirectoryInfo dirUser = new DirectoryInfo(HttpContext.Server.MapPath("~/").Trim("\\/ ".ToCharArray()) + "\\ContentUpload\\User\\Profile");
                        if (!dirUser.Exists)
                            dirUser.Create();

                        FileInfo objFile = new FileInfo(Path.Combine(HttpContext.Server.MapPath("~/").Trim("\\/ ".ToCharArray()) + "\\Content\\Images", "dummy-profile.png"));
                        objFile.CopyTo(Path.Combine(dirUser.FullName, sdtouser.UserID + ".logo"), true);
                    }

                    bfTransaction objAccTransaction = new bfTransaction(db);
                    objAccTransaction.InitiateMemberAccounts(sdtouser);

                    SetDisplayMessage("Member is created successfully");
                    return RedirectToAction("Index");
                }
            }

            ViewBag.AddressId = new SelectList(db.Address, "AddressId", "Address1", sdtouser.UserAddressId);
            ViewBag.ContactId = new SelectList(db.Contacts, "ContactId", "ContactName", sdtouser.UserContactId);
            ViewBag.GuaranterAddressId = new SelectList(db.Address, "AddressId", "Address1", sdtouser.GuaranterAddressId);
            ViewBag.GuaranterContactId = new SelectList(db.Contacts, "ContactId", "ContactName", sdtouser.GuaranterContactId);
            ViewBag.PermanentAddressId = new SelectList(db.Address, "AddressId", "Address1", sdtouser.PermanentAddressId);
            ViewBag.PermanentContactId = new SelectList(db.Contacts, "ContactId", "ContactName", sdtouser.PermanentContactId);
            var countries = db.Countries.ToList();
            countries.Insert(0, new sdtoCountry() { CountryId = 0, CountryName = "Select Country" });
            ViewBag.UserAddressCountryList = new SelectList(countries, "CountryId", "CountryName", sdtouser.UserAddress.CountryId);
            ViewBag.GuaranterAddressCountryList = new SelectList(countries, "CountryId", "CountryName", sdtouser.GuaranterAddress.CountryId);
            ViewBag.PermanentAddressCountryList = new SelectList(countries, "CountryId", "CountryName", sdtouser.PermanentAddress.CountryId);
            ViewBag.StateList = new SelectList(db.States, "StateId", "StateName", 0);
            //ViewBag.UserGroupId = new SelectList(db.Usergroup, "UserGroupId", "Name", sdtouser.UserGroupId);
            return View(sdtouser);
        }
        public ActionResult Edit(sdtoUser sdtouser, HttpPostedFileBase ProfileImage)
        {
            sdtouser.Password = sdtouser.ConfirmPassword;
            if (string.IsNullOrEmpty(sdtouser.Code))
                ModelState.AddModelError("", "Code cannot be empty");
            else if (string.IsNullOrEmpty(sdtouser.FirstName))
                ModelState.AddModelError("", "FirstName cannot be empty");
            else if (string.IsNullOrEmpty(sdtouser.LastName))
                ModelState.AddModelError("", "LastName cannot be empty");
            //else if (string.IsNullOrEmpty(sdtouser.UserAddress.Address1))
            //    ModelState.AddModelError("", "Communication Address cannot be empty");
            else if (string.IsNullOrWhiteSpace(sdtouser.PermanentAddress.Address1)
                || string.IsNullOrWhiteSpace(sdtouser.PermanentAddress.Address2)
                || sdtouser.PermanentAddress.CountryId == 0
                || sdtouser.PermanentAddress.StateId == 0
                || sdtouser.PermanentAddress.DistrictId == 0
                || sdtouser.PermanentAddress.TalukId == 0
                || sdtouser.PermanentAddress.VillageId == 0)
                ModelState.AddModelError("", "Permanent Address cannot be empty");

            if (ModelState.IsValid)
            {
                //var user  = db.User.Find(sdtouser.UserID);
                if (sdtouser.UserAddress != null)
                {
                    sdtouser.UserAddress.ModifiedOn = DateTime.Now;
                }

                if (sdtouser.Contacts != null)
                {
                    sdtouser.Contacts.ModifiedOn = DateTime.Now;
                    sdtouser.Contacts.ContactId = sdtouser.UserContactId == null ? 0 : sdtouser.UserContactId.Value;
                }
                if (sdtouser.PermanentAddress != null)
                {
                    sdtouser.PermanentAddress.ModifiedOn = DateTime.Now;
                }

                if (sdtouser.PermanentContacts != null)
                {
                    sdtouser.PermanentContacts.ModifiedOn = DateTime.Now;
                    sdtouser.PermanentContacts.ContactId = sdtouser.PermanentContactId == null ? 0 : sdtouser.PermanentContactId.Value;
                }
                if (sdtouser.GuaranterAddress != null)
                {
                    sdtouser.GuaranterAddress.ModifiedOn = DateTime.Now;
                }

                if (sdtouser.GuaranterContacts != null)
                {
                    sdtouser.GuaranterContacts.ModifiedOn = DateTime.Now;
                    sdtouser.GuaranterContacts.ContactId = sdtouser.GuaranterContactId == null ? 0 : sdtouser.GuaranterContactId.Value;
                }

                if (sdtouser.UserAddress != null)
                {
                    sdtouser.UserAddress.AddressId = sdtouser.UserAddressId == null ? 0 : sdtouser.UserAddressId.Value;
                    sdtouser.UserAddress.CountryId = sdtouser.UserAddress.CountryId == 0 ? null : sdtouser.UserAddress.CountryId;
                    sdtouser.UserAddress.StateId = sdtouser.UserAddress.StateId == 0 ? null : sdtouser.UserAddress.StateId;
                    sdtouser.UserAddress.DistrictId = sdtouser.UserAddress.DistrictId == 0 ? null : sdtouser.UserAddress.DistrictId;
                    sdtouser.UserAddress.TalukId = sdtouser.UserAddress.TalukId == 0 ? null : sdtouser.UserAddress.TalukId;
                    sdtouser.UserAddress.VillageId = sdtouser.UserAddress.VillageId == 0 ? null : sdtouser.UserAddress.VillageId;
                }

                if (sdtouser.PermanentAddress != null)
                {
                    sdtouser.PermanentAddress.AddressId = sdtouser.PermanentAddressId == null ? 0 : sdtouser.PermanentAddressId.Value;
                    sdtouser.PermanentAddress.CountryId = sdtouser.PermanentAddress.CountryId == 0 ? null : sdtouser.PermanentAddress.CountryId;
                    sdtouser.PermanentAddress.StateId = sdtouser.PermanentAddress.StateId == 0 ? null : sdtouser.PermanentAddress.StateId;
                    sdtouser.PermanentAddress.DistrictId = sdtouser.PermanentAddress.DistrictId == 0 ? null : sdtouser.PermanentAddress.DistrictId;
                    sdtouser.PermanentAddress.TalukId = sdtouser.PermanentAddress.TalukId == 0 ? null : sdtouser.PermanentAddress.TalukId;
                    sdtouser.PermanentAddress.VillageId = sdtouser.PermanentAddress.VillageId == 0 ? null : sdtouser.PermanentAddress.VillageId;
                }

                if (sdtouser.GuaranterAddress != null)
                {
                    sdtouser.GuaranterAddress.AddressId = sdtouser.GuaranterAddressId == null ? 0 : sdtouser.GuaranterAddressId.Value;
                    sdtouser.GuaranterAddress.CountryId = sdtouser.GuaranterAddress.CountryId == 0 ? null : sdtouser.GuaranterAddress.CountryId;
                    sdtouser.GuaranterAddress.StateId = sdtouser.GuaranterAddress.StateId == 0 ? null : sdtouser.GuaranterAddress.StateId;
                    sdtouser.GuaranterAddress.DistrictId = sdtouser.GuaranterAddress.DistrictId == 0 ? null : sdtouser.GuaranterAddress.DistrictId;
                    sdtouser.GuaranterAddress.TalukId = sdtouser.GuaranterAddress.TalukId == 0 ? null : sdtouser.GuaranterAddress.TalukId;
                    sdtouser.GuaranterAddress.VillageId = sdtouser.GuaranterAddress.VillageId == 0 ? null : sdtouser.GuaranterAddress.VillageId;
                }

                //sdtouser.UserAddress.AddressId = user.UserAddressId;
                //sdtouser.PermanentAddress = db.Address.Find(sdtouser.PermanentAddressId);
                //sdtouser.GuaranterAddress = db.Address.Find(sdtouser.GuaranterAddressId);
                //sdtouser.Contacts = db.Contacts.Find(sdtouser.ContactId);
                //sdtouser.PermanentContacts = db.Contacts.Find(sdtouser.PermanentContactId);
                //sdtouser.GuaranterContacts = db.Contacts.Find(sdtouser.GuaranterContactId);
                //db.User.Attach(user);

                sdtouser.ModifiedBy = CurrentUserSession.UserId;
                sdtouser.ModifiedOn = DateTime.Now;

                if (sdtouser.PermanentAddress != null && sdtouser.PermanentAddressId == null)
                {
                    db.Address.Add(sdtouser.PermanentAddress);
                    sdtouser.PermanentAddressId = sdtouser.PermanentAddress.AddressId;
                }
                else
                    db.Entry(sdtouser.PermanentAddress).State = EntityState.Modified;
                if (sdtouser.UserAddress != null && sdtouser.UserAddressId == null)
                {
                    db.Address.Add(sdtouser.UserAddress);
                    sdtouser.UserAddressId = sdtouser.UserAddress.AddressId;
                }
                else
                    db.Entry(sdtouser.UserAddress).State = EntityState.Modified;
                if (sdtouser.GuaranterAddress != null && sdtouser.GuaranterAddressId == null)
                {
                    db.Address.Add(sdtouser.GuaranterAddress);
                    sdtouser.GuaranterAddressId = sdtouser.GuaranterAddress.AddressId;
                }
                else
                    db.Entry(sdtouser.GuaranterAddress).State = EntityState.Modified;
                if (sdtouser.Contacts != null && sdtouser.UserContactId == null)
                {
                    db.Contacts.Add(sdtouser.Contacts);
                    sdtouser.UserContactId = sdtouser.Contacts.ContactId;
                }
                else
                    db.Entry(sdtouser.Contacts).State = EntityState.Modified;
                if (sdtouser.GuaranterContacts != null && sdtouser.GuaranterContactId == null)
                {
                    db.Contacts.Add(sdtouser.GuaranterContacts);
                    sdtouser.GuaranterContactId = sdtouser.GuaranterContacts.ContactId;
                }
                else
                    db.Entry(sdtouser.GuaranterContacts).State = EntityState.Modified;
                if (sdtouser.PermanentContacts != null && sdtouser.PermanentContactId == null)
                {
                    db.Contacts.Add(sdtouser.PermanentContacts);
                    sdtouser.PermanentContactId = sdtouser.PermanentContacts.ContactId;
                }
                else
                    db.Entry(sdtouser.PermanentContacts).State = EntityState.Modified;
                db.Entry(sdtouser).State = EntityState.Modified;
                db.SaveChanges();

                if (ProfileImage != null)
                {
                    FileUpload(sdtouser.UserID, ProfileImage);

                    System.IO.FileInfo fInfo = new FileInfo(ViewBag.UserProfileAvatar);
                    fInfo.CopyTo(Path.Combine(fInfo.Directory.FullName, sdtouser.UserID + ".logo"), true);
                    fInfo.Delete();
                }

                SetDisplayMessage("Member is saved successfully");
                return RedirectToAction("Index");
            }
            ViewBag.AddressId = new SelectList(db.Address, "AddressId", "Address1", sdtouser.UserAddressId);
            ViewBag.ContactId = new SelectList(db.Contacts, "ContactId", "ContactName", sdtouser.UserContactId);
            ViewBag.GuaranterAddressId = new SelectList(db.Address, "AddressId", "Address1", sdtouser.GuaranterAddressId);
            ViewBag.GuaranterContactId = new SelectList(db.Contacts, "ContactId", "ContactName", sdtouser.GuaranterContactId);
            ViewBag.PermanentAddressId = new SelectList(db.Address, "AddressId", "Address1", sdtouser.PermanentAddressId);
            ViewBag.PermanentContactId = new SelectList(db.Contacts, "ContactId", "ContactName", sdtouser.PermanentContactId);
            var countries = db.Countries.ToList();
            countries.Insert(0, new sdtoCountry() { CountryId = 0, CountryName = "Select Country" });
            ViewBag.UserAddressCountryList = new SelectList(countries, "CountryId", "CountryName", sdtouser.UserAddress != null ? sdtouser.UserAddress.CountryId : 0);
            ViewBag.GuaranterAddressCountryList = new SelectList(countries, "CountryId", "CountryName", sdtouser.GuaranterAddress == null ? 0 : sdtouser.GuaranterAddress.CountryId);
            ViewBag.PermanentAddressCountryList = new SelectList(countries, "CountryId", "CountryName", sdtouser.PermanentAddress == null ? 0 : sdtouser.PermanentAddress.CountryId);
            ViewBag.StateList = new SelectList(db.States, "StateId", "StateName", 0);
            return View(sdtouser);
        }
        public ActionResult SaveVillage(sdtoVillage village)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();

            if (ModelState.IsValid)
            {
                if (db.Villages.Count(x => x.VillageName.Equals(village.VillageName.Trim(), StringComparison.InvariantCultureIgnoreCase) && x.TalukId == village.TalukId && x.VillageId != village.VillageId) == 0)
                {
                    if (village.VillageId == 0)
                    {
                        db.Villages.Add(new sdtoVillage() { TalukId = village.TalukId, VillageName = village.VillageName, VillageAbbr = village.VillageAbbr, CreatedBy = userDetails.UserID, CreatedOn = DateTime.Now });
                    }
                    else
                    {
                        village.ModifiedBy = userDetails.UserID;
                        village.ModifiedOn = DateTime.Now;
                        db.Entry(village).State = EntityState.Modified;
                    }
                    db.SaveChanges();

                    return RedirectToAction("VillageList", new { TalukId = village.TalukId });
                }
                else
                    ModelState.AddModelError("VillageName", "Village already exists!!!");
            }

            sdtoTaluk taluk = db.Taluks.Find(village.TalukId);
            var district = db.Districts.Find(taluk.DistrictId);
            var state = db.States.Find(district.StateId);
            ViewBag.CountryId = state.CountryId;
            ViewBag.StateId = district.StateId;
            ViewBag.DistrictId = taluk.DistrictId;
            ViewBag.TalukId = taluk.TalukId;
            return View(village);
        }
        public ActionResult SaveVillage(long TalukId, long VillageId = 0)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();

            sdtoTaluk taluk = db.Taluks.Find(TalukId);
            if (taluk == null)
                taluk = new sdtoTaluk();
            var district = new sdtoDistrict();
            var state = new sdtoState();
            if (taluk != null)
                district = db.Districts.Find(taluk.DistrictId);

            if (district == null)
                district = new sdtoDistrict();

            if (district != null)
                state = db.States.Find(district.StateId);

            if (state == null)
                state = new sdtoState();

            sdtoVillage village = new sdtoVillage()
            {
                TalukId = taluk.TalukId
            };

            ViewBag.CountryId = state.CountryId;
            ViewBag.StateId = district.StateId;
            ViewBag.DistrictId = taluk.DistrictId;
            ViewBag.TalukId = taluk.TalukId;
            sdtoTaluk parent = db.Taluks.Find(taluk.TalukId);
            if (parent != null)
            {
                ViewBag.ParentId = parent.TalukId;
                village = db.Villages.Find(VillageId);
                if (village == null)
                    village = new sdtoVillage() { TalukId = parent.TalukId, TalukDetails = parent };
                else
                    village.TalukDetails = parent;
            }
            return View(village);
        }
        public ActionResult SaveCountry(long? CountryId = 0)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();

            sdtoCountry country = db.Countries.Find(CountryId);
            return View(country);
        }
        public ActionResult SaveTaluk(long CountryId, long StateId, long DistrictId, long TalukId = 0)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();
            sdtoTaluk taluk = new sdtoTaluk()
            {
                DistrictId = DistrictId
            };

            ViewBag.CountryId = CountryId;
            ViewBag.StateId = StateId;
            ViewBag.DistrictId = DistrictId;
            sdtoDistrict parent = db.Districts.Find(DistrictId);
            if (parent != null)
            {
                ViewBag.ParentId = parent.DistrictId;
                taluk = db.Taluks.Find(TalukId);
                if (taluk == null)
                    taluk = new sdtoTaluk() { DistrictId = parent.DistrictId, DistrictDetails = parent };
                else
                    taluk.DistrictDetails = parent;
            }
            return View(taluk);
        }
        public ActionResult SaveState(sdtoState state)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();

            if (ModelState.IsValid)
            {
                if (db.States.Count(x => x.StateName.Equals(state.StateName.Trim(), StringComparison.InvariantCultureIgnoreCase) && x.StateId != state.StateId && x.CountryId == state.CountryId) == 0)
                {
                    if (state.StateId == 0)
                    {
                        db.States.Add(new sdtoState() { CountryId = state.CountryId, StateName = state.StateName, StateAbbr = state.StateAbbr, CreatedBy = userDetails.UserID, CreatedOn = DateTime.Now });
                    }
                    else
                    {
                        state.ModifiedBy = userDetails.UserID;
                        state.ModifiedOn = DateTime.Now;
                        db.Entry(state).State = EntityState.Modified;
                    }
                    db.SaveChanges();
                }
                else
                    ModelState.AddModelError("StateName", "State already exists!!!");

                return RedirectToAction("StateList", new { CountryId = state.CountryId });
            }

            sdtoCountry country = db.Countries.Find(state.CountryId);
            ViewBag.CountryList = new SelectList(db.Countries, "CountryId", "CountryName", state.CountryId);
            ViewBag.CountryId = state.CountryId;
            state.CountryDetails = country;
            return View(state);
        }
        public ActionResult SaveState(long CountryId, long StateId = 0)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();

            sdtoCountry country = db.Countries.Find(CountryId);
            ViewBag.CountryList = new SelectList(db.Countries, "CountryId", "CountryName", CountryId);
            ViewBag.CountryId = CountryId;
            sdtoState state = db.States.Find(StateId);
            if (state == null)
                state = new sdtoState() { CountryId = country.CountryId, CountryDetails = country };
            else
                state.CountryDetails = country;
            return View(state);
        }
        public ActionResult SaveDistrict(sdtoDistrict district)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();

            if (ModelState.IsValid)
            {
                if (db.Districts.Count(x => x.DistrictName.Equals(district.DistrictName.Trim(), StringComparison.InvariantCultureIgnoreCase) && x.StateId == district.StateId && x.DistrictId != district.DistrictId) == 0)
                {
                    if (district.DistrictId == 0)
                    {
                        db.Districts.Add(new sdtoDistrict() { StateId = district.StateId, DistrictName = district.DistrictName, DistrictAbbr = district.DistrictAbbr, CreatedBy = userDetails.UserID, CreatedOn = DateTime.Now });
                    }
                    else
                    {
                        district.ModifiedBy = userDetails.UserID;
                        district.ModifiedOn = DateTime.Now;
                        db.Entry(district).State = EntityState.Modified;
                    }
                    db.SaveChanges();

                    var cn = db.States.Find(district.StateId);
                    return RedirectToAction("DistrictList", new { StateId = district.StateId, CountryId = cn.CountryId });
                }
                else
                    ModelState.AddModelError("DistrictName", "District already exists!!!");
            }

            var state = db.States.Find(district.StateId);
            ViewBag.CountryId = state.CountryId;
            return View(district);
        }
        public ActionResult SaveDistrict(long CountryId, long StateId, long DistrictId = 0)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();
            sdtoDistrict district = new sdtoDistrict();

            ViewBag.CountryId = CountryId;
            sdtoState parent = db.States.Find(StateId);
            if (parent != null)
            {
                ViewBag.ParentId = parent.StateId;
                district = db.Districts.Find(DistrictId);
                if (district == null)
                    district = new sdtoDistrict() { StateId = parent.StateId, StateDetails = parent };
                else
                    district.StateDetails = parent;
            }
            return View(district);
        }
        public ActionResult SaveCountry(sdtoCountry country)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();

            if (ModelState.IsValid)
            {
                if (db.Countries.Count(x => x.CountryName.Equals(country.CountryName.Trim(), StringComparison.InvariantCultureIgnoreCase) && x.CountryId != country.CountryId) == 0)
                {
                    if (country.CountryId == 0)
                    {
                        db.Countries.Add(new sdtoCountry() { CountryName = country.CountryName, CountryAbbr = country.CountryAbbr, CreatedBy = userDetails.UserID, CreatedOn = DateTime.Now });
                    }
                    else
                    {
                        country.ModifiedBy = userDetails.UserID;
                        country.ModifiedOn = DateTime.Now;
                        db.Entry(country).State = EntityState.Modified;
                    }
                    db.SaveChanges();
                }
                else
                    ModelState.AddModelError("CountryName", "Country already exists!!!");

                return RedirectToAction("CountryList");
            }

            return View(country);
        }
 // GET: /User/Create
 public ActionResult Create()
 {
     sdtoUser user = new sdtoUser() { CompanyId = 1 };
     ViewBag.UserGroupList = new SelectList(db.Usergroup, "UserGroupId", "Code");
     return View(user);
 }
        public ActionResult Create(sdtoUser user, HttpPostedFileBase ProfileImage)
        {
            if (ModelState.IsValid)
            {
                var validImageTypes = new string[]
                                        {
                                            "image/gif",
                                            "image/jpeg",
                                            "image/pjpeg",
                                            "image/png"
                                        };

                //if (ProfileImage == null || ProfileImage.ContentLength == 0)
                //{
                //    ModelState.AddModelError("ProfileImage", "This field is required");
                //}
                //else
                if (string.IsNullOrWhiteSpace(user.UserName))
                {
                    ModelState.AddModelError("UserName", "Please enter username");
                }
                else if (string.IsNullOrWhiteSpace(user.Password))
                {
                    ModelState.AddModelError("Password", "Please enter password");
                }
                else if (string.IsNullOrWhiteSpace(user.ConfirmPassword))
                {
                    ModelState.AddModelError("ConfirmPassword", "Please enter confirm password");
                }
                else if (!user.ConfirmPassword.Equals(user.Password))
                {
                    ModelState.AddModelError("ConfirmPassword", "Confirm password entered is mismatching");
                }
                else if (ProfileImage != null && !validImageTypes.Contains(ProfileImage.ContentType))
                {
                    ModelState.AddModelError("ProfileImage", "Please choose either a GIF, JPG or PNG image");
                }
                else if (user != null && db.User.Count(x => x.UserName.Equals(user.UserName, StringComparison.InvariantCultureIgnoreCase)) > 0)
                {
                    ModelState.AddModelError("UserName", string.Format("User name {0} is unavailable. Please select a different user name.", user.UserName));
                }
                else
                {
                    user.UserType = UserType.User;
                    user.CreatedOn = DateTime.Now;
                    if (CurrentUserSession != null && CurrentUserSession.UserId > 0)
                        user.CreatedBy = CurrentUserSession.UserId;
                    if (user.UserAddress != null)
                        user.UserAddress = db.Address.Add(user.UserAddress);
                    if (user.Contacts != null)
                        user.Contacts = db.Contacts.Add(user.Contacts);
                    user.CreatedOn = DateTime.Now;
                    user.IsActive = true;

                    user.GuaranterAddressId = null;
                    user.PermanentAddressId = null;
                    user.UserAddressId = null;
                    user.UserAddress = null;
                    user.PermanentAddress = null;
                    user.GuaranterAddress = null;

                    if (user.UserAddress != null)
                    {
                        user.UserAddress.CountryId = user.UserAddress.CountryId == 0 ? null : user.UserAddress.CountryId;
                        user.UserAddress.StateId = user.UserAddress.StateId == 0 ? null : user.UserAddress.StateId;
                        user.UserAddress.DistrictId = user.UserAddress.DistrictId == 0 ? null : user.UserAddress.DistrictId;
                        user.UserAddress.TalukId = user.UserAddress.TalukId == 0 ? null : user.UserAddress.TalukId;
                        user.UserAddress.VillageId = user.UserAddress.VillageId == 0 ? null : user.UserAddress.VillageId;
                    }

                    if (user.PermanentAddress != null)
                    {
                        user.PermanentAddress.CountryId = user.PermanentAddress.CountryId == 0 ? null : user.PermanentAddress.CountryId;
                        user.PermanentAddress.StateId = user.PermanentAddress.StateId == 0 ? null : user.PermanentAddress.StateId;
                        user.PermanentAddress.DistrictId = user.PermanentAddress.DistrictId == 0 ? null : user.PermanentAddress.DistrictId;
                        user.PermanentAddress.TalukId = user.PermanentAddress.TalukId == 0 ? null : user.PermanentAddress.TalukId;
                        user.PermanentAddress.VillageId = user.PermanentAddress.VillageId == 0 ? null : user.PermanentAddress.VillageId;
                    }

                    if (user.GuaranterAddress != null)
                    {
                        user.GuaranterAddress.CountryId = user.GuaranterAddress.CountryId == 0 ? null : user.GuaranterAddress.CountryId;
                        user.GuaranterAddress.StateId = user.GuaranterAddress.StateId == 0 ? null : user.GuaranterAddress.StateId;
                        user.GuaranterAddress.DistrictId = user.GuaranterAddress.DistrictId == 0 ? null : user.GuaranterAddress.DistrictId;
                        user.GuaranterAddress.TalukId = user.GuaranterAddress.TalukId == 0 ? null : user.GuaranterAddress.TalukId;
                        user.GuaranterAddress.VillageId = user.GuaranterAddress.VillageId == 0 ? null : user.GuaranterAddress.VillageId;
                    }

                    db.User.Add(user);
                    db.SaveChanges();
                    if (ProfileImage != null)
                    {
                        FileUpload(user.UserID, ProfileImage);

                        System.IO.FileInfo fInfo = new FileInfo(ViewBag.UserProfileAvatar);
                        fInfo.CopyTo(Path.Combine(fInfo.Directory.FullName, user.UserID + ".logo"), true);
                        fInfo.Delete();
                    }
                    else
                    {
                        DirectoryInfo dirUser = new DirectoryInfo(HttpContext.Server.MapPath("~/").Trim("\\/ ".ToCharArray()) + "\\ContentUpload\\User\\Profile");
                        if (!dirUser.Exists)
                            dirUser.Create();

                        FileInfo objFile = new FileInfo(Path.Combine(HttpContext.Server.MapPath("~/").Trim("\\/ ".ToCharArray()) + "\\Content\\Images", "dummy-profile.png"));
                        objFile.CopyTo(Path.Combine(dirUser.FullName, user.UserID + ".logo"), true);
                    }

                    if (User.Identity.IsAuthenticated)
                    {
                        SetDisplayMessage("User is created successfully");
                        return RedirectToAction("Index");
                    }
                    else
                        return RedirectToAction("Login", "Login");
                }
            }

            ViewBag.UserGroupList = new SelectList(db.Usergroup, "UserGroupId", "Code", user.UserGroupId);
            return View(user);
        }
        public ActionResult SaveTaluk(sdtoTaluk taluk)
        {
            sdtoUser userDetails = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser);
            if (userDetails == null)
                userDetails = new sdtoUser();

            if (ModelState.IsValid)
            {
                if (db.Taluks.Count(x => x.TalukName.Equals(taluk.TalukName.Trim(), StringComparison.InvariantCultureIgnoreCase) && x.DistrictId == taluk.DistrictId && x.TalukId != taluk.TalukId) == 0)
                {
                    if (taluk.TalukId == 0)
                    {
                        db.Taluks.Add(new sdtoTaluk() { DistrictId = taluk.DistrictId, TalukName = taluk.TalukName, TalukAbbr = taluk.TalukAbbr, CreatedBy = userDetails.UserID, CreatedOn = DateTime.Now });
                    }
                    else
                    {
                        taluk.ModifiedBy = userDetails.UserID;
                        taluk.ModifiedOn = DateTime.Now;
                        db.Entry(taluk).State = EntityState.Modified;
                    }
                    db.SaveChanges();

                    var district = db.Districts.Find(taluk.DistrictId);
                    var state = db.States.Find(district.StateId);
                    return RedirectToAction("TalukList", new { StateId = district.StateId, CountryId = state.CountryId, DistrictId = taluk.DistrictId });
                }
                else
                    ModelState.AddModelError("TalukName", "Taluk already exists!!!");
            }

            var districtst = db.Districts.Find(taluk.DistrictId);
            var statecn = db.States.Find(districtst.StateId);
            ViewBag.CountryId = statecn.CountryId;
            ViewBag.StateId = districtst.StateId;
            ViewBag.DistrictId = taluk.DistrictId;
            return View(taluk);
        }