Example #1
0
            private static Dictionary <string, IEnumerable <PriceLine> > PriceAdjustmentsToPriceLines(
                IEnumerable <SalesLine> salesLines,
                IDictionary <string, IList <PriceAdjustment> > priceAdjustments)
            {
                var itemPriceLines = new Dictionary <string, IEnumerable <PriceLine> >(StringComparer.OrdinalIgnoreCase);

                foreach (var item in salesLines)
                {
                    IList <PriceAdjustment> itemPriceAdjustments;
                    if (!priceAdjustments.TryGetValue(item.LineId, out itemPriceAdjustments))
                    {
                        itemPriceAdjustments = new PriceAdjustment[0];
                    }

                    var promoPrices = PriceAdjustmentsToPriceLines(itemPriceAdjustments);

                    // add set of price lines to the item map
                    if (!itemPriceLines.ContainsKey(item.LineId))
                    {
                        itemPriceLines.Add(item.LineId, promoPrices);
                    }
                }

                return(itemPriceLines);
            }
        public async Task ValidateChangePriceItem_RuleSetStrict_Valid()
        {
            // Arrange
            var aggregate = GetValidCartAggregate();
            var item      = _fixture.Create <LineItem>();

            item.IsGift          = false;
            aggregate.Cart.Items = new List <LineItem> {
                item
            };

            var newItemPrice = new PriceAdjustment
            {
                LineItemId = item.Id,
                NewPrice   = item.ListPrice + _fixture.Create <decimal>()
            };
            var validator = new ChangeCartItemPriceValidator();

            // Act
            var result = await validator.ValidateAsync(newItemPrice, options => options.IncludeRuleSets("strict"));

            // Assert
            result.IsValid.Should().BeTrue();
            result.Errors.Should().BeEmpty();
        }
        public async Task ValidateChangePriceItem_RuleSetStrict_Invalid()
        {
            // Arrange
            var aggregate = GetValidCartAggregate();
            var item      = _fixture.Create <LineItem>();

            item.IsGift          = false;
            aggregate.Cart.Items = new List <LineItem> {
                item
            };

            var newItemPrice = new PriceAdjustment
            {
                LineItemId = item.Id,
                LineItem   = item,
                NewPrice   = item.ListPrice - _fixture.Create <decimal>()
            };
            var validator = new ChangeCartItemPriceValidator();

            // Act
            var result = await validator.ValidateAsync(newItemPrice, options => options.IncludeRuleSets("strict"));

            // Assert
            result.IsValid.Should().BeFalse();
            result.Errors.Should().NotBeEmpty();
            result.Errors.Should().HaveCount(1);
            result.Errors.Should().Contain(x => x.ErrorCode == "UNABLE_SET_LESS_PRICE");
        }
Example #4
0
            private static bool IsMatchUnitOfMeasure(SalesLine line, PriceAdjustment adjustment)
            {
                bool isMatch = string.IsNullOrWhiteSpace(adjustment.UnitOfMeasure);

                if (!isMatch)
                {
                    isMatch = string.Equals(adjustment.UnitOfMeasure, line.SalesOrderUnitOfMeasure, StringComparison.OrdinalIgnoreCase);
                }

                return(isMatch);
            }
            public override string GetItemLabel()
            {
                var label = Name;

                if (PriceAdjustment.HasValue())
                {
                    label += " ({0})".FormatWith(PriceAdjustment);
                }

                return(label);
            }
Example #6
0
        public async Task <ActionResult> AdjustPrice(string id, AdjustPrice adjustPrice)
        {
            var rental             = GetRental(id);
            var adjustment         = new PriceAdjustment(adjustPrice, rental.Price);
            var modificationUpdate = Builders <Rental> .Update
                                     .Push(r => r.Adjustments, adjustment)
                                     .Set(r => r.Price, adjustPrice.NewPrice);

            //Context.Rentals.Update(Query.EQ("_id", new ObjectId(id)), modificationUpdate);
            await context.Rentals.UpdateOneAsync(r => r.Id == id, modificationUpdate);

            return(RedirectToAction("Index", "Rentals"));
        }
