// Ajax.
        public ActionResult AllDiscounts(string label, string selectedIds, DiscountType?type)
        {
            var discounts   = _discountService.GetAllDiscounts(type, null, true).ToList();
            var selectedArr = selectedIds.ToIntArray();

            if (label.HasValue())
            {
                discounts.Insert(0, new Discount {
                    Name = label, Id = 0
                });
            }

            var data = discounts
                       .Select(x => new
            {
                id       = x.Id.ToString(),
                text     = x.Name,
                selected = selectedArr.Contains(x.Id)
            })
                       .ToList();

            return(new JsonResult {
                Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Exemplo n.º 2
0
        public ActionResult List(DiscountListModel model, DataSourceRequest command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            DiscountType?discountType = null;

            if (model.SearchDiscountTypeId > 0)
            {
                discountType = (DiscountType)model.SearchDiscountTypeId;
            }
            var discounts = _discountService.GetAllDiscounts(discountType,
                                                             model.SearchDiscountCouponCode,
                                                             model.SearchDiscountName,
                                                             true);

            var gridModel = new DataSourceResult
            {
                Data = discounts.PagedForCommand(command).Select(x =>
                {
                    var discountModel = x.ToModel();
                    discountModel.DiscountTypeName         = x.DiscountType.GetLocalizedEnum(_localizationService, _workContext);
                    discountModel.PrimaryStoreCurrencyCode = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode;
                    discountModel.TimesUsed = _discountService.GetAllDiscountUsageHistory(x.Id, pageSize: 1).TotalCount;
                    return(discountModel);
                }),
                Total = discounts.Count
            };

            return(Json(gridModel));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets all discounts (cachable models)
        /// </summary>
        /// <param name="discountType">Discount type; pass null to load all records</param>
        /// <param name="couponCode">Coupon code to find (exact match); pass null or empty to load all records</param>
        /// <param name="discountName">Discount name; pass null or empty to load all records</param>
        /// <param name="showHidden">A value indicating whether to show expired and not started discounts</param>
        /// <returns>Discounts</returns>
        public virtual IList <DiscountForCaching> GetAllDiscountsForCaching(DiscountType?discountType = null,
                                                                            string couponCode         = null, string discountName = null, bool showHidden = false)
        {
            //we cache discounts between requests. Otherwise, they will be loaded for almost each HTTP request
            //we have to use the following workaround with cachable model (DiscountForCaching) because
            //Entity Framework doesn't support 2-level caching

            //we load all discounts, and filter them using "discountType" parameter later (in memory)
            //we do it because we know that this method is invoked several times per HTTP request with distinct "discountType" parameter
            //that's why let's access the database only once
            var cacheKey = string.Format(DiscountEventConsumer.DISCOUNT_ALL_KEY,
                                         showHidden, couponCode ?? string.Empty, discountName ?? string.Empty);
            var discounts = _cacheManager.Get(cacheKey, () =>
            {
                return(GetAllDiscounts(couponCode: couponCode, discountName: discountName, showHidden: showHidden)
                       .Select(discount => discount.MapDiscount()).ToList());
            });

            //we know that this method is usually inkoved multiple times
            //that's why we filter discounts by type on the application layer
            if (discountType.HasValue)
            {
                discounts = discounts.Where(discount => discount.DiscountType == discountType.Value).ToList();
            }

            return(discounts);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets all discounts
        /// </summary>
        /// <param name="discountType">Discount type; null to load all discount</param>
        /// <param name="couponCode">Coupon code to find (exact match)</param>
        /// <param name="discountName">Discount name</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Discounts</returns>
        public virtual IList <Discount> GetAllDiscounts(DiscountType?discountType = null,
                                                        string couponCode         = "", string discountName = "", bool showHidden = false)
        {
            var query = _discountRepository.Table;

            if (!showHidden)
            {
                //The function 'CurrentUtcDateTime' is not supported by SQL Server Compact.
                //That's why we pass the date value
                var nowUtc = DateTime.UtcNow;
                query = query.Where(d =>
                                    (!d.StartDateUtc.HasValue || d.StartDateUtc <= nowUtc) &&
                                    (!d.EndDateUtc.HasValue || d.EndDateUtc >= nowUtc));
            }
            if (!string.IsNullOrEmpty(couponCode))
            {
                query = query.Where(d => d.CouponCode == couponCode);
            }
            if (!string.IsNullOrEmpty(discountName))
            {
                query = query.Where(d => d.Name.Contains(discountName));
            }
            if (discountType.HasValue)
            {
                var discountTypeId = (int)discountType.Value;
                query = query.Where(d => d.DiscountTypeId == discountTypeId);
            }

            query = query.OrderBy(d => d.Name);

            var discounts = query.ToList();

            return(discounts);
        }
Exemplo n.º 5
0
        public virtual async Task <(IEnumerable <DiscountModel> discountModel, int totalCount)> PrepareDiscountModel(DiscountListModel model, int pageIndex, int pageSize)
        {
            DiscountType?discountType = null;

            if (model.SearchDiscountTypeId > 0)
            {
                discountType = (DiscountType)model.SearchDiscountTypeId;
            }
            var discounts = await _discountService.GetAllDiscounts(discountType, _workContext.CurrentCustomer.StaffStoreId,
                                                                   null,
                                                                   model.SearchDiscountCouponCode,
                                                                   model.SearchDiscountName,
                                                                   true);

            var items = new List <DiscountModel>();

            foreach (var x in discounts.Skip((pageIndex - 1) * pageSize).Take(pageSize))
            {
                var discountModel = x.ToModel(_dateTimeService);
                discountModel.DiscountTypeName = x.DiscountTypeId.GetTranslationEnum(_translationService, _workContext);
                discountModel.TimesUsed        = (await _discountService.GetAllDiscountUsageHistory(x.Id, pageSize: 1)).TotalCount;
                items.Add(discountModel);
            }
            ;
            return(items, discounts.Count);
        }
        /// <summary>
        /// Gets all discounts
        /// </summary>
        /// <param name="discountType">Discount type; null to load all discount</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Discount collection</returns>
        public virtual IList <Discount> GetAllDiscounts(DiscountType?discountType, bool showHidden = false)
        {
            int?discountTypeId = null;

            if (discountType.HasValue)
            {
                discountTypeId = (int)discountType.Value;
            }

            string key = string.Format(DISCOUNTS_ALL_KEY, showHidden, discountTypeId.HasValue ? discountTypeId.Value : 0);

            return(_cacheManager.Get(key, () =>
            {
                var query = _discountRepository.Table;
                if (!showHidden)
                {
                    //The function 'CurrentUtcDateTime' is not supported by SQL Server Compact.
                    //That's why we pass the date value
                    var nowUtc = DateTime.UtcNow;
                    query = query.Where(d =>
                                        (!d.StartDateUtc.HasValue || d.StartDateUtc <= nowUtc) &&
                                        (!d.EndDateUtc.HasValue || d.EndDateUtc >= nowUtc)
                                        );
                }
                if (discountTypeId.HasValue && discountTypeId.Value > 0)
                {
                    query = query.Where(d => d.DiscountTypeId == discountTypeId);
                }
                query = query.OrderByDescending(d => d.Id);

                var discounts = query.ToList();
                return discounts;
            }));
        }
        public ActionResult List(DiscountListModel model, DataSourceRequest command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            DiscountType?discountType = null;

            if (model.SearchDiscountTypeId > 0)
            {
                discountType = (DiscountType)model.SearchDiscountTypeId;
            }
            var couponCode = !String.IsNullOrEmpty(model.SearchDiscountCouponCode) ? model.SearchDiscountCouponCode.Trim() : null;
            var discounts  = _discountService.GetAllDiscounts(discountType, couponCode, true);
            var gridModel  = new DataSourceResult
            {
                Data = discounts.PagedForCommand(command).Select(x =>
                {
                    var discountModel = x.ToModel();
                    discountModel.PrimaryStoreCurrencyCode = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode;
                    return(discountModel);
                }),
                Total = discounts.Count
            };

            return(Json(gridModel));
        }
        public virtual async Task <(IEnumerable <DiscountModel> discountModel, int totalCount)> PrepareDiscountModel(DiscountListModel model, int pageIndex, int pageSize)
        {
            DiscountType?discountType = null;

            if (model.SearchDiscountTypeId > 0)
            {
                discountType = (DiscountType)model.SearchDiscountTypeId;
            }
            var discounts = await _discountService.GetAllDiscounts(discountType, _workContext.CurrentCustomer.StaffStoreId,
                                                                   model.SearchDiscountCouponCode,
                                                                   model.SearchDiscountName,
                                                                   true);

            var items = new List <DiscountModel>();

            foreach (var x in discounts.Skip((pageIndex - 1) * pageSize).Take(pageSize))
            {
                var discountModel = x.ToModel(_dateTimeHelper);
                discountModel.DiscountTypeName         = x.DiscountType.GetLocalizedEnum(_localizationService, _workContext);
                discountModel.PrimaryStoreCurrencyCode = x.CalculateByPlugin ? "" : (await _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId)).CurrencyCode;
                discountModel.TimesUsed = (await _discountService.GetAllDiscountUsageHistory(x.Id, pageSize: 1)).TotalCount;
                items.Add(discountModel);
            }
            ;
            return(items, discounts.Count);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets all discounts for caching.
        /// </summary>
        /// <param name="discountType">Type of the discount.</param>
        /// <param name="couponCode">The coupon code.</param>
        /// <param name="discountName">Name of the discount.</param>
        /// <param name="showHidden">if set to <c>true</c> [show hidden].</param>
        /// <returns>System.String.</returns>
        /// <remarks>Guidebee IT</remarks>
        public static string GetAllDiscountsForCaching(DiscountType?discountType = null, string couponCode = "", string discountName = "", bool showHidden = false)
        {
            var discountService        = EngineContext.Current.Resolve <IDiscountService>();
            var discountForCachingCore = discountService.GetAllDiscountsForCaching(discountType, couponCode, discountName, showHidden);
            var discountForCaching     = discountForCachingCore.MapSource <DiscountForCaching, DiscountForCaching>(AutoMapperConfiguration.Mapper);

            return(JsonConvert.SerializeObject(discountForCaching, Formatting.Indented));
        }
Exemplo n.º 10
0
        public InsuranceHealth(int code, string name, DiscountType?discountType, decimal discount)
        {
            Code         = code;
            Name         = name;
            DiscountType = discountType ?? DiscountType.NoDiscount;
            Discount     = discount;

            Validate();
        }
Exemplo n.º 11
0
 public override IList <Discount> GetAllDiscounts(DiscountType?discountType = null,
                                                  string couponCode         = null, string discountName = null, bool showHidden = false,
                                                  DateTime?startDateUtc     = null, DateTime?endDateUtc = null)
 {
     return(_discounts
            .Where(x => !discountType.HasValue || x.DiscountType == discountType.Value)
            .Where(x => string.IsNullOrEmpty(couponCode) || x.CouponCode == couponCode)
            .ToList());
 }
Exemplo n.º 12
0
        /// <summary>
        /// Gets all discounts
        /// </summary>
        /// <param name="discountType">Discount type; pass null to load all records</param>
        /// <param name="couponCode">Coupon code to find (exact match); pass null or empty to load all records</param>
        /// <param name="discountName">Discount name; pass null or empty to load all records</param>
        /// <param name="showHidden">A value indicating whether to show expired and not started discounts</param>
        /// <param name="startDateUtc">Discount start date; pass null to load all records</param>
        /// <param name="endDateUtc">Discount end date; pass null to load all records</param>
        /// <returns>Discounts</returns>
        public virtual IList <Discount> GetAllDiscounts(DiscountType?discountType = null,
                                                        string couponCode         = null, string discountName = null, bool showHidden = false,
                                                        DateTime?startDateUtc     = null, DateTime?endDateUtc = null)
        {
            //we load all discounts, and filter them using "discountType" parameter later (in memory)
            //we do it because we know that this method is invoked several times per HTTP request with distinct "discountType" parameter
            //that's why let's access the database only once
            var cacheKey = _cacheKeyService.PrepareKeyForDefaultCache(NopDiscountDefaults.DiscountAllCacheKey
                                                                      , showHidden, couponCode ?? string.Empty, discountName ?? string.Empty);

            var query = _discountRepository.Table;

            if (!showHidden)
            {
                query = query.Where(discount =>
                                    (!discount.StartDateUtc.HasValue || discount.StartDateUtc <= DateTime.UtcNow) &&
                                    (!discount.EndDateUtc.HasValue || discount.EndDateUtc >= DateTime.UtcNow));
            }

            //filter by coupon code
            if (!string.IsNullOrEmpty(couponCode))
            {
                query = query.Where(discount => discount.CouponCode == couponCode);
            }

            //filter by name
            if (!string.IsNullOrEmpty(discountName))
            {
                query = query.Where(discount => discount.Name.Contains(discountName));
            }

            query = query.OrderBy(discount => discount.Name).ThenBy(discount => discount.Id);

            query = query.ToCachedList(cacheKey).AsQueryable();

            //we know that this method is usually invoked multiple times
            //that's why we filter discounts by type and dates on the application layer
            if (discountType.HasValue)
            {
                query = query.Where(discount => discount.DiscountType == discountType.Value);
            }

            //filter by dates
            if (startDateUtc.HasValue)
            {
                query = query.Where(discount =>
                                    !discount.StartDateUtc.HasValue || discount.StartDateUtc >= startDateUtc.Value);
            }
            if (endDateUtc.HasValue)
            {
                query = query.Where(discount =>
                                    !discount.EndDateUtc.HasValue || discount.EndDateUtc <= endDateUtc.Value);
            }

            return(query.ToList());
        }
Exemplo n.º 13
0
 public override IList <DiscountForCaching> GetAllDiscountsForCaching(DiscountType?discountType = null,
                                                                      string couponCode         = null, string discountName = null,
                                                                      bool showHidden           = false)
 {
     return(_discountForCaching
            .Where(x => !discountType.HasValue || x.DiscountType == discountType.Value)
            .Where(x => string.IsNullOrEmpty(couponCode) || x.CouponCode == couponCode)
            //UNDONE other filtering such as discountName, showHidden (not actually required in unit tests)
            .ToList());
 }
        /// <summary>
        /// Gets all discounts
        /// </summary>
        /// <param name="discountType">Discount type; null to load all discount</param>
        /// <param name="couponCode">Coupon code to find (exact match)</param>
        /// <param name="discountName">Discount name</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Discounts</returns>
        public virtual async Task <IList <Discount> > GetAllDiscounts(DiscountType?discountType,
                                                                      string storeId = "", string couponCode = "", string discountName = "", bool showHidden = false)
        {
            //we load all discounts, and filter them by passed "discountType" parameter later
            //we do it because we know that this method is invoked several times per HTTP request with distinct "discountType" parameter
            //that's why let's access the database only once
            string key    = string.Format(CacheKey.DISCOUNTS_ALL_KEY, showHidden, storeId, couponCode, discountName);
            var    result = await _cacheBase.GetAsync(key, () =>
            {
                var query = _discountRepository.Table;
                if (!showHidden)
                {
                    //The function 'CurrentUtcDateTime' is not supported by SQL Server Compact.
                    //That's why we pass the date value
                    var nowUtc = DateTime.UtcNow;
                    query      = query.Where(d =>
                                             (!d.StartDateUtc.HasValue || d.StartDateUtc <= nowUtc) &&
                                             (!d.EndDateUtc.HasValue || d.EndDateUtc >= nowUtc) &&
                                             d.IsEnabled);
                }
                if (!string.IsNullOrEmpty(storeId) && !_catalogSettings.IgnoreStoreLimitations)
                {
                    //Store mapping
                    query = from p in query
                            where !p.LimitedToStores || p.Stores.Contains(storeId)
                            select p;
                }
                if (!string.IsNullOrEmpty(couponCode))
                {
                    var _coupon = _discountCouponRepository.Table.FirstOrDefault(x => x.CouponCode == couponCode);
                    if (_coupon != null)
                    {
                        query = query.Where(d => d.Id == _coupon.DiscountId);
                    }
                }
                if (!string.IsNullOrEmpty(discountName))
                {
                    query = query.Where(d => d.Name != null && d.Name.ToLower().Contains(discountName.ToLower()));
                }
                query = query.OrderBy(d => d.Name);

                var discounts = query.ToListAsync();
                return(discounts);
            });

            //we know that this method is usually inkoved multiple times
            //that's why we filter discounts by type on the application layer
            if (discountType.HasValue)
            {
                result = result.Where(d => d.DiscountType == discountType.Value).ToList();
            }
            return(result);
        }
Exemplo n.º 15
0
 public IEnumerable <DiscountDTO> ListDiscountsOfCompany(DiscountType?discountType, int companyId)
 {
     using (UnitOfWorkProvider.Create())
     {
         var query = discountsOfCompanyQuery;
         query.ClearSortCriterias();
         query.Filter = new DiscountFilter {
             DiscountType = discountType, CompanyId = companyId
         };
         return(discountsOfCompanyQuery.Execute() ?? new List <DiscountDTO>());
     }
 }
Exemplo n.º 16
0
        /// <summary>
        /// Gets all discounts
        /// </summary>
        /// <returns>Discounts</returns>
        public virtual async Task <IList <Discount> > GetAllDiscounts(DiscountType?discountType,
                                                                      string storeId = "", string currencyCode = "", string couponCode = "", string discountName = "", bool showHidden = false)
        {
            string key    = string.Format(CacheKey.DISCOUNTS_ALL_KEY, showHidden, storeId, currencyCode, couponCode, discountName);
            var    result = await _cacheBase.GetAsync(key, async() =>
            {
                var query = from m in _discountRepository.Table
                            select m;
                if (!showHidden)
                {
                    var nowUtc = DateTime.UtcNow;
                    query      = query.Where(d =>
                                             (!d.StartDateUtc.HasValue || d.StartDateUtc <= nowUtc) &&
                                             (!d.EndDateUtc.HasValue || d.EndDateUtc >= nowUtc) &&
                                             d.IsEnabled);
                }
                if (!string.IsNullOrEmpty(storeId) && !CommonHelper.IgnoreStoreLimitations)
                {
                    //Store acl
                    query = from p in query
                            where !p.LimitedToStores || p.Stores.Contains(storeId)
                            select p;
                }
                if (!string.IsNullOrEmpty(couponCode))
                {
                    var _coupon = _discountCouponRepository.Table.FirstOrDefault(x => x.CouponCode == couponCode);
                    if (_coupon != null)
                    {
                        query = query.Where(d => d.Id == _coupon.DiscountId);
                    }
                }
                if (!string.IsNullOrEmpty(discountName))
                {
                    query = query.Where(d => d.Name != null && d.Name.ToLower().Contains(discountName.ToLower()));
                }
                if (!string.IsNullOrEmpty(currencyCode))
                {
                    query = query.Where(d => d.CurrencyCode == currencyCode);
                }
                query = query.OrderBy(d => d.Name);

                var discounts = await Task.FromResult(query.ToList());
                return(discounts);
            });

            if (discountType.HasValue)
            {
                result = result.Where(d => d.DiscountTypeId == discountType.Value).ToList();
            }
            return(result);
        }
Exemplo n.º 17
0
        public virtual IEnumerable <Discount> GetAllDiscounts(DiscountType?discountType, string couponCode = "", bool showHidden = false)
        {
            int?discountTypeId = null;

            if (discountType.HasValue)
            {
                discountTypeId = (int)discountType.Value;
            }

            // we load all discounts, and filter them by passed "discountType" parameter later
            // we do it because we know that this method is invoked several times per HTTP request with distinct "discountType" parameter
            // that's why we access the database only once
            string key    = string.Format(DISCOUNTS_ALL_KEY, showHidden, couponCode);
            var    result = _requestCache.Get(key, () =>
            {
                var query = _discountRepository.Table;

                if (!showHidden)
                {
                    // The function 'CurrentUtcDateTime' is not supported by SQL Server Compact.
                    // That's why we pass the date value
                    var nowUtc = DateTime.UtcNow.Date;
                    query      = query.Where(d =>
                                             (!d.StartDateUtc.HasValue || d.StartDateUtc <= nowUtc) &&
                                             (!d.EndDateUtc.HasValue || d.EndDateUtc >= nowUtc));
                }

                if (!String.IsNullOrWhiteSpace(couponCode))
                {
                    couponCode = couponCode.Trim();
                    query      = query.Where(d => d.CouponCode == couponCode);
                }

                query = query.OrderByDescending(d => d.Id);

                var discounts = query.ToList();

                var map = new Multimap <int, Discount>();
                discounts.Each(x => map.Add(x.DiscountTypeId, x));

                return(map);
            });

            if (discountTypeId > 0)
            {
                return(result[discountTypeId.Value]);
            }

            return(result.SelectMany(x => x.Value));
        }
Exemplo n.º 18
0
        /// <summary>
        /// Gets all discounts
        /// </summary>
        /// <param name="discountType">Discount type; null to load all discount</param>
        /// <param name="couponCode">Coupon code to find (exact match)</param>
        /// <param name="discountName">Discount name</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Discounts</returns>
        public virtual IList <Discount> GetAllDiscounts(DiscountType?discountType,
                                                        string couponCode = "", string discountName = "", bool showHidden = false)
        {
            //we load all discounts, and filter them by passed "discountType" parameter later
            //we do it because we know that this method is invoked several times per HTTP request with distinct "discountType" parameter
            //that's why let's access the database only once
            string key = string.Format(DISCOUNTS_ALL_KEY, showHidden, couponCode, discountName);



            //var result = _cacheManager.Get(key, () =>
            //{
            var query = _discountRepository.Table;

            if (!showHidden)
            {
                //The function 'CurrentUtcDateTime' is not supported by SQL Server Compact.
                //That's why we pass the date value
                var nowUtc = DateTime.UtcNow;
                query = query.Where(d =>
                                    (!d.StartDateUtc.HasValue || d.StartDateUtc <= nowUtc) &&
                                    (!d.EndDateUtc.HasValue || d.EndDateUtc >= nowUtc)
                                    );
            }
            if (!String.IsNullOrEmpty(couponCode))
            {
                query = query.Where(d => d.CouponCode != null && d.CouponCode.ToLower().Contains(couponCode.ToLower()));
            }
            if (!String.IsNullOrEmpty(discountName))
            {
                query = query.Where(d => d.Name != null && d.Name.ToLower().Contains(discountName.ToLower()));
            }
            query = query.OrderBy(d => d.Name);

            var discounts = query.ToList();
            var result    = discounts;

            //return discounts;
            //});



            //we know that this method is usually inkoved multiple times
            //that's why we filter discounts by type on the application layer
            if (discountType.HasValue)
            {
                result = result.Where(d => d.DiscountType == discountType.Value).ToList();
            }
            return(result);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Gets all discounts
        /// </summary>
        /// <param name="discountType">Discount type; pass null to load all records</param>
        /// <param name="couponCode">Coupon code to find (exact match); pass null or empty to load all records</param>
        /// <param name="discountName">Discount name; pass null or empty to load all records</param>
        /// <param name="showHidden">A value indicating whether to show expired and not started discounts</param>
        /// <param name="startDateUtc">Discount start date; pass null to load all records</param>
        /// <param name="endDateUtc">Discount end date; pass null to load all records</param>
        /// <returns>Discounts</returns>
        public virtual IList <Discount> GetAllDiscounts(DiscountType?discountType = null,
                                                        string couponCode         = null, string discountName = null, bool showHidden = false,
                                                        DateTime?startDateUtc     = null, DateTime?endDateUtc = null)
        {
            var query = _discountRepository.Table;

            if (!showHidden)
            {
                //The function 'CurrentUtcDateTime' is not supported by SQL Server Compact, that's why we pass the date value
                var nowUtc = DateTime.UtcNow;
                query = query.Where(discount =>
                                    (!discount.StartDateUtc.HasValue || discount.StartDateUtc <= nowUtc) &&
                                    (!discount.EndDateUtc.HasValue || discount.EndDateUtc >= nowUtc));
            }

            //filter by dates
            if (startDateUtc.HasValue)
            {
                query = query.Where(discount => !discount.StartDateUtc.HasValue || discount.StartDateUtc >= startDateUtc.Value);
            }
            if (endDateUtc.HasValue)
            {
                query = query.Where(discount => !discount.EndDateUtc.HasValue || discount.EndDateUtc <= endDateUtc.Value);
            }

            //filter by coupon code
            if (!string.IsNullOrEmpty(couponCode))
            {
                query = query.Where(discount => discount.CouponCode == couponCode);
            }

            //filter by name
            if (!string.IsNullOrEmpty(discountName))
            {
                query = query.Where(discount => discount.Name.Contains(discountName));
            }

            //filter by type
            if (discountType.HasValue)
            {
                query = query.Where(discount => discount.DiscountTypeId == (int)discountType.Value);
            }

            query = query.OrderBy(discount => discount.Name).ThenBy(discount => discount.Id);

            return(query.ToList());
        }
Exemplo n.º 20
0
        /// <summary>
        /// Gets all discounts
        /// </summary>
        /// <param name="discountType">Discount type; pass null to load all records</param>
        /// <param name="couponCode">Coupon code to find (exact match); pass null or empty to load all records</param>
        /// <param name="discountName">Discount name; pass null or empty to load all records</param>
        /// <param name="showHidden">A value indicating whether to show expired and not started discounts</param>
        /// <param name="startDateUtc">Discount start date; pass null to load all records</param>
        /// <param name="endDateUtc">Discount end date; pass null to load all records</param>
        /// <returns>Discounts</returns>
        public virtual IList <Discount> GetAllDiscounts(DiscountType?discountType = null,
                                                        string couponCode         = null, string discountName = null, bool showHidden = false,
                                                        DateTime?startDateUtc     = null, DateTime?endDateUtc = null)
        {
            var query = _discountRepository.Table;

            if (!showHidden)
            {
                query = query.Where(discount =>
                                    (!discount.StartDateUtc.HasValue || discount.StartDateUtc <= DateTime.UtcNow) &&
                                    (!discount.EndDateUtc.HasValue || discount.EndDateUtc >= DateTime.UtcNow));
            }

            //filter by dates
            if (startDateUtc.HasValue)
            {
                query = query.Where(discount => !discount.StartDateUtc.HasValue || discount.StartDateUtc >= startDateUtc.Value);
            }
            if (endDateUtc.HasValue)
            {
                query = query.Where(discount => !discount.EndDateUtc.HasValue || discount.EndDateUtc <= endDateUtc.Value);
            }

            //filter by coupon code
            if (!string.IsNullOrEmpty(couponCode))
            {
                query = query.Where(discount => discount.CouponCode == couponCode);
            }

            //filter by name
            if (!string.IsNullOrEmpty(discountName))
            {
                query = query.Where(discount => discount.Name.Contains(discountName));
            }

            //filter by type
            if (discountType.HasValue)
            {
                query = query.Where(discount => discount.DiscountTypeId == (int)discountType.Value);
            }

            query = query.OrderBy(discount => discount.Name).ThenBy(discount => discount.Id);

            return(query.ToList());
        }
Exemplo n.º 21
0
        public virtual IEnumerable <Discount> GetAllDiscounts(DiscountType?discountType, string couponCode = "", bool showHidden = false)
        {
            var discountTypeId = discountType.HasValue ? (int)discountType.Value : 0;

            // We load all discounts and filter them by passed "discountType" parameter later because
            // this method is invoked several times per HTTP request with distinct "discountType" parameter.
            var key    = string.Format(DISCOUNTS_ALL_KEY, showHidden, couponCode);
            var result = _requestCache.Get(key, () =>
            {
                var query = _discountRepository.Table;

                if (!showHidden)
                {
                    var utcNow = DateTime.UtcNow;

                    query = query.Where(d =>
                                        (!d.StartDateUtc.HasValue || d.StartDateUtc <= utcNow) &&
                                        (!d.EndDateUtc.HasValue || d.EndDateUtc >= utcNow));
                }

                if (!string.IsNullOrWhiteSpace(couponCode))
                {
                    couponCode = couponCode.Trim();
                    query      = query.Where(d => d.CouponCode == couponCode);
                }

                query = query.OrderByDescending(d => d.Id);

                var discounts = query.ToList();

                var map = new Multimap <int, Discount>();
                discounts.Each(x => map.Add(x.DiscountTypeId, x));

                return(map);
            });

            if (discountTypeId > 0)
            {
                return(result[discountTypeId]);
            }

            return(result.SelectMany(x => x.Value));
        }
Exemplo n.º 22
0
        /// <summary>
        /// NOP 3.826
        /// </summary>
        /// <returns></returns>
        public ActionResult ExportXlsx()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            DiscountType?discountType = null;

            try
            {
                var bytes = _exportManager.ExportDiscountsToXlsx(_discountService.GetAllDiscounts(discountType: discountType, showHidden: true));

                return(File(bytes, MimeTypes.TextXlsx, "discounts.xlsx"));
            }
            catch (Exception exc)
            {
                ErrorNotification(exc);
                return(RedirectToAction("List"));
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// NOP 3.826
        /// </summary>
        /// <returns></returns>
        public ActionResult ExportXml()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            DiscountType?discountType = null;

            try
            {
                var discounts = _discountService.GetAllDiscounts(discountType: discountType, showHidden: true);
                var xml       = _exportManager.ExportDiscountsToXml(discounts);
                return(new XmlDownloadResult(xml, "discounts.xml"));
            }
            catch (Exception exc)
            {
                ErrorNotification(exc);
                return(RedirectToAction("List"));
            }
        }
Exemplo n.º 24
0
        /// <summary>
        /// Gets all discounts (cachable models)
        /// </summary>
        /// <param name="discountType">Discount type; null to load all discount</param>
        /// <param name="couponCode">Coupon code to find (exact match)</param>
        /// <param name="discountName">Discount name</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Discounts</returns>
        public virtual IList <DiscountForCaching> GetAllDiscountsForCaching(DiscountType?discountType = null,
                                                                            string couponCode         = "", string discountName = "", bool showHidden = false)
        {
            //we cache discounts between requests. Otherwise, they will be loaded for almost each HTTP request
            //we have to use the following workaround with cachable model (DiscountForCaching) because
            //Entity Framework doesn't support 2-level caching

            //we load all discounts, and filter them using "discountType" parameter later (in memory)
            //we do it because we know that this method is invoked several times per HTTP request with distinct "discountType" parameter
            //that's why let's access the database only once
            var discounts = GetAllDiscounts(null, couponCode, discountName, showHidden);
            var result    = discounts.Select(d => d.MapDiscount()).ToList();;

            //we know that this method is usually inkoved multiple times
            //that's why we filter discounts by type on the application layer
            if (discountType.HasValue)
            {
                result = result.Where(d => d.DiscountType == discountType.Value).ToList();
            }
            return(result);
        }
        public virtual (IEnumerable <DiscountModel> discountModel, int totalCount) PrepareDiscountModel(DiscountListModel model, int pageIndex, int pageSize)
        {
            DiscountType?discountType = null;

            if (model.SearchDiscountTypeId > 0)
            {
                discountType = (DiscountType)model.SearchDiscountTypeId;
            }
            var discounts = _discountService.GetAllDiscounts(discountType,
                                                             model.SearchDiscountCouponCode,
                                                             model.SearchDiscountName,
                                                             true);

            return(discounts.Skip((pageIndex - 1) * pageSize).Take(pageSize).Select(x =>
            {
                var discountModel = x.ToModel();
                discountModel.DiscountTypeName = x.DiscountType.GetLocalizedEnum(_localizationService, _workContext);
                discountModel.PrimaryStoreCurrencyCode = x.CalculateByPlugin ? "" : _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode;
                discountModel.TimesUsed = _discountService.GetAllDiscountUsageHistory(x.Id, pageSize: 1).TotalCount;
                return discountModel;
            }), discounts.Count);
        }
Exemplo n.º 26
0
 /// <summary>
 /// Gets discounts of specific company
 /// </summary>
 /// <param name="companyId">id of company</param>
 /// <param name="discountType">type of discount</param>
 /// <returns></returns>
 public IEnumerable <DiscountDTO> ListDiscountsOfCompany(DiscountType?discountType, int companyId)
 {
     return(discountService.ListDiscountsOfCompany(discountType, companyId));
 }
Exemplo n.º 27
0
 /// <summary>
 /// Gets all discounts (cachable models)
 /// </summary>
 /// <param name="discountType">Discount type; null to load all discount</param>
 /// <param name="couponCode">Coupon code to find (exact match)</param>
 /// <param name="discountName">Discount name</param>
 /// <param name="showHidden">A value indicating whether to show hidden records</param>
 /// <returns>Discounts</returns>
 public IList <DiscountForCaching> GetAllDiscountsForCaching(DiscountType?discountType = null,
                                                             string couponCode         = "", string discountName = "", bool showHidden = false)
 {
     return(_discountService.GetAllDiscountsForCaching(discountType, couponCode, discountName, showHidden));
 }
Exemplo n.º 28
0
        /// <summary>
        /// TODO: (mh) (core) Add documentation.
        /// </summary>
        ///
        public async Task <IActionResult> AllDiscountsAsync(string label, string selectedIds, DiscountType?type)
        {
            var discounts = await _db.Discounts
                            .AsNoTracking()
                            .Where(x => x.DiscountTypeId == (int)type)
                            .ToListAsync();

            var selectedArr = selectedIds.ToIntArray();

            if (label.HasValue())
            {
                discounts.Insert(0, new Discount {
                    Name = label, Id = 0
                });
            }

            var data = discounts
                       .Select(x => new ChoiceListItem
            {
                Id       = x.Id.ToString(),
                Text     = x.Name,
                Selected = selectedArr.Contains(x.Id)
            })
                       .ToList();

            return(new JsonResult(data));
        }