diff --git a/yuthon_property_approval/models/inherit_yuthon_property.py b/yuthon_property_approval/models/inherit_yuthon_property.py index 8e657dc3c..38a41727d 100644 --- a/yuthon_property_approval/models/inherit_yuthon_property.py +++ b/yuthon_property_approval/models/inherit_yuthon_property.py @@ -217,6 +217,10 @@ class PropertyBankStatements(models.Model): # 获取当前记录的日期(用于判断出租方变更行的生效期) target_date = record.start_time.date() if record.start_time else fields.Date.today() + # ====================== 提取目标年月 ====================== + target_year = target_date.year + target_month = target_date.month + for lessee in record.lessee_ids: # 从承租方的租户行提取合同 tenant_lines = lessee.property_contract_ids.filtered( @@ -228,13 +232,27 @@ class PropertyBankStatements(models.Model): if not contract: continue + # 按【年月】判断合同是否有效 is_contract_valid = False - if contract.actual_start_time and contract.actual_start_time <= target_date: - if not contract.actual_end_time or contract.actual_end_time >= target_date: - is_contract_valid = True + if contract.actual_start_time: + start_year = contract.actual_start_time.year + start_month = contract.actual_start_time.month + # 合同开始年月 <= 目标年月 + if (start_year < target_year) or ( + start_year == target_year and start_month <= target_month): + # 合同结束时间判断:未设置 或 结束年月 >= 目标年月 + if not contract.actual_end_time: + is_contract_valid = True + else: + end_year = contract.actual_end_time.year + end_month = contract.actual_end_time.month + if (end_year > target_year) or ( + end_year == target_year and end_month >= target_month): + is_contract_valid = True if not is_contract_valid: continue + # 1. 筛选合同的出租方变更行:生效日期≤目标日期,失效日期≥目标日期/未设置 valid_lessor_lines = contract.lessor_line.filtered( lambda l: