public ActionResult EditFolder(CCFolder folderObj)
        {
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];
            CCFolder folderResponse;
            int folderType = folderObj.Type;

            if (ModelState.IsValid && accountObj != null)
            {
                folderObj.AccountGUID = accountObj.AccountGUID;
                folderObj.IsPaused = false;
                folderObj.isOverFlow = false;
                if (folderObj.Type == 3)
                    folderObj.isCrimeDiary = true;
                //Save Folder
                folderResponse = CCFolderRepository.SaveFolder(folderObj);

                var folders = CCFolderRepository.CCFolders.Where(guid => guid.AccountGUID == accountObj.AccountGUID).ToList();
                TempData["SelectedMenu"] = "Manage";
                TempData["SelectedSubMenu"] = "ManageFolders";

                if (folderObj.FolderID > 0)
                {
                    var fields = CCFieldRepository.CCFolderFields.Where(u => u.FolderID == folderObj.FolderID & u.AccountGUID == accountObj.AccountGUID).ToList();
                    if (fields.Count == 0)
                    {
                        //Save default field
                        List<string> needtosave = new List<string>();
                        List<CCFolderField> savedobj = new List<CCFolderField>();

                        if (folderType == 1) {
                            if (accountObj.PlanID == 1)
                            {
                                needtosave = FieldsConfigHelper.GetFieldForContactSimple();
                            }
                            else if (accountObj.PlanID == 2)
                            {
                                needtosave = FieldsConfigHelper.GetFieldForContactBusiness();
                            }
                            else
                            {
                                needtosave = FieldsConfigHelper.GetFieldForContactFull();
                            }
                        }
                        else if (folderType == 2) { needtosave = FieldsConfigHelper.GetFieldForAppointmentSimple(); }
                        else if (folderType == 3) { needtosave = FieldsConfigHelper.GetFieldForCrimeDiaryAppointment(); }
                        else { }

                        foreach (var field in needtosave)
                        {
                            CCFolderField folderField = new CCFolderField();
                            folderField.FieldName = FieldsConfigHelper.GetRealName(field);
                            folderField.FieldType = FieldsConfigHelper.GetVariableType(field);
                            folderField.FolderID = folderObj.FolderID;
                            folderField.FieldCaption = field;
                            folderField.Restriction = "none";
                            folderField.AccountGUID = accountObj.AccountGUID;
                            folderField.isActive = true;
                            savedobj.Add(folderField);
                        }

                        var resp = CCFieldRepository.SaveFolderFieldsObj(savedobj);

                    }

                    // create note into grops
                    var alreadyExist = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == folderResponse.FolderID & fid.GroupName == "Note" & fid.AccountGUID == accountObj.AccountGUID).ToList();

                    if (alreadyExist.Count == 0)
                    {
                        CCGroup objGroup = new CCGroup();
                        objGroup.GroupName = "Note";
                        objGroup.FolderID = folderResponse.FolderID;
                        objGroup.AccountGUID = accountObj.AccountGUID;
                        var user = CCGroupRepository.SaveGroup(objGroup);
                    }

                    // create default group
                    var isDefaultGrpExist = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == folderResponse.FolderID & fid.GroupName == "Default" & fid.AccountGUID == accountObj.AccountGUID).ToList();

                    if (isDefaultGrpExist.Count == 0)
                    {
                        // add default group
                        CCGroup objGroup = new CCGroup();
                        objGroup.GroupName = "Default";
                        objGroup.FolderID = folderResponse.FolderID;
                        objGroup.AccountGUID = accountObj.AccountGUID;
                        var grp = CCGroupRepository.SaveGroup(objGroup);

                        if (grp.GroupID != 0)
                        {
                            // added all default field
                            var defaultfields = CCFieldRepository.CCFolderFields.Where(u => u.FolderID == folderObj.FolderID & u.AccountGUID == accountObj.AccountGUID).ToList();
                            int i = 1;
                            foreach (var fieldItem in defaultfields)
                            {
                                CCGroupField objgrpfield = new CCGroupField();
                                objgrpfield.FieldID = fieldItem.FieldID;
                                objgrpfield.FieldOrder = i++;
                                objgrpfield.FolderID = grp.FolderID;
                                objgrpfield.GroupID = grp.GroupID;
                                objgrpfield.AccountGUID = accountObj.AccountGUID;
                                var aa = CCGroupFieldRepository.SaveGroupField(objgrpfield);
                            }
                        }
                    }

                    // create default layout
                    var isDefaultLayoutExist = CCLayoutRepository.CCLayouts.Where(fid => fid.FolderID == folderResponse.FolderID & fid.LayoutName == "Default" & fid.AccountGUID == accountObj.AccountGUID).ToList();

                    if (isDefaultLayoutExist.Count == 0)
                    {
                        // add default layout
                        CCLayout layout = new CCLayout();
                        layout.LayoutName = "Default";
                        layout.FolderID = folderObj.FolderID;
                        layout.AccountGUID = accountObj.AccountGUID;
                        var resLayout = CCLayoutRepository.Savelayout(layout);

                        // add default group into default layout
                        var defaultExistGrp = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == folderResponse.FolderID & fid.GroupName == "Default" & fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();
                        if (defaultExistGrp != null)
                        {
                            CCLayoutGroup objLayoutGroup = new CCLayoutGroup();
                            objLayoutGroup.GroupID = defaultExistGrp.GroupID;
                            objLayoutGroup.GroupOrder = 1;
                            objLayoutGroup.FolderID = resLayout.FolderID;
                            objLayoutGroup.LayoutID = resLayout.LayoutID;
                            objLayoutGroup.AccountGUID = accountObj.AccountGUID;
                            var aa = CCLayoutGroupRepository.SavelayoutGroup(objLayoutGroup);
                        }
                    }

                }

                var folderList = CCFolderRepository.CCFolders.Where(guid => guid.AccountGUID == accountObj.AccountGUID).ToList();
                Session["folderss"] = folderList;

                LimitationsViewModel limitationsObjMain = (LimitationsViewModel)Session["limitations"];
                HelperFunctions HF = new HelperFunctions();
                limitationsObjMain = HF.updateAccountLimitations(accountObj);
                Session["limitations"] = limitationsObjMain;

                return RedirectToAction("ManageFolders", "Folder");
            }
            else
            {
                var folderList = CCFolderRepository.CCFolders.Where(guid => guid.AccountGUID == accountObj.AccountGUID).ToList();
                Session["folderss"] = folderList;

                LimitationsViewModel limitationsObjMain = (LimitationsViewModel)Session["limitations"];
                HelperFunctions HF = new HelperFunctions();
                limitationsObjMain = HF.updateAccountLimitations(accountObj);
                Session["limitations"] = limitationsObjMain;

                return View(folderObj);
            }
        }
        public ActionResult SelectFolderFields(string fields, FolderFieldsSelectModel objFolderFields)
        {
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];
            List<string> needtosave = new List<string>();
            List<CCFolderField> savedobj = new List<CCFolderField>();
            List<CCFolderField> activeFields = new List<CCFolderField>();
            List<CCFolderField> needToInActive = new List<CCFolderField>();

            var objFields = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(fields).ToList();
            needtosave = CCFieldRepository.IsAvailableField(objFields, objFolderFields.FolderID);

            foreach (var field in needtosave)
            {
                CCFolderField folderField = new CCFolderField();
                folderField.FieldName = FieldsConfigHelper.GetRealName(field);
                folderField.FieldType = FieldsConfigHelper.GetVariableType(field);
                folderField.FolderID = objFolderFields.FolderID;
                folderField.FieldCaption = field;
                folderField.Restriction = "none";
                folderField.AccountGUID = accountObj.AccountGUID;
                folderField.isActive = true;
                savedobj.Add(folderField);
            }

            foreach (var field in objFields)
            {
                CCFolderField folderField = CCFieldRepository.CCFolderFields.Where(f => f.FieldCaption == field & f.AccountGUID == accountObj.AccountGUID & f.FolderID == objFolderFields.FolderID).FirstOrDefault();
                activeFields.Add(folderField);
            }

            var resp = CCFieldRepository.SaveFolderFieldsObj(savedobj);

            var fieldList = CCFieldRepository.CCFolderFields.Where(f => f.AccountGUID == accountObj.AccountGUID & f.FolderID == objFolderFields.FolderID).ToList();

            var toInActiveList = fieldList.Except(activeFields).ToList();

            int flag = 0;

            foreach (var f1 in fieldList)
            {
                flag = 0;
                foreach (var f2 in activeFields)
                {
                    try
                    {
                        if (f2.FieldID == f1.FieldID)
                        {
                            if (f1.isActive != false)
                            {
                                flag = 1;
                                break;
                            }
                            else
                            {
                                flag = 2;
                                break;
                            }

                        }
                    }
                    catch (Exception ex)
                    {  }

                }

                if (flag == 0)
                {
                    f1.isActive = false;
                    CCFieldRepository.SaveFolderFields(f1);
                }

                else if (flag == 2)
                {
                    f1.isActive = true;
                    CCFieldRepository.SaveFolderFields(f1);
                }
            }

            // add field into default group
            var defaultGrp = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == objFolderFields.FolderID & fid.GroupName == "Default" & fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();

            if (defaultGrp != null)
            {
                foreach (var field in resp)
                {
                    long grpFieldOrder = 0;
                    // get group fileds last order
                    var groupFieldOrder = CCGroupFieldRepository.CCGroupsFields.Where(gid => gid.GroupID == defaultGrp.GroupID & gid.AccountGUID == accountObj.AccountGUID).OrderByDescending(grp => grp.FieldOrder).FirstOrDefault();
                    if (groupFieldOrder != null) { grpFieldOrder = groupFieldOrder.FieldOrder; }

                    CCGroupField objgrpfield = new CCGroupField();
                    objgrpfield.FieldID = field.FieldID;
                    objgrpfield.FieldOrder = grpFieldOrder + 1;
                    objgrpfield.FolderID = defaultGrp.FolderID;
                    objgrpfield.GroupID = defaultGrp.GroupID;
                    objgrpfield.AccountGUID = accountObj.AccountGUID;
                    var aa = CCGroupFieldRepository.SaveGroupField(objgrpfield);
                }
            }

            return RedirectToAction("ViewFields", "Folder", new { id = objFolderFields.FolderID });
        }
        public CCFolderField SaveFolderFields(CCFolderField fieldObj)
        {
            if (fieldObj.FieldID == 0)
            {
                context.CCFolderFields.Add(fieldObj);
                context.SaveChanges();

                //Schedule Force ReSync

                List<CCConnection> connList = new List<CCConnection>();
                connList = CCConnectionRepository.CCSubscriptions.Where(con => con.FolderID == fieldObj.FolderID).ToList();

                foreach (var con in connList)
                {
                    CCConnection conEntry = context.CCSubscriptions.Find(con.ConnectionID);
                    conEntry.ResetAtNextSync = true;
                    context.SaveChanges();
                }
            }
            else
            {
                CCFolderField dbEntry = context.CCFolderFields.Find(fieldObj.FieldID);
                if (dbEntry != null)
                {
                    dbEntry.FieldCaption = fieldObj.FieldCaption;
                    dbEntry.FieldName = fieldObj.FieldName;
                    dbEntry.FieldType = fieldObj.FieldType;
                    dbEntry.Restriction = fieldObj.Restriction;
                    dbEntry.RestrictionValues = fieldObj.RestrictionValues;
                    dbEntry.isActive = fieldObj.isActive;
                    context.SaveChanges();
                }

                List<CCConnection> connList = new List<CCConnection>();
                connList = CCConnectionRepository.CCSubscriptions.Where(con => con.FolderID == fieldObj.FolderID).ToList();

                foreach (var con in connList)
                {
                    CCConnection conEntry = context.CCSubscriptions.Find(con.ConnectionID);
                    conEntry.ResetAtNextSync = true;
                    context.SaveChanges();
                }

            }
            return fieldObj;
        }
        public ActionResult Field(CCFolderField folderObj)
        {
            if (CCFieldRepository.IsFieldAvailable(folderObj.FieldName, folderObj.FolderID) || folderObj.FieldID > 0)
            {
                User userObj = (User)Session["user"];
                Account accountObj = (Account)Session["account"];

                if (folderObj.Restriction == "none")
                    folderObj.RestrictionValues = null;
                if (ModelState.IsValid)
                {
                    folderObj.AccountGUID = accountObj.AccountGUID;
                    folderObj.isActive = true;
                    var resField = CCFieldRepository.SaveFolderFields(folderObj);
                    if (resField.FieldID != 0)
                    {
                        // add created field into Default group
                        var isDefaultExist = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == folderObj.FolderID & fid.GroupName == "Default" & fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();
                        if (isDefaultExist != null)
                        {
                            //is field exist into default
                            var isFieldexist = CCGroupFieldRepository.CCGroupsFields.Where(fid => fid.FieldID == resField.FieldID & fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();
                            if (isFieldexist == null)
                            {
                                // get group fileds last order
                                var groupFieldOrder = CCGroupFieldRepository.CCGroupsFields.Where(gid => gid.GroupID == isDefaultExist.GroupID & gid.AccountGUID == accountObj.AccountGUID).OrderByDescending(grp => grp.FieldOrder).FirstOrDefault();

                                CCGroupField objgrpfield = new CCGroupField();
                                objgrpfield.FieldID = resField.FieldID;
                                objgrpfield.FieldOrder = groupFieldOrder.FieldOrder + 1;
                                objgrpfield.FolderID = isDefaultExist.FolderID;
                                objgrpfield.GroupID = isDefaultExist.GroupID;
                                objgrpfield.AccountGUID = accountObj.AccountGUID;
                                var resGrpField = CCGroupFieldRepository.SaveGroupField(objgrpfield);
                            }
                        }
                    }
                    return RedirectToAction("ViewFields", "Folder", new { id = folderObj.FolderID });
                }
                else
                {
                    return View(folderObj);
                }
            }
            else
            {
                ViewBag.Message = "That field is already in this folder";
                return View(folderObj);
            }
        }
        public Account saveFeaturesCommon(string plan, int quantities)
        {
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];
            int planLevel = Int32.Parse(plan);
            int existingPlanID = 1;

            //Delete old purchasedfeature
            var purchasedfeatures = purchRepository.Purchases.Where(p => p.AccountGUID == accountObj.AccountGUID).ToList();
            foreach (var purch in purchasedfeatures)
            {
                bool res = purchRepository.DeleteFPurchasedFeature(purch.ID);

            }

            var featuresForPlan = featureRepository.Features.Where(pl => pl.PlanLevel == planLevel).ToList();

            // save default feature
            var featuresForDefaultPlan = featureRepository.Features.Where(pl => pl.PlanLevel == planLevel && pl.Price == 0).ToList();

            foreach (var feat in featuresForDefaultPlan)
            {
                PurchasedFeatures objFeature = new PurchasedFeatures();
                objFeature.AccountGUID = accountObj.AccountGUID;
                objFeature.FeatureID = feat.ID;
                objFeature.ExpiryDate = DateTime.UtcNow;
                objFeature.Enabled = true;

                if (feat.Type == "Max Items per Folder")
                {
                    objFeature.Quantity = (feat.Quantity) * quantities;
                }
                else
                {
                    objFeature.Quantity = feat.Quantity;
                }

                purchRepository.SavePurchase(objFeature);
            }

            //Ajust Folder Fields START

            List<string> needtosave = new List<string>();
            List<CCFolderField> savedobj = new List<CCFolderField>();
            List<CCFolderField> activeFields = new List<CCFolderField>();
            List<CCFolderField> needToInActive = new List<CCFolderField>();

            List<String> objFields = new List<string>();

            if (planLevel == 10)
                objFields = FieldsConfigHelper.GetFieldForContactSimple();
            else if (planLevel == 20)
                objFields = FieldsConfigHelper.GetFieldForContactBusiness();
            else
                objFields = FieldsConfigHelper.GetFieldForContactFull();

            var folderListForAccount = CCFolderRepository.CCFolders.Where(F => F.AccountGUID == accountObj.AccountGUID).ToList();

            foreach (var fold in folderListForAccount.Where(f=>f.Type==1))
            {
                needtosave = CCFieldRepository.IsAvailableField(objFields, fold.FolderID);

                foreach (var field in needtosave)
                {
                    CCFolderField folderField = new CCFolderField();
                    folderField.FieldName = FieldsConfigHelper.GetRealName(field);
                    folderField.FieldType = FieldsConfigHelper.GetVariableType(field);
                    folderField.FolderID = fold.FolderID;
                    folderField.FieldCaption = field;
                    folderField.Restriction = "none";
                    folderField.AccountGUID = accountObj.AccountGUID;
                    folderField.isActive = true;
                    savedobj.Add(folderField);
                }

                var resp = CCFieldRepository.SaveFolderFieldsObj(savedobj);

                foreach (var field in objFields)
                {
                    CCFolderField folderField = CCFieldRepository.CCFolderFields.Where(f => f.FieldCaption == field & f.AccountGUID == accountObj.AccountGUID & f.FolderID == fold.FolderID).FirstOrDefault();
                    activeFields.Add(folderField);
                }

                var fieldList = CCFieldRepository.CCFolderFields.Where(f => f.AccountGUID == accountObj.AccountGUID & f.FolderID == fold.FolderID).ToList();

                var toInActiveList = fieldList.Except(activeFields).ToList();

                int flag = 0;

                foreach (var f1 in fieldList)
                {
                    flag = 0;
                    foreach (var f2 in activeFields)
                    {
                        try
                        {
                            if (f2.FieldID == f1.FieldID)
                            {
                                if (f1.isActive != false)
                                {
                                    flag = 1;
                                    break;
                                }
                                else
                                {
                                    flag = 2;
                                    break;
                                }

                            }
                        }
                        catch (Exception ex)
                        { }

                    }

                    if (flag == 0)
                    {
                        f1.isActive = false;
                        CCFieldRepository.SaveFolderFields(f1);
                    }

                    else if (flag == 2)
                    {
                        f1.isActive = true;
                        CCFieldRepository.SaveFolderFields(f1);
                    }
                }

                // add field into default group
                var defaultGrp = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == fold.FolderID & fid.GroupName == "Default" & fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();

                if (defaultGrp != null)
                {
                    foreach (var field in resp)
                    {
                        long grpFieldOrder = 0;
                        // get group fileds last order
                        var groupFieldOrder = CCGroupFieldRepository.CCGroupsFields.Where(gid => gid.GroupID == defaultGrp.GroupID & gid.AccountGUID == accountObj.AccountGUID).OrderByDescending(grp => grp.FieldOrder).FirstOrDefault();
                        if (groupFieldOrder != null) { grpFieldOrder = groupFieldOrder.FieldOrder; }

                        CCGroupField objgrpfield = new CCGroupField();
                        objgrpfield.FieldID = field.FieldID;
                        objgrpfield.FieldOrder = grpFieldOrder + 1;
                        objgrpfield.FolderID = defaultGrp.FolderID;
                        objgrpfield.GroupID = defaultGrp.GroupID;
                        objgrpfield.AccountGUID = accountObj.AccountGUID;
                        var aa = CCGroupFieldRepository.SaveGroupField(objgrpfield);
                    }
                }
            }

            //Ajust Folder Fields END

            // change the plain id
            var planid = planRepository.Plans.FirstOrDefault(pl => pl.PlanLevel == planLevel).ID;

            var currentAccount = accountRepo.Accounts.FirstOrDefault(ac => ac.ID == userObj.AccountID);

            Account account = new Account();
            account = currentAccount;
            existingPlanID = account.PlanID;
            account.PlanID = planid;
            account.SyncPeriod = (short)featureRepository.Features.Where(pl => pl.PlanLevel == planLevel && pl.Type == "Sync Period").First().Quantity;
            account = accountRepo.SaveAccount(account);

            return account;
        }