Exemple #1
0
 public async Task CreateInvoiceForChangedOrder(OrderDTO oldOrder, OrderDTO newOrder)
 {
     // discount or VAT changed, generate credit note and new invoice
     if (oldOrder.TotalPrice.VatRate != newOrder.TotalPrice.VatRate)
     {
         await CreateCreditNote(oldOrder, oldOrder.TotalPrice);
         await CreateInvoice(newOrder, newOrder.TotalPrice);
     }
     else if (oldOrder.TotalPrice.Price < newOrder.TotalPrice.Price)
     {
         // generate an invoice for the amount difference
         var difference = new PriceDataDTO()
         {
             VatRate      = newOrder.TotalPrice.Price,
             BasePrice    = newOrder.TotalPrice.BasePrice - oldOrder.TotalPrice.BasePrice,
             Price        = newOrder.TotalPrice.Price - oldOrder.TotalPrice.Price,
             PriceInclVat = newOrder.TotalPrice.Price - oldOrder.TotalPrice.Price
         };
         await CreateInvoice(newOrder, difference);
     }
     else if (oldOrder.TotalPrice.Price > newOrder.TotalPrice.Price)
     {
         // generate a credit note for the amount difference
         var difference = new PriceDataDTO()
         {
             VatRate      = newOrder.TotalPrice.Price,
             BasePrice    = oldOrder.TotalPrice.BasePrice - newOrder.TotalPrice.BasePrice,
             Price        = oldOrder.TotalPrice.Price - newOrder.TotalPrice.Price,
             PriceInclVat = oldOrder.TotalPrice.Price - newOrder.TotalPrice.Price
         };
         await CreateCreditNote(newOrder, difference);
     }
 }
        private decimal CalculateDiscountPrice(DiscountCodeRule rule, PriceDataDTO orderItemPrice)
        {
            decimal discountPrice = 0;

            if (rule.DiscountAmount != null)
            {
                discountPrice = rule.DiscountAmount.Value;
            }
            else if (rule.DiscountPercent != null)
            {
                discountPrice = OrderPriceCalculationFacade.Round(orderItemPrice.BasePrice * rule.DiscountPercent.Value / 100m);
            }

            return(discountPrice);
        }
        public async Task<CalculateOrderResultDTO> CalculatePriceForOrderAndItems(EventDTO eventData, CalculateOrderDTO order, bool invalidateDiscountCoupon = false)
        {
            var now = dateTimeProvider.Now;
            
            // calculate prices for order items
            var orderItemPrices = CalculateItemPrices(eventData, order, now);

            // validate there is only one currency
            if (orderItemPrices.Select(p => p.CurrencyCode).Distinct().Count() > 1)
            {
                throw new OrderItemsMustUseTheSameCurrencyException();
            }

            // apply discount
            if (!string.IsNullOrEmpty(order.DiscountCode))
            {
                await orderDiscountFacade.ApplyDiscount(eventData, order, orderItemPrices, invalidateDiscountCoupon);
            }

            // get current VAT rate
            var vatRate = DetermineVat(order, now);

            // apply VAT
            ApplyVat(orderItemPrices, vatRate);

            // calculate total for order
            var totalPrice = new PriceDataDTO()
            {
                BasePrice = Round(orderItemPrices.Sum(p => p.BasePrice)),
                Price = Round(orderItemPrices.Sum(p => p.Price)),
                VatRate = vatRate,
                PriceInclVat = Round(orderItemPrices.Sum(p => p.PriceInclVat)),
                CurrencyCode = orderItemPrices.First().CurrencyCode
            };

            return new CalculateOrderResultDTO()
            {
                TotalPrice = totalPrice,
                OrderItemPrices = orderItemPrices
            };
        }
        private List<PriceDataDTO> CalculateItemPrices(EventDTO eventData, CalculateOrderDTO order, DateTime now)
        {
            var orderItemPrices = new List<PriceDataDTO>();
            foreach (var i in order.OrderItems)
            {
                var eventPrice = eventData.Prices.SingleOrDefault(p => p.BeginDate <= now && now < p.EndDate && p.Sku == i.Sku);
                if (eventPrice == null)
                {
                    throw new InvalidSkuException();
                }

                var orderItemPrice = new PriceDataDTO();
                orderItemPrice.BasePrice = (decimal) eventPrice.Price.Value;
                orderItemPrice.CurrencyCode = eventPrice.CurrencyCode;
                orderItemPrice.Price = Round(i.Amount * orderItemPrice.BasePrice);

                orderItemPrices.Add(orderItemPrice);
            }

            return orderItemPrices;
        }
Exemple #5
0
 private async Task CreateCreditNote(OrderDTO order, PriceDataDTO price)
 {
     throw new NotImplementedException();
 }