Пример #1
0
        /// <summary>
        ///     Adds a new purchase
        /// </summary>
        /// <param name="purchase"></param>
        public void AddPurchase(Purchase purchase)
        {
            if (purchase.SupplierId == null)
            {
                throw new ArgumentNullException(nameof(purchase.SupplierId), "Purchase SupplierId is null");
            }
            using (var scope = new TransactionScope())
            {
                using (var connection = Connector.GetConnection())
                {
                    var purchaseDal = new PurchaseDal(connection);
                    var itemDal     = new ItemDal(connection);
                    purchaseDal.Insert(purchase.SupplierId.Value, purchase.Amount, purchase.Note, purchase.IsSettled);
                    purchase.Id = purchaseDal.GetLastInsertId();

                    if (purchase.Id == null)
                    {
                        throw new ArgumentNullException(nameof(purchase.Id),
                                                        "Purchase Id null after insert");
                    }

                    var purchaseItemDal = new PurchaseItemDal(connection);

                    // Insert purchase items
                    purchaseItemDal.InsertMultiple(purchase.Id.Value, purchase.PurchaseItems);

                    // Update inventory
                    foreach (var purchaseItem in purchase.PurchaseItems)
                    {
                        itemDal.Update(purchaseItem.ItemId, stockIncrement: (int)purchaseItem.Qty);
                    }
                }
                scope.Complete();
            }
        }
Пример #2
0
        /// <summary>
        ///     Adds a new Order
        /// </summary>
        public void AddOrder(Order order)
        {
            // Exception handling
            if (order.OrderItems == null)
            {
                throw new ArgumentNullException(nameof(order.OrderItems),
                                                "Attempted inserting order without orderEntries initialized");
            }
            if (order.IsCancelled)
            {
                throw new ArgumentException("Attempted inserting a cancelled order", nameof(order.IsCancelled));
            }

            using (var scope = new TransactionScope())
            {
                using (var connection = Connector.GetConnection())
                {
                    var orderDal     = new OrderDal(connection);
                    var orderItemDal = new OrderItemDal(connection);
                    var itemDal      = new ItemDal(connection);

                    // Insert order record
                    orderDal.Insert(order.CustomerId, order.Amount, order.Note);
                    order.Id = orderDal.GetLastInsertId();
                    if (order.Id == null)
                    {
                        throw new ArgumentNullException(nameof(order.Id),
                                                        "Order has not been set after insert");
                    }

                    // Insert order entries
                    orderItemDal.InsertMultiple(order.Id.Value, order.OrderItems);
                    // Update inventory
                    foreach (var orderItem in order.OrderItems)
                    {
                        itemDal.Update(orderItem.ProductId, stockIncrement: (int)-orderItem.Qty);
                    }
                }
                scope.Complete();
            }
        }