Пример #1
0
        public ActionResult Import(HttpPostedFileBase excelfile, int?StoreId)
        {
            try
            {
                if (excelfile == null || excelfile.ContentLength == 0)
                {
                    ViewBag.Import = "Bạn vui lòng chọn 1 file excel";
                    CreateViewBag();
                    return(View("Index"));
                }
                else
                {
                    using (var package = new ExcelPackage(excelfile.InputStream))
                    {
                        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                        var            rowCnt    = worksheet.Dimension.End.Row - 4;
                        int            col       = 1;
                        for (int row = 5; worksheet.Cells[row, col].Value != null; row++)
                        {
                            #region kiểm tra giá trị null
                            if (string.IsNullOrEmpty(worksheet.Cells[row, 3].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 4].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 5].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 14].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 15].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 16].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 17].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 18].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 19].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 20].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 21].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 22].Text) ||
                                string.IsNullOrEmpty(worksheet.Cells[row, 23].Text))
                            {
                                ViewBag.Import = "vui lòng nhập các thông tin bắt buộc tại dòng " + row + "!";
                                CreateViewBag();
                                return(View("Index"));
                            }
                            #endregion
                            #region gán giatri
                            string  ProductCode       = worksheet.Cells[row, 3].Value.ToString();
                            string  ProductName       = worksheet.Cells[row, 4].Value.ToString();
                            int     ProductTypeId     = int.Parse(worksheet.Cells[row, 5].Value.ToString());
                            decimal ImportPrice       = Decimal.Parse(worksheet.Cells[row, 14].Value.ToString());
                            int     CurrencyId        = int.Parse(worksheet.Cells[row, 15].Value.ToString());;
                            decimal ExchangeRate      = Decimal.Parse(worksheet.Cells[row, 16].Value.ToString());
                            decimal ShippingFee       = Decimal.Parse(worksheet.Cells[row, 17].Value.ToString());
                            decimal COGS              = Decimal.Parse(worksheet.Cells[row, 18].Value.ToString());
                            decimal pprice1           = Decimal.Parse(worksheet.Cells[row, 19].Value.ToString());
                            decimal pprice2           = Decimal.Parse(worksheet.Cells[row, 20].Value.ToString());
                            decimal pprice3           = Decimal.Parse(worksheet.Cells[row, 21].Value.ToString());
                            decimal BeginInventoryQty = Decimal.Parse(worksheet.Cells[row, 22].Value.ToString());
                            bool    Actived           = bool.Parse(worksheet.Cells[row, 23].Value.ToString());
                            #endregion
                            #region cập nhật
                            if (worksheet.Cells[row, 2].Text != "")
                            {
                                ProductModel p         = new ProductModel();
                                int          ProductId = Int32.Parse(worksheet.Cells[row, 2].Value.ToString());
                                p = _context.ProductModel.Where(pp => pp.ProductId == ProductId).FirstOrDefault();
                                if (p != null)
                                {
                                    p.ProductCode   = ProductCode;
                                    p.ProductName   = ProductName;
                                    p.ProductTypeId = ProductTypeId;
                                    if (worksheet.Cells[row, 6].Text != "")
                                    {
                                        p.CategoryId = int.Parse(worksheet.Cells[row, 6].Value.ToString());
                                    }
                                    ;
                                    p.SEOProductName = Library.ConvertToNoMarkString(p.ProductName);
                                    p.CreatedDate    = DateTime.Now;
                                    p.CreatedAccount = currentAccount.UserName;
                                    if (worksheet.Cells[row, 7].Text != "")
                                    {
                                        p.OriginOfProductId = int.Parse(worksheet.Cells[row, 7].Value.ToString());
                                    }
                                    ;
                                    if (worksheet.Cells[row, 8].Text != "")
                                    {
                                        p.PolicyInStockId = int.Parse(worksheet.Cells[row, 8].Value.ToString());
                                    }
                                    ;
                                    if (worksheet.Cells[row, 9].Text != "")
                                    {
                                        p.PolicyOutOfStockId = int.Parse(worksheet.Cells[row, 9].Value.ToString());
                                    }
                                    ;
                                    if (worksheet.Cells[row, 10].Text != "")
                                    {
                                        p.LocationOfProductId = int.Parse(worksheet.Cells[row, 10].Value.ToString());
                                    }
                                    ;
                                    if (worksheet.Cells[row, 11].Text != "")
                                    {
                                        p.ProductStatusId = int.Parse(worksheet.Cells[row, 11].Value.ToString());
                                    }
                                    ;
                                    if (worksheet.Cells[row, 12].Text != "")
                                    {
                                        p.ShippingWeight = int.Parse(worksheet.Cells[row, 12].Value.ToString());
                                    }
                                    ;
                                    if (worksheet.Cells[row, 13].Text != "")
                                    {
                                        p.UnitId = int.Parse(worksheet.Cells[row, 13].Value.ToString());
                                    }
                                    ;
                                    p.ImportPrice  = ImportPrice;
                                    p.CurrencyId   = CurrencyId;
                                    p.ExchangeRate = ExchangeRate;
                                    p.ShippingFee  = ShippingFee;
                                    p.COGS         = COGS;
                                    //p.StoreId = Store;
                                    ProductRepository ProductRepository = new ProductRepository(_context);
                                    p.ProductStoreCode = ProductRepository.GetProdcutStoreCode(StoreId.Value, p.ProductTypeId.Value, p.CategoryId.Value);
                                    //Product Price 1
                                    ProductPriceModel price1 = _context.ProductPriceModel.Where(pp => pp.ProductId == ProductId && pp.CustomerLevelId == 1).FirstOrDefault();
                                    price1.Price = pprice1;
                                    //Product Price 2
                                    ProductPriceModel price2 = _context.ProductPriceModel.Where(pp => pp.ProductId == ProductId && pp.CustomerLevelId == 2).FirstOrDefault();
                                    price2.Price = pprice2;

                                    //Product Price 3
                                    ProductPriceModel price3 = _context.ProductPriceModel.Where(pp => pp.ProductId == ProductId && pp.CustomerLevelId == 3).FirstOrDefault();
                                    price3.Price = pprice3;
                                    //Kiểm tra Field Kích hoạt
                                    p.Actived           = Actived;
                                    p.BeginInventoryQty = BeginInventoryQty;

                                    _context.Entry(price1).State = System.Data.Entity.EntityState.Modified;
                                    _context.Entry(price2).State = System.Data.Entity.EntityState.Modified;
                                    _context.Entry(price3).State = System.Data.Entity.EntityState.Modified;
                                    _context.Entry(p).State      = System.Data.Entity.EntityState.Modified;
                                    _context.SaveChanges();
                                }
                                else
                                {
                                    ViewBag.Import = "Không tìm thấy sản phẩm dòng " + row + " !";
                                    CreateViewBag();
                                    return(View("Index"));
                                }
                            }
                            #endregion
                            #region Thêm mới
                            else //Thêm mới
                            {
                                ProductModel p = new ProductModel();
                                p.ProductCode   = ProductCode;
                                p.ProductName   = ProductName;
                                p.ProductTypeId = ProductTypeId;
                                if (worksheet.Cells[row, 6].Text != "")
                                {
                                    p.CategoryId = int.Parse(worksheet.Cells[row, 5].Value.ToString());
                                }
                                ;
                                p.SEOProductName = Library.ConvertToNoMarkString(p.ProductName);
                                p.CreatedDate    = DateTime.Now;
                                p.CreatedAccount = currentAccount.UserName;
                                if (worksheet.Cells[row, 7].Text != "")
                                {
                                    p.OriginOfProductId = int.Parse(worksheet.Cells[row, 7].Value.ToString());
                                }
                                ;
                                if (worksheet.Cells[row, 8].Text != "")
                                {
                                    p.PolicyInStockId = int.Parse(worksheet.Cells[row, 8].Value.ToString());
                                }
                                ;
                                if (worksheet.Cells[row, 9].Text != "")
                                {
                                    p.PolicyOutOfStockId = int.Parse(worksheet.Cells[row, 9].Value.ToString());
                                }
                                ;
                                if (worksheet.Cells[row, 10].Text != "")
                                {
                                    p.LocationOfProductId = int.Parse(worksheet.Cells[row, 10].Value.ToString());
                                }
                                ;
                                if (worksheet.Cells[row, 11].Text != "")
                                {
                                    p.ProductStatusId = int.Parse(worksheet.Cells[row, 11].Value.ToString());
                                }
                                ;
                                if (worksheet.Cells[row, 12].Text != "")
                                {
                                    p.ShippingWeight = int.Parse(worksheet.Cells[row, 12].Value.ToString());
                                }
                                ;
                                if (worksheet.Cells[row, 13].Text != "")
                                {
                                    p.UnitId = int.Parse(worksheet.Cells[row, 13].Value.ToString());
                                }
                                ;
                                p.ImportPrice  = ImportPrice;
                                p.CurrencyId   = CurrencyId;
                                p.ExchangeRate = ExchangeRate;
                                p.ShippingFee  = ShippingFee;
                                p.COGS         = COGS;
                                //p.StoreId = Store;
                                ProductRepository ProductRepository = new ProductRepository(_context);
                                p.ProductStoreCode  = ProductRepository.GetProdcutStoreCode(StoreId.Value, p.ProductTypeId.Value, p.CategoryId.Value);
                                p.Actived           = Actived;
                                p.CreatedAccount    = currentAccount.UserName;
                                p.BeginInventoryQty = BeginInventoryQty;
                                AccountModel      Account           = _context.AccountModel.Where(pp => pp.UserName == p.CreatedAccount).FirstOrDefault();
                                IEOtherRepository IEOtherRepository = new IEOtherRepository(_context);
                                _context.Entry(p).State = System.Data.Entity.EntityState.Added;
                                _context.SaveChanges();

                                //Product Price 1
                                ProductPriceModel price1 = new ProductPriceModel()
                                {
                                    Price           = pprice1,
                                    CustomerLevelId = 1,
                                    ProductId       = p.ProductId
                                };
                                _context.Entry(price1).State = System.Data.Entity.EntityState.Added;
                                _context.SaveChanges();

                                // Product Price 2
                                ProductPriceModel price2 = new ProductPriceModel()
                                {
                                    Price           = pprice2,
                                    CustomerLevelId = 2,
                                    ProductId       = p.ProductId
                                };
                                _context.Entry(price2).State = System.Data.Entity.EntityState.Added;
                                _context.SaveChanges();

                                // Product Price 3
                                ProductPriceModel price3 = new ProductPriceModel()
                                {
                                    Price           = pprice3,
                                    CustomerLevelId = 3,
                                    ProductId       = p.ProductId
                                };

                                _context.Entry(price3).State = System.Data.Entity.EntityState.Added;
                                _context.SaveChanges();

                                //Kiểm tra Field Kích hoạt
                                if (p.BeginInventoryQty != 0)
                                {
                                    // Lưu vào IEOtherMasterModel
                                    IEOtherMasterModel IEMaster = new IEOtherMasterModel()
                                    {
                                        IEOtherMasterCode = IEOtherRepository.GetIEOtherCode(),
                                        WarehouseId       = p.BeginWarehouseId.Value,
                                        InventoryTypeId   = EnumInventoryType.ĐK,
                                        Note              = "Tồn đầu",
                                        CreatedDate       = DateTime.Now,
                                        CreatedAccount    = currentAccount.UserName,
                                        Actived           = true,
                                        CreatedEmployeeId = Account.EmployeeId,
                                        TotalPrice        = p.BeginInventoryQty * p.COGS
                                    };
                                    _context.Entry(IEMaster).State = System.Data.Entity.EntityState.Added;
                                    _context.SaveChanges();

                                    // Lưu vào IEOtherDetailModel
                                    IEOtherDetailModel detailmodel = new IEOtherDetailModel()
                                    {
                                        IEOtherMasterId    = IEMaster.IEOtherMasterId,
                                        ProductId          = p.ProductId,
                                        ImportQty          = p.BeginInventoryQty,
                                        ExportQty          = 0,
                                        Price              = p.COGS,
                                        UnitShippingWeight = (decimal)(p.ShippingWeight.HasValue ? p.ShippingWeight.Value : 1) * p.BeginInventoryQty,
                                        UnitPrice          = p.BeginInventoryQty * p.COGS,
                                        Note = IEMaster.Note
                                    };
                                    _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added;
                                    _context.SaveChanges();

                                    // Lưu vào InventoyMaster
                                    InventoryMasterModel InvenMaster = new InventoryMasterModel()
                                    {
                                        StoreId           = p.StoreId,
                                        InventoryTypeId   = EnumInventoryType.ĐK,
                                        WarehouseModelId  = p.BeginWarehouseId,
                                        InventoryCode     = IEMaster.IEOtherMasterCode,
                                        CreatedDate       = IEMaster.CreatedDate,
                                        CreatedAccount    = IEMaster.CreatedAccount,
                                        CreatedEmployeeId = IEMaster.CreatedEmployeeId,
                                        Actived           = true,
                                        BusinessId        = IEMaster.IEOtherMasterId, // Id nghiệp vụ
                                        BusinessName      = "IEOtherMasterModel",     // Tên bảng nghiệp vụ
                                        ActionUrl         = "/IEOtherMaster/Details/" // Đường dẫn ( cộng ID cho truy xuất)
                                    };
                                    _context.Entry(InvenMaster).State = System.Data.Entity.EntityState.Added;
                                    _context.SaveChanges();

                                    // Lưu vào InventoryDetailModel
                                    InventoryDetailModel InvenDetail = new InventoryDetailModel()
                                    {
                                        InventoryMasterId = InvenMaster.InventoryMasterId,
                                        ProductId         = p.ProductId,
                                        BeginInventoryQty = p.BeginInventoryQty,
                                        COGS            = p.COGS, // nhập
                                        Price           = 0,      // => Xuất
                                        ImportQty       = p.BeginInventoryQty,
                                        ExportQty       = 0,
                                        UnitCOGS        = p.COGS * p.BeginInventoryQty, // nhập
                                        UnitPrice       = 0,                            // => Xuất
                                        EndInventoryQty = p.BeginInventoryQty
                                    };
                                    _context.Entry(InvenDetail).State = System.Data.Entity.EntityState.Added;
                                    _context.SaveChanges();
                                }
                            }
                            #endregion
                        }
                    }

                    //Thread thread = new Thread(() => ImportThread(excelfile, StoreId));
                    //thread.Start();
                    //ViewBag.Import = "Đang Import ...";
                    ViewBag.Import = "Import thành công !";
                    CreateViewBag();
                    return(View("Index"));
                }
            }
            catch //(Exception ex)
            {
                ViewBag.Import = "Lỗi! Vui lòng liên hệ kĩ thuật viên để được giúp đỡ !";
                CreateViewBag();
                return(View("Index"));
            }
        }
