예제 #1
0
        public List <OrderLineItemBO> AddOrder(int CustomerId, DateTime date, List <OrderLineItemBO> orderLineItems)
        {
            // Loop through entered line items and store all the line items who'se quantity is too high in a list
            var loopThroughItems = new List <OrderLineItemBO>();

            foreach (var item in orderLineItems)
            {
                loopThroughItems.Add(item);
            }
            var invalidItems = new List <OrderLineItemBO>();
            int ItemCount    = orderLineItems.Count;

            for (int i = 0; i < loopThroughItems.Count; i++)
            {
                var item      = loopThroughItems[i];
                int remaining = repository.UpdateProductQuantity(item.Quantity, item.Product.Id);
                if (remaining < 1)
                {
                    item.Quantity = -1 * remaining;

                    invalidItems.Add(item);
                    orderLineItems.Remove(item);
                    continue;
                }
            }

            // If there isn't a single item in orderline items return empty list
            if (orderLineItems.Count < 1)
            {
                return(new List <OrderLineItemBO>());
            }

            // Check if Order Present for same customer on same date
            bool orderExists   = false;
            var  existingOrder = new OrderBO();

            if (repository.GetOrders().ToList().Exists(o => ((o.Customer.CustomerId == CustomerId) &&
                                                             (o.Date.ToShortDateString() == date.ToShortDateString()))))
            {
                orderExists = true;
                // If order exists get existing Order
                existingOrder = mapper.Map <OrderBO>(repository.GetOrderForCustomerOnDate(CustomerId, date));
            }
            int associatedOrderId = 0;

            if (!orderExists)
            {
                // For order that doesn't exist add the line items after creating the order
                var order = new OrderDO();
                order.Customer = customerRepository.GetCustomerById(CustomerId);
                order.Date     = date;
                order.Total    = orderLineItems.Sum(l => l.Total);
                int createdOrderId = repository.AddOrder(mapper.Map <OrderDO>(order));

                associatedOrderId = createdOrderId;
                order.OrderId     = createdOrderId;
                repository.AddLineItemsForOrder(mapper.Map <List <OrderLineItemDO> >(orderLineItems), mapper.Map <OrderDO>(order));
            }
            else
            {
                // For existing order. Add the order details to each line item and then update the order
                associatedOrderId = existingOrder.Id;
                // repository.UpdateLineItemsForOrder(mapper.Map<List<OrderLineItemDO>>(orderLineItems), existingOrder.Id);
                foreach (var newItem in orderLineItems)
                {
                    newItem.Order   = existingOrder;
                    newItem.OrderId = existingOrder.Id;
                    existingOrder.LineItems.Add(newItem);
                }
                existingOrder.Total = existingOrder.LineItems.Sum(l => l.Total);

                UpdateOrder(existingOrder);
            }
            // Adds the created or existing orderId to end of the list being returned to controller
            invalidItems.Add(new OrderLineItemBO()
            {
                OrderId = associatedOrderId
            });
            return(invalidItems);
        }