diff --git a/website/moneybirdsynchronization/models.py b/website/moneybirdsynchronization/models.py index 4c84d12d3..c4d9f0fff 100644 --- a/website/moneybirdsynchronization/models.py +++ b/website/moneybirdsynchronization/models.py @@ -10,7 +10,7 @@ from django.utils.translation import gettext_lazy as _ from events.models import EventRegistration -from members.models import Member +from members.models import Member, Membership from moneybirdsynchronization.moneybird import get_moneybird_api_service from payments.models import BankAccount, Payment from payments.payables import payables @@ -64,6 +64,12 @@ def ledger_id_for_payable_model(obj) -> Optional[int]: return None +def membership_to_mb_period(membership: Membership) -> str: + """Convert a membership to a Moneybird period.""" + start_date = membership.since + return f"{start_date.strftime('%Y%m%d')}..{start_date.strftime('%Y%m%d')}" + + class MoneybirdProject(models.Model): name = models.CharField( _("Name"), @@ -269,6 +275,13 @@ def to_moneybird(self): ledger_id = ledger_id_for_payable_model(self.payable_object) + period = None + tax_rate_id = None + if isinstance(self.payable_object, (Registration, Renewal)): + tax_rate_id = settings.MONEYBIRD_ZERO_TAX_RATE_ID + if self.payable_object.membership is not None: + period = membership_to_mb_period(self.payable_object.membership) + source_url = settings.BASE_URL + reverse( f"admin:{self.payable_object._meta.app_label}_{self.payable_object._meta.model_name}_change", args=(self.object_id,), @@ -306,6 +319,12 @@ def to_moneybird(self): data["external_sales_invoice"]["details_attributes"][0]["id"] = int( self.moneybird_details_attribute_id ) + if period is not None: + data["external_sales_invoice"]["details_attributes"][0]["period"] = period + if tax_rate_id is not None: + data["external_sales_invoice"]["details_attributes"][0][ + "tax_rate_id" + ] = int(tax_rate_id) return data diff --git a/website/thaliawebsite/settings.py b/website/thaliawebsite/settings.py index ed9908f77..16c2f5e22 100644 --- a/website/thaliawebsite/settings.py +++ b/website/thaliawebsite/settings.py @@ -1115,3 +1115,9 @@ def from_env( if os.environ.get("MONEYBIRD_CARD_FINANCIAL_ACCOUNT_ID") else None ) + +MONEYBIRD_ZERO_TAX_RATE_ID: Optional[int] = ( + int(os.environ.get("MONEYBIRD_ZERO_TAX_RATE_ID")) + if os.environ.get("MONEYBIRD_ZERO_TAX_RATE_ID") + else None +)