示例#1
0
        public InventoryItemStockTransactionBuilder AddInventoryItemStock(long inventoryItemId, string unitOfMeasureCode, decimal unitCost, int quantity)
        {
            if (m_inventoryItemStockTransaction == null)
            {
                throw new InvalidOperationException("Begin has not been called.");
            }

            //if (m_order != null) throw new InvalidOperationException("Cannot add stock when processing order.");

            var dbInventoryItemStock = new InventoryItemStock()
            {
                InventoryItemId   = inventoryItemId,
                UnitOfMeasureCode = unitOfMeasureCode,
                UnitCost          = unitCost,
                StockDateTimeUtc  = m_utcNow,
                OriginalQuantity  = quantity,
                CurrentQuantity   = quantity,
            };

            _ = m_ctx.InventoryItemStocks.Add(dbInventoryItemStock);

            var dbInventoryItemStockTransactionItem = new InventoryItemStockTransactionItem()
            {
                InventoryItemStock            = dbInventoryItemStock,
                InventoryItemStockTransaction = m_inventoryItemStockTransaction,
                Quantity = quantity,
                Cost     = unitCost * quantity,
            };

            _ = m_ctx.InventoryItemStockTransactionItems.Add(dbInventoryItemStockTransactionItem);

            dbInventoryItemStockTransactionItem.InventoryItemStock.InventoryItem.Quantity += quantity;

            return(this);
        }
示例#2
0
        //public InventoryItemStockTransactionBuilder Begin(long orderId)
        //{
        //    if (m_inventoryItemStockTransaction != null) throw new InvalidOperationException("Begin has already been called.");

        //    m_inventoryItemStockTransaction = new InventoryItemStockTransaction()
        //    {
        //        TransactionDateTimeUtc = m_utcNow,
        //        TotalCost = 0 // Updated by Create.
        //    };
        //    _ = m_ctx.InventoryItemStockTransactions.Add(m_inventoryItemStockTransaction);

        //    m_order = m_ctx.Orders.Where(r => r.OrderId == orderId).Single();
        //    m_inventoryItemStockTransactionItems = new List<InventoryItemStockTransactionItem>();

        //    return this;
        //}

        public InventoryItemStockTransactionBuilder ConsumeInventoryItemStock(long inventoryItemId, string unitOfMeasureCode, int quantity)
        {
            if (m_inventoryItemStockTransaction == null)
            {
                throw new InvalidOperationException("Begin has not been called.");
            }

            //if (m_order == null) throw new InvalidOperationException("Cannot consume stock wihtout order.");

            while (quantity > 0)
            {
                var dbInventoryItemStockTransactionItem = m_inventoryItemStockTransactionItems.Where(r => r.InventoryItemStock.InventoryItemId == inventoryItemId).SingleOrDefault();
                if (dbInventoryItemStockTransactionItem == null)
                {
                    var dbInventoryItemStock = m_ctx.InventoryItemStocks.Where(r => r.InventoryItemId == inventoryItemId && r.CurrentQuantity > 0).OrderBy(r => r.StockDateTimeUtc).First();

                    dbInventoryItemStockTransactionItem = new InventoryItemStockTransactionItem()
                    {
                        InventoryItemStock            = dbInventoryItemStock,
                        InventoryItemStockTransaction = m_inventoryItemStockTransaction,
                        Quantity = 0,
                        Cost     = 0m,
                    };
                    _ = m_ctx.InventoryItemStockTransactionItems.Add(dbInventoryItemStockTransactionItem);
                }

                if (dbInventoryItemStockTransactionItem.InventoryItemStock.UnitOfMeasureCode != unitOfMeasureCode)
                {
                    throw new ArgumentException("Unit of measure mismatch.");
                }

                var transactionQuantity = Math.Min(quantity, dbInventoryItemStockTransactionItem.InventoryItemStock.CurrentQuantity);
                Debug.Assert(transactionQuantity > 0);

                dbInventoryItemStockTransactionItem.Quantity -= transactionQuantity;
                dbInventoryItemStockTransactionItem.InventoryItemStock.CurrentQuantity        -= transactionQuantity;
                dbInventoryItemStockTransactionItem.InventoryItemStock.InventoryItem.Quantity -= transactionQuantity;

                dbInventoryItemStockTransactionItem.Cost = dbInventoryItemStockTransactionItem.Quantity * dbInventoryItemStockTransactionItem.InventoryItemStock.UnitCost;

                if (dbInventoryItemStockTransactionItem.InventoryItemStock.CurrentQuantity == 0)
                {
                    _ = m_inventoryItemStockTransactionItems.Remove(dbInventoryItemStockTransactionItem);
                }

                quantity -= transactionQuantity;
                Debug.Assert(quantity >= 0);
            }

            return(this);
        }