public void Save(AdminProductTypeViewModel adminProductTypeViewModel)
 {
     ProductType pb = new ProductType(adminProductTypeViewModel.Id)
     {
         Name = adminProductTypeViewModel.Name,
         Description=adminProductTypeViewModel.Description,
         Code=adminProductTypeViewModel.Code,
     };
     _productTypeRepository.Save(pb);
 }
        public ActionResult ImportProductType(HttpPostedFileBase file)
        {

            try
            {

                var fileName = Path.GetFileName(file.FileName);


                var directory = Server.MapPath("~/Uploads");
                if (Directory.Exists(directory) == false)
                {
                    Directory.CreateDirectory(directory);
                }
                var path = Server.MapPath("~/Uploads") + "\\" + fileName;


                file.SaveAs(path);


                string fileExtension = Path.GetExtension(fileName);
                if (fileExtension == ".xlsx")
                {
                    ViewBag.msg = "Please wait. Upload in progress";

                    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;'";

                    OleDbConnection conn = new OleDbConnection(connectionString);
                    try
                    {
                        conn.Open();
                        OleDbCommand command = new OleDbCommand("SELECT code,name,description FROM [Sheet1$]", conn);
                        OleDbDataReader reader = command.ExecuteReader();
                        AdminProductTypeViewModel pdvm = new AdminProductTypeViewModel();
                        while (reader.Read())
                        {

                            string code = reader["code"].ToString();
                            string name = reader["name"].ToString();
                            string description = reader["description"].ToString();
                            bool hasDuplicateName = _adminProductTypeViewModelBuilder.GetAll()
                            .Any(p => p.Name == name);

                            if (hasDuplicateName)
                            { }
                            else
                            {
                                pdvm.Name = name;
                                pdvm.Code = code;
                                pdvm.Description = description;
                                _adminProductTypeViewModelBuilder.Save(pdvm);
                            }
                        }
                    }
                    catch (OleDbException ex)
                    {
                        ViewBag.msg = ex.ToString();
                        return View();
                    }

                    finally
                    {
                        conn.Close();

                    }

                    fi = new FileInfo(path);

                    fi.Delete();
                    _auditLogViewModelBuilder.AddAuditLog(this.User.Identity.Name, "Import", "Product Type", DateTime.Now);
                    ViewBag.msg = "Upload Successful";
                    return RedirectToAction("ListProductTypes");
                }

                else
                {
                    fi = new FileInfo(path);

                    fi.Delete();
                    ViewBag.msg = "Please upload excel file with extension .xlsx";
                    return View();
                }
            }
            catch (Exception ex)
            {

                ViewBag.msg = ex.ToString();
                return View();
            }


        }
        public ActionResult CreateProductType(AdminProductTypeViewModel adminProductTypeViewModel)
        {

          
            try
            {
                if (adminProductTypeViewModel.Name == null)
                {
                    ModelState.AddModelError("Product Type", "Product Type Name Must Be Provided");
                    return View();
                   
                }
                else
                {
                    adminProductTypeViewModel.Id = Guid.NewGuid();
                _adminProductTypeViewModelBuilder.Save(adminProductTypeViewModel);
                TempData["msg"] = "Product Type Successfully Created";
                _auditLogViewModelBuilder.AddAuditLog(this.User.Identity.Name, "Create", "Product Type", DateTime.Now);
                return RedirectToAction("listproducttypes");
                }
            }
            catch (DomainValidationException ve)
            {
                ValidationSummary.DomainValidationErrors(ve, ModelState);
                _log.Debug("Failed to create product type" + ve.Message);
                _log.Error("Failed to create product type" + ve.ToString());
                return View();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                _log.Debug("Failed to create product type" + ex.Message);
                _log.Error("Failed to create product type" + ex.ToString());
                return View();
            }
            
          
        }
        public ActionResult EditProductType(AdminProductTypeViewModel vm)
        {
            try
            {
                _adminProductTypeViewModelBuilder.Save(vm);
                _auditLogViewModelBuilder.AddAuditLog(this.User.Identity.Name, "Edit", "Product Type", DateTime.Now);
                TempData["msg"] = "Product Type Successfully Edited";
                return RedirectToAction("listproducttypes");
            }
            catch (DomainValidationException ve) {

                ValidationSummary.DomainValidationErrors(ve, ModelState);
                _log.Debug("Failed to load product type" + ve.Message);
                _log.Error("Failed to load product type" + ve.ToString());
                return View();
            
            }
            catch (Exception ex)
            {

                ModelState.AddModelError("", ex.Message);
                _log.Debug("Failed to edit product type" + ex.Message);
                _log.Error("Failed to edit product type" + ex.ToString());
                return View();
            }
        }