public async Task <IActionResult> Edit(int id, [Bind("KLHKG_PriceBookID,UniqueCode,CustType,SKU,Currency,Qty,ListPrice,MgrPrice,SDPrice,ID")] KLHKG_PriceBook kLHKG_PriceBook)
        {
            if (id != kLHKG_PriceBook.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(kLHKG_PriceBook);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!KLHKG_PriceBookExists(kLHKG_PriceBook.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(kLHKG_PriceBook));
        }
        public async Task <IActionResult> Create([Bind("KLHKG_PriceBookID,UniqueCode,CustType,SKU,Currency,Qty,ListPrice,MgrPrice,SDPrice,ID")] KLHKG_PriceBook kLHKG_PriceBook)
        {
            if (ModelState.IsValid)
            {
                _context.Add(kLHKG_PriceBook);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(kLHKG_PriceBook));
        }
        public async Task <JsonResult> ImportExcel()
        {
            string result = string.Empty, info = string.Empty;

            var  files    = Request.Form.Files;
            long filesize = files.Sum(f => f.Length);

            if (filesize > 1024 * 1024 * 30)
            {
                result = "Error";
                info   = "File size must be less than 30 MB.";
            }

            if (files.Count > 0 && filesize > 0)
            {
                foreach (var f in files)
                {
                    string fileExtend = new FileInfo(f.FileName).Extension;
                    var    filePath   = _hostingEnvironment.WebRootPath + @"\assets\upload\hkprice\" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + fileExtend;

                    if (f.Length > 0)
                    {
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await f.CopyToAsync(stream);
                        }
                    }


                    FileInfo file = new FileInfo(filePath);
                    try
                    {
                        using (ExcelPackage package = new ExcelPackage(file))
                        {
                            StringBuilder  sb        = new StringBuilder();
                            string         sheetname = package.Workbook.Worksheets[1].Name;
                            ExcelWorksheet worksheet = package.Workbook.Worksheets[sheetname];
                            int            rowCount  = worksheet.Dimension.Rows;
                            int            ColCount  = worksheet.Dimension.Columns;

                            KLHKG_PriceBook[] klprice = new KLHKG_PriceBook[rowCount - 1];

                            int i     = 0;
                            int maxid = _context.KLHKG_PriceBook.Max(m => m.ID);

                            for (int row = 2; row <= rowCount; row++)
                            {
                                string uniqueCode = worksheet.Cells[row, 2].Value.ToString().Trim();
                                uniqueCode += worksheet.Cells[row, 3].Value.ToString().Trim();
                                uniqueCode += ((int)Convert.ToDecimal(worksheet.Cells[row, 4].Value.ToString().Trim())).ToString();
                                uniqueCode += worksheet.Cells[row, 1].Value != null ? worksheet.Cells[row, 1].Value.ToString().Trim() : "";

                                KLHKG_PriceBook checkmodel = _context.KLHKG_PriceBook.Where(m => m.UniqueCode == uniqueCode).SingleOrDefault();
                                if (checkmodel != null)
                                {
                                    checkmodel.ListPrice = Convert.ToDecimal(worksheet.Cells[row, 5].Value.ToString().Trim());
                                    checkmodel.MgrPrice  = Convert.ToDecimal(worksheet.Cells[row, 6].Value.ToString().Trim());
                                    checkmodel.SDPrice   = Convert.ToDecimal(worksheet.Cells[row, 7].Value.ToString().Trim());
                                    await _context.SaveChangesAsync();
                                }
                                else
                                {
                                    klprice[i] = new KLHKG_PriceBook
                                    {
                                        ID = maxid + (i + 1),
                                        KLHKG_PriceBookID = Guid.NewGuid().ToString(),
                                        UniqueCode        = uniqueCode,
                                        CustType          = worksheet.Cells[row, 1].Value != null ? worksheet.Cells[row, 1].Value.ToString().Trim() : "",
                                        SKU       = worksheet.Cells[row, 2].Value.ToString().Trim(),
                                        Currency  = worksheet.Cells[row, 3].Value.ToString().Trim(),
                                        Qty       = Convert.ToDecimal(worksheet.Cells[row, 4].Value.ToString().Trim()),
                                        ListPrice = Convert.ToDecimal(worksheet.Cells[row, 5].Value.ToString().Trim()),
                                        MgrPrice  = Convert.ToDecimal(worksheet.Cells[row, 6].Value.ToString().Trim()),
                                        SDPrice   = Convert.ToDecimal(worksheet.Cells[row, 7].Value.ToString().Trim())
                                    };
                                    i++;
                                }
                            }
                            if (i > 0)
                            {
                                KLHKG_PriceBook[] klprice2 = new KLHKG_PriceBook[i];
                                for (int i1 = 0; i1 < i; i1++)
                                {
                                    klprice2[i1] = klprice[i1];
                                }
                                _context.KLHKG_PriceBook.AddRange(klprice2);
                            }
                            await _context.SaveChangesAsync();

                            //return Content(sb.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        result = "Error";
                        info   = ex.Message; break;

                        // Content(ex.Message);
                    }
                }
                if (string.IsNullOrEmpty(result))
                {
                    result = "Ok";
                    info   = "Update Successful.";
                }
            }
            else
            {
                result = "Error";
                info   = "Please choose upload file!";
            }
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("result", result);
            dic.Add("info", info);

            return(Json(dic));
        }