98 lines
3.6 KiB
Python
98 lines
3.6 KiB
Python
import datetime
|
||
import calendar
|
||
|
||
from odoo import api, models
|
||
|
||
|
||
class DepositMonthRefundReport(models.Model):
|
||
_inherit = 'property.bills.list'
|
||
|
||
@api.model
|
||
def get_deposit_month_refund_report(self, params):
|
||
"""
|
||
押金本月已退报表:
|
||
- 数据源:property.bills.list
|
||
- 管理费用类型为"押金"(management_type_id关联的记录name为"押金")
|
||
- actual_date有值且在查询月内
|
||
"""
|
||
date_start = params.get("date_start")
|
||
query_date = datetime.datetime.fromisoformat(date_start).date() if date_start else datetime.date.today()
|
||
month_first = query_date.replace(day=1)
|
||
last_day = calendar.monthrange(query_date.year, query_date.month)[1]
|
||
month_last = query_date.replace(day=last_day)
|
||
|
||
# 获取押金类型的ID
|
||
deposit_type = self.env['yuthon.management.type'].search([('name', '=', '押金')], limit=1)
|
||
|
||
domain = [
|
||
('actual_date', '!=', False),
|
||
('actual_date', '>=', month_first),
|
||
('actual_date', '<=', month_last),
|
||
]
|
||
|
||
# 如果找到了押金类型,则添加类型过滤
|
||
if deposit_type:
|
||
domain.append(('management_type_id', '=', deposit_type.id))
|
||
else:
|
||
# 如果没找到押金类型,用name匹配作为备选
|
||
domain.append(('management_type_id.name', '=', '押金'))
|
||
|
||
keyword = params.get("keyword")
|
||
if keyword:
|
||
domain += ['|', '|',
|
||
('contract_id.contract_code', 'ilike', keyword),
|
||
('property_name', 'ilike', keyword),
|
||
('lessee_id.name', 'ilike', keyword),
|
||
]
|
||
|
||
condition = params.get('condition')
|
||
partner_id = params.get('partner_id')
|
||
if condition:
|
||
domain.append(('tenure_type', '=', condition))
|
||
if partner_id:
|
||
domain.append(('precinct_id', '=', partner_id))
|
||
|
||
lines = self.sudo().search(domain, order="contract_id asc")
|
||
|
||
rows = []
|
||
total_amount = 0.0
|
||
for idx, rec in enumerate(lines, start=1):
|
||
amount = round(rec.amount_actual or 0.0, 2)
|
||
total_amount += amount
|
||
|
||
contract = rec.contract_id
|
||
# 物业穿透
|
||
property_val = ''
|
||
if contract and contract.lease_ids and contract.lease_ids[0].address_id:
|
||
prop = contract.lease_ids[0].address_id
|
||
property_val = {
|
||
'model': 'yuthon.property',
|
||
'id': prop.id,
|
||
'display_name': rec.property_name or prop.name,
|
||
}
|
||
elif rec:
|
||
property_val = rec.property_name or ''
|
||
|
||
rows.append({
|
||
"序号": idx,
|
||
"退押日期": rec.actual_date.strftime("%Y-%m-%d") if rec.actual_date else "",
|
||
"合同编号": {
|
||
'model': 'property.lease.contract',
|
||
'id': contract.id,
|
||
'display_name': contract.contract_code,
|
||
} if contract else "",
|
||
"物业名称": property_val,
|
||
"承租方名称": {
|
||
'model': 'property.tenant.information',
|
||
'id': rec.lessee_id.id,
|
||
'display_name': rec.lessee_id.name,
|
||
} if rec.lessee_id else "",
|
||
"金额": amount,
|
||
})
|
||
|
||
stats = [
|
||
{'name': '记录总数', 'amount': len(rows)},
|
||
{'name': '合计退款', 'amount': round(total_amount, 2)},
|
||
]
|
||
return {"rows": rows, "stats": stats}
|