private async Task UpdateExchangeRatesAsync(IEnumerable <ExchangeRateSyncDTO> rates)
        {
            if (rates == null || !rates.Any())
            {
                return;
            }

            var localRates = (await this.currencyRepository.GetExchangeRatesAsync());

            var userId         = (await this.userRepository.GetFirstUserAsync()).Id;
            var currenciesDict = (await this.currencyRepository.GetCurriencesAsync())
                                 .ToDictionary(x => x.Code, x => x.Id);

            foreach (var rate in rates)
            {
                var localRate      = localRates.Where(x => x.ExternalId == rate.ExternalId).FirstOrDefault();
                var fromCurrencyId = currenciesDict.ContainsKey(rate.FromCurrency) ? currenciesDict[rate.FromCurrency] : (int?)null;
                var toCurrencyId   = currenciesDict.ContainsKey(rate.ToCurrency) ? currenciesDict[rate.ToCurrency] : (int?)null;
                if (fromCurrencyId.HasValue && toCurrencyId.HasValue)
                {
                    if (localRate == null)
                    {
                        var exchangeRate = ExchangeRate.Create(fromCurrencyId.Value, toCurrencyId.Value, rate.Rate, userId, rate.ExternalId, rate.IsDeleted, rate.Date);
                        await this.currencyRepository.AddExchangeRateAsync(exchangeRate);
                    }
                    else
                    {
                        localRate.Edit(fromCurrencyId.Value, toCurrencyId.Value, rate.Rate, rate.Date, rate.IsDeleted);
                    }
                }
            }
        }
        public void Create_With1Dot2053Rate_RateShouldBeInitTo1Dot2053()
        {
            const double expectedRate = 1.2053;
            var          exchangeRate = ExchangeRate.Create(expectedRate);

            Assert.AreEqual(expectedRate, exchangeRate.Rate);
        }
        public void ChangeInvert_From662Dot9150With0Dot9661Rate_ShouldReturn686Dot1833()
        {
            var exchangeRate = ExchangeRate.Create(0.9661);

            var exchangeValue = exchangeRate.ChangeBack(662.9150);

            Assert.AreEqual(686.1833, exchangeValue);
        }
        public void Change_From550With1Dot2053Rate_ShouldReturn662Dot9150()
        {
            var exchangeRate = ExchangeRate.Create(1.2053);

            var exchangeValue = exchangeRate.Change(550);

            Assert.AreEqual(662.9150, exchangeValue);
        }
Exemple #5
0
        private ModificationInvoice CreateModificationInvoice(InvoiceNumber originalDocumentNumber, Receiver receiver)
        {
            var nowUtc      = DateTime.UtcNow.Date;
            var item1Amount = new Amount(net: new AmountValue(-1694.92m), gross: new AmountValue(-2000), tax: new AmountValue(-305.08m));
            var item2Amount = new Amount(new AmountValue(-2362.20m), new AmountValue(-3000), new AmountValue(-637.8m));
            var item3Amount = new Amount(new AmountValue(-952.38m), new AmountValue(-1000), new AmountValue(-47.62m));
            var unitAmount1 = new ItemAmounts(item1Amount, item1Amount, 0.18m);
            var unitAmount2 = new ItemAmounts(item2Amount, item2Amount, 0.27m);
            var unitAmount3 = new ItemAmounts(item3Amount, item3Amount, 0.05m);
            var items       = new[]
            {
                new InvoiceItem(
                    consumptionDate: nowUtc,
                    totalAmounts: new ItemAmounts(item1Amount, item1Amount, 0.18m),
                    description: Description.Create("Item 1 description").Success.Get(),
                    measurementUnit: MeasurementUnit.Night,
                    quantity: -1,
                    unitAmounts: unitAmount1,
                    exchangeRate: ExchangeRate.Create(1).Success.Get()
                    ),
                new InvoiceItem(
                    consumptionDate: nowUtc,
                    totalAmounts: new ItemAmounts(item2Amount, item2Amount, 0.27m),
                    description: Description.Create("Item 2 description").Success.Get(),
                    measurementUnit: MeasurementUnit.Night,
                    quantity: -1,
                    unitAmounts: unitAmount2,
                    exchangeRate: ExchangeRate.Create(1).Success.Get()
                    ),
                new InvoiceItem(
                    consumptionDate: nowUtc,
                    totalAmounts: new ItemAmounts(item3Amount, item3Amount, 0.05m),
                    description: Description.Create("Item 3 description").Success.Get(),
                    measurementUnit: MeasurementUnit.Night,
                    quantity: -1,
                    unitAmounts: unitAmount3,
                    exchangeRate: ExchangeRate.Create(1).Success.Get()
                    )
            };

            return(new ModificationInvoice(
                       number: InvoiceNumber.Create($"REBATE-{Guid.NewGuid()}").Success.Get(),
                       supplierInfo: CreateSupplierInfo(),
                       receiver: receiver,
                       items: Sequence.FromPreordered(items, startIndex: 1).Get(),
                       currencyCode: CurrencyCode.Create("EUR").Success.Get(),
                       issueDate: nowUtc,
                       paymentDate: nowUtc,
                       itemIndexOffset: 3,
                       modificationIndex: 1,
                       modifyWithoutMaster: false,
                       originalDocumentNumber: originalDocumentNumber,
                       paymentMethod: PaymentMethod.Cash
                       ));
        }
