添加功能

This commit is contained in:
李鹏宇
2025-12-02 17:41:41 +08:00
parent 6290d634ec
commit 4e369fcdbd
@@ -172,8 +172,8 @@ class PropertyReconciliationWizard(models.Model):
raise UserError(f'合同{contract_id.name}类型为{contract_receivables.management_type_id.name}的应收日期为空,请先填写应收日期')
next_month_first = (fields.Date.from_string(contract_receivables.due_date) +
relativedelta(months=1)).replace(day=1)
# 检查是否产生违约
if fields.Date.from_string(self.actual_date) >= next_month_first and contract_receivables.original_amount != contract_receivables.delivered1:
# 检查是否产生违约(只计算一点没有交的)
if fields.Date.from_string(self.actual_date) >= next_month_first and contract_receivables.original_amount == contract_receivables.delivered1:
# 计算违约天数和违约金
late_days = (fields.Date.from_string(self.actual_date) - next_month_first).days + 1
# 找到符合月份的租金
@@ -249,114 +249,115 @@ class PropertyReconciliationWizard(models.Model):
total = sum(line.rent for line in self.reconciliation_line_ids)
self.receivable_total = total
all_due_dates = []
for line in self.reconciliation_line_ids:
if line.collection_type == 'rent' and line.due_date:
all_due_dates.append(fields.Date.from_string(line.due_date))
# 2. 找到最早的到期日期
if all_due_dates:
earliest_due_date = min(all_due_dates)
# 获取实际日期(假设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()
# all_due_dates = []
# for line in self.reconciliation_line_ids:
# if line.collection_type == 'rent' and line.due_date:
# all_due_dates.append(fields.Date.from_string(line.due_date))
# # 2. 找到最早的到期日期
# if all_due_dates:
# earliest_due_date = min(all_due_dates)
# # 获取实际日期(假设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 = []
#
# # 遍历从起始月到结束月的所有月份
# 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 = []
# 遍历从起始月到结束月的所有月份
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 = []
if target_rent_months:
existing_penalty_months = set()
for line in self.reconciliation_line_ids:
if line.collection_type == 'penalty' and line.due_date:
penalty_month = fields.Date.from_string(line.due_date).strftime('%Y-%m')
penalty_key = (
line.contract_id.id if line.contract_id else None,
penalty_month
)
existing_penalty_months.add(penalty_key)
# 3. 补全缺失的跨月违约金(按合同+年月匹配,缺失则新增)
missing_penalty_lines = []
for rent_info in target_rent_months:
contract = rent_info['contract_id']
due_month = rent_info['due_month']
# 构建待检查的违约金key(合同+年月)
check_key = (
contract.id if contract else None,
due_month
)
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'
and line.contract_id == contract
and line.due_date # 有到期日期
and fields.Date.from_string(line.due_date).strftime('%Y-%m') == last_month_str
), None)
# 提取上个月租金的rent金额
last_month_rent = last_month_rent_line.rent if last_month_rent_line else 0.0
penalty_amount = penalty_days * round(
last_month_rent * float(contract.percentage_liquidated_damages.name), 2
) if (contract and last_month_rent > 0) else 0.0
if penalty_amount > 0:
missing_penalty_lines.append((0, 0, {
'contract_id': contract.id if contract else None,
'rent_month': 0,
'due_date': fields.Date.to_string(first_day),
'rent': penalty_amount,
'amount': 0,
'collection_type': 'penalty',
'remake': f'违约金({fields.Date.to_string(first_day)}至{fields.Date.to_string(self.actual_date)},共{penalty_days}天',
}))
# 添加缺失的违约金记录
if missing_penalty_lines:
self.write({'reconciliation_line_ids': missing_penalty_lines})
# if target_rent_months:
# existing_penalty_months = set()
# for line in self.reconciliation_line_ids:
# if line.collection_type == 'penalty' and line.due_date:
# penalty_month = fields.Date.from_string(line.due_date).strftime('%Y-%m')
# penalty_key = (
# line.contract_id.id if line.contract_id else None,
# penalty_month
# )
# existing_penalty_months.add(penalty_key)
#
# # # 3. 补全缺失的跨月违约金(按合同+年月匹配,缺失则新增)
# # missing_penalty_lines = []
# # for rent_info in target_rent_months:
# # contract = rent_info['contract_id']
# # due_month = rent_info['due_month']
# #
# # # 构建待检查的违约金key(合同+年月)
# # check_key = (
# # contract.id if contract else None,
# # due_month
# # )
# #
# # 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'
# # and line.contract_id == contract
# # and line.due_date # 有到期日期
# # and fields.Date.from_string(line.due_date).strftime('%Y-%m') == last_month_str
# # ), None)
# # # 提取上个月租金的rent金额
# # last_month_rent = last_month_rent_line.rent if last_month_rent_line else 0.0
# # penalty_amount = penalty_days * round(
# # last_month_rent * float(contract.percentage_liquidated_damages.name), 2
# # ) if (contract and last_month_rent > 0) else 0.0
# #
# # if penalty_amount > 0:
# # missing_penalty_lines.append((0, 0, {
# # 'contract_id': contract.id if contract else None,
# # 'rent_month': 0,
# # 'due_date': fields.Date.to_string(first_day),
# # 'rent': penalty_amount,
# # 'amount': 0,
# # 'collection_type': 'penalty',
# # 'remake': f'违约金({fields.Date.to_string(first_day)}至{fields.Date.to_string(self.actual_date)},共{penalty_days}天',
# # }))
# #
# # # 添加缺失的违约金记录
# # if missing_penalty_lines:
# # self.write({'reconciliation_line_ids': missing_penalty_lines})
self.create_reconciliation_vals()
line_types = []