Ejemplo n.º 1
0
        public async Task <IHttpActionResult> PostSupplyItems(List <SupplyItemProductViewModel> supplyItems,
                                                              Guid supplyGuid)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var stockController = new StocksController();

            //create the Supplyitems and link them to the Supply
            foreach (var p in supplyItems)
            {
                var supplyItem = new SupplyItem
                {
                    SupplyItemGuid      = Guid.NewGuid(),
                    SupplyItemSupply    = _db.Supplys.FirstOrDefault(g => g.SupplyGuid == supplyGuid),
                    SupplyItemStockItem =
                        _db.StockItems.FirstOrDefault(g => g.StockItemProduct.ProductGuid == p.ProductGuid),
                    SupplyQuantity = p.SupplyQuantity
                };

                _db.SupplyItems.Add(supplyItem);

                try
                {
                    await _db.SaveChangesAsync();
                }
                catch (DbUpdateException)
                {
                }

                await stockController.CreateStockTransaction(new StockItemViewModel
                {
                    StockItemGuid     = supplyItem.SupplyItemStockItem.StockItemGuid,
                    StockItemQuantity = supplyItem.SupplyQuantity,
                    Supply            = supplyItem.SupplyItemSupply,
                    Order             = null
                });
            }

            return(CreatedAtRoute("DefaultApi", new { id = supplyItems }, supplyItems));
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> PostOrderItems(List <OrderItemProductViewModel> orderItems, Guid orderGuid)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var stockController = new StocksController();

            //create the orderitems and link them to the order
            foreach (var p in orderItems)
            {
                var orderItem = new OrderItem
                {
                    OrderItemGuid      = Guid.NewGuid(),
                    OrderItemOrder     = db.Orders.FirstOrDefault(g => g.OrderGuid == orderGuid),
                    OrderItemStockItem =
                        db.StockItems.FirstOrDefault(g => g.StockItemProduct.ProductGuid == p.ProductGuid),
                    OrderQuantity = p.OrderQuantity
                };

                db.OrderItems.Add(orderItem);

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateException)
                {
                }

                await stockController.CreateStockTransaction(new StockItemViewModel
                {
                    StockItemGuid     = orderItem.OrderItemStockItem.StockItemGuid,
                    StockItemQuantity = -orderItem.OrderQuantity,
                    Order             = orderItem.OrderItemOrder,
                    Supply            = null
                });
            }

            return(CreatedAtRoute("DefaultApi", new { id = orderItems }, orderItems));
        }