Exemple #6
0
        private Invoice CreateInvoice(Receiver receiver, InvoiceNumber invoiceNumber = null)
        {
            var nowUtc      = DateTime.UtcNow.Date;
            var item1Amount = new Amount(net: new AmountValue(1694.92m), gross: new AmountValue(2000), tax: new AmountValue(305.08m));
            var item2Amount = new Amount(new AmountValue(2362.20m), new AmountValue(3000), new AmountValue(637.8m));
            var item3Amount = new Amount(new AmountValue(952.38m), new AmountValue(1000), new AmountValue(47.62m));
            var unitAmount1 = new ItemAmounts(item1Amount, item1Amount, 0.18m);
            var unitAmount2 = new ItemAmounts(item2Amount, item2Amount, 0.27m);
            var unitAmount3 = new ItemAmounts(item3Amount, item3Amount, 0.05m);
            var items       = new[]
            {
                new InvoiceItem(
                    consumptionDate: nowUtc,
                    totalAmounts: new ItemAmounts(item1Amount, item1Amount, 0.18m),
                    description: Description.Create("Item 1 description").Success.Get(),
                    measurementUnit: MeasurementUnit.Night,
                    quantity: 1,
                    unitAmounts: unitAmount1,
                    exchangeRate: ExchangeRate.Create(1).Success.Get()
                    ),
                new InvoiceItem(
                    consumptionDate: nowUtc,
                    totalAmounts: new ItemAmounts(item2Amount, item2Amount, 0.27m),
                    description: Description.Create("Item 2 description").Success.Get(),
                    measurementUnit: MeasurementUnit.Night,
                    quantity: 1,
                    unitAmounts: unitAmount2,
                    exchangeRate: ExchangeRate.Create(1).Success.Get()
                    ),
                new InvoiceItem(
                    consumptionDate: nowUtc,
                    totalAmounts: new ItemAmounts(item3Amount, item3Amount, 0.05m),
                    description: Description.Create("Item 3 description").Success.Get(),
                    measurementUnit: MeasurementUnit.Night,
                    quantity: 1,
                    unitAmounts: unitAmount3,
                    exchangeRate: ExchangeRate.Create(1).Success.Get()
                    )
            };

            return(new Invoice(
                       number: invoiceNumber ?? InvoiceNumber.Create($"INVOICE-{Guid.NewGuid()}").Success.Get(),
                       issueDate: nowUtc,
                       supplierInfo: CreateSupplierInfo(),
                       receiver: receiver,
                       items: Sequence.FromPreordered(items, startIndex: 1).Get(),
                       paymentDate: nowUtc,
                       currencyCode: CurrencyCode.Create("EUR").Success.Get(),
                       paymentMethod: PaymentMethod.Card
                       ));
        }
 private async Task ImportExchangeRatesAsync(List <ExchangeRateSyncDTO> exchangeRates)
 {
     if (exchangeRates != null && exchangeRates.Any())
     {
         var currenciesDict = (await this.currencyRepository.GetCurriencesAsync())
                              .ToDictionary(x => x.Code, x => x.Id);
         foreach (var item in exchangeRates)
         {
             var fromCurrencyId = currenciesDict.ContainsKey(item.FromCurrency) ? currenciesDict[item.FromCurrency] : (int?)null;
             var toCurrencyId   = currenciesDict.ContainsKey(item.ToCurrency) ? currenciesDict[item.ToCurrency] : (int?)null;
             if (fromCurrencyId.HasValue && toCurrencyId.HasValue)
             {
                 var exchangeRate = ExchangeRate.Create(fromCurrencyId.Value, toCurrencyId.Value, item.Rate, item.ExternalId.ToString(), item.IsDeleted, item.Date);
                 await this.currencyRepository.AddExchangeRateAsync(exchangeRate);
             }
         }
     }
 }
