Exemple #1
0
        public static double ParseExpression(string text)
        {
            try {
                if (string.IsNullOrEmpty(text))
                {
                    return(0);
                }

                return(RPNCalculator.EvaluateExpression(text));
            } catch (ExpressionErrorException) {
                return(0);
            }
        }
Exemple #2
0
        public static bool IsValidExpression(string text)
        {
            try {
                if (string.IsNullOrEmpty(text))
                {
                    return(false);
                }

                RPNCalculator.EvaluateExpression(text);
                return(true);
            } catch (ExpressionErrorException) {
                return(false);
            }
        }
Exemple #3
0
        public static bool TryParseExpression(string text, out double value)
        {
            try {
                if (string.IsNullOrEmpty(text))
                {
                    value = 0;
                    return(false);
                }

                value = RPNCalculator.EvaluateExpression(text);
                return(true);
            } catch (ExpressionErrorException) {
                value = 0;
                return(false);
            }
        }
Exemple #4
0
        private static string GetSubstring(string textValue, Match match)
        {
            string startString = match.Groups ["start"].Value.Replace(LENGTH_IN_FORMAT, textValue.Length.ToString());
            int    start       = Convert.ToInt32(RPNCalculator.EvaluateExpression(startString));

            if (start < 0 || start >= textValue.Length)
            {
                return(string.Empty);
            }

            string countString = match.Groups ["count"].Value.Replace(LENGTH_IN_FORMAT, textValue.Length.ToString());
            int    count       = Convert.ToInt32(RPNCalculator.EvaluateExpression(countString));

            if (count < 0 || start + count > textValue.Length)
            {
                return(string.Empty);
            }

            return(textValue.Substring(start, count));
        }
        public PriceRule.Result Apply <T> (PriceRule priceRule, Operation <T> operation) where T : OperationDetail
        {
            if (error)
            {
                return(PriceRule.Result.Success);
            }

            string stringValue;

            switch (Type)
            {
            case PriceRule.ActionType.Stop:
                BusinessDomain.OnPriceRuleMessage(new PriceRuleMessageEventArgs(values [0].ToString(), ErrorSeverity.Warning));
                return(PriceRule.Result.StopOperation);

            case PriceRule.ActionType.Exit:
                BusinessDomain.OnPriceRuleMessage(new PriceRuleMessageEventArgs(values [0].ToString(), ErrorSeverity.Warning));
                return(PriceRule.Result.StopRules);

            case PriceRule.ActionType.Message:
                BusinessDomain.OnPriceRuleMessage(new PriceRuleMessageEventArgs(values [0].ToString(), ErrorSeverity.Information));
                break;

            case PriceRule.ActionType.Email:
                BusinessDomain.WorkflowManager.SendEmailAsync(operation, values [0].ToString(), values [1].ToString());
                break;

            case PriceRule.ActionType.ServiceCharge:
                List <T> detailsWithService         = new List <T> ();
                const PriceRule.AppliedActions mask = PriceRule.AppliedActions.ServiceChargeItem;
                detailsWithService.AddRange(operation.Details.Where(d =>
                                                                    priceRule.CheckApplicableToDetail(operation.Details, d, mask)));
                detailsWithService.AddRange(operation.AdditionalDetails.Where(d =>
                                                                              priceRule.CheckApplicableToDetail(operation.AdditionalDetails, d, mask)));

                Item       item           = (Item)values [0];
                PriceGroup priceGroup     = operation.GetPriceGroup();
                double     operationTotal = detailsWithService.Sum(detail => detail.Total);
                double     serviceChargeAmount;
                if (values [1] is double)
                {
                    serviceChargeAmount = operationTotal * (double)values [1] / 100;
                }
                else
                {
                    string expression = (string)values [1];
                    try {
                        serviceChargeAmount = RPNCalculator.EvaluateExpression(expression.Replace(DOCUMENT_SUM, operationTotal.ToString(CultureInfo.InvariantCulture)));
                    } catch (Exception ex) {
                        ErrorHandling.LogException(ex);
                        serviceChargeAmount = 0;
                    }
                }

                if (serviceChargeAmount > 0)
                {
                    item.SetPriceGroupPrice(priceGroup, serviceChargeAmount);
                    string note   = operation.Note;
                    T      detail = operation.AddNewDetail();
                    if (detail.ItemEvaluate(item, priceGroup, true))
                    {
                        detail.Note = note;
                        detail.AppliedPriceRules = PriceRule.AppliedActions.ServiceChargeItem;
                    }
                    else
                    {
                        operation.RemoveDetail(operation.Details.Count - 1, false);
                    }
                }
                break;

            case PriceRule.ActionType.AddGlobalGood:
                foreach (T detail in GetDetailsForPromotionalItems <T> (formula))
                {
                    if (operation.OperationType == OperationType.RestaurantOrder)
                    {
                        detail.LotId = Int32.MinValue;
                    }
                    operation.Details.Add(detail);
                }
                break;

            case PriceRule.ActionType.Payment:
                Payment payment = new Payment(operation, (int)BasePaymentType.Coupon, PaymentMode.Paid);
                stringValue = PriceRule.GetStringValue(formula, ' ');
                try {
                    payment.Quantity = RPNCalculator.EvaluateExpression(stringValue
                                                                        .Replace(DOCUMENT_SUM, operation.TotalPlusVAT.ToString(CultureInfo.InvariantCulture)));
                    payment.CommitAdvance();
                } catch (Exception ex) {
                    ErrorHandling.LogException(ex);
                }
                break;

            case PriceRule.ActionType.AskAdvancePayment:
                stringValue = PriceRule.GetStringValue(formula, ' ');
                try {
                    BusinessDomain.OnPriceRulePriceRuleAskForAdvance(new PriceRuleAskAdvanceEventArgs(operation.PartnerId,
                                                                                                      RPNCalculator.EvaluateExpression(stringValue.Replace(DOCUMENT_SUM, operation.TotalPlusVAT.ToString(CultureInfo.InvariantCulture)))));
                } catch (Exception ex) {
                    ErrorHandling.LogException(ex);
                }
                break;
            }

            return(PriceRule.Result.Success);
        }
        public void Apply <T> (T operationDetail, Operation <T> operation) where T : OperationDetail
        {
            if (error)
            {
                return;
            }

            switch (Type)
            {
            case PriceRule.ActionType.AddGood:
                if (operation != null)
                {
                    foreach (T detail in GetDetailsForPromotionalItems <T> (formula))
                    {
                        if (operation.OperationType == OperationType.RestaurantOrder)
                        {
                            detail.LotId = Int32.MinValue;
                        }
                        detail.PromotionForDetailHashCode = operationDetail.GetHashCode();
                        operation.Details.Add(detail);
                        operationDetail.AppliedPriceRules |= PriceRule.AppliedActions.PromotionalItemSource;
                    }
                }
                break;

            case PriceRule.ActionType.Price:
                PriceGroup priceGroup;
                string []  parts = GetPriceExpressionParts(formula, out priceGroup);
                if (parts.Length > 1)
                {
                    double price;
                    switch (priceGroup)
                    {
                    case PriceGroup.TradeInPrice:
                        price = operationDetail.OriginalPriceIn;
                        break;

                    case PriceGroup.RegularPriceInOperation:
                        price = operationDetail.OriginalPriceOut;
                        break;

                    default:
                        Item item = Item.GetById(operationDetail.ItemId);
                        price = item.GetPriceGroupPrice(priceGroup);
                        break;
                    }

                    try {
                        operationDetail.OriginalPriceOut = RPNCalculator.EvaluateExpression(price + parts [1] + parts [2]);
                    } catch (Exception ex) {
                        ErrorHandling.LogException(ex);
                        break;
                    }
                }
                else
                {
                    operationDetail.OriginalPriceOut = Double.Parse(parts [0], CultureInfo.InvariantCulture);
                }

                operationDetail.PriceOutEvaluate();
                operationDetail.AppliedPriceRules |= PriceRule.AppliedActions.PriceChanged;
                break;

            case PriceRule.ActionType.Discount:
                operationDetail.DiscountEvaluate((double)values [0]);
                operationDetail.AppliedPriceRules |= PriceRule.AppliedActions.DiscountChanged;
                break;
            }
        }