/// <summary> /// Updates all prices. /// </summary> /// <param name="context">The request context.</param> /// <param name="transaction">The sales transaction.</param> /// <param name="dateToCheck">The date to check.</param> public void UpdateAllPrices(RequestContext context, SalesTransaction transaction, DateTime dateToCheck) { if (transaction == null) { throw new ArgumentNullException("transaction"); } var linesToCalculate = transaction.PriceCalculableSalesLines.ToList(); // build price calculation context string customerAccount = string.IsNullOrWhiteSpace(transaction.CustomerId) ? string.Empty : transaction.CustomerId; string customerPriceGroup = GetCustomerPriceGroup(context, customerAccount); PricingDataServiceManager pricingDataManager = new PricingDataServiceManager(context); var priceContext = PE.PriceContextHelper.CreatePriceContext(context, pricingDataManager, transaction, this.priceParameters, new ChannelCurrencyOperations(context), dateToCheck, customerAccount, customerPriceGroup, transaction.IsTaxIncludedInPrice, PricingCalculationMode.Transaction, DiscountCalculationMode.None); priceContext.IsTaxInclusive = transaction.IsTaxIncludedInPrice; // store old line prices var oldPrices = linesToCalculate.ToDictionary(l => l.LineId, l => l.Price, StringComparer.OrdinalIgnoreCase); // calculate the prices on the transaction PE.PricingEngine.CalculatePricesForSalesLines(linesToCalculate, priceContext, pricingDataManager); // Check if any prices were changed/invalid foreach (var salesLine in linesToCalculate) { if (salesLine.PriceInBarcode && !salesLine.IsPriceOverridden) { if (salesLine.Price != 0 && salesLine.Price != salesLine.BarcodeEmbeddedPrice) { decimal quantity = salesLine.BarcodeEmbeddedPrice / salesLine.Price; var roundingRequest = new GetRoundQuantityServiceRequest(quantity, salesLine.UnitOfMeasureSymbol); var response = context.Execute <GetRoundQuantityServiceResponse>(roundingRequest); quantity = response.RoundedValue; salesLine.Quantity = quantity; salesLine.BarcodeCalculatedQuantity = quantity; } else { salesLine.Price = salesLine.BarcodeEmbeddedPrice; salesLine.BarcodeCalculatedQuantity = 1; } } decimal oldPrice = oldPrices[salesLine.LineId]; if (salesLine.Price != oldPrice) { salesLine.WasChanged = true; } // if price change, raise Price changed notification RaiseNotificationIfPriceIsChanged(context, oldPrice, salesLine); // if price is invalid, raise Price invalid notification RaiseNotificationIfPriceIsInvalid(context, salesLine); } }
private static CalculateQuantityFromPriceServiceResponse CalculateQuantityFromPrice(CalculateQuantityFromPriceServiceRequest request) { decimal quantity = 0; if (request.BarcodePrice > 0) { if (request.DefaultProductPrice != request.BarcodePrice && request.DefaultProductPrice != decimal.Zero) { quantity = request.BarcodePrice / request.DefaultProductPrice; var roundingRequest = new GetRoundQuantityServiceRequest(quantity, request.UnitOfMeasure); var response = request.RequestContext.Execute <GetRoundQuantityServiceResponse>(roundingRequest); quantity = response.RoundedValue; } } return(new CalculateQuantityFromPriceServiceResponse(quantity)); }
/// <summary> /// Round quantity for the given unit of measure. /// </summary> /// <param name="request">Service request.</param> /// <returns>Rounded value.</returns> private GetRoundQuantityServiceResponse RoundQuantity(GetRoundQuantityServiceRequest request) { var unitsOfMeasureIds = new List <string> { request.UnitOfMeasure }; var getUnitsOfMeasureByUnitIdDataRequest = new GetUnitsOfMeasureDataRequest(unitsOfMeasureIds, QueryResultSettings.SingleRecord); UnitOfMeasure uom = request.RequestContext.Runtime.Execute <EntityDataServiceResponse <UnitOfMeasure> >(getUnitsOfMeasureByUnitIdDataRequest, request.RequestContext).PagedEntityCollection.FirstOrDefault(); int decimalPrecision = DefaultRoundingPrecision; if (uom != null) { decimalPrecision = uom.DecimalPrecision; } string roundedString = request.Value.ToString(string.Format("N{0}", decimalPrecision)); return(new GetRoundQuantityServiceResponse(Convert.ToDecimal(roundedString))); }