diff --git a/yuthon_property_approval/wizard/property_reconciliation_wizard.py b/yuthon_property_approval/wizard/property_reconciliation_wizard.py index 520d4665c..924b539d1 100644 --- a/yuthon_property_approval/wizard/property_reconciliation_wizard.py +++ b/yuthon_property_approval/wizard/property_reconciliation_wizard.py @@ -183,27 +183,39 @@ class PropertyReconciliationWizard(models.Model): # 2. 找到最早的到期日期 if all_due_dates: earliest_due_date = min(all_due_dates) - # 3. 计算最早日期的下个月(作为分组起始月) - if earliest_due_date.month == 12: - next_month_of_earliest = earliest_due_date.replace(year=earliest_due_date.year + 1, month=1, day=1) - else: - next_month_of_earliest = earliest_due_date.replace(month=earliest_due_date.month + 1, day=1) + # 获取实际日期(假设actual_date是日期对象,如果是字符串需要先转换) + actual_date = fields.Date.from_string(self.actual_date) if isinstance(self.actual_date, + str) else self.actual_date + + # 计算起始月份(最早到期日的月份)和结束月份(实际日期的月份) + start_month = earliest_due_date + relativedelta(months=1) + end_month = actual_date - relativedelta(months=1) temp_rent_set = set() target_rent_months = [] - for line in self.reconciliation_line_ids: - if line.collection_type == 'rent': - due_month = next_month_of_earliest.strftime('%Y-%m') - unique_key = ( - line.contract_id.id if line.contract_id else None, - due_month - ) - if unique_key not in temp_rent_set: - temp_rent_set.add(unique_key) - target_rent_months.append({ - 'contract_id': line.contract_id, - 'due_month': due_month - }) + + # 遍历从起始月到结束月的所有月份 + current_month = start_month + while current_month <= end_month: + due_month = current_month.strftime('%Y-%m') + # 为每个租金行添加当前月份的记录(去重) + for line in self.reconciliation_line_ids: + if line.collection_type == 'rent': + unique_key = ( + line.contract_id.id if line.contract_id else None, + due_month + ) + if unique_key not in temp_rent_set: + temp_rent_set.add(unique_key) + target_rent_months.append({ + 'contract_id': line.contract_id, + 'due_month': due_month + }) + # 移动到下个月 + if current_month.month == 12: + current_month = current_month.replace(year=current_month.year + 1, month=1, day=1) + else: + current_month = current_month.replace(month=current_month.month + 1, day=1) else: # 没有符合条件的明细行时,返回空列表 target_rent_months = [] @@ -221,18 +233,6 @@ class PropertyReconciliationWizard(models.Model): # 3. 补全缺失的跨月违约金(按合同+年月匹配,缺失则新增) missing_penalty_lines = [] - year, month = map(int, due_month.split('-')) - first_day = datetime(year, month, 1).date() - penalty_days = (self.actual_date - first_day).days + 1 - - if month == 1: - last_month_year = year - 1 - last_month = 12 - else: - last_month_year = year - last_month = month - 1 - last_month_str = f"{last_month_year}-{last_month:02d}" - for rent_info in target_rent_months: contract = rent_info['contract_id'] due_month = rent_info['due_month'] @@ -244,8 +244,20 @@ class PropertyReconciliationWizard(models.Model): ) if check_key not in existing_penalty_months: + year, month = map(int, due_month.split('-')) + first_day = datetime(year, month, 1).date() + penalty_days = (self.actual_date - first_day).days + 1 + # 计算补全违约金的日期范围:当月1号 ~ 当月最后1天 # 查找上个月的租金明细行 + if month == 1: + last_month_year = year - 1 + last_month = 12 + else: + last_month_year = year + last_month = month - 1 + last_month_str = f"{last_month_year}-{last_month:02d}" + last_month_rent_line = next(( line for line in self.reconciliation_line_ids if line.collection_type == 'rent'