public void RemoveProduct(int productId)
        {
            FileStream locker = _FileLocker.LockObj(_productAndStockLockerFile);

            var product = FindProductInner(productId);

            try
            {
                if (product != null)
                {
                    GetStocks();

                    var stock = Stocks.FirstOrDefault(s => s.ProductId == productId);

                    if (stock != null)
                    {
                        throw new InvalidOperationException("Cannot remove product which is still in stock.");
                    }
                    if (!Products.ProductList.Remove(product))
                    {
                        throw new InvalidOperationException("Unknow problem.");
                    }
                }
                else
                {
                    throw new IndexOutOfRangeException($"Product {productId} does not exist.");
                }

                _jsonHandler.SaveRecords(_productFile, Products);
            }
            finally
            {
                _FileLocker.UnlockObj(locker);
            }
        }
        private void OnProjectChanged()
        {
            if (myProjectHost.Project == null)
            {
                return;
            }

            var ctx = myProjectHost.Project.GetAssetsContext();

            Stocks.AddRange(ctx.Stocks);
            SelectedStock = Stocks.FirstOrDefault();
        }
        public int GetStock(int productId)
        {
            FileStream locker = _FileLocker.LockObj(_productAndStockLockerFile);

            GetStocks();

            var stock = Stocks.FirstOrDefault(s => s.ProductId == productId);

            _FileLocker.UnlockObj(locker);

            return(stock?.Count ?? -1);
        }
        private void BalanceOnPropertyChanged(object sender, PropertyChangedEventArgs args)
        {
            var balance = sender as Model.Balance;

            if (args.PropertyName != nameof(Model.Balance.Value) || balance == null)
            {
                return;
            }

            var stock = Stocks.FirstOrDefault(x => x.Balance.Equals(balance));

            _correctionsCreator.CreateCorrection(stock, stock.Balance.Value - stock.Balance.PreviousValue, Strings.ManualStockUpdate);
        }
        public bool Sale(long id, int vol)
        {
            var stockToChange = Stocks.FirstOrDefault(r => r.Id == id);

            if (stockToChange != null)
            {
                stockToChange.stock.Volume = stockToChange.stock.Volume + vol;
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public bool ChangeStockType(long id, string typeprod)
        {
            var stockToChange = Stocks.FirstOrDefault(r => r.Id == id);

            if (stockToChange != null)
            {
                stockToChange.Typeofproduct = typeprod;
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public bool DeleteStock(long id)
        {
            var stockToRemove = Stocks.FirstOrDefault(r => r.Id == id);

            if (stockToRemove != null)
            {
                Stocks.Remove(stockToRemove);
                return(true);
            }

            else
            {
                return(false);
            }
        }
        public Stock PostStock(Stock stock)
        {
            var item = Stocks.FirstOrDefault(c => c.StockCode == stock.StockCode);

            if (item != null)
            {
                item = stock;
                return(item);
            }
            else
            {
                Stocks.Add(stock);
                return(stock);
            }
        }
        public void DecreaseStock(int productId, int count)
        {
            if (count <= 0)
            {
                throw new ArgumentOutOfRangeException($"Cannot decrease {count} product from stock.");
            }

            FileStream locker = _FileLocker.LockObj(_productAndStockLockerFile);

            var product = FindProductInner(productId);

            try
            {
                if (product != null)
                {
                    GetStocks();

                    var stock = Stocks.FirstOrDefault(s => s.ProductId == productId && s.Count >= count);

                    if (stock != null)
                    {
                        stock.Count -= count;
                        if (stock.Count == 0)
                        {
                            Stocks.Remove(stock);
                        }
                        _jsonHandler.SaveRecords(_stockFile, Stocks);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Product {productId} is out of stock or not enough.");
                    }
                }
                else
                {
                    throw new IndexOutOfRangeException($"Product {productId} does not exist.");
                }
            }
            finally
            {
                _FileLocker.UnlockObj(locker);
            }
        }
        public void IncreaseStock(int productId, int count)
        {
            if (count <= 0)
            {
                throw new ArgumentOutOfRangeException($"Cannot increase {count} product to stock.");
            }
            FileStream locker = _FileLocker.LockObj(_productAndStockLockerFile);

            var product = FindProductInner(productId);

            try
            {
                if (product != null)
                {
                    GetStocks();

                    var stock = Stocks.FirstOrDefault(s => s.ProductId == productId);

                    if (stock != null)
                    {
                        stock.Count += count;
                    }
                    else
                    {
                        Stocks.Add(new ProductItem()
                        {
                            ProductId = productId, Count = count
                        });
                    }

                    _jsonHandler.SaveRecords(_stockFile, Stocks);
                }
                else
                {
                    throw new IndexOutOfRangeException($"Product {productId} does not exist.");
                }
            }
            finally
            {
                _FileLocker.UnlockObj(locker);
            }
        }
        public string ListProducts()
        {
            FileStream locker = _FileLocker.LockObj(_productAndStockLockerFile);

            GetProducts();
            GetStocks();

            List <string> results = new List <string>();

            foreach (Product p in Products.ProductList)
            {
                var stock = Stocks.FirstOrDefault(s => s.ProductId == p.Id);
                results.Add(p.ToString() + $" Stock = {stock?.Count ?? 0}");
            }

            _FileLocker.UnlockObj(locker);

            return(results.Count > 0
                ? string.Join(Environment.NewLine, results)
                : "");
        }
Exemple #12
0
 // Get specific stock from stocks list in memory
 public static async Task <Stock> GetStockAsync(string symbol)
 {
     return(await Task.FromResult(Stocks.FirstOrDefault(s => s.Symbol == symbol)));
 }
        public Receipt Checkout(List <ProductItem> items)
        {
            if (items == null || items.Count == 0)
            {
                throw new InvalidOperationException("There is nothing in the shopping cart.");
            }

            FileStream locker        = _FileLocker.LockObj(_productAndStockLockerFile);
            FileStream lockerReceipt = null;

            try
            {
                GetProducts();
                GetStocks();

                if (items.Exists(
                        i => Products.ProductList.FirstOrDefault(
                            p => p.Id == i.ProductId) == null))
                {
                    throw new InvalidOperationException($"Some products do not exist.");
                }

                if (items.Exists(
                        i => Stocks.FirstOrDefault(
                            s => s.ProductId == i.ProductId && s.Count >= i.Count) == null))
                {
                    throw new InvalidOperationException($"Some products are out of stock or not enough.");
                }

                Receipt receipt = new Receipt()
                {
                    ShoppingItems   = new List <ShoppingItem>(),
                    TransactionTime = DateTimeOffset.Now
                };

                foreach (var item in items)
                {
                    int productId = item.ProductId;
                    int count     = item.Count;
                    var stock     = Stocks.FirstOrDefault(s => s.ProductId == productId && s.Count >= count);
                    stock.Count -= count;
                    if (stock.Count == 0)
                    {
                        Stocks.Remove(stock);
                    }

                    Product      product      = FindProductInner(productId);
                    ShoppingItem shoppingItem = new ShoppingItem()
                    {
                        ProductId   = item.ProductId,
                        ProductName = product.Name,
                        Price       = product.Price,
                        Count       = count
                    };
                    receipt.ShoppingItems.Add(shoppingItem);
                }

                lockerReceipt = _FileLocker.LockObj(_receiptLockerFile);

                GetReceipts();
                receipt.Id = ++Receipts.ReceiptCurrentId;
                Receipts.ReceiptList.Add(receipt);

                _jsonHandler.SaveRecords(_receiptFile, Receipts);
                _jsonHandler.SaveRecords(_stockFile, Stocks);

                return(receipt);
            }
            finally
            {
                _FileLocker.UnlockObj(locker);
                if (lockerReceipt != null)
                {
                    _FileLocker.UnlockObj(lockerReceipt);
                }
            }
        }