public ActionResult BeatHierarchyAddition()
        {
            List <FileHeadersBeatHierarchy> list = (List <FileHeadersBeatHierarchy>)Session["BeatHierarchy"];
            int?id    = (int?)Session["CompanyID"];
            int newId = id ?? default(int);
            var list1 = list.GroupBy(x => x.NSM);


            foreach (var keyitem in list1)
            {
                foreach (var item in keyitem)
                {
                    NationalSalesManager NSM = new NationalSalesManager
                    {
                        Name             = keyitem.Key,
                        EmailId          = item.NSMEmailId,
                        SecondaryEmailId = item.NSMSecondaryEmailId,
                        Zone             = item.NSMZone,
                        CompanyId        = newId
                    };
                    NSMRepository NSMRepo = new NSMRepository();
                    List <NationalSalesManager> NSMList = NSMRepo.GetNSM(newId);

                    if (!NSMList.Exists(x => x.Name == NSM.Name))
                    {
                        NSMRepo.AddNSM(NSM);
                    }
                    //Insert Regions
                    long NSMPk = db.NationalSalesManagers.Where(x => x.Name == item.NSM).Select(x => x.Id).FirstOrDefault();

                    ZonalSalesManager ZSM = new ZonalSalesManager();
                    ZSM.Name                   = item.ZSM;
                    ZSM.EmailId                = item.ZSMEmailId;
                    ZSM.SecondaryEmailId       = item.ZSMSecondaryEmailId;
                    ZSM.Zone                   = item.ZSMZone;
                    ZSM.NationalSalesManagerId = NSMPk;
                    ZSM.CompanyId              = newId;


                    ZSMRepository            ZSMRepo = new ZSMRepository();
                    List <ZonalSalesManager> ZSMList = ZSMRepo.GetZSM(newId);
                    if (!ZSMList.Exists(x => x.Name == ZSM.Name))
                    {
                        ZSMRepo.AddZSM(ZSM);
                    }

                    long ZSMPk = db.ZonalSalesManagers.Where(x => x.Name == item.ZSM).Select(x => x.Id).FirstOrDefault();

                    RegionalSalesManager RSM = new RegionalSalesManager();
                    RSM.Name                = item.RSM;
                    RSM.EmailId             = item.RSMEmailId;
                    RSM.SecondaryEmailId    = item.RSMSecondaryEmailId;
                    RSM.Zone                = item.RSMZone;
                    RSM.ZonalSalesManagerId = ZSMPk;
                    RSM.CompanyId           = newId;

                    RSMRepository RSMRepo = new RSMRepository();
                    List <RegionalSalesManager> RSMList = RSMRepo.GetRSM(newId);
                    if (!RSMList.Exists(x => x.Name == RSM.Name))
                    {
                        RSMRepo.AddRSM(RSM);
                    }

                    long RSMPk = db.RegionalSalesManagers.Where(x => x.Name == item.RSM).Select(x => x.Id).FirstOrDefault();

                    AreaSalesManager ASM = new AreaSalesManager();
                    ASM.Name                   = item.ASM;
                    ASM.EmailId                = item.ASMEmailId;
                    ASM.SecondaryEmailId       = item.ASMSecondaryEmailId;
                    ASM.Zone                   = item.ASMZone;
                    ASM.RegionalSalesManagerId = RSMPk;
                    ASM.CompanyId              = newId;

                    ASMRepository           ASMRepo = new ASMRepository();
                    List <AreaSalesManager> ASMList = ASMRepo.GetASM(newId);
                    if (!ASMList.Exists(x => x.Name == ASM.Name))
                    {
                        ASMRepo.AddASM(ASM);
                    }

                    long ASMPk = db.ClientEmployees.Where(x => x.Name == item.ASM).Select(x => x.Id).FirstOrDefault();

                    ClientEmployee ESM = new ClientEmployee();
                    ESM.Name             = item.ESM;
                    ESM.EmailId          = item.ESMEmailId;
                    ESM.SecondaryEmailId = item.ESMSecondaryEmailId;
                    //   ESM.Zone = item.ESMZone;
                    ESM.ClientSideId       = item.ESMErpId;
                    ESM.AreaSalesManagerId = ASMPk;
                    ESM.Company            = newId;

                    ESMRepository         ESMRepo = new ESMRepository();
                    List <ClientEmployee> ESMList = ESMRepo.GetESM(newId);
                    if (!ESMList.Exists(x => x.Name == ESM.Name))
                    {
                        ESMRepo.AddESM(ESM);
                    }

                    LocationBeat BEAT = new LocationBeat();
                    BEAT.Name         = item.FinalBeatName;
                    BEAT.ErpId        = item.BeatErpId;
                    BEAT.City         = item.BeatDistrict;
                    BEAT.State        = item.BeatState;
                    BEAT.DivisionZone = item.BeatZone;

                    BEATRepository      BEATRepo = new BEATRepository();
                    List <LocationBeat> BEATList = BEATRepo.GetBEAT(newId);

                    if (!BEATList.Exists(x => x.Name == BEAT.Name))
                    {
                        BEATRepo.AddBEAT(BEAT);
                    }
                    Distributor Distributor = new Distributor();
                    Distributor.Name         = item.DistributorName;
                    Distributor.EmailId      = item.DistributorEmailId;
                    Distributor.ClientSideId = item.DistributorErpId;
                    Distributor.Place        = item.DistributorLocation;

                    DistributorRepository DistributorRepo = new DistributorRepository();
                    List <Distributor>    DIstributorList = DistributorRepo.GetDistributor(newId);

                    if (!DIstributorList.Exists(x => x.Name == Distributor.Name))
                    {
                        DistributorRepo.AddDistributor(Distributor);
                    }

                    DistributorBeatMappingRepository DBMRepo = new DistributorBeatMappingRepository();
                    List <DistributorBeatMapping>    DBMList = DBMRepo.GetDistributorBeatMap(newId);

                    long BeatId        = db.LocationBeats.Where(x => x.Name == item.FinalBeatName).Select(x => x.Id).FirstOrDefault();
                    long DistributorId = db.Distributors.Where(x => x.Name == item.DistributorName).Select(x => x.Id).FirstOrDefault();

                    DistributorBeatMapping DBM = new DistributorBeatMapping();
                    DBM.BeatId        = BeatId;
                    DBM.DistributorId = DistributorId;
                    DBM.CompanyId     = newId;

                    if (!DBMList.Contains(DBM))
                    {
                        DBMRepo.AddDistributorBeatMap(DBM);
                    }
                }
            }
            return(View());
        }
        public ActionResult BeatHierarchyAdditionCheck(string ID)
        {
            int?Id = int.Parse(ID);

            Session["CompanyID"] = Id;
            List <FileHeadersBeatHierarchy> beatHierarchyList = (List <FileHeadersBeatHierarchy>)Session["BeatHierarchy"];
            NSMRepository NSMRepo = new NSMRepository();
            List <NationalSalesManager> NSMList         = NSMRepo.GetNSM(Id);
            ZSMRepository               ZSMRepo         = new ZSMRepository();
            List <ZonalSalesManager>    ZSMList         = ZSMRepo.GetZSM(Id);
            RSMRepository               RSMRepo         = new RSMRepository();
            List <RegionalSalesManager> RSMList         = RSMRepo.GetRSM(Id);
            ASMRepository               ASMRepo         = new ASMRepository();
            List <AreaSalesManager>     ASMList         = ASMRepo.GetASM(Id);
            ESMRepository               ESMRepo         = new ESMRepository();
            List <ClientEmployee>       ESMList         = ESMRepo.GetESM(Id);
            BEATRepository              BEATRepo        = new BEATRepository();
            List <LocationBeat>         BEATList        = BEATRepo.GetBEAT(Id);
            DistributorRepository       DistributorRepo = new DistributorRepository();
            List <Distributor>          DIstributorList = DistributorRepo.GetDistributor(Id);
            List <FACompanyZone>        CompanyZone     = db.FACompanyZones.Where(x => x.CompanyId == Id).ToList();
            List <ErrorTemplates>       errorList       = new List <ErrorTemplates>();

            foreach (var item in beatHierarchyList)
            {
                if (NSMList.Exists(x => x.Name == item.NSM))
                {
                    ErrorTemplates er = new ErrorTemplates();
                    er.ErrorComments = "NSM : " + item.NSM + " already exists cannot be added";
                    errorList.Add(er);
                }

                if (ZSMList.Exists(x => x.Name == item.ZSM))
                {
                    ErrorTemplates er = new ErrorTemplates();
                    er.ErrorComments = "ZSM : " + item.ZSM + " already exists cannot be added";
                    errorList.Add(er);
                }

                if (RSMList.Exists(x => x.Name == item.RSM))
                {
                    ErrorTemplates er = new ErrorTemplates();
                    er.ErrorComments = "RSM : " + item.RSM + " already exists cannot be added";
                    errorList.Add(er);
                }

                if (ASMList.Exists(x => x.Name == item.ASM))
                {
                    ErrorTemplates er = new ErrorTemplates();
                    er.ErrorComments = "ASM : " + item.ASM + " already exists cannot be added";
                    errorList.Add(er);
                }

                if (ESMList.Exists(x => x.Name == item.ESM))
                {
                    ErrorTemplates er = new ErrorTemplates();
                    er.ErrorComments = "ESM : " + item.ESM + " already exists cannot be added";
                    errorList.Add(er);
                }

                if (BEATList.Exists(x => x.Name == item.FinalBeatName))
                {
                    ErrorTemplates er = new ErrorTemplates();
                    er.ErrorComments = "FinalBeatName : " + item.FinalBeatName + " already exists cannot be added";
                    errorList.Add(er);
                }
                if (BEATList.Exists(x => x.ErpId == item.BeatErpId) && item.BeatErpId != null)
                {
                    ErrorTemplates er = new ErrorTemplates();
                    er.ErrorComments = "BeatErpId : " + item.BeatErpId + " cannot be assigned as it already existing ";
                    errorList.Add(er);
                }

                if (DIstributorList.Exists(x => x.Name == item.DistributorName))
                {
                    ErrorTemplates er = new ErrorTemplates();
                    er.ErrorComments = "Distrbutor : " + item.DistributorName + " already exists cannot be added";
                    errorList.Add(er);
                }
                if (CompanyZone.Exists(x => x.Name == item.ESMZone))
                {
                    ErrorTemplates er = new ErrorTemplates();
                    er.ErrorComments = "ESMZone : " + item.ESMZone + " already exists cannot be added";
                    errorList.Add(er);
                }
            }

            return(View(errorList));
        }