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);
        }
 BLL.CompanyDetail CompanyDetailDAL_BLL(DAL.CompanyDetail d)
 {
     return(d.toCopy <BLL.CompanyDetail>(new BLL.CompanyDetail()));
 }