Exemple #8
0
        public ExchangeRate CalculateCrossRate(ExchangeRate rate1, ExchangeRate rate2, DateTime timestampUtc)
        {
            Argument.NotNull(rate1, "rate1");
            Argument.NotNull(rate2, "rate2");

            if (rate1.Base.ISOName != rate2.Base.ISOName)
            {
                throw new ArgumentException("Can't calculate cross rate for rates with different base currencies.");
            }
            if (rate1.Foreign.ISOName == rate2.Foreign.ISOName)
            {
                throw new ArgumentException("Can't calculate cross rate for rates with same foreign currencies.");
            }
            var buyRate  = rate2.BuyRate / rate1.SellRate;
            var sellRate = rate2.SellRate / rate1.BuyRate;

            return(ExchangeRate.Create(rate1.Foreign, rate2.Foreign, buyRate, sellRate, timestampUtc));
        }
Exemple #9
0
 internal static InvoiceInfo CreateInvoiceInfo(
     string invoiceSerialNumber = "50020",
     string invoiceSeries       = "0",
     string currencyCode        = "EUR",
     string invoiceIdentifier   = null,
     decimal?exchangeRate       = null)
 {
     return(InvoiceInfo.Create(
                header: new InvoiceHeader(
                    invoiceSeries: String1To50.CreateUnsafe(invoiceSeries),
                    invoiceSerialNumber: String1To50.CreateUnsafe(invoiceSerialNumber),
                    invoiceIssueDate: DateTime.Now,
                    invoiceIdentifier: invoiceIdentifier,
                    currencyCode: CurrencyCode.Create(currencyCode).Success.Get(),
                    exchangeRate: exchangeRate.IsNotNull().Match(t => ExchangeRate.Create(exchangeRate.Value).Success.Get(), f => null)
                    ),
                issuer: CreateInvoiceParty(Countries.Greece, UserVatNumber)
                ).Success.Get());
 }
Exemple #10
0
        public async void ParseXml_WithValidXml_ShouldReturnExchangeRates()
        {
            var xml    = @"<?xml version=""1.0"" encoding=""ISO-8859-1""?>
<exchangerates type=""Valutakurser"" author=""Danmarks Nationalbank"" refcur=""DKK"" refamt=""1"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
  <dailyrates id=""2018-08-17"">
    <currency code=""AUD"" desc=""Australske dollar"" rate=""475,93"" />
    <currency code=""BGN"" desc=""Bulgarske lev"" rate=""381,32"" />
    <currency code=""BRL"" desc=""Brasilianske real"" rate=""165,73"" />
  </dailyrates>
</exchangerates>";
            var actual = await ImportWorkflow.ParseRatesXml(xml, null);


            Assert.NotEmpty(actual);
            var expectedDate = new DateTime(2018, 08, 17);

            Assert.Contains(ExchangeRate.Create("AUD", "DKK", 4.7593m, expectedDate), actual);
            Assert.Contains(ExchangeRate.Create("BGN", "DKK", 3.8132m, expectedDate), actual);
            Assert.Contains(ExchangeRate.Create("BRL", "DKK", 1.6573m, expectedDate), actual);
        }
        private ModificationInvoice GetModificationInvoice()
        {
            var amounts     = GetItemAmounts(amount: 100, exchangeRate: 300);
            var unitAmounts = GetItemAmounts(amount: 100, exchangeRate: 300);
            var item        = new InvoiceItem(
                consumptionDate: new DateTime(2020, 08, 30),
                totalAmounts: amounts,
                description: Description.Create("NIGHT 8/30/2020").Success.Get(),
                measurementUnit: MeasurementUnit.Night,
                quantity: -1,
                unitAmounts: unitAmounts,
                exchangeRate: ExchangeRate.Create(300).Success.Get()
                );

            var amounts1     = GetItemAmounts(amount: 100, exchangeRate: 300);
            var unitAmounts1 = GetItemAmounts(amount: 100, exchangeRate: 300);
            var item1        = new InvoiceItem(
                consumptionDate: new DateTime(2020, 08, 31),
                totalAmounts: amounts1,
                description: Description.Create("NIGHT2 8/31/2020").Success.Get(),
                measurementUnit: MeasurementUnit.Night,
                quantity: 1,
                unitAmounts: unitAmounts1,
                exchangeRate: ExchangeRate.Create(300).Success.Get()
                );

            return(new ModificationInvoice(
                       number: InvoiceNumber.Create("ABC-18abfcefsaa").Success.Get(),
                       supplierInfo: GetSupplierInfo(),
                       customerInfo: GetCustomerInfo(),
                       items: Sequence.FromPreordered(new[] { item, item1 }, startIndex: 1).Get(),
                       currencyCode: CurrencyCode.Create("USD").Success.Get(),
                       issueDate: new DateTime(2020, 08, 31),
                       paymentDate: new DateTime(2020, 08, 31),
                       itemIndexOffset: 4,
                       modificationIndex: 4,
                       modifyWithoutMaster: true,
                       originalDocumentNumber: InvoiceNumber.Create("ABC-18afasadafa").Success.Get()
                       ));
        }
        private Invoice GetInvoice()
        {
            var item1Amount = new Amount(new AmountValue(1), new AmountValue(1), new AmountValue(0));
            var item2Amount = new Amount(new AmountValue(20m), new AmountValue(16.81m), new AmountValue(3.19m));
            var unitAmount1 = new ItemAmounts(item1Amount, item2Amount, 0.05m);
            var items       = new[]
            {
                new InvoiceItem(
                    consumptionDate: new DateTime(2020, 06, 30),
                    totalAmounts: new ItemAmounts(item1Amount, item1Amount, 0.05m),
                    description: Description.Create("Httt hzi serts (fl)").Success.Get(),
                    measurementUnit: MeasurementUnit.Night,
                    quantity: 15,
                    unitAmounts: unitAmount1,
                    exchangeRate: ExchangeRate.Create(1).Success.Get()
                    ),
                new InvoiceItem(
                    consumptionDate: new DateTime(2020, 06, 30),
                    totalAmounts: new ItemAmounts(item2Amount, item2Amount, 0.05m),
                    description: Description.Create("Httt hzi serts (fl)").Success.Get(),
                    measurementUnit: MeasurementUnit.Night,
                    quantity: -15,
                    unitAmounts: unitAmount1,
                    exchangeRate: ExchangeRate.Create(1).Success.Get()
                    ),
            };

            var address = GetAddress();

            return(new Invoice(
                       number: InvoiceNumber.Create("ABC-18a").Success.Get(),
                       issueDate: new DateTime(2020, 06, 30),
                       supplierInfo: GetSupplierInfo(),
                       customerInfo: GetCustomerInfo(),
                       items: Sequence.FromPreordered(items, startIndex: 1).Get(),
                       paymentDate: new DateTime(2020, 06, 14),
                       currencyCode: CurrencyCode.Create("EUR").Success.Get()
                       ));
        }
