public void AddEarningAsync(decimal total)
        {
            using (var worker = new BackgroundWorker())
            {
                worker.DoWork += (object sender, DoWorkEventArgs e) =>
                {
                    using (var context = new EugenieEntities(connectionStringAsString))
                    {
                        var flag = 0;
                        var dailyEarning = context.DailyEarnings.FirstOrDefault(x => x.Date == DateTime.Today);

                        if (dailyEarning == null)
                        {
                            flag = 1;
                            dailyEarning = new DailyEarning {Date = DateTime.Today};
                        }

                        dailyEarning.Earnings += total;

                        if (flag == 1)
                        {
                            context.DailyEarnings.Add(dailyEarning);
                        }

                        context.SaveChanges();
                    }
                };

                worker.RunWorkerAsync();
            }
        }
        public void AddBarcodeAsync(Product originalProduct, Barcode barcode)
        {
            using (var worker = new BackgroundWorker())
            {
                worker.DoWork += (object sender, DoWorkEventArgs e) =>
                {
                    using (var context = new EugenieEntities(connectionStringAsString))
                    {
                        var product = context.Products.FirstOrDefault(x => x.ID == originalProduct.ID);

                        product.Barcodes.Add(barcode);
                        context.SaveChanges();
                    }
                };

                worker.RunWorkerAsync();
            }
        }
 public int GetProductsCount()
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         return context.Products.Count();
     }
 }
 public int GetDailyEarningsCount()
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         return context.DailyEarnings.Count();
     }
 }
 public IEnumerable<Product> GetLowQuantityProducts(double quantity)
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var products = context.Products.Where(x => x.Quantity <= quantity).OrderBy(x => x.Quantity).ToList();
         return products;
     }
 }
 public IEnumerable<Sell> GetAllSells(DateTime dateFrom, DateTime dateTo)
 {
     dateTo = dateTo.AddDays(1);
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var sells =
             context.Sells.Where(x => x.Date.CompareTo(dateFrom) >= 0 && x.Date.CompareTo(dateTo) <= 0).ToList();
         return sells;
     }
 }
 public IEnumerable<Waste> GetAllWaste(DateTime dateFrom, DateTime dateTo)
 {
     dateTo = dateTo.AddDays(1);
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var waste =
             context.Wastes.Where(x => x.Date.CompareTo(dateFrom) >= 0 && x.Date.CompareTo(dateTo) <= 0).ToList();
         return waste;
     }
 }
        public void UpdateProductAsync(Product originalProduct, double quantity = 0)
        {
            using (var worker = new BackgroundWorker())
            {
                worker.DoWork += (object sender, DoWorkEventArgs e) =>
                {
                    using (var context = new EugenieEntities(connectionStringAsString))
                    {
                        var product = context.Products.FirstOrDefault(x => x.ID == originalProduct.ID);

                        product.Name = originalProduct.Name;
                        product.BuyingPrice = originalProduct.BuyingPrice;
                        product.SellingPrice = originalProduct.SellingPrice;
                        product.Measure = originalProduct.Measure;
                        product.Quantity += quantity;

                        context.SaveChanges();
                    }
                };

                worker.RunWorkerAsync();
            }
        }
        public void AddWasteToSellerAsync(Seller originalSeller, Waste waste)
        {
            using (var worker = new BackgroundWorker())
            {
                worker.DoWork += (object sender, DoWorkEventArgs e) =>
                {
                    using (var context = new EugenieEntities(connectionStringAsString))
                    {
                        var seller = context.Sellers.Find(originalSeller.ID);
                        seller.Wastes.Add(waste);
                        context.SaveChanges();
                    }
                };

                worker.RunWorkerAsync();
            }
        }
 public IEnumerable<NotFoundProduct> GetAllNotFoundProducts()
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var result = context.NotFoundProducts.OrderByDescending(x => x.Date).ToList();
         return result;
     }
 }
 public HashSet<Product> GetAllProducts(int skip, int take)
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var products =
             new HashSet<Product>(
                 context.Products.Include("Barcodes")
                     .Include("ExpirationDates")
                     .OrderBy(x => x.ID)
                     .Skip(skip)
                     .Take(take)
                     .ToList());
         return products;
     }
 }
 public IEnumerable<Product> FindProductsByName(string name)
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var nameAsArray = name.ToLower().Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
         var result =
             context.Products.Where(x => nameAsArray.All(n => x.Name.ToLower().Contains(n)))
                 .OrderByDescending(x => nameAsArray.Any(n => x.Name.StartsWith(n)))
                 .ToList();
         return result;
     }
 }
 public IEnumerable<DailyEarning> GetAllDailyEarnings(int skip, int take)
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var dailyEarnings = context.DailyEarnings.OrderByDescending(x => x.ID).Skip(skip).Take(take).ToList();
         return dailyEarnings;
     }
 }
 public IEnumerable<Product> FindProductByID(int id)
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         return context.Products.Where(x => x.ID == id).ToList();
     }
 }
 public Product FindProductByBarcode(string barcode)
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var product =
             context.Products.Include("Barcodes").FirstOrDefault(x => x.Barcodes.Any(y => y.Barcode1 == barcode));
         return product;
     }
 }
        public void DeleteProductAsync(Product originalProduct)
        {
            using (var worker = new BackgroundWorker())
            {
                worker.DoWork += (s, e) =>
                {
                    using (var context = new EugenieEntities(connectionStringAsString))
                    {
                        var product = context.Products.FirstOrDefault(x => x.ID == originalProduct.ID);

                        var barcodes = product.Barcodes.ToList();
                        foreach (var barcode in barcodes)
                        {
                            context.Barcodes.Remove(barcode);
                        }

                        var shipments = product.Shipments.ToList();
                        foreach (var shipment in shipments)
                        {
                            context.Shipments.Remove(shipment);
                        }

                        var expirationDates = product.ExpirationDates.ToList();
                        foreach (var date in expirationDates)
                        {
                            context.ExpirationDates.Remove(date);
                        }

                        context.Products.Remove(product);
                        context.SaveChanges();
                    }
                };

                worker.RunWorkerAsync();
            }
        }
 public IEnumerable<Shipment> GetShipments(DateTime dateFrom, DateTime dateTo)
 {
     dateTo = dateTo.AddDays(1);
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var shipments =
             context.Shipments.Include("Product")
                 .Where(x => x.Date.CompareTo(dateFrom) >= 0 && x.Date.CompareTo(dateTo) <= 0)
                 .ToList();
         return shipments;
     }
 }
        public void AddProductAsync(Product originalProduct)
        {
            using (var worker = new BackgroundWorker())
            {
                worker.DoWork += (object sender, DoWorkEventArgs e) =>
                {
                    using (var context = new EugenieEntities(connectionStringAsString))
                    {
                        context.Products.Add(originalProduct);
                        context.SaveChanges();
                    }
                };

                worker.RunWorkerAsync();
            }
        }
 public IEnumerable<Waste> GetWasteForSeller(Seller seller, DateTime dateFrom, DateTime dateTo)
 {
     dateTo = dateTo.AddDays(1);
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var wastes =
             context.Wastes.Where(
                 x => x.SellerID == seller.ID && x.Date.CompareTo(dateFrom) >= 0 && x.Date.CompareTo(dateTo) <= 0)
                 .ToList();
         return wastes;
     }
 }
 public IEnumerable<Seller> GetAllSellers()
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         var sellers = context.Sellers.ToList();
         return sellers;
     }
 }
 public void AddNotFoundProduct(NotFoundProduct product)
 {
     using (var context = new EugenieEntities(connectionStringAsString))
     {
         context.NotFoundProducts.Add(product);
         context.SaveChanges();
     }
 }
        public void AddShipmentAsync(Product product, double quantity)
        {
            using (var worker = new BackgroundWorker())
            {
                worker.DoWork += (object sender, DoWorkEventArgs e) =>
                {
                    using (var context = new EugenieEntities(connectionStringAsString))
                    {
                        var shipment = new Shipment();
                        shipment.Quantity = quantity;
                        shipment.Date = DateTime.Today;
                        shipment.ProductID = product.ID;

                        var existingShipment =
                            context.Shipments.FirstOrDefault(
                                x => x.ProductID == shipment.ProductID && x.Date.CompareTo(DateTime.Today) == 0);
                        if (existingShipment == null)
                        {
                            context.Shipments.Add(shipment);
                        }
                        else
                        {
                            existingShipment.Quantity += shipment.Quantity;
                        }

                        context.SaveChanges();
                    }
                };

                worker.RunWorkerAsync();
            }
        }