Invoicing: Allow to show exchange rates based on sources/rules (#3122)

This commit is contained in:
Raphael Michel
2023-02-15 13:22:04 +01:00
committed by GitHub
parent e358bacfa3
commit 2ba9514b6f
11 changed files with 339 additions and 65 deletions

View File

@@ -0,0 +1,145 @@
#
# This file is part of pretix (Community Edition).
#
# Copyright (C) 2014-2020 Raphael Michel and contributors
# Copyright (C) 2020-2021 rami.io GmbH and contributors
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
# Public License as published by the Free Software Foundation in version 3 of the License.
#
# ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are
# applicable granting you additional permissions and placing additional restrictions on your usage of this software.
# Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive
# this file, see <https://pretix.eu/about/en/license>.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
#
from datetime import date, datetime, timedelta
from decimal import Decimal
import requests
from django.conf import settings
from django.db.models import Max
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _
from lxml import etree
from pretix.base.models import ExchangeRate
from pretix.base.signals import periodic_task
from pretix.celery_app import app
SOURCE_NAMES = {
None: _('European Central Bank'), # backwards-compatibility
'eu:ecb:eurofxref-daily': _('European Central Bank'),
'cz:cnb:rate-fixing-daily': _('Czech National Bank'),
}
@receiver(signal=periodic_task)
def fetch_rates(sender, **kwargs):
if not settings.FETCH_ECB_RATES:
return
source_tasks = {
'eu:ecb:eurofxref-daily': fetch_ecb_rates,
'cz:cnb:rate-fixing-daily': fetch_cnb_cz_rates,
}
for source_name, task in source_tasks.items():
last_source_date = ExchangeRate.objects.filter(source=source_name).aggregate(m=Max('source_date'))['m']
if last_source_date and last_source_date >= date.today():
# We assume that the rates we fetch are only updated daily
continue
last_fetch_date = ExchangeRate.objects.filter(source=source_name).aggregate(m=Max('updated'))['m']
if last_fetch_date and last_fetch_date >= datetime.utcnow() - timedelta(hours=1):
# Only try to fetch once per hour
continue
# Today's rate not yet published, let's try to fetch it
task.apply_async()
@app.task()
def fetch_ecb_rates():
"""
Fetches currency rates from the European Central Bank.
"""
d = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
r = requests.get(d)
r.raise_for_status()
# File looks like this:
# <?xml version="1.0" encoding="UTF-8"?>
# <gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
# xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
# <gesmes:subject>Reference rates</gesmes:subject>
# <gesmes:Sender>
# <gesmes:name>European Central Bank</gesmes:name>
# </gesmes:Sender>
# <Cube>
# <Cube time="2023-02-14">
# <Cube currency="USD" rate="1.0759"/>
# ...
# </Cube>
# </Cube>
# </gesmes:Envelope>
root = etree.fromstring(r.content)
namespaces = {
'gesmes': 'http://www.gesmes.org/xml/2002-08-01',
'eurofxref': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'
}
outercube = root.xpath('./eurofxref:Cube/eurofxref:Cube[@time]', namespaces=namespaces)[0]
source_date = date.fromisoformat(outercube.get("time"))
for cube in outercube.xpath('./eurofxref:Cube[@currency][@rate]', namespaces=namespaces):
currency = cube.get('currency')
rate = Decimal(cube.get('rate'))
ExchangeRate.objects.update_or_create(
source='eu:ecb:eurofxref-daily',
source_currency='EUR',
other_currency=currency,
defaults=dict(
source_date=source_date,
rate=rate,
)
)
@app.task()
def fetch_cnb_cz_rates():
"""
Fetches currency rates from the Czech National Bank.
"""
d = f'https://www.cnb.cz/en/financial-markets/foreign-exchange-market/central-bank-exchange-rate-fixing/' \
f'central-bank-exchange-rate-fixing/daily.txt?date={date.today().strftime("%d.%m.%Y")}'
r = requests.get(d)
r.raise_for_status()
lines = r.text.splitlines()
# File looks like this:
# 14 Feb 2023 #32
# Country|Currency|Amount|Code|Rate
# Australia|dollar|1|AUD|15.412
source_date = datetime.strptime(lines[0].split("#")[0].strip(), "%d %b %Y").date()
for line in lines[2:]:
country, currency, amount, code, rate = line.split("|")
rate = Decimal(rate).quantize(Decimal('0.000001')) / int(amount)
ExchangeRate.objects.update_or_create(
source='cz:cnb:rate-fixing-daily',
source_currency=code,
other_currency='CZK',
defaults=dict(
source_date=source_date,
rate=rate,
)
)

View File

@@ -33,16 +33,12 @@
# License for the specific language governing permissions and limitations under the License.
import inspect
import json
import logging
import urllib.error
from datetime import date, timedelta
from datetime import timedelta
from decimal import ROUND_HALF_UP, Decimal
import vat_moss.exchange_rates
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.serializers.json import DjangoJSONEncoder
from django.db import connection, transaction
from django.db.models import Count
from django.dispatch import receiver
@@ -56,11 +52,10 @@ from i18nfield.strings import LazyI18nString
from pretix.base.i18n import language
from pretix.base.models import (
Invoice, InvoiceAddress, InvoiceLine, Order, OrderFee,
ExchangeRate, Invoice, InvoiceAddress, InvoiceLine, Order, OrderFee,
)
from pretix.base.models.tax import EU_CURRENCIES
from pretix.base.services.tasks import TransactionAwareTask
from pretix.base.settings import GlobalSettingsObject
from pretix.base.signals import invoice_line_text, periodic_task
from pretix.celery_app import app
from pretix.helpers.database import OF_SELF, rolledback_transaction
@@ -144,27 +139,54 @@ def build_invoice(invoice: Invoice) -> Invoice:
invoice.invoice_to += "\n" + pgettext("invoice", "VAT-ID: %s") % ia.vat_id
invoice.invoice_to_vat_id = ia.vat_id
cc = str(ia.country)
if cc in EU_CURRENCIES and EU_CURRENCIES[cc] != invoice.event.currency and invoice.event.settings.invoice_eu_currencies:
invoice.foreign_currency_display = EU_CURRENCIES[cc]
if invoice.event.settings.invoice_eu_currencies == 'True':
cc = str(ia.country)
if cc in EU_CURRENCIES and EU_CURRENCIES[cc] != invoice.event.currency:
invoice.foreign_currency_display = EU_CURRENCIES[cc]
if settings.FETCH_ECB_RATES:
rate = ExchangeRate.objects.filter(
source='eu:ecb:eurofxref-daily',
source_currency=invoice.event.currency,
other_currency=invoice.foreign_currency_display,
source_date__gt=now().date() - timedelta(days=7)
).first()
if rate:
invoice.foreign_currency_rate = rate.rate.quantize(Decimal('0.0001'), ROUND_HALF_UP)
invoice.foreign_currency_rate_date = rate.source_date
invoice.foreign_currency_source = 'eu:ecb:eurofxref-daily'
else:
rate_eur_to_event = ExchangeRate.objects.filter(
source='eu:ecb:eurofxref-daily',
source_currency='EUR',
other_currency=invoice.event.currency,
source_date__gt=now().date() - timedelta(days=7)
).first()
rate_eur_to_wanted = ExchangeRate.objects.filter(
source='eu:ecb:eurofxref-daily',
source_currency='EUR',
other_currency=invoice.foreign_currency_display,
source_date__gt=now().date() - timedelta(days=7)
).first()
if rate_eur_to_wanted and rate_eur_to_event:
invoice.foreign_currency_rate = (
rate_eur_to_wanted.rate / rate_eur_to_event.rate
).quantize(Decimal('0.0001'), ROUND_HALF_UP)
invoice.foreign_currency_rate_date = min(rate_eur_to_wanted.source_date, rate_eur_to_event.source_date)
invoice.foreign_currency_source = 'eu:ecb:eurofxref-daily'
elif invoice.event.settings.invoice_eu_currencies == 'CZK' and invoice.event.currency != 'CZK':
invoice.foreign_currency_display = 'CZK'
if settings.FETCH_ECB_RATES:
gs = GlobalSettingsObject()
rates_date = gs.settings.get('ecb_rates_date', as_type=date)
rates_dict = gs.settings.get('ecb_rates_dict', as_type=dict)
convert = (
rates_date and rates_dict and
rates_date > (now() - timedelta(days=7)).date() and
invoice.event.currency in rates_dict and
invoice.foreign_currency_display in rates_dict
)
if convert:
invoice.foreign_currency_rate = (
Decimal(rates_dict[invoice.foreign_currency_display])
/ Decimal(rates_dict[invoice.event.currency])
).quantize(Decimal('0.0001'), ROUND_HALF_UP)
invoice.foreign_currency_rate_date = rates_date
rate = ExchangeRate.objects.filter(
source='cz:cnb:rate-fixing-daily',
source_currency=invoice.event.currency,
other_currency=invoice.foreign_currency_display,
source_date__gt=now().date() - timedelta(days=7)
).first()
if rate:
invoice.foreign_currency_rate = rate.rate.quantize(Decimal('0.0001'), ROUND_HALF_UP)
invoice.foreign_currency_rate_date = rate.source_date
invoice.foreign_currency_source = 'cz:cnb:rate-fixing-daily'
except InvoiceAddress.DoesNotExist:
ia = None
@@ -474,23 +496,6 @@ def build_preview_invoice_pdf(event):
return event.invoice_renderer.generate(invoice)
@receiver(signal=periodic_task)
def fetch_ecb_rates(sender, **kwargs):
if not settings.FETCH_ECB_RATES:
return
gs = GlobalSettingsObject()
if gs.settings.ecb_rates_date == now().strftime("%Y-%m-%d"):
return
try:
date, rates = vat_moss.exchange_rates.fetch()
gs.settings.ecb_rates_date = date
gs.settings.ecb_rates_dict = json.dumps(rates, cls=DjangoJSONEncoder)
except urllib.error.URLError:
logger.exception('Could not retrieve rates from ECB')
@receiver(signal=periodic_task)
@scopes_disabled()
def send_invoices_to_organizer(sender, **kwargs):