Exemple #13
0
        public ExchangeRate CalculateFromConversionRate(ConversionRate conversionRate, DateTime timstampUtc, Rounding rounding)
        {
            Argument.NotNull(conversionRate, "conversionRate");

            var baseCurrency = _currencyRepository.Find(conversionRate.Conversion.From);

            if (baseCurrency == null)
            {
                var message = string.Format("Can't find currency [{0}] in the database.", conversionRate.Conversion.From);
                throw new InvalidOperationException(message);
            }
            var foreignCurrency = _currencyRepository.Find(conversionRate.Conversion.To);

            if (foreignCurrency == null)
            {
                var message = string.Format("Can't find currency [{0}] in the database.", conversionRate.Conversion.To);
                throw new InvalidOperationException(message);
            }
            var buyRate  = MoneyMath.Round(conversionRate.Rate * (decimal)Randomizer.FromRange(_settings.BuyRateCoefficientRange), rounding);
            var sellRate = MoneyMath.Round(conversionRate.Rate * (decimal)Randomizer.FromRange(_settings.SellRateCoefficientRange), rounding);

            return(ExchangeRate.Create(baseCurrency, foreignCurrency, buyRate, sellRate, timstampUtc));
        }
Exemple #14
0
        public async Task AddExchangeRateAsync(AddExchangeRate command)
        {
            var rate = ExchangeRate.Create(command.FromCurrencyId, command.ToCurrencyId, command.Rate, command.ExternalId.ToString(), false, command.Date);

            await this.currencyRepository.AddExchangeRateAsync(rate);
        }
Exemple #15
0
        public async Task AddExchangeRateAsync(AddExchangeRate command)
        {
            var rate = ExchangeRate.Create(command.FromCurrencyId, command.ToCurrencyId, command.Rate, userIdentityContext.UserId, command.ExternalId, false, command.Date);

            await this.currencyRepository.AddExchangeRateAsync(rate);
        }
        public void Create_With1Dot2053Rate_InvertRateShouldEqual1Dot0351()
        {
            var exchangeRate = ExchangeRate.Create(0.9661);

            Assert.AreEqual(1.0351, exchangeRate.InvertRate);
        }