Ejemplo n.º 1
0
        public async Task <ActionResult <IEnumerable <OrderChargeResponse> > > ReverseCalculate(OrderRequest requestDto)
        {
            var currency = new Currency(requestDto.Currency);

            var orderItems = requestDto.OrderItems.Select(oi =>
                                                          new OrderItem(
                                                              quantity: new Quantity(oi.Quantity),
                                                              weight: Weight.InKilograms(oi.Weight),
                                                              vatRate: new Rate(oi.VatRate),
                                                              dutyRate: new Rate(oi.DutyRate),
                                                              inputPrice: oi.Price))
                             .ToList();

            var order = new Order(
                country: new Country(requestDto.DeclarationCountry),
                currency: new Currency(requestDto.Currency),
                orderItems: orderItems,
                deliveryPrice: requestDto.DeliveryPrice);

            var config               = _options.Single(x => x.Id == requestDto.DeclarationCountry);
            var baseCharges          = config.DeminimisBaseCharges.Select(chargeName => new ChargeName(chargeName));
            var chargeConfigurations = config.ChargeConfigurations.Select(config => ChargeConfigurationFactory.CreateFromOptions(config));

            var calculatorConfiguration = new CalculatorConfiguration(chargeConfigurations, baseCharges);

            var request  = new ReverseCalculatorRequest(order, calculatorConfiguration);
            var response = await _mediator.Send(request);

            return(new OkObjectResult(response));
        }
        public void Create_MultipleCalculators_ShouldCorrectlyCreateCalculator()
        {
            // Arrange
            var chargeConfiguration1 = ChargeConfigurationFactory.CreateFromOptions(new ChargeConfigurationOptions
            {
                CalculationType    = CalculationType.Fixed,
                DeminimisThreshold = "EUR0",
                FixedChargeAmount  = "EUR10",
                ChargeName         = ChargeNames.Duty,
                Rate = 5
            });
            var chargeConfiguration2 = ChargeConfigurationFactory.CreateFromOptions(new ChargeConfigurationOptions
            {
                CalculationType    = CalculationType.RateBased,
                DeminimisThreshold = "EUR0",
                BaseCharges        = { "Item", ChargeNames.Duty },
                ChargeName         = ChargeNames.Vat,
                Rate = 5
            });
            var chargeConfiguration3 = ChargeConfigurationFactory.CreateFromOptions(new ChargeConfigurationOptions
            {
                CalculationType    = CalculationType.WeightBased,
                DeminimisThreshold = "EUR0",
                ChargeName         = "Fee",
                Rate = 5
            });

            var calculationRange = new CalculationRange(new Price("EUR100"), new List <ChargeConfiguration> {
                chargeConfiguration1, chargeConfiguration2, chargeConfiguration3
            });

            // Act
            var calculator = ForwardCalculatorFactory.Create(calculationRange).GetInvocationList();

            // Assert
            calculator.Should().HaveCount(3);
            calculator[0].Method.DeclaringType.Should().Be(typeof(FixedChargeCalculator));
            calculator[1].Method.DeclaringType.Should().Be(typeof(RateBasedChargeCalculator));
            calculator[2].Method.DeclaringType.Should().Be(typeof(WeightBasedChargeCalculator));
        }