105 lines
4.2 KiB
Python
105 lines
4.2 KiB
Python
import datetime
|
|
import calendar
|
|
import logging
|
|
|
|
from odoo import api, models
|
|
from .report_utils import _id_list # 导入 ID 列表解析工具函数,支持多选参数
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class PropertyFeeForm(models.Model):
|
|
# 改为继承 property.fee.form 模型
|
|
_inherit = 'property.fee.form'
|
|
|
|
@api.model
|
|
def get_management_fee_monthly_report(self, params):
|
|
"""管理费本月支出报表。
|
|
|
|
数据源:property.fee.form
|
|
过滤条件:
|
|
- expenses_type.name = '管理费'
|
|
- pay_date(支付日期)在查询月本月范围内(月初~月末)
|
|
字段:标题、所属公司、支付金额、申请人、支付日期、权属类型
|
|
"""
|
|
# ========== 一、参数解析 ==========
|
|
date_start = params.get("date_start")
|
|
keyword = params.get("keyword")
|
|
|
|
# ========== 二、查询日期解析 ==========
|
|
if date_start:
|
|
try:
|
|
query_date = datetime.datetime.fromisoformat(date_start.split(' ')[0]).date()
|
|
except Exception:
|
|
query_date = datetime.date.today()
|
|
else:
|
|
query_date = datetime.date.today()
|
|
|
|
# 计算查询月月初和月末
|
|
month_first = query_date.replace(day=1)
|
|
_, last_day = calendar.monthrange(query_date.year, query_date.month)
|
|
month_last = query_date.replace(day=last_day)
|
|
|
|
# ========== 三、构建domain ==========
|
|
domain = [
|
|
('expenses_type.name', '=', '管理费'), # 支出类型为管理费
|
|
('pay_date', '>=', month_first), # 支付日期在查询月月初及以后
|
|
('pay_date', '<=', month_last), # 支付日期在查询月月末及以前
|
|
]
|
|
|
|
# 关键字过滤:按标题模糊搜索
|
|
if keyword:
|
|
domain += ['|', ('name', 'ilike', keyword),
|
|
('users_id.name', 'ilike', keyword)]
|
|
|
|
# 公司过滤:company_id1 为所属公司字段
|
|
company_id = _id_list(params.get('company_id'))
|
|
partner_id = _id_list(params.get('partner_id')) # 解析片区 ID 为整数列表(多选支持)
|
|
if company_id:
|
|
domain.append(('company_id1', 'in', company_id))
|
|
# 片区(partner_id):通过 precinct.message -> employee -> user 关联申请人
|
|
if partner_id:
|
|
# partner_id 是 yuthon.precinct.message 的 ID,需转换为 res.users ID
|
|
precincts = self.env['yuthon.precinct.message'].sudo().browse(partner_id)
|
|
user_ids = precincts.mapped('employee_id.user_id').ids
|
|
if user_ids:
|
|
domain.append(('users_id', 'in', user_ids))
|
|
else:
|
|
# 片区没有关联到任何用户,返回空结果
|
|
domain.append(('id', '=', False))
|
|
|
|
# 查询记录
|
|
records = self.sudo().search(domain, order='pay_date asc')
|
|
|
|
# ========== 四、构建报表行 ==========
|
|
rows = []
|
|
total_fee = 0.0 # 支付金额合计
|
|
|
|
for idx, rec in enumerate(records, start=1):
|
|
pay_date_str = rec.pay_date.strftime("%Y-%m-%d") if rec.pay_date else ""
|
|
fee_val = round(rec.form_fee or 0.0, 2)
|
|
total_fee += fee_val
|
|
|
|
rows.append({
|
|
"序号": idx,
|
|
"标题": rec.name or "",
|
|
"所属公司": rec.company_id1.name if rec.company_id1 else "",
|
|
"支付金额": fee_val,
|
|
"申请人": rec.users_id.name if rec.users_id else "",
|
|
"支付日期": pay_date_str,
|
|
"权属类型": dict(rec._fields['tenure_type'].selection).get(rec.tenure_type, rec.tenure_type or ""),
|
|
})
|
|
|
|
# ========== 五、构建统计汇总数据 ==========
|
|
stats = [
|
|
{'name': '记录总数', 'amount': len(rows)},
|
|
{'name': '支付合计', 'amount': round(total_fee, 2)},
|
|
]
|
|
|
|
_logger.info(
|
|
"[MANAGEMENT_FEE_MONTHLY] query_date=%s, month_range=[%s, %s], rows=%d, total_fee=%.2f",
|
|
query_date, month_first, month_last, len(rows), total_fee,
|
|
)
|
|
|
|
return {"rows": rows, "stats": stats}
|