Example #1
0
        public static AutoOrder GetCustomerAutoOrder(int customerID, int autoOrderID)
        {
            var context = Exigo.OData();

            // Setup the base query
            var autoOrderData = context.AutoOrders.Expand("Details")
                .Where(c => c.CustomerID == customerID)
                .Where(c => c.AutoOrderID == autoOrderID)
                .FirstOrDefault();
            if (autoOrderData == null) return null;

            var autoOrder = (AutoOrder)autoOrderData;

            // Get the payment methods if applicable
            var paymentMethods = GetCustomerPaymentMethods(new GetCustomerPaymentMethodsRequest
            {
                CustomerID = customerID
            });

            IPaymentMethod paymentMethod;
            switch (autoOrder.AutoOrderPaymentTypeID)
            {
                case 1: paymentMethod = paymentMethods.Where(c => c is CreditCard && ((CreditCard)c).Type == CreditCardType.Primary).FirstOrDefault(); break;
                case 2: paymentMethod = paymentMethods.Where(c => c is CreditCard && ((CreditCard)c).Type == CreditCardType.Secondary).FirstOrDefault(); break;
                case 3: paymentMethod = paymentMethods.Where(c => c is BankAccount && ((BankAccount)c).Type == ExigoService.BankAccountType.Primary).FirstOrDefault(); break;
                default: paymentMethod = null; break;
            }
            autoOrder.PaymentMethod = paymentMethod;

            // Determine if each autoOrder contains only virtual items if applicable
            var itemIDs = new List<int>();
            if (autoOrder.Details != null && autoOrder.Details.Count() > 0)
            {
                itemIDs.AddRange(autoOrder.Details.Select(c => c.ItemID));
            }
            itemIDs = itemIDs.Distinct().ToList();

            // Determine which item IDs are virtual
            var virtualItems = context.Items
                .Where(c => c.IsVirtual)
                .Where(itemIDs.ToOrExpression<Common.Api.ExigoOData.Item, int>("ItemID"))
                .Select(c => new { c.ItemID })
                .ToList();
            var virtualItemIDs = virtualItems.Select(c => c.ItemID).ToList();

            // Loop through each auto order, setting the details
            foreach (var detail in autoOrder.Details)
            {
                if (virtualItemIDs.Contains(detail.ItemID))
                {
                    detail.IsVirtual = true;
                }

                detail.ImageUrl = context.Items.Where(c => c.ItemCode == detail.ItemCode).FirstOrDefault().SmallImageUrl;
            }

            // Return the results
            return autoOrder;
        }
Example #2
0
        public static IEnumerable<AutoOrder> GetCustomerAutoOrders(GetCustomerAutoOrdersRequest request)
        {
            var context = Exigo.OData();

            // Setup the base query
            var basequery = context.AutoOrders;
            if (request.IncludeDetails) basequery = basequery.Expand("Details");

            // Setup the query
            var query = basequery.Where(c => c.CustomerID == request.CustomerID);

            // Filters
            if (!request.IncludeCancelledAutoOrders)
            {
                query = query.Where(c => c.AutoOrderStatusID == 0);
            }

            // Get the data
            var autoOrderData = query.Select(c => c);

            var autoOrders = new List<AutoOrder>();
            foreach (var autoOrder in autoOrderData)
            {
                autoOrders.Add((AutoOrder)autoOrder);
            }

            // If we don't have any autoOrder, stop here.
            if (autoOrders.Count == 0) yield break;

            // Get the payment methods if applicable
            if (request.IncludePaymentMethods)
            {
                var paymentMethods = GetCustomerPaymentMethods(new GetCustomerPaymentMethodsRequest
                {
                    CustomerID = request.CustomerID
                });
                foreach (var autoOrder in autoOrders)
                {
                    IPaymentMethod paymentMethod;
                    switch (autoOrder.AutoOrderPaymentTypeID)
                    {
                        case 1: paymentMethod = paymentMethods.Where(c => c is CreditCard && ((CreditCard)c).Type == CreditCardType.Primary).FirstOrDefault(); break;
                        case 2: paymentMethod = paymentMethods.Where(c => c is CreditCard && ((CreditCard)c).Type == CreditCardType.Secondary).FirstOrDefault(); break;
                        case 3: paymentMethod = paymentMethods.Where(c => c is BankAccount && ((BankAccount)c).Type == ExigoService.BankAccountType.Primary).FirstOrDefault(); break;
                        default: paymentMethod = null; break;
                    }
                    autoOrder.PaymentMethod = paymentMethod;
                }
            }

            // Determine if each autoOrder contains only virtual items if applicable
            if (request.IncludeDetails)
            {
                var itemIDs = new List<int>();
                autoOrders.ForEach(a =>
                {
                    if (a.Details != null && a.Details.Count() > 0)
                    {
                        itemIDs.AddRange(a.Details.Select(c => c.ItemID));
                    }
                });
                itemIDs = itemIDs.Distinct().ToList();

                // Determine which item IDs are virtual
                var virtualItems = context.Items
                    .Where(c => c.IsVirtual)
                    .Where(itemIDs.ToOrExpression<Common.Api.ExigoOData.Item, int>("ItemID"))
                    .Select(c => new { c.ItemID })
                    .ToList();
                var virtualItemIDs = virtualItems.Select(c => c.ItemID).ToList();

                // Loop through each auto order, setting the details
                foreach (var autoOrder in autoOrders)
                {
                    foreach (var detail in autoOrder.Details)
                    {
                        if (virtualItemIDs.Contains(detail.ItemID))
                        {
                            detail.IsVirtual = true;
                        }
                    }
                }
            }

            // Get the images
            if (request.IncludeDetails)
            {
                var itemcodes = new List<string>();
                foreach (var order in autoOrders)
                {
                    itemcodes.AddRange(order.Details.Select(c => c.ItemCode));
                }

                var images = new List<Common.Api.ExigoOData.Item>();
                if (itemcodes.Count > 0)
                {
                    images = context.Items
                        .Where(itemcodes.ToOrExpression<Common.Api.ExigoOData.Item, string>("ItemCode"))
                        .Select(c => new Common.Api.ExigoOData.Item
                        {
                            ItemCode = c.ItemCode,
                            SmallImageUrl = c.SmallImageUrl
                        })
                        .ToList();
                }

                // Loop through each auto order, setting the details
                foreach (var autoOrder in autoOrders)
                {
                    foreach (var detail in autoOrder.Details)
                    {
                        var image = images.Where(c => c.ItemCode == detail.ItemCode).FirstOrDefault();
                        if (image != null) detail.ImageUrl = image.SmallImageUrl;
                    }
                }
            }

            // Return the results
            foreach (var autoOrder in autoOrders)
            {
                yield return autoOrder;
            }
        }