Example #7
0
        public async Task <ActionResult> AdjustPriceWithModifications(string id, AdjustPrice adjustPrice)
        //public async Task<ActionResult> AdjustPrice(string id, AdjustPrice adjustPrice)
        {
            var rental       = GetRental(id);
            var adjustment   = new PriceAdjustment(adjustPrice, rental.Price);
            var modification = Builders <Rental> .Update
                               .Push(r => r.Adjustments, adjustment)
                               .Set(r => r.Price, adjustPrice.NewPrice);

            await ContextNew.Rentals.UpdateOneAsync(r => r.Id == id, modification);

            //Context.Rentals.Save(rental);
            return(RedirectToAction("Index"));
        }
Example #8
0
        public async Task ValidateChangePriceItem_RuleSetDefault_Valid()
        {
            // Arrange
            var aggregate    = GetValidCartAggregate();
            var newItemPrice = new PriceAdjustment(_fixture.Create <string>(), _fixture.Create <decimal>());
            var validator    = new ChangeCartItemPriceValidator(aggregate);

            // Act
            var result = await validator.ValidateAsync(newItemPrice, ruleSet : "default");

            // Assert
            result.IsValid.Should().BeTrue();
            result.Errors.Should().BeEmpty();
        }
Example #9
0
            private static bool IsAdjustmentActiveOnSalesLine(
                SalesLine line,
                PriceAdjustment adjustment,
                DateTimeOffset defaultDate)
            {
                var activeDate = line.SalesDate ?? defaultDate;

                return(InternalValidationPeriod.ValidateDateAgainstValidationPeriod(
                           (DateValidationType)adjustment.DateValidationType,
                           adjustment.ValidationPeriod,
                           adjustment.ValidFromDate,
                           adjustment.ValidToDate,
                           activeDate));
            }
Example #10
0
        public ActionResult AdjustPriceUsingModifications(string id, AdjustPrice adjustPrice)
        {
            var rental     = GetRental(id);
            var adjustment = new PriceAdjustment(adjustPrice, rental.Price);
            //var modificationUpdate = Update<Rental>
            //    .Push(r => r.Adjustments, adjustment)
            //    .Set(r => r.Price, adjustment.NewPrice);
            var filter = Builders <Rental> .Filter.Eq(s => s.Id, id);

            var updateDefinition = Builders <Rental> .Update.Push(r => r.Adjustments, adjustment)
                                   .Set(r => r.Price, adjustment.NewPrice);

            context.Rentals.UpdateOne(filter, updateDefinition);
            return(RedirectToAction("Index", "Rentals"));
        }
Example #11
0
        public void ModificationUpdateOfRentalAdjustPrice()
        {
            var rental = new Rental {
                Price = 100
            };
            var adjustPrice = new AdjustPrice {
                NewPrice = 200, Reason = "Charge more!!!"
            };
            var adjustment = new PriceAdjustment(adjustPrice, rental.Price);

            var modificationUpdate = Update <Rental>
                                     .Push(r => r.Adjustments, adjustment)
                                     .Set(r => r.Price, adjustPrice.NewPrice);

            Console.WriteLine(modificationUpdate);
        }
Example #12
0
            public override string GetItemLabel()
            {
                var label = Name;

                if (QuantityInfo > 1)
                {
                    label = "{0} x {1}".FormatCurrentUI(QuantityInfo, label);
                }

                if (PriceAdjustment.HasValue())
                {
                    label += " ({0})".FormatWith(PriceAdjustment);
                }

                return(label);
            }
        public async Task ValidateChangePriceItem_RuleSetDefault_Valid()
        {
            // Arrange

            var newItemPriceAdjustment = new PriceAdjustment
            {
                LineItemId = _fixture.Create <string>(),
                NewPrice   = _fixture.Create <decimal>()
            };
            var validator = new ChangeCartItemPriceValidator();

            // Act
            var result = await validator.ValidateAsync(newItemPriceAdjustment, options => options.IncludeRuleSets("default"));

            // Assert
            result.IsValid.Should().BeTrue();
            result.Errors.Should().BeEmpty();
        }
