public ActionResult Add(MCompanyVM model)
 {
     if (ModelState.IsValid)
     {
         if (MCompanyRepo.CheckCompany(model) == true)
         {
             var result = new
             {
                 success      = true,
                 alertType    = "warning",
                 alertStrong  = "Error !",
                 alertMessage = "Company Already Created"
             };
             return(Json(result, JsonRequestBehavior.AllowGet));
         }
         else if (MCompanyRepo.CheckCompany(model) == false)
         {
             MCompanyRepo.insert(model);
             var result = new
             {
                 success      = false,
                 alertType    = "success",
                 alertStrong  = "Data Saved !",
                 alertMessage = "New company has been add with Code" + model.code
             };
             return(Json(result, JsonRequestBehavior.AllowGet));
         }
     }
     return(PartialView("_Add", model));
 }
 public ActionResult Edit(MCompanyVM model)
 {
     if (ModelState.IsValid)
     {
         if (MCompanyRepo.CheckCompany(model) == true)
         {
             var result = new
             {
                 success      = true,
                 alertType    = "warning",
                 alertStrong  = "Error !",
                 alertMessage = "Company Already Created"
             };
             return(Json(result, JsonRequestBehavior.AllowGet));
         }
         else if (MCompanyRepo.CheckCompany(model) == false)
         {
             MCompanyRepo.update(model);
             var result = new
             {
                 success      = false,
                 alertType    = "success",
                 alertStrong  = "Data Updated !",
                 alertMessage = "Data company has been Updated"
             };
             return(Json(result, JsonRequestBehavior.AllowGet));
         }
     }
     return(PartialView(model));
 }
        public ActionResult Delete(int id)
        {
            MCompanyVM model = MCompanyRepo.getbyid(id);

            if (MEmployeeRepo.Hitung(model.id) == true)
            {
                var result = new
                {
                    success      = true,
                    alertType    = "warning",
                    alertStrong  = "Error!",
                    alertMessage = "Company still has employees"
                };
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            else if (MEmployeeRepo.Hitung(model.id) == false)
            {
                MCompanyRepo.Delete(model);
                var result = new
                {
                    success      = false,
                    alertType    = "success",
                    alertStrong  = "Data Deleted !",
                    alertMessage = "Data Deleted! Data Company With Code" + model.code + "Has been delete"
                };
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            return(PartialView("_Delete", model));
        }
        public ActionResult Add()
        {
            MCompanyVM model = new MCompanyVM();

            model.code    = MCompanyRepo.KodeAuto();
            ViewBag.Title = "Add";
            return(PartialView("_Add", model));
        }
        public ActionResult Detail(int id)
        {
            MCompanyVM model = MCompanyRepo.GetDetail(id);

            ViewBag.Title = "View Company-" + model.name + "(" + model.code + ")";


            return(PartialView(model));
        }
        //public static List<m_companyVM> SearchData(m_companyVM model)
        //{

        //    var data = new List<m_companyVM>();
        //    using (AppEntity db = new AppEntity())
        //    {
        //        data = db.m_company.Select(x => new m_companyVM()
        //        {
        //            id = x.id,
        //            code = x.code,
        //            name = x.name,
        //            address = x.address,
        //            phone = x.phone,
        //            email = x.email,
        //            is_active = x.is_active,
        //            created_by = x.created_by,
        //            created_date = x.created_date,
        //            updated_by = x.updated_by,
        //            updated_date = x.updated_date,

        //        })
        //        .Where(x => x.is_active == true && ( x.code.Contains(model.code)  || x.name.Contains(model.name)||(x.created_date.Day == model.created_date.Day && x.created_date.Month == model.created_date.Month && x.created_date.Year == model.created_date.Year)))
        //        .ToList();
        //    }
        //    return data;
        //}

        public static bool CheckCompany(MCompanyVM model)
        {
            bool result = false;

            using (AppEntity db = new AppEntity())
            {
                var data = db.m_company.Where(x => x.name == model.name && x.id != model.id).ToList();
                if (data.Count > 0)
                {
                    result = true;
                }
            }
            return(result);
        }
        public static bool Delete(MCompanyVM model)
        {
            bool result = false;

            using (AppEntity db = new AppEntity())
            {
                m_company item = db.m_company.Find(model.id);
                item.is_active    = false;
                item.updated_by   = 1;
                item.updated_date = DateTime.Now;
                try { db.SaveChanges(); result = true; } catch (Exception) { throw; }
            }

            return(result);
        }
        public static MCompanyVM getbyid(int id)
        {
            MCompanyVM hasil = new MCompanyVM();

            using (AppEntity db = new AppEntity())
            {
                hasil = db.m_company.Select(x => new MCompanyVM()
                {
                    id           = x.id,
                    code         = x.code,
                    name         = x.name,
                    address      = x.address,
                    phone        = x.phone,
                    email        = x.email,
                    created_by   = x.created_by,
                    created_date = x.created_date
                }).Where(x => x.id == id).FirstOrDefault();
            }
            return(hasil);
        }
        public static bool update(MCompanyVM model)
        {
            bool result = false;

            using (AppEntity db = new AppEntity())
            {
                m_company item = db.m_company.Find(model.id);
                item.id           = model.id;
                item.code         = model.code;
                item.name         = model.name;
                item.address      = model.address;
                item.phone        = model.phone;
                item.is_active    = true;
                item.email        = model.email;
                item.updated_by   = 1;
                item.updated_date = DateTime.Now;
                try { db.SaveChanges(); result = true; } catch (Exception) { throw; }
            }
            return(result);
        }
        public static bool insert(MCompanyVM model)
        {
            bool result = false;

            using (AppEntity db = new AppEntity())
            {
                m_company item = new m_company()
                {
                    code         = model.code,
                    name         = model.name,
                    address      = model.address,
                    phone        = model.phone,
                    email        = model.email,
                    is_active    = true,
                    created_by   = 1,
                    created_date = DateTime.Now
                };
                db.m_company.Add(item);
                try { db.SaveChanges(); result = true; } catch (Exception) { throw; }
            }
            return(result);
        }
        public ActionResult Edit(int id)
        {
            MCompanyVM model = MCompanyRepo.getbyid(id);

            return(PartialView(model));
        }