private void _createCheckWarehouse(CheckWarehouse data) { using (var con = new inventorymanagementEntities()) { con.CheckWarehouses.Add(data); con.SaveChanges(); } }
protected void btnImport_Click(object sender, EventArgs e) { DateTime currentDate = DateTime.Now; string username = Request.Cookies["usernameLoginSystem"].Value; var acc = AccountController.GetByUsername(username); if (acc == null) { return; } var now = DateTime.Now; var checkWarehouse = new CheckWarehouse() { Name = String.Format("{0} - {1:dd/MM/yyyy HH:mm}", hdfTestName.Value.Trim(), now), Stock = hdfStock.Value.ToInt(), Active = true, CreatedDate = now, CreatedBy = acc.Username, ModifiedDate = now, ModifiedBy = acc.Username }; _createCheckWarehouse(checkWarehouse); var data = JsonConvert.DeserializeObject <List <ProductGetOut> >(hdfvalue.Value); var details = new List <CheckWarehouseDetail>(); #region Xử lý với sản phẩm không có biến thể var products = data.Where(x => x.ProductStyle == 1).ToList(); foreach (var item in products) { var temp = new CheckWarehouseDetail() { CheckWarehouseID = checkWarehouse.ID, ProductID = item.ID, ProductVariableID = 0, ProductSKU = item.SKU, QuantityOld = Convert.ToInt32(item.WarehouseQuantity), CreatedDate = now, CreatedBy = acc.Username, ModifiedDate = now, ModifiedBy = acc.Username }; details.Add(temp); } if (details.Count > 0) { _createCheckWarehouseDetail(details); details.Clear(); } #endregion #region Xử lý với sản phẩm có biến thể IList <ProductVariation> variations; #region Lấy thông tin stock using (var con = new inventorymanagementEntities()) { var productIDs = data.Except(products).Select(x => x.ID).Distinct().ToList(); var productVariations = con.tbl_ProductVariable .Where(x => x.ProductID.HasValue) .Where(x => productIDs.Contains(x.ProductID.Value)) .Select(x => new { productID = x.ProductID.Value, productVariableID = x.ID, sku = x.SKU }) .OrderBy(o => o.productID) .ThenBy(o => o.productVariableID); // Lọc ra những dòng stock cần lấy var stockFilter = con.tbl_StockManager .Join( productVariations, s => s.SKU, v => v.sku, (s, v) => s ) .Select(x => new { ParentID = x.ParentID, ProductVariableID = x.ProductVariableID, SKU = x.SKU, CreatedDate = x.CreatedDate, Quantity = x.Quantity, QuantityCurrent = x.QuantityCurrent, Type = x.Type }) .OrderBy(o => o.ParentID) .ThenBy(o => o.ProductVariableID) .ThenBy(o => o.CreatedDate); // Lấy dòng stock cuối cùng var stockLast = stockFilter .Select(x => new { parentID = x.ParentID.Value, productVariableID = x.ProductVariableID.Value, createDate = x.CreatedDate }) .GroupBy(x => new { x.parentID, x.productVariableID }) .Select(g => new { parentID = g.Key.parentID, productVariableID = g.Key.productVariableID, doneAt = g.Max(x => x.createDate) }); // Thông tin stock var stocks = stockFilter .Join( stockLast, rec => new { parentID = rec.ParentID.Value, productVariableID = rec.ProductVariableID.Value, doneAt = rec.CreatedDate }, last => new { parentID = last.parentID, productVariableID = last.productVariableID, doneAt = last.doneAt }, (rec, last) => new { parentID = last.parentID, productVariableID = last.productVariableID, sku = rec.SKU, quantity = rec.Quantity.HasValue ? rec.Quantity.Value : 0, quantityCurrent = rec.QuantityCurrent.HasValue ? rec.QuantityCurrent.Value : 0, type = rec.Type.HasValue ? rec.Type.Value : 0 } ) .Select(x => new { productID = x.parentID, productVariableID = x.productVariableID, sku = x.sku, calQuantity = x.quantityCurrent + x.quantity * (x.type == 1 ? 1 : -1) }) .OrderBy(o => o.productID) .ThenBy(o => o.productVariableID); variations = productVariations .GroupJoin( stocks, v => v.sku, s => s.sku, (v, s) => new { variation = v, stock = s } ) .SelectMany( x => x.stock.DefaultIfEmpty(), (parent, child) => new { variation = parent.variation, stock = child } ) .Select(x => new ProductVariation() { productID = x.variation.productID, productVariableID = x.variation.productVariableID, sku = x.variation.sku, calQuantity = x.stock != null ? x.stock.calQuantity : 0D }) .ToList(); } #endregion foreach (var item in variations) { var temp = new CheckWarehouseDetail() { CheckWarehouseID = checkWarehouse.ID, ProductID = item.productID, ProductVariableID = item.productVariableID, ProductSKU = item.sku, QuantityOld = Convert.ToInt32(item.calQuantity), CreatedDate = now, CreatedBy = acc.Username, ModifiedDate = now, ModifiedBy = acc.Username }; details.Add(temp); } if (details.Count > 0) { _createCheckWarehouseDetail(details); details.Clear(); } #endregion PJUtils.ShowMessageBoxSwAlert("Tạo phiên kiểm kho thành công!", "s", true, Page); }