@@ -754,6 +754,14 @@ def handle_change_of_registration_purpose(
754754
755755 return payload
756756
757+ @staticmethod
758+ def _is_reportable_operation_year (
759+ operation_year : tuple [UUID , int ],
760+ added_operation_years : set [tuple [UUID , int ]],
761+ existing_reports : set [tuple [UUID , int ]],
762+ ) -> bool :
763+ return operation_year not in added_operation_years and operation_year not in existing_reports
764+
757765 @classmethod
758766 def _get_previous_reporting_years (cls ) -> QuerySet [ReportingYear ]:
759767 """
@@ -788,9 +796,9 @@ def list_previous_reportable_operations(
788796 user_guid : UUID ,
789797 ) -> list [dict ]:
790798 """
791- Returns the reporting year/operation combinations for which the current user is eligible to create a report
799+ Returns the reporting year/operation combinations for which the current user is eligible to create a report
792800
793- An reporting year/operation combination is eligible if:
801+ An reporting year/operation combination is eligible if:
794802 1. The operation was designated to the user's operator for that reporting year, or
795803 2. The operation is currently registered to the user's operator as a fallback
796804
@@ -802,7 +810,6 @@ def list_previous_reportable_operations(
802810 reporting_years = cls ._get_previous_reporting_years ()
803811 year_values = {reporting_year .reporting_year for reporting_year in reporting_years }
804812
805- # Find all registered operation timelines associated with the user's operator
806813 timelines = list (
807814 OperationDesignatedOperatorTimeline .objects .select_related ("operation" )
808815 .filter (
@@ -812,7 +819,6 @@ def list_previous_reportable_operations(
812819 .order_by ("operation__name" , "start_date" )
813820 )
814821
815- # Find all currently registered operations for fallback handling
816822 current_registered_operations = list (
817823 Operation .objects .filter (
818824 operator_id = user_operator .operator_id ,
@@ -824,7 +830,6 @@ def list_previous_reportable_operations(
824830 operation .id for operation in current_registered_operations
825831 }
826832
827- # Bulk fetch existing reports
828833 existing_reports = set (
829834 Report .objects .filter (
830835 operation_id__in = all_operation_ids ,
@@ -835,7 +840,6 @@ def list_previous_reportable_operations(
835840 )
836841 )
837842
838- # Bulk fetch designated operator timelines
839843 designations_lookup = (
840844 OperationDesignatedOperatorTimelineService .get_operation_designated_operators_for_reporting_years (
841845 operation_ids = all_operation_ids ,
@@ -846,52 +850,50 @@ def list_previous_reportable_operations(
846850 reportable_operations : list [dict ] = []
847851 added_operation_years : set [tuple [UUID , int ]] = set ()
848852
849- # Add combinations that are valid based on historical designated operator timelines
850853 for timeline in timelines :
851854 for reporting_year in reporting_years :
852855 operation_year = (
853856 timeline .operation .id ,
854857 reporting_year .reporting_year ,
855858 )
856859
857- if operation_year in added_operation_years :
858- continue
859-
860- if operation_year in existing_reports :
860+ if not cls ._is_reportable_operation_year (
861+ operation_year ,
862+ added_operation_years ,
863+ existing_reports ,
864+ ):
861865 continue
862866
863867 designated_operator_timeline = designations_lookup .get (operation_year )
864868
865- if not designated_operator_timeline :
866- continue
867-
868- if designated_operator_timeline .operator .id != user_operator .operator_id :
869- continue
870-
871- reportable_operations .append (
872- {
873- ** cls ._build_reportable_operation_row (
874- timeline ,
875- reporting_year ,
876- ),
877- "is_current_registered_fallback" : False ,
878- }
879- )
869+ if (
870+ designated_operator_timeline
871+ and designated_operator_timeline .operator .id == user_operator .operator_id
872+ ):
873+ reportable_operations .append (
874+ {
875+ ** cls ._build_reportable_operation_row (
876+ timeline ,
877+ reporting_year ,
878+ ),
879+ "is_current_registered_fallback" : False ,
880+ }
881+ )
880882
881- added_operation_years .add (operation_year )
883+ added_operation_years .add (operation_year )
882884
883- # Fallback: include current registered operations for operation/year combinations not already returned by the historical designation logic
884885 for operation in current_registered_operations :
885886 for reporting_year in reporting_years :
886887 operation_year = (
887888 operation .id ,
888889 reporting_year .reporting_year ,
889890 )
890891
891- if operation_year in added_operation_years :
892- continue
893-
894- if operation_year in existing_reports :
892+ if not cls ._is_reportable_operation_year (
893+ operation_year ,
894+ added_operation_years ,
895+ existing_reports ,
896+ ):
895897 continue
896898
897899 reportable_operations .append (
0 commit comments