Example #1
0
        public User CommitChanges()
        {
            if (BusinessDomain.AppConfiguration.AutoGenerateUserCodes && string.IsNullOrWhiteSpace(code))
            {
                AutoGenerateCode();
            }

            if (!string.IsNullOrEmpty(groupName) && groupId <= 1)
            {
                UsersGroup g = GroupBase <UsersGroup> .EnsureByPath(groupName, UsersGroup.Cache);

                groupId = g.Id;
            }

            bool isNew = id < 0;

            BusinessDomain.DataAccessProvider.AddUpdateUser(this);
            cache.Set(this);

            if (isNew)
            {
                BusinessDomain.RestrictionTree.ResetLevelRestrictions(id, userLevel);
                BusinessDomain.RestrictionTree.SaveRestrictions();
            }

            if (lockedLocationIdDirty)
            {
                ConfigEntry.SaveValue(LOCKED_LOCATION_KEY, LockedLocationId, id);
                lockedLocationIdDirty = false;
            }

            if (lockedPartnerIdDirty)
            {
                ConfigEntry.SaveValue(LOCKED_PARTNER_KEY, LockedPartnerId, id);
                lockedPartnerIdDirty = false;
            }

            if (defaultPartnerIdDirty)
            {
                ConfigEntry.SaveValue(DEFAULT_PARTNER_KEY, DefaultPartnerId, id);
                defaultPartnerIdDirty = false;
            }

            if (defaultCompanyIdDirty)
            {
                ConfigEntry.SaveValue(DEFAULT_COMPANY_KEY, DefaultCompanyId, id);
                BusinessDomain.CurrentCompany = CompanyRecord.GetDefault();
                defaultCompanyIdDirty         = false;
            }

            if (hideItemsPurchasePriceDirty)
            {
                ConfigEntry.SaveValue(HIDE_ITEMS_PURCHASE_PRICE_KEY, HideItemsPurchasePrice, id);
                hideItemsPurchasePriceDirty = false;
            }

            if (hideItemsAvailabilityDirty)
            {
                ConfigEntry.SaveValue(HIDE_ITEMS_AVAILABILITY_KEY, HideItemsAvailability, id);
                hideItemsAvailabilityDirty = false;
            }

            if (allowZeroPricesDirty)
            {
                ConfigEntry.SaveValue(ALLOW_ZERO_PRICES_KEY, AllowZeroPrices, id);
                allowZeroPricesDirty = false;
            }

            if (BusinessDomain.LoggedUser.Id == id)
            {
                BusinessDomain.LoggedUser = this;
            }

            return(this);
        }
Example #2
0
        public static void ReapplyOnUserChanged <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.User:
                    return((long)c.Values [0] == operation.PartnerId);

                case ConditionType.UserGroup:
                    string stringValue = (string)c.Values [0];

                    long longValue;
                    UsersGroup usersGroup = long.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out longValue) ?
                                            UsersGroup.Cache.GetById(longValue) :
                                            UsersGroup.Cache.GetByCode(stringValue);

                    if (usersGroup == null)
                    {
                        return(false);
                    }

                    User user = User.Cache.GetById(operation.UserId);
                    if (user == null)
                    {
                        return(false);
                    }

                    return(usersGroup.Id == user.GroupId);
                }

                return(false);
            }))
            {
                return;
            }

            reapplyConfirmation(Translator.GetString("You have changed the operator. 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);
        }