Files
gzth/yuthon_report/models/current_receivable_rent_report.py
T
2026-01-27 11:44:42 +08:00

122 lines
4.7 KiB
Python

import datetime
from odoo import api, models
class CurrentReceivableRentReport(models.Model):
_inherit = 'property.accounts.receivable'
@api.model
def get_current_receivable_rent_report(self, params):
"""
当期应收:
- 过滤应收日期区间
- 默认管理类型包含“租”,可通过 management_type_id 精确指定
- 含税金额:original_amount
- 不含税金额:original_amount / (1 + tax/100)
"""
domain = []
keyword = params.get("keyword")
if keyword:
domain += [
'|', '|',
('contract_id.contract_code', 'ilike', keyword),
('property_name', 'ilike', keyword),
('lessee_id.name', 'ilike', keyword),
]
# 日期过滤
date_start = params.get("date_start")
date_end = params.get("date_end")
if date_start:
start_dt = datetime.datetime.fromisoformat(date_start).date()
domain.append(("due_date", ">=", start_dt))
if date_end:
end_dt = datetime.datetime.fromisoformat(date_end).date()
domain.append(("due_date", "<=", end_dt))
condition = params.get('condition')
partner_id = params.get('partner_id')
company_id = params.get('company_id')
management_type_id = params.get('management_type_id')
if condition:
domain.append(('tenure_type', '=', condition))
if partner_id:
domain.append(('precinct_id', '=', partner_id))
if company_id:
domain.append(('company_id', '=', company_id))
if management_type_id:
domain.append(('management_type_id', '=', management_type_id))
# 删除默认过滤,显示所有类型
receivables = self.search(domain, order="due_date asc")
rows = []
# 按管理类型分组统计
type_totals = {} # {'管理类型名称': {'amount': 0, 'amount_nt': 0}}
for idx, rec in enumerate(receivables, start=1):
amt = rec.original_amount or 0.0
discount = rec.discount_amount or 0.0
# 实际应收 = 原始金额 - 减免金额
actual_amt = amt - discount
tax_rate = rec.tax or 0.0
actual_amt_nt = actual_amt / (1 + tax_rate / 100) if tax_rate else actual_amt
# 统计每个管理类型的合计
type_name = rec.management_type_id.name if rec.management_type_id else "未分类"
if type_name not in type_totals:
type_totals[type_name] = {'amount': 0.0, 'amount_nt': 0.0}
type_totals[type_name]['amount'] += actual_amt
type_totals[type_name]['amount_nt'] += actual_amt_nt
rows.append({
"序号": idx,
"应收开始": rec.due_date.strftime("%Y-%m-%d") if rec.due_date else "",
"应收结束": rec.due_date_end.strftime("%Y-%m-%d") if rec.due_date_end else "",
"承租方名称": rec.lessee_id.name if rec.lessee_id else "",
"原始金额": round(amt, 2),
"减免金额": round(discount, 2) if discount else "",
"实际金额(含税)": round(actual_amt, 2),
"税率": tax_rate,
"实际金额(不含税)": round(actual_amt_nt, 2),
"物业名称": rec.property_name or "",
"产权证": rec.title_deed or "",
"合同编号": rec.contract_id.contract_code if rec.contract_id else "",
"管理员": rec.admin_id.name if rec.admin_id else "",
"acc_type": rec.acc_type if rec.acc_type else False,
})
# 构造合计数据:将各分类合计作为独立的统计数据返回
summary_stats = []
total_all_amount = 0.0
total_all_amount_nt = 0.0
# 按管理类型名称排序
for type_name in sorted(type_totals.keys()):
totals = type_totals[type_name]
total_all_amount += totals['amount']
total_all_amount_nt += totals['amount_nt']
summary_stats.append({
'name': type_name,
'amount': round(totals['amount'], 2),
'amount_nt': round(totals['amount_nt'], 2)
})
# 添加总合计卡片
summary_stats.append({
'name': '总合计',
'amount': round(total_all_amount, 2),
'amount_nt': round(total_all_amount_nt, 2)
})
# 返回字典格式,只包含数据行和统计卡片数据
return {
"rows": rows,
"stats": summary_stats
}