public SAPModel UpdateModelSAPExsits(SAPModel res, SAPModel model)
        {
            SAPModel update = res;

            update.Quantity = new SAPQtyModel
            {
                Unrestricted       = res.Quantity.Unrestricted + model.Quantity.Unrestricted,
                QualityInspection  = res.Quantity.QualityInspection + model.Quantity.QualityInspection,
                Blocked            = res.Quantity.Blocked + model.Quantity.Blocked,
                TransitAndTransfer = res.Quantity.TransitAndTransfer + model.Quantity.TransitAndTransfer
            };
            return(update);
        }
        public IActionResult UploadSAP(IFormFile fExcelSAP)
        {
            // import data
            if (fExcelSAP != null && CheckGetExtentionsFileIsSupported(fExcelSAP))
            {
                using (var stream = new MemoryStream())
                {
                    fExcelSAP.CopyTo(stream);
                    using (ExcelPackage package = new ExcelPackage(stream))
                    {
                        ExcelWorksheet workSheet = package.Workbook.Worksheets.First();
                        if (workSheet != null)
                        {
                            // List to ADD database
                            List <SAPModel> saps      = new List <SAPModel>();
                            int             totalRows = workSheet.Dimension.Rows;
                            for (int i = 2; i <= totalRows; i++)
                            {
                                SAPModel model = new SAPModel
                                {
                                    MaterialCode = workSheet.Cells[i, 1].Value.ToString(),
                                    MaterialDesc = String.Empty,
                                    LocalStorage = workSheet.Cells[i, 3].Value.ToString() != String.Empty ? workSheet.Cells[i, 3].Value.ToString(): "_",
                                    Batch        = workSheet.Cells[i, 5].Value.ToString() != String.Empty ? workSheet.Cells[i, 5].Value.ToString() : "_",
                                    Quantity     = new SAPQtyModel
                                    {
                                        Unrestricted       = Convert.ToInt32(workSheet.Cells[i, 7].Value),
                                        QualityInspection  = Convert.ToInt32(workSheet.Cells[i, 13].Value),
                                        Blocked            = Convert.ToInt32(workSheet.Cells[i, 17].Value),
                                        TransitAndTransfer = Convert.ToInt32(workSheet.Cells[i, 11].Value)
                                    }
                                };
                                if (IsStorageLocationIlegal(model.LocalStorage))
                                {
                                    if (checkExsistingInListSAP(saps, model))
                                    {
                                        SAPModel res = saps.SingleOrDefault(p => p.Batch == model.Batch && p.LocalStorage == model.LocalStorage && p.MaterialCode == model.LocalStorage);
                                        res = UpdateModelSAPExsits(res, model);
                                    }
                                    else
                                    {
                                        SAPModel res = new SAPModel()
                                        {
                                            MaterialCode = model.MaterialCode,
                                            MaterialDesc = model.MaterialDesc,
                                            Batch        = model.Batch,
                                            LocalStorage = model.LocalStorage,
                                            Quantity     = model.Quantity
                                        };
                                        saps.Add(res);
                                    }
                                }
                            }

                            HttpContext.Session.SetComplexData("SAPList", saps.OrderBy(p => p.LocalStorage).ToList());
                            return(RedirectToAction("Index"));
                        }
                        else
                        {
                            ViewBag.Error = "Không tìm thấy sheet cần thiết của hệ thống để import dữ liệu! Vui lòng kiểm tra tên của Sheet theo yêu cầu của hệ thống !";
                            return(View("Index"));
                        }
                    }
                }
            }
            else
            {
                ViewBag.Error = "Vui lòng chọn file excel hoặc định dạng file của bạn không được hỗ trợ. Lưu ý những file được hỗ trợ bao gồm : .xlsx, .csv ";
                return(View("Index"));
            }
        }
 public bool checkExsistingInListSAP(List <SAPModel> saps, SAPModel model)
 {
     return(saps.SingleOrDefault(p => p.Batch == model.Batch && p.LocalStorage == model.LocalStorage && p.MaterialCode == model.LocalStorage) != null);
 }