Exemple #1
0
        public Item CommitChanges()
        {
            if (barCode3 != null && barCode3.Length > 254)
            {
                while (barCode3.Length > 254 && barCode3.Contains(","))
                {
                    barCode3 = barCode3.Substring(0, barCode3.LastIndexOf(','));
                }

                if (barCode3.Length > 254)
                {
                    barCode3 = barCode3.Substring(0, 254);
                }
            }

            if (BusinessDomain.AppConfiguration.AutoGenerateItemCodes && string.IsNullOrWhiteSpace(code))
            {
                AutoGenerateCode();
            }

            if (!string.IsNullOrEmpty(groupName) && groupId <= 1)
            {
                ItemsGroup g = ItemsGroup.EnsureByPath(groupName, ItemsGroup.Cache);
                groupId = g.Id;
            }

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

            return(this);
        }
        public bool CheckApplicableToDetail <T> (IList <T> operationDetails, OperationDetail operationDetail) where T : OperationDetail
        {
            if (error)
            {
                return(false);
            }

            bool ret;

            switch (type)
            {
            case PriceRule.ConditionType.Good:
                ret = operationDetail.ItemId == Convert.ToInt64(values [0]);
                break;

            case PriceRule.ConditionType.GoodGroup:
                ItemsGroup itemsGroup = ItemsGroup.Cache.GetById(operationDetail.ItemGroupId);

                string groupCode = null;
                long?  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 = itemsGroup.Code.StartsWith(groupCode);
                break;

            case PriceRule.ConditionType.GoodQttySum:
                double from;
                double to;
                GetConditionNumericInterval(formula, out from, out to);
                double quantity = operationDetails
                                  .Where(detail => detail.ItemId == operationDetail.ItemId)
                                  .Select(detail => detail.Quantity)
                                  .Sum();
                ret = from <= quantity && quantity <= to;
                break;

            default:
                return(true);
            }

            return(ret != IsException);
        }
        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);
        }