Пример #2
0
        public ActionResult Autoimex(int id)
        {
            WarehouseInventoryMasterModel model = _context.WarehouseInventoryMasterModel
                                                  .Where(p => p.WarehouseInventoryMasterId == id && p.Actived == true && p.CreatedIEOther == false)
                                                  .FirstOrDefault();
            // Bước 1 : insert IEOtherMaster
            // Bước 2 : insert IEOtherDetail : dựa vào AmountDifference để nhập hay xuất kho

            var Resuilt = "";

            if (model == null)
            {
                Resuilt = "Không tìm thấy đơn hàng yêu cầu !";
            }
            else
            {
                try
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        #region insert IEOtherMaster
                        IEOtherRepository  IEOtherRepository  = new IEOtherRepository(_context);
                        IEOtherMasterModel IEOtherMasterModel = new IEOtherMasterModel()
                        {
                            IEOtherMasterCode = IEOtherRepository.GetIEOtherCodePKK(),
                            WarehouseId       = model.WarehouseId.Value,
                            InventoryTypeId   = EnumInventoryType.KK,
                            Note              = "Bù trừ kiểm kho khớp với hệ thống ",
                            CreatedDate       = DateTime.Now,
                            CreatedAccount    = currentAccount.UserName,
                            CreatedEmployeeId = currentEmployee.EmployeeId,
                            Actived           = true
                        };
                        _context.Entry(IEOtherMasterModel).State = System.Data.Entity.EntityState.Added;
                        _context.SaveChanges();
                        #endregion

                        #region InventoryMasterModel
                        int idStore = _context.WarehouseModel.Where(p => p.WarehouseId == model.WarehouseId).Select(p => p.StoreId.Value).FirstOrDefault();
                        InventoryMasterModel InvenMaster = new InventoryMasterModel()
                        {
                            StoreId           = idStore,
                            WarehouseModelId  = model.WarehouseId,
                            InventoryTypeId   = EnumInventoryType.KK,
                            InventoryCode     = IEOtherMasterModel.IEOtherMasterCode,
                            CreatedDate       = IEOtherMasterModel.CreatedDate,
                            CreatedAccount    = IEOtherMasterModel.CreatedAccount,
                            CreatedEmployeeId = IEOtherMasterModel.CreatedEmployeeId,
                            Actived           = true,
                            BusinessId        = IEOtherMasterModel.IEOtherMasterId, // Id nghiệp vụ
                            BusinessName      = "IEOtherMasterModel",               // Tên bảng nghiệp vụ
                            ActionUrl         = "/IEOtherMaster/Details/"           // Đường dẫn ( cộng ID cho truy xuất)
                        };
                        _context.Entry(InvenMaster).State = System.Data.Entity.EntityState.Added;
                        _context.SaveChanges(); // insert tạm để lấy InvenMasterID
                        #endregion

                        #region Duyệt WarehouseInventoryDetailModel insert vào InventoryDetailModel
                        var LstWarehouseInventoryDetailModel = _context.WarehouseInventoryDetailModel
                                                               .Where(p => p.WarehouseInventoryMasterId == model.WarehouseInventoryMasterId)
                                                               .OrderByDescending(p => p.WarehouseInventoryDetailId)
                                                               .ToList();
                        foreach (var item in LstWarehouseInventoryDetailModel)
                        {
                            if (item.AmountDifference != 0)
                            {
                                #region Kiểm tra tồn # database
                                var temp = (from detal in _context.InventoryDetailModel
                                            join master in _context.InventoryMasterModel on detal.InventoryMasterId equals master.InventoryMasterId
                                            orderby detal.InventoryDetailId descending
                                            where master.Actived == true && detal.ProductId == item.ProductId
                                            select new
                                {
                                    TonCuoi = detal.EndInventoryQty.Value
                                }).FirstOrDefault();
                                decimal TonTrongDatabase = temp != null ? temp.TonCuoi : 0;
                                if (item.Inventory != TonTrongDatabase)
                                {
                                    return(Json("Số lượng tồn không chính xác, vui lòng nhấn nút 'Xem' để cập nhật lại", JsonRequestBehavior.AllowGet));
                                }
                                #endregion

                                #region IEOtherDetailModel
                                IEOtherDetailModel detailmodel = new IEOtherDetailModel()
                                {
                                    IEOtherMasterId    = IEOtherMasterModel.IEOtherMasterId,
                                    ProductId          = item.ProductId,
                                    ImportQty          = item.AmountDifference > 0 ? item.AmountDifference : 0, // Nhập kho sp thừa , xuất kho sp thiếu
                                    ExportQty          = item.AmountDifference < 0 ? Math.Abs(item.AmountDifference.Value) : 0,
                                    Price              = 0,
                                    UnitShippingWeight = 0,
                                    UnitPrice          = 0
                                };
                                _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added;
                                _context.SaveChanges();
                                #endregion

                                #region Insert InventoryDetail

                                decimal tondau;
                                tondau = TonTrongDatabase;
                                if (item.AmountDifference > 0) // Nhập
                                {
                                    InventoryDetailModel InvenDetail = new InventoryDetailModel()
                                    {
                                        InventoryMasterId = InvenMaster.InventoryMasterId,
                                        ProductId         = item.ProductId,
                                        BeginInventoryQty = tondau,
                                        COGS            = 0, // nhập
                                        Price           = 0, // => Xuất
                                        ImportQty       = item.AmountDifference,
                                        ExportQty       = 0,
                                        UnitCOGS        = 0, // nhập
                                        UnitPrice       = 0, // => Xuất
                                        EndInventoryQty = tondau + item.AmountDifference
                                    };
                                    _context.Entry(InvenDetail).State = System.Data.Entity.EntityState.Added;
                                    _context.SaveChanges();
                                }
                                else
                                {
                                    InventoryDetailModel InvenDetail = new InventoryDetailModel()
                                    {
                                        InventoryMasterId = InvenMaster.InventoryMasterId,
                                        ProductId         = item.ProductId,
                                        BeginInventoryQty = tondau,
                                        COGS            = 0, // nhập
                                        Price           = 0, // => Xuất
                                        ImportQty       = 0,
                                        ExportQty       = Math.Abs(item.AmountDifference.Value),
                                        UnitCOGS        = 0, // nhập
                                        UnitPrice       = 0, // => Xuất
                                        EndInventoryQty = tondau - Math.Abs(item.AmountDifference.Value)
                                    };
                                    _context.Entry(InvenDetail).State = System.Data.Entity.EntityState.Added;
                                    _context.SaveChanges();
                                }
                                #endregion
                            }
                        }
                        #endregion

                        model.CreatedIEOther        = true;
                        _context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                        _context.SaveChanges();
                        ts.Complete();
                        Resuilt = "success";
                    }
                }
                catch
                {
                    Resuilt = "Xảy ra lỗi trong quá trình tạo xuất nhập kho!";
                }
            }
            return(Json(Resuilt, JsonRequestBehavior.AllowGet));
        }
