diff --git a/enterprise_subsidy/apps/subsidy/signals.py b/enterprise_subsidy/apps/subsidy/signals.py index 28eff8b1..22bad383 100644 --- a/enterprise_subsidy/apps/subsidy/signals.py +++ b/enterprise_subsidy/apps/subsidy/signals.py @@ -4,8 +4,9 @@ from django.db.models.signals import pre_save from django.dispatch import receiver from openedx_ledger.api import create_ledger +from openedx_ledger.models import SalesContractReferenceProvider -from .models import Subsidy +from .models import Subsidy, SubsidyReferenceChoices @receiver(pre_save, sender=Subsidy) @@ -21,6 +22,16 @@ def subsidy_pre_save(sender, instance, *args, **kwargs): # pylint: disable=unus if instance.ledger or not instance._state.adding: return + # In order to call create_ledger() later, we first need to get or create a SalesContractReferenceProvider. In order + # to avoid manual intervention, we mirror the SubsidyReferenceChoices selection into the + # SalesContractReferenceProvider table as needed. The normal steady-state is to always just re-use (get) an existing + # provider. + subsidy_reference_choices = dict((slug, name) for slug, name in SubsidyReferenceChoices.CHOICES) + sales_contract_reference_provider, _ = SalesContractReferenceProvider.objects.get_or_create( + slug=instance.reference_type, + defaults={"name": subsidy_reference_choices[instance.reference_type]}, + ) + # create_ledger() saves the ledger instance. # If a transaction for the starting_balance is created, # that transaction record is also saved during @@ -29,4 +40,6 @@ def subsidy_pre_save(sender, instance, *args, **kwargs): # pylint: disable=unus unit=instance.unit, subsidy_uuid=instance.uuid, initial_deposit=instance.starting_balance, + sales_contract_reference_id=instance.reference_id, + sales_contract_reference_provider=sales_contract_reference_provider, ) diff --git a/requirements/base.txt b/requirements/base.txt index becb2a09..b018e8a2 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -199,7 +199,7 @@ openedx-events==9.11.0 # -r requirements/base.in # edx-event-bus-kafka # openedx-ledger -openedx-ledger==1.5.1 +openedx-ledger==1.5.3 # via -r requirements/base.in packaging==24.1 # via drf-yasg diff --git a/requirements/dev.txt b/requirements/dev.txt index 5ad9845f..0dbfb796 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -379,7 +379,7 @@ openedx-events==9.11.0 # -r requirements/validation.txt # edx-event-bus-kafka # openedx-ledger -openedx-ledger==1.5.1 +openedx-ledger==1.5.3 # via -r requirements/validation.txt packaging==24.1 # via diff --git a/requirements/doc.txt b/requirements/doc.txt index 77635198..9b1a88a6 100644 --- a/requirements/doc.txt +++ b/requirements/doc.txt @@ -358,7 +358,7 @@ openedx-events==9.11.0 # -r requirements/test.txt # edx-event-bus-kafka # openedx-ledger -openedx-ledger==1.5.1 +openedx-ledger==1.5.3 # via -r requirements/test.txt packaging==24.1 # via diff --git a/requirements/production.txt b/requirements/production.txt index 7acddc68..0095298e 100644 --- a/requirements/production.txt +++ b/requirements/production.txt @@ -244,7 +244,7 @@ openedx-events==9.11.0 # -r requirements/base.txt # edx-event-bus-kafka # openedx-ledger -openedx-ledger==1.5.1 +openedx-ledger==1.5.3 # via -r requirements/base.txt packaging==24.1 # via diff --git a/requirements/quality.txt b/requirements/quality.txt index ef033acf..b58ff340 100644 --- a/requirements/quality.txt +++ b/requirements/quality.txt @@ -339,7 +339,7 @@ openedx-events==9.11.0 # -r requirements/test.txt # edx-event-bus-kafka # openedx-ledger -openedx-ledger==1.5.1 +openedx-ledger==1.5.3 # via -r requirements/test.txt packaging==24.1 # via diff --git a/requirements/test.txt b/requirements/test.txt index 90af0832..d7b472b9 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -287,7 +287,7 @@ openedx-events==9.11.0 # -r requirements/base.txt # edx-event-bus-kafka # openedx-ledger -openedx-ledger==1.5.1 +openedx-ledger==1.5.3 # via -r requirements/base.txt packaging==24.1 # via diff --git a/requirements/validation.txt b/requirements/validation.txt index b83dc1f8..99ce8c3f 100644 --- a/requirements/validation.txt +++ b/requirements/validation.txt @@ -435,7 +435,7 @@ openedx-events==9.11.0 # -r requirements/test.txt # edx-event-bus-kafka # openedx-ledger -openedx-ledger==1.5.1 +openedx-ledger==1.5.3 # via # -r requirements/quality.txt # -r requirements/test.txt