public async Task <IActionResult> AddBranchOffice(BranchOfficeForm branchOffices)
        {
            BranchOffices bo = new BranchOffices()
            {
                Name      = branchOffices.Name,
                Address   = branchOffices.Address,
                City      = branchOffices.City,
                Telephone = branchOffices.Telephone.ToString()
            };

            CarCompany carCompany = new CarCompany();

            carCompany = context.Carcompanies.Find(branchOffices.IdCompany);

            bo.Id_company = carCompany;



            context.BranchOffices.Add(bo);

            await context.SaveChangesAsync();

            context.Entry(carCompany).State = EntityState.Modified;

            return(Ok());
        }
        public void EditBranchOfficeManagement(BranchOfficeManagementModel model)
        {
            BranchOffices result = ent.BranchOffices.Where(x => x.BranchOfficeId == model.BranchOfficeId).FirstOrDefault();

            model.BranchOfficeCode  = result.BranchOfficeCode;
            result.BranchOfficeId   = model.BranchOfficeId;
            result.BranchOfficeName = model.BranchOffice;
            result.NativeCountryId  = model.NativeCountry;
            result.ZoneId           = model.Zone;
            result.DistrictId       = model.District;
            result.Address          = model.Address;
            result.Phone            = model.Phone;
            result.Email            = model.Email;
            result.FaxNo            = model.Fax;
            result.PanNo            = model.PanNo;
            result.Web           = model.Web;
            result.Status        = Convert.ToBoolean(model.status);
            result.TimeZoneId    = model.TimeZone;
            result.isSystem      = model.isSystem;
            result.UpdatedBy     = model.UpdatedBy;
            result.UpdatedDate   = DateTime.UtcNow;
            result.BranchClassId = model.BranchClassId;

            ent.ApplyCurrentValues(result.EntityKey.EntitySetName, result);
            ent.SaveChanges();

            UpdateGeneralLegder(model);
        }
        public IHttpActionResult PutBranchOffices(int id, BranchOffices branchOffices)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != branchOffices.ID)
            {
                return(BadRequest());
            }

            db.Entry(branchOffices).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BranchOfficesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public int CreateBranchOfficeManagement(BranchOfficeManagementModel model)
        {
            int           BranchOfficeId = 0;
            BranchOffices obj            = new BranchOffices();

            obj.BranchOfficeName = model.BranchOffice;
            obj.NativeCountryId  = model.NativeCountry;
            obj.ZoneId           = model.Zone;
            obj.DistrictId       = model.District;
            obj.Address          = model.Address;
            obj.Phone            = model.Phone;
            obj.Email            = model.Email;
            obj.FaxNo            = model.Fax;
            obj.PanNo            = model.PanNo;
            obj.Web           = model.Web;
            obj.Status        = Convert.ToBoolean(model.status);
            obj.TimeZoneId    = model.TimeZone;
            obj.isSystem      = model.isSystem;
            obj.CreatedBy     = model.CreatedBy;
            obj.CreatedDate   = DateTime.UtcNow;
            obj.BranchClassId = model.BranchClassId;
            ent.AddToBranchOffices(obj);
            ent.SaveChanges();

            BranchOfficeId = obj.BranchOfficeId;
            string BranchOfficeCode = UpdateBranchOfficeCode(BranchOfficeId);

            return(BranchOfficeId);
        }
        public string UpdateBranchOfficeCode(int BranchOfficeId)
        {
            BranchOffices branchofficecode = ent.BranchOffices.Where(u => u.BranchOfficeId == BranchOfficeId).FirstOrDefault();
            string        code             = BranchOfficeId.ToString().PadLeft(4, '0');

            branchofficecode.BranchOfficeCode = "AHB-" + code;
            ent.ApplyCurrentValues(branchofficecode.EntityKey.EntitySetName, branchofficecode);
            ent.SaveChanges();
            return(branchofficecode.BranchOfficeCode);
        }
        public IHttpActionResult GetBranchOffices(int id)
        {
            BranchOffices branchOffices = db.BranchOffices.Find(id);

            if (branchOffices == null)
            {
                return(NotFound());
            }

            return(Ok(branchOffices));
        }
        public IHttpActionResult PostBranchOffices(BranchOffices branchOffices)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.BranchOffices.Add(branchOffices);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = branchOffices.ID }, branchOffices));
        }
        public bool CheckDuplicateBranchName(string BranchName)
        {
            BranchOffices Branch = ent.BranchOffices.Where(ii => ii.BranchOfficeName == BranchName).FirstOrDefault();

            if (Branch != null)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
        public bool CheckEditDuplicateEmail(string email, int branchofficeid)
        {
            BranchOffices branchoffice = ent.BranchOffices.Where(ii => ii.Email == email && ii.BranchOfficeId != branchofficeid).FirstOrDefault();

            if (branchoffice == null)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
        public bool CheckDuplicateEmail(string email)
        {
            BranchOffices BranchOffice = ent.BranchOffices.Where(ii => ii.Email == email).FirstOrDefault();

            if (BranchOffice != null)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
        public IHttpActionResult DeleteBranchOffices(int id)
        {
            BranchOffices branchOffices = db.BranchOffices.Find(id);

            if (branchOffices == null)
            {
                return(NotFound());
            }

            db.BranchOffices.Remove(branchOffices);
            db.SaveChanges();

            return(Ok(branchOffices));
        }
        public BranchOfficeManagementModel BranchOfficeManagementDetail(int BranchOfficeId)
        {
            BranchOffices     result            = ent.BranchOffices.Where(x => x.BranchOfficeId == BranchOfficeId).FirstOrDefault();
            View_AgentDetails Agentadmindetails = _agentdetailsprovider.GetAgentAdminUser(BranchOfficeId);

            BranchOfficeManagementModel model = new BranchOfficeManagementModel();

            model.BranchOfficeId    = result.BranchOfficeId;
            model.BranchOfficeId    = result.BranchOfficeId;
            model.BranchOfficeCode  = result.BranchOfficeCode;
            model.BranchOffice      = result.BranchOfficeName;
            model.NativeCountry     = result.NativeCountryId;
            model.NativeCountryName = result.Countries.CountryName;
            model.Zone         = result.ZoneId;
            model.ZoneName     = result.Zones.ZoneName;
            model.District     = result.DistrictId;
            model.DistrictName = result.Districts.DistrictName;
            model.Address      = result.Address;
            model.Phone        = result.Phone;
            model.Email        = result.Email;
            model.Fax          = result.FaxNo;
            model.PanNo        = result.PanNo;
            model.Web          = result.Web;
            model.status       = Convert.ToInt32(result.Status);
            model.TimeZone     = result.TimeZoneId;
            model.isSystem     = result.isSystem;
            model.CreatedBy    = result.CreatedBy;
            model.CreatedDate  = result.CreatedDate;
            model.UpdatedBy    = result.UpdatedBy;
            model.UpdatedDate  = result.UpdatedDate;


            View_BranchDetails BranchUserDetail = GetBranchUserDetails(BranchOfficeId);

            if (BranchUserDetail != null)
            {
                model.FullName        = BranchUserDetail.FullName;
                model.UserName        = BranchUserDetail.UserName;
                model.UserEmail       = BranchUserDetail.Email;
                model.UserAddress     = BranchUserDetail.UserAddress;
                model.UserPhone       = BranchUserDetail.Phone;
                model.MobileNo        = BranchUserDetail.MobileNumber;
                model.Password        = BranchUserDetail.Password;
                model.ConfirmPassword = BranchUserDetail.Password;
            }

            return(model);
        }
        public async Task <IActionResult> UpdateBranchOffice(BranchOfficeForm branchOffices)
        {
            BranchOffices branch = new BranchOffices();

            branch.Id                   = Int32.Parse(branchOffices.Id);
            branch.Name                 = branchOffices.Name;
            branch.Telephone            = branchOffices.Telephone.ToString();
            branch.Address              = branchOffices.Address;
            branch.City                 = branchOffices.City;
            context.Entry(branch).State = EntityState.Modified;

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(NotFound());
            }

            return(NoContent());
        }