public static bool TryAddServiceCharge <T, TDetail> (T operation, ConfirmationToApply <TDetail> confirmationToApply = null, IEnumerable <PriceRule> allRules = null) where T : Operation <TDetail>, new () where TDetail : OperationDetail, new () { List <PriceRule> serviceRules = new List <PriceRule> (); foreach (PriceRule priceRule in (allRules ?? GetAll()) .Where(p => p.actions.Any(a => a.Type == ActionType.ServiceCharge) && p.CheckApplicableToOperation(operation))) { if (BusinessDomain.AppConfiguration.ConfirmPriceRules) { serviceRules.Add(priceRule); } else { Result result = priceRule.Apply(operation); if (result == Result.StopOperation) { return(false); } if (result == Result.StopRules) { break; } } } if (!BusinessDomain.AppConfiguration.ConfirmPriceRules) { return(true); } T clone = operation.Clone <T, TDetail> (); int countNoService = clone.Details.Count; foreach (PriceRule priceRule in serviceRules) { Result result = priceRule.Apply(clone); if (result == Result.StopOperation) { return(false); } if (result == Result.StopRules) { break; } } List <TDetail> serviceCharges = new List <TDetail> (clone.Details.Count - countNoService); for (int i = countNoService; i < clone.Details.Count; i++) { serviceCharges.Add(clone.Details [i]); } if (serviceCharges.Count > 0) { foreach (PriceRule priceRule in confirmationToApply != null ? confirmationToApply(serviceRules, operation) : serviceRules) { priceRule.Apply(operation); } } return(true); }
public static bool ApplyBeforeOperationSaved <T> (Operation <T> operation, ConfirmationToApply <T> confirmationToApply = null, bool operationComplete = true, IEnumerable <PriceRule> allPriceRules = null) where T : OperationDetail { if (allPriceRules == null) { allPriceRules = GetAll(); } IDictionary <PriceRule, IList <T> > rulesToApply = new Dictionary <PriceRule, IList <T> > (); foreach (PriceRule priceRule in allPriceRules.Where(p => p.actions.All(a => a.Type != ActionType.Email && a.Type != ActionType.Payment && a.Type != ActionType.AskAdvancePayment && a.Type != ActionType.ServiceCharge) && p.CheckApplicableToOperation(operation))) { if (priceRule.IsGlobal) { if (operationComplete) { rulesToApply.Add(priceRule, null); } } else if (operationComplete == priceRule.DependsOnDocumentSum()) { priceRule.CheckApplicableDetails(operation.Details, ref rulesToApply); priceRule.CheckApplicableDetails(operation.AdditionalDetails, ref rulesToApply); } } List <PriceRule> selectedRulesToApply; if (BusinessDomain.AppConfiguration.ConfirmPriceRules && confirmationToApply != null && rulesToApply.Any(r => r.Key.actions.All(a => a.Type != ActionType.AddGlobalGood) && r.Value != null && r.Value.Count > 0)) { selectedRulesToApply = confirmationToApply(rulesToApply.Keys, operation); } else { selectedRulesToApply = new List <PriceRule> (rulesToApply.Keys); } if (selectedRulesToApply.Count == 0) { return(true); } foreach (KeyValuePair <PriceRule, IList <T> > ruleToApply in rulesToApply) { PriceRule priceRule = ruleToApply.Key; if (!selectedRulesToApply.Contains(priceRule)) { continue; } if (priceRule.IsGlobal) { Result result = priceRule.Apply(operation); if (result == Result.StopOperation) { return(false); } if (result == Result.StopRules) { break; } } else { foreach (T detail in ruleToApply.Value) { priceRule.Apply(detail, operation); } } } return(true); }