void UserSetup(BLL.CompanyDetail cmp)
        {
            DAL.UserAccount ua = new DAL.UserAccount();
            ua.LoginId  = cmp.UserId;
            ua.UserName = cmp.UserId;
            ua.Password = cmp.Password;

            DAL.UserType ut = new DAL.UserType();
            ut.TypeOfUser = BLL.DataKeyValue.Administrator_Key;
            ut.CompanyId  = cmp.Id;
            ut.UserAccounts.Add(ua);

            foreach (var utfd in DB.UserTypeFormDetails)
            {
                DAL.UserTypeDetail utd = new DAL.UserTypeDetail();
                utd.UserTypeFormDetailId = utfd.Id;
                utd.IsViewForm           = true;
                utd.AllowInsert          = true;
                utd.AllowUpdate          = true;
                utd.AllowDelete          = true;
                ut.UserTypeDetails.Add(utd);
            }

            DB.UserTypes.Add(ut);
            DB.SaveChanges();

            insertDataKeyValue(cmp.Id, ut.TypeOfUser, ut.Id);
        }
        void AccountSetup(BLL.CompanyDetail cmp)
        {
            DAL.AccountGroup pr = new DAL.AccountGroup();
            pr.GroupName = BLL.DataKeyValue.Primary_Key;
            pr.GroupCode = "";
            pr.CompanyId = cmp.Id;
            DB.AccountGroups.Add(pr);
            DB.SaveChanges();
            insertDataKeyValue(cmp.Id, pr.GroupName, pr.Id);


            AccountSetup_Asset(pr);
            AccountSetup_Liabilities(pr);
            AccountSetup_Income(pr);
            AccountSetup_Expense(pr);

            DAL.Ledger PL = new DAL.Ledger();
            PL.LedgerName     = BLL.DataKeyValue.Profit_Loss_Ledger_Key;
            PL.AccountGroupId = pr.Id;
            DB.Ledgers.Add(PL);
            DB.SaveChanges();
            insertDataKeyValue(pr.CompanyId, PL.LedgerName, PL.Id);
        }
        public int CompanyDetail_Save(BLL.CompanyDetail cm)
        {
            try
            {
                cm.IsActive = true;
                DAL.CompanyDetail d = DB.CompanyDetails.Where(x => x.Id == cm.Id).FirstOrDefault();

                if (d == null)
                {
                    d = new DAL.CompanyDetail();
                    DB.CompanyDetails.Add(d);

                    cm.toCopy <DAL.CompanyDetail>(d);

                    DB.SaveChanges();
                    cm.Id = d.Id;
                    if (d.Id != 0)
                    {
                        CompanySetup(cm);
                        CurrencySetup(cm);
                        if (d.UnderCompanyId != null)
                        {
                            var lstCompany = DB.CompanyDetails.Where(x => x.Id == d.UnderCompanyId || (x.Id != d.Id && x.UnderCompanyId == d.UnderCompanyId)).ToList();
                            int AGId       = DB.DataKeyValues.Where(x => x.CompanyId == cm.Id && x.DataKey == BLL.DataKeyValue.BranchDivisions_Key).FirstOrDefault().DataValue;

                            foreach (var c in lstCompany)
                            {
                                DAL.Ledger dl1 = new DAL.Ledger();
                                dl1.LedgerName     = string.Format("{0}-{1}", cm.CompanyType == "Company" ? "CM" : (cm.CompanyType == "Warehouse" ? "WH" : "DL"), cm.CompanyName);
                                dl1.AccountGroupId = DB.DataKeyValues.Where(x => x.CompanyId == c.Id && x.DataKey == BLL.DataKeyValue.BranchDivisions_Key).FirstOrDefault().DataValue;
                                dl1.AddressLine1   = cm.AddressLine1;
                                dl1.AddressLine2   = cm.AddressLine2;
                                dl1.CityName       = cm.CityName;
                                dl1.EMailId        = cm.EMailId;
                                dl1.GSTNo          = cm.GSTNo;
                                dl1.MobileNo       = cm.MobileNo;
                                dl1.TelephoneNo    = cm.TelephoneNo;
                                DB.Ledgers.Add(dl1);
                                DB.SaveChanges();

                                DAL.Ledger dl2 = new DAL.Ledger();
                                dl2.LedgerName     = string.Format("{0}-{1}", c.CompanyType == "Company" ? "CM" : (c.CompanyType == "Warehouse" ? "WH" : "DL"), c.CompanyName);
                                dl2.AccountGroupId = AGId;
                                dl2.AddressLine1   = c.AddressLine1;
                                dl2.AddressLine2   = c.AddressLine2;
                                dl2.CityName       = c.CityName;
                                dl2.EMailId        = c.EMailId;
                                dl2.GSTNo          = c.GSTNo;
                                dl2.MobileNo       = c.MobileNo;
                                dl2.TelephoneNo    = c.TelephoneNo;
                                DB.Ledgers.Add(dl2);
                                DB.SaveChanges();
                            }
                        }
                    }
                }
                else
                {
                    var CName = d.CompanyName;
                    cm.toCopy <DAL.CompanyDetail>(d);
                    DB.SaveChanges();

                    var LName     = string.Format("{0}-{1}", cm.CompanyType == "Company" ? "CM" : (cm.CompanyType == "Warehouse" ? "WH" : "DL"), CName);
                    var lstLedger = DB.Ledgers.Where(x => x.LedgerName == LName).ToList();
                    foreach (var dl in lstLedger)
                    {
                        dl.LedgerName   = string.Format("{0}-{1}", cm.CompanyType == "Company" ? "CM" : (cm.CompanyType == "Warehouse" ? "WH" : "DL"), cm.CompanyName);
                        dl.AddressLine1 = cm.AddressLine1;
                        dl.AddressLine2 = cm.AddressLine2;
                        dl.CityName     = cm.CityName;
                        dl.EMailId      = cm.EMailId;
                        dl.GSTNo        = cm.GSTNo;
                        dl.MobileNo     = cm.MobileNo;
                        dl.TelephoneNo  = cm.TelephoneNo;
                    }
                    DB.SaveChanges();
                }

                //  Clients.All.CompanyDetail_Save(cm);
                Clients.Clients(OtherLoginClientsOnGroup).CompanyDetail_Save(cm);

                return(cm.Id);
            }
            catch (Exception ex) { }
            return(0);
        }
 private void CompanySetup(BLL.CompanyDetail sgp)
 {
     UserSetup(sgp);
     AccountSetup(sgp);
 }