private void txtBarcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Barcode = txtBarcode.Text.ToString();

                Product product = new Product();
                product = ProductService.GetProductByBarcode(Barcode);

                if (product == null)
                {
                    MessageBox.Show("Your product is not valid.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    txtBarcode.Focus();
                    txtBarcode.SelectionStart = 0;
                    txtBarcode.SelectionLength = txtBarcode.Text.Length;
                }
                else
                {
                    txtProduct.Text = product.Name;
                    spnCurQty.EditValue = product.Quantity;

                    e.Handled = false;

                    spnQty.Focus();
                    spnQty.SelectionStart = 0;
                    spnQty.SelectionLength = spnQty.Text.Length;
                }
            }
        }
        public void SaveProduct(Product product)
        {
            Product existingData = GetProductByID(product.ProductID);

            if (existingData == null)
            {
                product.CreatedOn = DateTime.Now;
                product.UpdatedOn = DateTime.Now;
                _context.Products.AddObject(product);
            }
            else
            {
                existingData.Name = product.Name;
                existingData.Description = product.Description;
                existingData.IsActive = product.IsActive;
                existingData.Deleted = product.Deleted;
                existingData.CreatedOn = existingData.CreatedOn;
                existingData.UpdatedOn = DateTime.Now;
                existingData.UpdatedMethod = product.UpdatedMethod;
                existingData.ProductUnitID = product.ProductUnitID;

                if (!_context.IsAttached(existingData))
                    _context.Products.Attach(existingData);
            }
            _context.SaveChanges();
        }
        private void spnQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (spnQty.Text.Length > 4)
                {
                    //lueBarcode.Text = spnQty.Text;
                    //lueBarcode.SelectionStart = 4;

                    string barcode = spnQty.Text;
                    spnQty.EditValue = 0;

                    barcode = barcode.Replace(",", "");

                    txtBarcode.Text = barcode;
                    //txtBarcode.SelectionStart = 4;
                    //txtBarcode.Focus();

                    Barcode = txtBarcode.Text.ToString();

                    Product product = new Product();
                    product = ProductService.GetProductByBarcode(Barcode);

                    if (product == null)
                    {
                        MessageBox.Show("Your product is not valid.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        txtBarcode.Focus();
                        txtBarcode.SelectionStart = 0;
                        txtBarcode.SelectionLength = txtBarcode.Text.Length;
                    }
                    else
                    {
                        txtProduct.Text = product.Name;
                        spnCurQty.EditValue = product.Quantity;

                        spnQty.Focus();
                        spnQty.SelectionStart = 0;
                        spnQty.SelectionLength = spnQty.Text.Length;
                    }
                }
                else
                {
                    AddQty();
                }
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Products EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToProducts(Product product)
 {
     base.AddObject("Products", product);
 }
 /// <summary>
 /// Create a new Product object.
 /// </summary>
 /// <param name="productID">Initial value of the ProductID property.</param>
 public static Product CreateProduct(global::System.Int32 productID)
 {
     Product product = new Product();
     product.ProductID = productID;
     return product;
 }
        /// <summary>
        /// Import all the products from CSV 
        /// </summary>
        /// <param name="srProductInCSV">The stream reader value of product CSV</param>
        /// <returns>Product</returns>
        public int ImportCSVProduct(StreamReader srProductInCSV)
        {
            List<Product> existingproducts = GetAllProducts();
            List<Manufacturer> manufacturers = GetAllManufacturers();
            int recordAdded = 0; int totalQuantities = 0;
            List<Product> products = new List<Product>();
            List<ProductInCSV> missingProducts = new List<ProductInCSV>();

            CsvFileDescription inputFileDescription = new CsvFileDescription
            {
                SeparatorChar = ',',
                FirstLineHasColumnNames = true
            };

            CsvContext cc = new CsvContext();

            IEnumerable<ProductInCSV> rawCSVProducts = cc.Read<ProductInCSV>(srProductInCSV, inputFileDescription);

            var csvProducts = (from p in rawCSVProducts
                                   select p).ToList();

            foreach (var csvProduct in csvProducts)
            {
                Product existingproduct = new Product();

                if(	existingproducts != null && existingproducts.Count > 0)
                    existingproduct = existingproducts.FirstOrDefault(c => c.Barcode == csvProduct.BarCode);

                if (existingproduct == null || existingproduct.ProductID == 0)
                {
                    if (csvProduct.BarCode != null && csvProduct.Origin != null)
                    {
                        Product product = new Product();
                        product.Name = csvProduct.ProductFullName;
                        product.Barcode = csvProduct.BarCode;
                        product.ManufacturerID = manufacturers.FirstOrDefault(c => c.Name == csvProduct.Origin.Trim().ToLowerInvariant()).ManufacturerID;

                        int quantities = 0;
                        int.TryParse(csvProduct.Quantities, out quantities);

                        if (quantities > 10)
                        {
                            product.Quantity = 10;
                            product.RemainingQuantity = quantities - 10;
                        }
                        else
                        {
                            product.Quantity = quantities;
                            product.RemainingQuantity = 0;
                        }

                        products.Add(product);

                        totalQuantities = totalQuantities + quantities;

                        recordAdded++;
                    }
                    else
                    {
                        missingProducts.Add(csvProduct);
                    }
                }
            }
            BulkInsertProducts(products);
            return recordAdded;
        }
        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (txtProduct.Text.Length > 0)
            {
                Product product = new Product();

                //for Edit Data
                if (ProductID.Length > 0)
                {
                    product.ProductID = int.Parse(ProductID);
                    product.CreatedOn = Convert.ToDateTime(txtProduct.Tag.ToString());
                }
                product.Name = txtProduct.Text.ToString();
                product.Description = txtProduct.Text.ToString();
                //product.ProductUnitID = int.Parse(lueProductUnit.EditValue.ToString());
                product.CategoryID = int.Parse(lueCategory.EditValue.ToString());
                product.IsActive = Convert.ToBoolean(chkIsActive.EditValue);
                product.UpdatedOn = DateTime.Now;

                //for New Data
                if (ProductID.Length < 0)
                {
                    product.CreatedOn = DateTime.Now;
                }

                ProductService.SaveProduct(product);

                ProductID = product.ProductID.ToString();

                grdProduct.DataSource = ProductService.GetAllProducts(0, 16);

                LoadVarient();

                ClearForm();
            }
            else
            {
                MessageBox.Show("Please, fill the product name up!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtProduct.Focus();
            }
        }
        /// <summary>
        /// Add Product to cache 
        /// </summary>
        /// <param name="product">The Product Object</param>
        private void AddProductToCache(Product product)
        {
            if (CacheEnabled && product != null)
            {
                List<Product> products = new List<Product>();

                if (CacheProducts != null)
                    if (CacheProducts.Count > 0)
                        products = CacheProducts;

                Product cacheProduct = new Product();
                if (CacheProducts != null)
                    cacheProduct = CacheProducts.FirstOrDefault(c => c.ProductID == product.ProductID);

                if (CacheProducts != null && CacheProducts.Count > 0)
                {
                    products.Add(product);
                    CacheProducts = products;
                }
            }
        }
        /// <summary>
        /// Save Products use for both Insert and Update 
        /// </summary>
        /// <param name="Product">The product.</param>
        public void SaveProduct(Product product)
        {
            Product existingData = GetProductByID(product.ProductID);

            RemoveProductFromTheCache(existingData);

            if (existingData == null)
            {
                product.CreatedOn = DateTime.Now;
                product.UpdatedOn = DateTime.Now;
                _context.Products.AddObject(product);
            }
            else
            {
                existingData.Name = product.Name;
                existingData.Description = product.Description;
                existingData.IsActive = product.IsActive;
                existingData.CreatedOn = existingData.CreatedOn;
                existingData.UpdatedOn = DateTime.Now;

                if (!_context.IsAttached(existingData))
                    _context.Products.Attach(existingData);
            }
            _context.SaveChanges();

            AddProductToCache(existingData);
        }
        /// <summary>
        /// Get Product By ID. Firstly look from the project cache and get from the database 
        /// </summary>
        /// <param name="productID">The product identifier</param>
        /// <returns>Product</returns>
        public Product GetProductByID(int productID)
        {
            if (productID == 0)
                return null;

            Product product = new Product();
            if (CacheProducts != null)
                product = CacheProducts.FirstOrDefault(c => c.ProductID == productID);

            if (product == null || product.ProductID == 0)
            {
                product = _context.Products.FirstOrDefault(c => c.ProductID == productID);
                AddProductToCache(product);
                return product;
            }
            return product;
        }
        /// <summary>
        /// Remove Specific Product  from the cache 
        /// </summary>
        /// <param name="product">The Product  Object</param>
        private void RemoveProductFromTheCache(Product product)
        {
            if (CacheEnabled && product != null)
            {
                Product cacheProduct = new Product();
                if (CacheProducts != null)
                    cacheProduct = CacheProducts.FirstOrDefault(c => c.ProductID == product.ProductID);

                if (cacheProduct != null)
                {
                    List<Product> products = new List<Product>();

                    if (CacheProducts != null && CacheProducts.Count > 0)
                        products = CacheProducts;

                    products.Remove(cacheProduct);
                    CacheProducts = products;
                }
            }
        }
        private void FixupnpProduct(Product previousValue, bool skipKeys = false)
        {
            if (IsDeserializing)
            {
                return;
            }

            if (previousValue != null && previousValue.npProductVariants.Contains(this))
            {
                previousValue.npProductVariants.Remove(this);
            }

            if (npProduct != null)
            {
                if (!npProduct.npProductVariants.Contains(this))
                {
                    npProduct.npProductVariants.Add(this);
                }

                ProductID = npProduct.ProductID;
            }
            else if (!skipKeys)
            {
                ProductID = null;
            }

            if (ChangeTracker.ChangeTrackingEnabled)
            {
                if (ChangeTracker.OriginalValues.ContainsKey("npProduct")
                    && (ChangeTracker.OriginalValues["npProduct"] == npProduct))
                {
                    ChangeTracker.OriginalValues.Remove("npProduct");
                }
                else
                {
                    ChangeTracker.RecordOriginalValue("npProduct", previousValue);
                }
                if (npProduct != null && !npProduct.ChangeTracker.ChangeTrackingEnabled)
                {
                    npProduct.StartTracking();
                }
            }
        }