Example #14
0
        public ActionResult AdjustPrice(string id, AdjustPrice adjustPrice)
        {
            var rental = Context.Rentals.Find(x => x.Id == id).FirstOrDefault();

            //UPDATE - TYPE: REPLACE DOCUMENT
            //rental.AdjustPrice(adjustPrice);
            //Context.Rentals.ReplaceOne(x => x.Id == rental.Id, rental); //here I am replacing the whole document for a new version of the document.

            //UPDATE - TYPE: MODIFY DOCUMENT
            var adjustment = new PriceAdjustment(adjustPrice, rental.Price);
            //here we are adding the adjustment to the collection and updating the field Price to a new number
            var updateDefinition = new UpdateDefinitionBuilder <Rental>().Push(x => x.Adjustments, adjustment).Set(x => x.Price, adjustPrice.NewPrice);
            //here we actually execute the update.
            var updateResult = Context.Rentals.UpdateOne(x => x.Id == rental.Id, updateDefinition);

            //NOTE: GENERALY IN OOP IS USED REPLACEMENT INSTEAD OF MODIFICATION. ALTHOUGH MOFICATION HAS BETTER PERFORMANCE

            return(RedirectToAction("Index"));
        }
Example #15
0
        public async Task ValidateChangePriceItem_RuleSetDefault_Invalid()
        {
            // Arrange
            var aggregate = GetValidCartAggregate();

            var newItemPrice = new PriceAdjustment(null, -1);

            // Act
            var validator = new ChangeCartItemPriceValidator(aggregate);
            var result    = await validator.ValidateAsync(newItemPrice, ruleSet : "default");

            // Assert
            result.IsValid.Should().BeFalse();
            result.Errors.Should().NotBeEmpty();
            result.Errors.Should().HaveCount(3);
            result.Errors.Should().Contain(x => x.PropertyName == "NewPrice" && x.ErrorCode == nameof(GreaterThanOrEqualValidator));
            result.Errors.Should().Contain(x => x.PropertyName == "LineItemId" && x.ErrorCode == nameof(NotEmptyValidator));
            result.Errors.Should().Contain(x => x.PropertyName == "LineItemId" && x.ErrorCode == nameof(NotNullValidator));
        }
        public async Task ValidateChangePriceItem_RuleSetDefault_Invalid()
        {
            var newItemPrice = new PriceAdjustment()
            {
                NewPrice = -1
            };

            // Act
            var validator = new ChangeCartItemPriceValidator();
            var result    = await validator.ValidateAsync(newItemPrice, options => options.IncludeRuleSets("default"));

            // Assert
            result.IsValid.Should().BeFalse();
            result.Errors.Should().NotBeEmpty();
            result.Errors.Should().HaveCount(3);
            result.Errors.Should().Contain(x => x.PropertyName == "NewPrice" && x.ErrorCode.Contains("GreaterThanOrEqualValidator"));
            result.Errors.Should().Contain(x => x.PropertyName == "LineItemId" && x.ErrorCode.Contains("NotEmptyValidator"));
            result.Errors.Should().Contain(x => x.PropertyName == "LineItemId" && x.ErrorCode.Contains("NotNullValidator"));
        }
