public Partner CommitChanges() { if (BusinessDomain.AppConfiguration.AutoGeneratePartnerCodes && string.IsNullOrWhiteSpace(code)) { AutoGenerateCode(); } if (!string.IsNullOrEmpty(groupName) && groupId <= 1) { PartnersGroup g = PartnersGroup.EnsureByPath(groupName, PartnersGroup.Cache); groupId = g.Id; } BusinessDomain.DataAccessProvider.AddUpdatePartner(this); cache.Set(this); return(this); }
public static void ReapplyOnPartnerChanged <T> (Operation <T> operation, Action <string, Action> reapplyConfirmation, IListModel <PriceRule> allRules = null) where T : OperationDetail, new () { if (operation.Details.Count == 0) { return; } if (allRules == null) { allRules = GetAll(); } if (!allRules.Where(p => p.Enabled).SelectMany(r => r.Conditions).Any(c => { if (c.Error || c.Values == null || c.Values.Length <= 0 || c.Values [0] == null) { return(false); } switch (c.Type) { case ConditionType.Partner: return((long)c.Values [0] == operation.PartnerId); case ConditionType.PartnerGroup: string stringValue = (string)c.Values [0]; long longValue; PartnersGroup partnersGroup = long.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out longValue) ? PartnersGroup.Cache.GetById(longValue) : PartnersGroup.Cache.GetByCode(stringValue); if (partnersGroup == null) { return(false); } Partner partner = Partner.Cache.GetById(operation.PartnerId); if (partner == null) { return(false); } return(partnersGroup.Id == partner.GroupId); } return(false); })) { return; } reapplyConfirmation(Translator.GetString("You have changed the partner. Would you like to reapply your price rules?"), () => { for (int i = operation.Details.Count - 1; i >= 0; i--) { if ((operation.Details [i].AppliedPriceRules & AppliedActions.PromotionalItem) == AppliedActions.PromotionalItem) { operation.RemoveDetail(i, false); } else { operation.Details [i].AppliedPriceRules = AppliedActions.None; } } ApplyBeforeOperationSaved(operation, null, false, allRules); }); }
public bool CheckApplicableToOperation <T> (Operation <T> operation) where T : OperationDetail { if (error) { return(false); } DateTime dateFrom; DateTime dateTo; double @from; double to; bool ret; string groupCode = null; long? intValue; switch (type) { case PriceRule.ConditionType.Partner: ret = operation.PartnerId == Convert.ToInt64(values [0]); break; case PriceRule.ConditionType.PartnerGroup: intValue = PriceRule.GetLongValue(formula); if (intValue != null) { PartnersGroup byId = PartnersGroup.Cache.GetById(intValue.Value); if (byId != null) { groupCode = byId.Code; } } if (groupCode == null) { groupCode = PriceRule.GetStringValue(formula); } Partner partner = Partner.Cache.GetById(operation.PartnerId); ret = PartnersGroup.Cache.GetById(partner.GroupId).Code.StartsWith(groupCode); break; case PriceRule.ConditionType.Object: ret = operation.LocationId == Convert.ToInt64(values [0]); break; case PriceRule.ConditionType.ObjectGroup: intValue = PriceRule.GetLongValue(formula); if (intValue != null) { LocationsGroup byId = LocationsGroup.Cache.GetById(intValue.Value); if (byId != null) { groupCode = byId.Code; } } if (groupCode == null) { groupCode = PriceRule.GetStringValue(formula); } Location location = Location.Cache.GetById(operation.LocationId); ret = LocationsGroup.Cache.GetById(location.GroupId).Code.StartsWith(groupCode); break; case PriceRule.ConditionType.User: ret = operation.UserId == Convert.ToInt64(values [0]); break; case PriceRule.ConditionType.UserGroup: intValue = PriceRule.GetLongValue(formula); if (intValue != null) { UsersGroup byId = UsersGroup.Cache.GetById(intValue.Value); if (byId != null) { groupCode = byId.Code; } } if (groupCode == null) { groupCode = PriceRule.GetStringValue(formula); } User user = User.Cache.GetById(operation.UserId); ret = UsersGroup.Cache.GetById(user.GroupId).Code.StartsWith(groupCode); break; case PriceRule.ConditionType.Time: GetConditionDateTimeInterval(formula, out dateFrom, out dateTo); DateTime now = BusinessDomain.Now; if (dateFrom.TimeOfDay < dateTo.TimeOfDay) { ret = dateFrom.TimeOfDay <= now.TimeOfDay && now.TimeOfDay <= dateTo.TimeOfDay; } else { ret = dateFrom.TimeOfDay <= now.TimeOfDay || now.TimeOfDay <= dateTo.TimeOfDay; } break; case PriceRule.ConditionType.Date: GetConditionDateTimeInterval(formula, out dateFrom, out dateTo); ret = dateFrom <= operation.Date && operation.Date <= dateTo; break; case PriceRule.ConditionType.DocumentSum: GetConditionNumericInterval(formula, out from, out to); ret = from <= operation.Total && operation.Total <= to; break; case PriceRule.ConditionType.TurnoverSum: GetConditionNumericInterval(formula, out from, out to); double turnover = Partner.GetTurnover(operation.PartnerId); ret = from <= turnover && turnover <= to; break; case PriceRule.ConditionType.PaymentSum: GetConditionNumericInterval(formula, out from, out to); double debt = Partner.GetDebt(operation.PartnerId); ret = from <= debt && debt <= to; break; case PriceRule.ConditionType.Weekdays: List <DayOfWeek> ruleWeekDays = GetRuleWeekDays(); ret = ruleWeekDays.Contains(operation.Date.DayOfWeek); break; case PriceRule.ConditionType.UnpaidDocumentsSum: GetConditionNumericInterval(formula, out from, out to); double sum = Partner.GetUnpaidAmountWithExpiredDueDate(operation.PartnerId, operation.Date); ret = from <= sum && sum <= to; break; case PriceRule.ConditionType.ContainsGood: intValue = Convert.ToInt64(values [0]); ret = operation.Details.Any(d => d.ItemId == intValue) || operation.AdditionalDetails.Any(d => d.ItemId == intValue); break; case PriceRule.ConditionType.GoodGroup: case PriceRule.ConditionType.ContainsGGroup: Dictionary <long, string> codes = new Dictionary <long, string> (); foreach (T detail in operation.Details) { AddGroupCode(codes, detail.ItemGroupId); } foreach (T additionalDetail in operation.AdditionalDetails) { AddGroupCode(codes, additionalDetail.ItemGroupId); } intValue = PriceRule.GetLongValue(formula); if (intValue != null) { ItemsGroup byId = ItemsGroup.Cache.GetById(intValue.Value); if (byId != null) { groupCode = byId.Code; } } if (groupCode == null) { groupCode = PriceRule.GetStringValue(formula); } ret = codes.Values.Any(c => c.StartsWith(groupCode)); break; case PriceRule.ConditionType.PaymentTypeUsed: List <BasePaymentType> paymentTypes = GetConditionPaymentTypes(); ret = operation.Payments.Any(payment => paymentTypes.Contains(payment.Type.BaseType)); break; case PriceRule.ConditionType.DatabaseUpdated: ret = BusinessDomain.GetDatabaseLastUpdate().AddMinutes(30) > DateTime.Now; break; default: return(true); } return(ret != IsException); }