public void Save(AdminProductPackagingTypeViewModel adminProductPackagingTypeViewModel)
 {
     ProductPackagingType packType = new ProductPackagingType(adminProductPackagingTypeViewModel.Id)
     {
         Name = adminProductPackagingTypeViewModel.Name,
         Description = adminProductPackagingTypeViewModel.Description,
         Code = adminProductPackagingTypeViewModel.Code,
     };
     _productPackagingTypeRepository.Save(packType);
 }
 public ActionResult CreateProductPackagingType(AdminProductPackagingTypeViewModel adminProductPackagingViewModel)
 {
     try
     {
         adminProductPackagingViewModel.Id = Guid.NewGuid();
         _adminProductPackagingTypeViewModelBuilder.Save(adminProductPackagingViewModel);
         _auditLogViewModelBuilder.AddAuditLog(this.User.Identity.Name, "Create", "Product Packaging Type", DateTime.Now);
         TempData["msg"] = "Packaging Type Successfully Created";
         return RedirectToAction("ListProductPackagingType");
     }
     catch (DomainValidationException dve)
     {
         ValidationSummary.DomainValidationErrors(dve, ModelState);
         _log.InfoFormat("Creating product packaging type error. Message=" + dve.Message);
         return View();
     }
     catch (Exception err)
     {
         ViewBag.msg = err.Message;
         return View();
     }
 }
        public ActionResult ImportProductPackagingType(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();
                        AdminProductPackagingTypeViewModel pdvm = new AdminProductPackagingTypeViewModel();
                        while (reader.Read())
                        {

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

                            if (hasDuplicateName)
                            { }
                            else
                            {
                                pdvm.Name = name;
                                pdvm.Code = code;
                                pdvm.Description = description;
                                _adminProductPackagingTypeViewModelBuilder.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 Packaging Type", DateTime.Now);
                    ViewBag.msg = "Upload Successful";
                    return RedirectToAction("ListProductPackagingType");
                }

                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();
            }


        }