Пример #3
0
        //#region GetUnitShippingWeight
        //public ActionResult GetUnitShippingWeight(int SelectedProductid)
        //{
        //    var UnitShippingWeight = _context
        //               .ProductModel
        //               .Where(p => p.ProductId == SelectedProductid)
        //               .Select(p => new
        //               {
        //                   Price = p.ImportPrice,
        //                   ShippingWeight = p.ShippingWeight,
        //                   ShippingFee = p.ShippingFee
        //               })
        //               .FirstOrDefault();
        //    return Json(UnitShippingWeight, JsonRequestBehavior.AllowGet);
        //}
        //#endregion
        #region Save

        public ActionResult Save(IEOtherMasterModel model, List <IEOtherDetailViewModel> detail)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    model.CreatedDate    = DateTime.Now;
                    model.CreatedAccount = currentAccount.UserName;
                    AccountModel Account = _context.AccountModel.Where(p => p.UserName == model.CreatedAccount).FirstOrDefault();
                    model.CreatedEmployeeId = Account.EmployeeId;
                    int CustomerId = Convert.ToInt32(model.CustomerName);
                    #region XÁc nhận mã IEOtherMasterCode 1 lần nữa trước khi insert
                    // Insert InventoryMaster
                    InventoryMasterModel InvenMaster = new InventoryMasterModel();
                    //Xác định Nhập hay xuất
                    var IsImport = _context.InventoryTypeModel.Where(p => p.InventoryTypeId == model.InventoryTypeId).FirstOrDefault();
                    IEOtherRepository IEOtherRepository = new IEOtherRepository(_context);
                    model.IEOtherMasterCode = IEOtherRepository.GetIEOtherCode(IsImport.isImport.Value);
                    #endregion
                    model.CustomerName          = _context.CustomerModel.Where(p => p.CustomerId == CustomerId).Select(p => p.FullName).FirstOrDefault();
                    _context.Entry(model).State = System.Data.Entity.EntityState.Added;
                    _context.SaveChanges(); // LƯU TẠM ĐỂ LẤY IMPORTMASTERID (SẼ BỊ SCROLLBACK KHI XẢY RA LỖI)
                    if (IsImport.isImport == true)
                    {
                        InvenMaster.InventoryTypeId = EnumInventoryType.NK;
                    }
                    else
                    {
                        InvenMaster.InventoryTypeId = EnumInventoryType.XK;
                    }
                    InvenMaster.WarehouseModelId      = model.WarehouseId;
                    InvenMaster.InventoryCode         = model.IEOtherMasterCode;
                    InvenMaster.CreatedDate           = model.CreatedDate;
                    InvenMaster.CreatedAccount        = model.CreatedAccount;
                    InvenMaster.CreatedEmployeeId     = model.CreatedEmployeeId;
                    InvenMaster.Actived               = true;
                    InvenMaster.BusinessId            = model.IEOtherMasterId;     // Id nghiệp vụ
                    InvenMaster.BusinessName          = "IEOtherMasterModel";      // Tên bảng nghiệp vụ
                    InvenMaster.ActionUrl             = "/IEOtherMaster/Details/"; // Đường dẫn ( cộng ID cho truy xuất)
                    _context.Entry(InvenMaster).State = System.Data.Entity.EntityState.Added;
                    _context.SaveChanges();                                        // insert tạm để lấy InvenMasterID
                    if (detail != null)
                    {
                        if (detail.GroupBy(p => p.ProductId).ToList().Count < detail.Count)
                        {
                            //khong duoc trung san pham
                            return(Content("Vui lòng không chọn thông tin sản phẩm trùng nhau"));
                        }
                        foreach (var item in detail)
                        {
                            IEOtherDetailModel detailmodel = new IEOtherDetailModel()
                            {
                                IEOtherMasterId = model.IEOtherMasterId,
                                ProductId       = item.ProductId,
                                //Qty = item.Qty,
                                ImportQty          = IsImport.isImport == true? item.Qty : 0,
                                ExportQty          = IsImport.isImport == false? item.Qty : 0,
                                Price              = item.Price,
                                UnitShippingWeight = item.UnitShippingWeight,
                                UnitPrice          = item.UnitPrice,
                                Note = item.Note
                            };
                            //_context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added;
                            _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added;

                            // Insert InventoryDetail
                            //var temp = _context.InventoryDetailModel.OrderByDescending(p => p.InventoryDetailId).Where(p => p.ProductId == item.ProductId).Select(p => p.EndInventoryQty).FirstOrDefault();
                            var temp = (from detal in _context.InventoryDetailModel
                                        join master in _context.InventoryMasterModel on detal.InventoryMasterId equals master.InventoryMasterId
                                        orderby detal.InventoryDetailId descending
                                        where master.Actived == true && detal.ProductId == item.ProductId
                                        select new
                            {
                                TonCuoi = detal.EndInventoryQty.Value
                            }).FirstOrDefault();
                            decimal tondau;
                            if (temp != null)
                            {
                                tondau = Convert.ToInt32(temp.TonCuoi);
                            }
                            else
                            {
                                tondau = 0;
                            }
                            var     tempt2 = _context.ProductModel.Where(p => p.ProductId == item.ProductId).FirstOrDefault();
                            decimal GiaVon = tempt2.COGS.Value;
                            if (IsImport.isImport == true)
                            {
                                InventoryDetailModel InvenDetail = new InventoryDetailModel()
                                {
                                    InventoryMasterId = InvenMaster.InventoryMasterId,
                                    ProductId         = item.ProductId,
                                    BeginInventoryQty = tondau,
                                    COGS = GiaVon,// nhập
                                    //Price = 0, // => Xuất
                                    ImportQty = item.Qty,
                                    //ExportQty = 0,
                                    UnitCOGS = GiaVon * item.Qty, // nhập
                                    //UnitPrice = 0, // => Xuất
                                    EndInventoryQty = tondau + item.Qty
                                };
                                _context.Entry(InvenDetail).State = System.Data.Entity.EntityState.Added;
                            }
                            else
                            {
                                InventoryDetailModel InvenDetail = new InventoryDetailModel()
                                {
                                    InventoryMasterId = InvenMaster.InventoryMasterId,
                                    ProductId         = item.ProductId,
                                    BeginInventoryQty = tondau,
                                    //COGS = 0,// nhập
                                    Price = item.Price, // => Xuất
                                    //ImportQty = 0,
                                    ExportQty = item.Qty,
                                    //UnitCOGS = 0, // nhập
                                    UnitPrice       = item.Price * item.Qty, // => Xuất
                                    EndInventoryQty = tondau - item.Qty
                                };
                                _context.Entry(InvenDetail).State = System.Data.Entity.EntityState.Added;
                            }
                        }
                        _context.SaveChanges();
                        // đánh dấu Transaction hoàn tất
                        ts.Complete();
                        return(Content("success"));
                    }
                    else
                    {
                        //chua nhap tt san pham
                        return(Content("Vui lòng chọn thông tin sản phẩm"));
                    }
                }
            }
            catch
            {
                return(Content("Xảy ra lỗi trong quá trình thêm mới nhà cung cấp"));
            }
        }