public void Save(AdminProductPackagingViewModel adminProductPackagingViewModel)
 {
     ProductPackaging pPackaging = new ProductPackaging(adminProductPackagingViewModel.Id)
     {
         Name = adminProductPackagingViewModel.Name,
         Description = adminProductPackagingViewModel.Description,
         Code = adminProductPackagingViewModel.Code
        
     };
     _productPackagingRepository.Save(pPackaging);
 }
        public ActionResult ImportProductPackaging(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();
                        AdminProductPackagingViewModel pdvm = new AdminProductPackagingViewModel();
                        while (reader.Read())
                        {
                           
                            string code = reader["code"].ToString();
                            string name = reader["name"].ToString();
                            string description = reader["description"].ToString();
                            bool hasDuplicateName = _adminProductPackaginViewModelBuilder.GetAll()
                            .Any(p => p.Name == name);

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

                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 CreateProductPackaging(AdminProductPackagingViewModel adminProductPackagingViewModel)
 {
     try
     {
         ViewBag.msg = null;
         adminProductPackagingViewModel.Id = Guid.NewGuid();
         _adminProductPackaginViewModelBuilder.Save(adminProductPackagingViewModel);
         _auditLogViewModelBuilder.AddAuditLog(this.User.Identity.Name, "Create", "Product Packaging", DateTime.Now);
         TempData["msg"] = "Packaging Successfully Created";
         return RedirectToAction("ListProductPackaging");
     }
     catch (DomainValidationException ve)
     {
         ValidationSummary.DomainValidationErrors(ve,ModelState);
         _log.InfoFormat("Failed to create product packaging"+ve.Message);
         return View();
     }
     catch(Exception ex)
     {
         ViewBag.msg = ex.Message;
         _log.InfoFormat("Failed to create product packaging" + ex.Message);
         return View();
     }
 }
 public ActionResult EditProductPackaging(AdminProductPackagingViewModel vm)
 {
     //Validation
     try
     {
         _adminProductPackaginViewModelBuilder.Save(vm);
         _auditLogViewModelBuilder.AddAuditLog(this.User.Identity.Name, "Edit", "Product Packaging", DateTime.Now);
         TempData["msg"] = "Packaging Successfully Edited";
         return RedirectToAction("ListProductPackaging");
     }
     catch (DomainValidationException ve)
     {
          ValidationSummary.DomainValidationErrors(ve,ModelState);
          _log.InfoFormat("Edtiting product packaging error. Message="+ve.Message);
         return View();
     }
     catch (Exception ex)
     {
         ViewBag.msg = ex.Message;
         _log.InfoFormat("Edtiting product packaging error. Message=" + ex.Message);
         return View();
     }
 }