From b7db1dfa05a7a5ad86e44eeb14f347bd8662e3be Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20Warnon?= <awa@odoo.com>
Date: Wed, 9 Aug 2023 07:07:36 +0000
Subject: [PATCH] [FIX] website_event_track_exhibitor: fix sponsor 'midnight'
 opening hours

Currently, configuring opening hours for event sponsors 'at midnight' is not
handled properly.

If you set either the start time or the end time opening hours to "00:00",
the sponsor will be considered as always open.

We fix that by correctly taking into account that configuration.

(Note: if the user configures a sponsor to be open 'until midnight', we assume
it means midnight 'the next day').

Tests have been altered to take this into account.

Task-3459777

closes odoo/odoo#131348

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
---
 .../models/event_sponsor.py                   |  7 ++--
 .../tests/test_sponsor_internals.py           | 35 ++++++++++++++++++-
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/addons/website_event_track_exhibitor/models/event_sponsor.py b/addons/website_event_track_exhibitor/models/event_sponsor.py
index 878938f739a2..462c1be45332 100644
--- a/addons/website_event_track_exhibitor/models/event_sponsor.py
+++ b/addons/website_event_track_exhibitor/models/event_sponsor.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 # Part of Odoo. See LICENSE file for full copyright and licensing details.
 
-from datetime import datetime
+from datetime import datetime, timedelta
 from pytz import timezone, utc
 
 from odoo import api, fields, models
@@ -73,7 +73,7 @@ class EventSponsor(models.Model):
         for sponsor in self:
             if not sponsor.event_id.is_ongoing:
                 sponsor.is_in_opening_hours = False
-            elif not sponsor.hour_from or not sponsor.hour_to:
+            elif sponsor.hour_from is False or sponsor.hour_to is False:
                 sponsor.is_in_opening_hours = True
             else:
                 event_tz = timezone(sponsor.event_id.date_tz)
@@ -86,6 +86,9 @@ class EventSponsor(models.Model):
                 # compute opening hours
                 opening_from_tz = event_tz.localize(datetime.combine(now_tz.date(), float_to_time(sponsor.hour_from)))
                 opening_to_tz = event_tz.localize(datetime.combine(now_tz.date(), float_to_time(sponsor.hour_to)))
+                if sponsor.hour_to == 0:
+                    # when closing 'at midnight', we consider it's at midnight the next day
+                    opening_to_tz = opening_to_tz + timedelta(days=1)
 
                 opening_from = max([dt_begin, opening_from_tz])
                 opening_to = min([dt_end, opening_to_tz])
diff --git a/addons/website_event_track_exhibitor/tests/test_sponsor_internals.py b/addons/website_event_track_exhibitor/tests/test_sponsor_internals.py
index e011510662d1..39458072f6d4 100644
--- a/addons/website_event_track_exhibitor/tests/test_sponsor_internals.py
+++ b/addons/website_event_track_exhibitor/tests/test_sponsor_internals.py
@@ -31,7 +31,7 @@ class TestSponsorData(TestEventTrackOnlineCommon):
     @users('user_eventmanager')
     def test_event_date_computation(self):
         """ Test date computation. Pay attention that mocks returns UTC values, meaning
-        we have to take into account Europe/Brussels offset """
+        we have to take into account Europe/Brussels offset (+2 in July) """
         event = self.env['event.event'].browse(self.event_0.id)
         sponsor = self.env['event.sponsor'].browse(self.sponsor_0.id)
         event.invalidate_cache(fnames=['is_ongoing'])
@@ -85,3 +85,36 @@ class TestSponsorData(TestEventTrackOnlineCommon):
         sponsor.invalidate_cache(fnames=['is_in_opening_hours'])
         self.assertFalse(sponsor.is_in_opening_hours)
         self.assertFalse(event.is_ongoing)
+
+        # Use "00:00" as opening hours for sponsor -> should still work
+        event.invalidate_cache(fnames=['is_ongoing'])
+        sponsor.hour_from = 0.0  # 0 -> 18
+
+        # Inside opening hours (17 < 18)
+        self.mock_wevent_dt.now.return_value = datetime(2020, 7, 6, 15, 0, 1)
+        self.mock_wevent_exhib_dt.now.return_value = datetime(2020, 7, 6, 15, 0, 1)
+        sponsor.invalidate_cache(fnames=['is_in_opening_hours'])
+        self.assertTrue(sponsor.is_in_opening_hours)
+
+        # Outside opening hours (21 > 18)
+        self.mock_wevent_dt.now.return_value = datetime(2020, 7, 6, 19, 0, 1)
+        self.mock_wevent_exhib_dt.now.return_value = datetime(2020, 7, 6, 19, 0, 1)
+        sponsor.invalidate_cache(fnames=['is_in_opening_hours'])
+        self.assertFalse(sponsor.is_in_opening_hours)
+
+        # Use "00:00" as closing hours for sponsor -> should still work
+        # (considered 'at midnight the next day')
+        sponsor.hour_from = 10.0
+        sponsor.hour_to = 0.0
+
+        # Inside opening hours (11 > 10)
+        self.mock_wevent_dt.now.return_value = datetime(2020, 7, 6, 9, 0, 1)
+        self.mock_wevent_exhib_dt.now.return_value = datetime(2020, 7, 6, 9, 0, 1)
+        sponsor.invalidate_cache(fnames=['is_in_opening_hours'])
+        self.assertTrue(sponsor.is_in_opening_hours)
+
+        # Outside opening hours (7 < 10)
+        self.mock_wevent_dt.now.return_value = datetime(2020, 7, 6, 5, 0, 1)
+        self.mock_wevent_exhib_dt.now.return_value = datetime(2020, 7, 6, 5, 0, 1)
+        sponsor.invalidate_cache(fnames=['is_in_opening_hours'])
+        self.assertFalse(sponsor.is_in_opening_hours)
-- 
GitLab