Example #17
0
        private IEnumerable <IShippingQueryParameter> CreateAdditionalParameters(ShippingMethodDto shippingMethod)
        {
            var hasEdi = bool.Parse(shippingMethod.GetShippingMethodParameterValue(ParameterNames.Edi, "true"));

            yield return(new Edi(hasEdi));

            var shippedFromPostOffice =
                bool.Parse(shippingMethod.GetShippingMethodParameterValue(ParameterNames.PostingAtPostOffice, "false"));

            yield return(new ShippedFromPostOffice(shippedFromPostOffice));

            int priceAdjustmentPercent;

            int.TryParse(shippingMethod.GetShippingMethodParameterValue(ParameterNames.PriceAdjustmentPercent, "0"), out priceAdjustmentPercent);

            if (priceAdjustmentPercent > 0)
            {
                bool priceAdjustmentAdd;
                bool.TryParse(shippingMethod.GetShippingMethodParameterValue(ParameterNames.PriceAdjustmentOperator, "true"), out priceAdjustmentAdd);

                yield return(priceAdjustmentAdd ? PriceAdjustment.IncreasePercent(priceAdjustmentPercent) : PriceAdjustment.DecreasePercent(priceAdjustmentPercent));
            }

            var productCode = shippingMethod.GetShippingMethodParameterValue(ParameterNames.BringProductId, null)
                              ?? Product.Servicepakke.Code;

            yield return(new Products(Product.GetByCode(productCode)));

            var customerNumber = shippingMethod.GetShippingMethodParameterValue(ParameterNames.BringCustomerNumber, null);

            if (!string.IsNullOrWhiteSpace(customerNumber))
            {
                yield return(new CustomerNumber(customerNumber));
            }

            var additionalServicesCodes = shippingMethod.GetShippingMethodParameterValue(ParameterNames.AdditionalServices);
            var services = additionalServicesCodes.Split(',')
                           .Select(code => AdditionalService.All.FirstOrDefault(x => x.Code == code))
                           .Where(service => service != null);

            yield return(new AdditionalServices(services.ToArray()));
        }
Example #18
0
        public async Task ValidateChangePriceItem_RuleSetStrict_Valid()
        {
            // Arrange
            var aggregate = GetValidCartAggregate();
            var item      = _fixture.Create <LineItem>();

            aggregate.Cart.Items = new List <LineItem> {
                item
            };

            var newItemPrice = new PriceAdjustment(item.Id, item.ListPrice + _fixture.Create <decimal>());
            var validator    = new ChangeCartItemPriceValidator(aggregate);

            // Act
            var result = await validator.ValidateAsync(newItemPrice, ruleSet : "strict");

            // Assert
            result.IsValid.Should().BeTrue();
            result.Errors.Should().BeEmpty();
        }
        public async Task <ActionResult> AdjustPrice(string id, AdjustPrice adjustPrice)
        {
            var rental = await GetRental(id);

            /*
             * rental.AdjustPrice(adjustPrice);
             *
             * await Context.Rentals.ReplaceOneAsync<Rental>(r => r.Id == id, rental);
             */

            //****************Update document partially ***********************************/
            var adjustment = new PriceAdjustment(adjustPrice, rental.Price);

            var update = Builders <Rental> .Update.Set(r => r.Price, adjustPrice.NewPrice)
                         .Push(r => r.Adjustments, adjustment);

            await Context.Rentals.UpdateOneAsync <Rental>(r => r.Id == id, update);

            /******************************************************************************/

            return(RedirectToAction("index"));
        }
Example #20
0
        public override async Task <CartAggregate> Handle(ChangeCartItemPriceCommand request, CancellationToken cancellationToken)
        {
            var cartAggregate = await GetOrCreateCartFromCommandAsync(request);

            if (cartAggregate == null)
            {
                var tcs = new TaskCompletionSource <CartAggregate>();
                tcs.SetException(new OperationCanceledException("Cart not found!"));
                return(await tcs.Task);
            }

            var lineItem        = cartAggregate.Cart.Items.FirstOrDefault(x => x.Id.Equals(request.LineItemId));
            var priceAdjustment = new PriceAdjustment
            {
                LineItem   = lineItem,
                LineItemId = request.LineItemId,
                NewPrice   = request.Price
            };

            await cartAggregate.ChangeItemPriceAsync(priceAdjustment);

            return(await SaveCartAsync(cartAggregate));
        }
Example #21
0
 private static bool IsMatchCurrency(string storeCurrencyCode, PriceAdjustment adjustment)
 {
     return(string.IsNullOrEmpty(adjustment.CurrencyCode) ? false : string.Equals(storeCurrencyCode, adjustment.CurrencyCode, StringComparison.OrdinalIgnoreCase));
 }