示例#1
0
        public void SetPrice(OrderLine orderLine)
        {
            try
            {
                if (orderLine == null)
                {
                    return;
                }

                Product prod    = Products.FirstOrDefault(p => p.Id == orderLine.ProductId);
                Catalog catalog = Catalogs.FirstOrDefault(c => c.Id == prod.CatalogId);

                if (prod == null || catalog == null)
                {
                    return;
                }

                if (string.IsNullOrWhiteSpace(orderLine.Description))
                {
                    orderLine.Description = prod.Name;
                }

                // if the orderline has a custom price, then we will NOT re-evaluate it
                if (!orderLine.IsCustomPrice)
                {
                    decimal price = 0;

                    // price component 1: sales price defined on product
                    price = prod.SalesPrice;

                    // price component 2: prices of all the selected option values
                    if (orderLine.OrderLineOptions != null && orderLine.OrderLineOptions.Count > 0)
                    {
                        foreach (OrderLineOption olo in orderLine.OrderLineOptions.Where(olo => !string.IsNullOrWhiteSpace(olo.ProductOptionValueId)))
                        {
                            ProductOptionValue productOptionValue = cache.GetProductOptionValue(olo.ProductOptionValueId);

                            if (productOptionValue != null)
                            {
                                price += productOptionValue.Price;
                            }
                        }
                    }

                    // price component 3: if there is a formula defined, add the result to the price
                    if (!string.IsNullOrWhiteSpace(prod.SalesPriceFormulaInternal))
                    {
                        CalculationSvc calcSvc      = new CalculationSvc();
                        decimal        formulaPrice = calcSvc.Calculate(prod.SalesPriceFormulaInternal, orderLine);
                        price += formulaPrice;
                    }

                    orderLine.UnitPrice = price;
                }

                orderLine.TotalPrice = Math.Round(orderLine.UnitPrice * orderLine.Quantity, 2);

                string taxClassId = null;

                if (prod.SalesTaxClassId != null)
                {
                    taxClassId = prod.SalesTaxClassId;
                }
                else
                {
                    taxClassId = catalog.DefaultSalesTaxClassId;
                }

                orderLine.TaxPctg = 0;

                if (taxClassId != null)
                {
                    SysObject taxClass = TaxClasses.FirstOrDefault(so => so.Id == taxClassId);

                    if (taxClass != null && taxClass.Value.HasValue)
                    {
                        orderLine.TaxPctg = taxClass.Value.Value;
                    }
                }

                orderLine.Tax = Math.Round(orderLine.CalculateTotalTax(), 2);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex.ToString());
                throw;
            }
        }