/// <summary>
        /// Attempts to add the coupons discounts to the invoice
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The <see cref="Attempt"/>.
        /// </returns>
        public override Attempt<IInvoice> PerformTask(IInvoice value)
        {
            // check if there are any coupon offers
            if (!SalePreparation.OfferCodes.Any()) return Attempt<IInvoice>.Succeed(value);

            if (!(SalePreparation is IBasketSalePreparation))
                return Attempt<IInvoice>.Fail(value, new InvalidCastException("SalePreparation object is not IBasketSalePreparation"));
            _basketSalePreparation = SalePreparation as IBasketSalePreparation;

            foreach (var code in SalePreparation.OfferCodes)
            {
                var foundCoupon = CouponOfferManager.GetByOfferCode(code, SalePreparation.Customer);
                if (!foundCoupon.Success)
                {
                    continue;
                }

                var coupon = foundCoupon.Result;
                var clone = LineItemExtensions.CreateNewItemCacheLineItemContainer(value.Items.Where(x => x.LineItemType != LineItemType.Tax));
                var apply = coupon.TryApply(clone, this.SalePreparation.Customer).AsCouponRedemptionResult(coupon);
                if (apply.Success)
                {
                    this.CouponOfferManager.SafeAddCouponAttemptContainer<InvoiceLineItem>(value, apply, true);
                }
            }

            return Attempt<IInvoice>.Succeed(value);
        }
Beispiel #2
0
        /// <summary>
        /// Attempts to add the coupons discounts to the invoice
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The <see cref="Attempt"/>.
        /// </returns>
        public override Attempt <IInvoice> PerformTask(IInvoice value)
        {
            // check if there are any coupon offers
            if (!this.SalePreparation.OfferCodes.Any())
            {
                return(Attempt <IInvoice> .Succeed(value));
            }

            if (!(this.SalePreparation is IBasketSalePreparation))
            {
                return(Attempt <IInvoice> .Fail(value, new InvalidCastException("SalePreparation object is not IBasketSalePreparation")));
            }
            this._basketSalePreparation = this.SalePreparation as IBasketSalePreparation;

            foreach (var code in this.SalePreparation.OfferCodes)
            {
                var foundCoupon = this.CouponOfferManager.GetByOfferCode(code, this.SalePreparation.Customer);
                if (!foundCoupon.Success)
                {
                    continue;
                }

                var coupon = foundCoupon.Result;
                var clone  = LineItemExtensions.CreateNewItemCacheLineItemContainer(value.Items.Where(x => x.LineItemType != LineItemType.Tax));
                var apply  = coupon.TryApply(clone, this.SalePreparation.Customer).AsCouponRedemptionResult(coupon);
                if (apply.Success)
                {
                    this.CouponOfferManager.SafeAddCouponAttemptContainer <InvoiceLineItem>(value, apply, true);
                }
            }

            return(Attempt <IInvoice> .Succeed(value));
        }