Exemplo n.º 1
0
        public void Init(Order order)
        {
            try
            {
                if (order == null || order.OrderLines == null)
                {
                    return;
                }

                var productIds = order.OrderLines.Select(ol => ol.ProductId);
                Products = cache.GetProducts(productIds);  //_Ctx.Product.Where(p => productIds.Contains(p.Id)).ToList();

                var catalogIds = Products.Select(p => p.CatalogId).Distinct();
                Catalogs = cache.GetCatalogs(catalogIds);   //_Ctx.Catalog.Where(c => catalogIds.Contains(c.Id)).ToList();

                List <string> productOptionValueIds = new List <string>();

                foreach (var orderLine in order.OrderLines)
                {
                    if (orderLine.OrderLineOptions == null)
                    {
                        continue;
                    }

                    productOptionValueIds.AddRange(orderLine.OrderLineOptions.Where(olo => !string.IsNullOrWhiteSpace(olo.ProductOptionValueId)).Select(olo => olo.ProductOptionValueId));
                }

                // pre-load productOptionValue's (needed to calculate correct price)
                cache.GetProductOptionValues(productOptionValueIds);

                List <string> taxClassIds = new List <string>();

                var productTaxClassIds = Products.Where(p => p.SalesTaxClassId != null).Select(p => p.SalesTaxClassId).Distinct();
                if (productTaxClassIds != null && productTaxClassIds.Count() > 0)
                {
                    taxClassIds.AddRange(productTaxClassIds);
                }

                var catalogTaxClassIds = Catalogs.Where(c => c.DefaultSalesTaxClassId != null).Select(c => c.DefaultSalesTaxClassId).Distinct();
                if (catalogTaxClassIds != null && catalogTaxClassIds.Count() > 0)
                {
                    taxClassIds.AddRange(catalogTaxClassIds);
                }

                TaxClasses = cache.GetSysObjects(taxClassIds);  //_Ctx.SysObject.Where(o => taxClassIds.Contains(o.Id)).ToList();
            }
            catch (Exception ex)
            {
                _Logger.Error(ex.ToString());
                throw;
            }
        }