From 616499bc72510e652c59ac324e8702d5d2b1a275 Mon Sep 17 00:00:00 2001 From: zhou <123@> Date: Thu, 31 Jul 2025 23:05:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/inherit_yuthon_property.py | 2 +- .../wizard/property_reconciliation_wizard.py | 58 ++++++++++++------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/yuthon_property_approval/models/inherit_yuthon_property.py b/yuthon_property_approval/models/inherit_yuthon_property.py index 688b0963f..2a1e3a745 100644 --- a/yuthon_property_approval/models/inherit_yuthon_property.py +++ b/yuthon_property_approval/models/inherit_yuthon_property.py @@ -344,7 +344,7 @@ class PropertyAccountsReceivable(models.Model): contract_id = fields.Many2one('property.lease.contract', string="关联租赁合同审批") tenure_type = fields.Selection(related='property_id.tenure_type', string="权属类型", store=True) precinct_id = fields.Many2one(related='property_id.precinct_id', string="所属片区", store=True) - penalty = fields.Float(string="违约金") + penalty = fields.Float(string="违约金基数") penalty_date = fields.Date(string="违约金日期") # 报表新增字段 rent_owed = fields.Float(string="欠租金额") diff --git a/yuthon_property_approval/wizard/property_reconciliation_wizard.py b/yuthon_property_approval/wizard/property_reconciliation_wizard.py index 0cb180fcb..9d230214c 100644 --- a/yuthon_property_approval/wizard/property_reconciliation_wizard.py +++ b/yuthon_property_approval/wizard/property_reconciliation_wizard.py @@ -54,26 +54,40 @@ class PropertyReconciliationWizard(models.Model): self.amount_reconciled = 0.0 return - # 没有精确匹配,按原逻辑分配 - for index, line in enumerate(self.reconciliation_line_ids): + groups = {} + for line in self.reconciliation_line_ids: + key = (line.contract_id.id if line.contract_id else None, + line.property_id.id if line.property_id else None) + if key not in groups: + groups[key] = [] + groups[key].append(line) + + # 2. 每组内按due_date升序排序(日期最早的先分配) + sorted_lines = [] + for group_key in groups: + sorted_group = sorted( + groups[group_key], + key=lambda x: x.due_date if x.due_date else fields.Date.max + ) + sorted_lines.extend(sorted_group) + + # 3. 按排序后的顺序分配金额 + for line in sorted_lines: rent = line.rent or 0 + line.amount = 0 # 重置金额 if remaining_amount <= 0: - line.amount = 0 - else: - if index < line_count - 1: - # 非最后一行:分配不超过rent的金额 - line.amount = min(rent, remaining_amount) - remaining_amount -= line.amount - else: - # 最后一行:分配剩余所有金额 - line.amount = min(rent, remaining_amount) - remaining_amount -= line.amount + continue - # 检查是否违约 - if(line.due_date and self.actual_date and - line.collection_type != 'deposit'): - # 计算应收日期的下月1号 + # 分配不超过租金和剩余金额的数值 + assign_amount = min(rent, remaining_amount) + line.amount = assign_amount + remaining_amount -= assign_amount + + # 检查是否违约(保留原有逻辑) + if (line.due_date and self.actual_date and + line.collection_type != 'deposit'): + # 计算应收日期的下月1号 next_month_first = (fields.Date.from_string(line.due_date) + relativedelta(months=1)).replace(day=1) # 如果实收日期晚于下月1号,产生违约 @@ -88,13 +102,13 @@ class PropertyReconciliationWizard(models.Model): penalty_line_vals = { 'property_id': line.property_id.id, 'contract_id': line.contract_id.id, - 'rent_month': 0, # 违约金的当月租金为0 + 'rent_month': 0, 'due_date': line.due_date, - 'rent': penalty_amount, # 违约金的应收租金为0 + 'rent': penalty_amount, 'amount': 0, - 'collection_type': 'penalty', # 收款类型为违约金 + 'collection_type': 'penalty', 'remake': f'违约金 ({late_days}天)', - 'sequence': int(line.sequence) + 1, # 顺序值为原记录+1 + 'sequence': int(line.sequence) + 1, } new_lines.append((0, 0, penalty_line_vals)) @@ -122,7 +136,7 @@ class PropertyReconciliationWizard(models.Model): # 跳过金额为0的行 if line.amount == 0 and line.collection_type != 'penalty': continue - first_day = self.actual_date.replace(day=1) + first_day = line.due_date.replace(day=1) last_day = (first_day + relativedelta(months=1)) - relativedelta(days=1) domain = [ ('lessee_id', '=', first_line.tenant_id.id), @@ -151,6 +165,7 @@ class PropertyReconciliationWizard(models.Model): self.env['property.accounts.receivable'].create({ 'is_reconciliation': True, 'due_date': line.due_date, + 'penalty': line.amount, 'management_type_id': management_type_id.id, 'lessee_id': first_line.tenant_id.id, 'property_id': line.property_id.id, @@ -162,6 +177,7 @@ class PropertyReconciliationWizard(models.Model): }) + class PropertyReconciliationLine(models.Model): _name = 'property.reconciliation.line' _description = '对账明细'