From 9dadd723dbab9e1284723f909cef874633bb7c85 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 28 Jul 2022 16:34:53 +0200
Subject: [PATCH 12/14] pricing: always pass a pricing_date in context (#67675)

to be able to get the correct QF value
---
 lingo/pricing/forms.py                       | 4 ++++
 lingo/pricing/models.py                      | 6 +++++-
 tests/pricing/manager/test_agenda_pricing.py | 5 +++++
 tests/pricing/test_models.py                 | 1 +
 4 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/lingo/pricing/forms.py b/lingo/pricing/forms.py
index ca8dbe5..acbb8b1 100644
--- a/lingo/pricing/forms.py
+++ b/lingo/pricing/forms.py
@@ -433,8 +433,12 @@ class PricingTestToolForm(forms.Form):
     def compute_for_flat_fee_schedule(self):
         if self.agenda_pricing.subscription_required and not self.serialized_subscription:
             return
+        pricing_date = self.agenda_pricing.date_start
+        if self.cleaned_data.get('billing_date'):
+            pricing_date = self.cleaned_data['billing_date'].date_start
         return self.agenda_pricing.get_pricing_data(
             request=self.request,
+            pricing_date=pricing_date,
             subscription=self.serialized_subscription,
             user_external_id=self.cleaned_data['user_external_id'],
             adult_external_id=self.cleaned_data['adult_external_id'],
diff --git a/lingo/pricing/models.py b/lingo/pricing/models.py
index cb273fe..78c924f 100644
--- a/lingo/pricing/models.py
+++ b/lingo/pricing/models.py
@@ -16,6 +16,7 @@
 
 import copy
 import dataclasses
+import datetime
 import decimal
 from typing import List
 
@@ -399,10 +400,11 @@ class AgendaPricing(models.Model):
 
         return created, agenda_pricing
 
-    def get_pricing_data(self, request, user_external_id, adult_external_id, subscription=None):
+    def get_pricing_data(self, request, pricing_date, user_external_id, adult_external_id, subscription=None):
         # compute pricing for flat_fee_schedule mode
         # subscription is None if subscription_required is False
         data = {
+            'pricing_date': pricing_date,  # date to use for QF
             'subscription': subscription,
         }
         context = self.get_pricing_context(
@@ -425,7 +427,9 @@ class AgendaPricing(models.Model):
         self, request, agenda, event, subscription, check_status, booking, user_external_id, adult_external_id
     ):
         # compute pricing for an event
+        event_date = datetime.datetime.fromisoformat(event['start_datetime']).date()
         data = {
+            'pricing_date': event_date,  # date to use for QF
             'event': event,
             'subscription': subscription,
             'booking': booking,
diff --git a/tests/pricing/manager/test_agenda_pricing.py b/tests/pricing/manager/test_agenda_pricing.py
index 019cfcc..6ec7f9f 100644
--- a/tests/pricing/manager/test_agenda_pricing.py
+++ b/tests/pricing/manager/test_agenda_pricing.py
@@ -1135,6 +1135,7 @@ def test_detail_agenda_pricing_test_tool_for_flat_fee_schedule(
     assert mock_pricing_data.call_args_list == [
         mock.call(
             request=mock.ANY,
+            pricing_date=datetime.date(2021, 9, 1),
             subscription={'date_start': '2021-09-02', 'date_end': '2021-09-03'},
             user_external_id='user:1',
             adult_external_id='adult:1',
@@ -1169,6 +1170,7 @@ def test_detail_agenda_pricing_test_tool_for_flat_fee_schedule(
     assert mock_pricing_data.call_args_list == [
         mock.call(
             request=mock.ANY,
+            pricing_date=datetime.date(2021, 9, 1),
             subscription={'date_start': '2021-09-02', 'date_end': '2021-09-03'},
             user_external_id='user:1',
             adult_external_id='adult:1',
@@ -1201,6 +1203,7 @@ def test_detail_agenda_pricing_test_tool_for_flat_fee_schedule(
     assert mock_pricing_data.call_args_list == [
         mock.call(
             request=mock.ANY,
+            pricing_date=datetime.date(2021, 9, 15),
             subscription={'date_start': '2021-09-15', 'date_end': '2021-09-16'},
             user_external_id='user:1',
             adult_external_id='adult:1',
@@ -1218,6 +1221,7 @@ def test_detail_agenda_pricing_test_tool_for_flat_fee_schedule(
     assert mock_pricing_data.call_args_list == [
         mock.call(
             request=mock.ANY,
+            pricing_date=datetime.date(2021, 9, 15),
             subscription={'date_start': '2021-09-30', 'date_end': '2021-10-01'},
             user_external_id='user:1',
             adult_external_id='adult:1',
@@ -1239,6 +1243,7 @@ def test_detail_agenda_pricing_test_tool_for_flat_fee_schedule(
     assert mock_pricing_data.call_args_list == [
         mock.call(
             request=mock.ANY,
+            pricing_date=datetime.date(2021, 9, 1),
             subscription=None,
             user_external_id='user:1',
             adult_external_id='adult:1',
diff --git a/tests/pricing/test_models.py b/tests/pricing/test_models.py
index 48a2e3b..2a0bc01 100644
--- a/tests/pricing/test_models.py
+++ b/tests/pricing/test_models.py
@@ -952,6 +952,7 @@ def test_get_pricing_data(context):
     for subscription in [None, {'foo': 'bar'}]:
         assert agenda_pricing.get_pricing_data(
             request=context['request'],
+            pricing_date=datetime.date(year=2021, month=9, day=1),
             subscription=subscription,
             user_external_id='child:42',
             adult_external_id='parent:35',
-- 
2.35.1

