import datetime import calendar from odoo import fields, models, api from .report_utils import _id_list # partner_id 多选适配工具 class YuthonProperty(models.Model): _inherit = 'yuthon.property' @api.model def get_complete_ledger_report(self, params): """ 物业台账报表(按查询月) - 管理面积:manage_information_ids 中 end_date >= 查询月最后一天(或 end_date 为空)且 start_time <= 查询月最后一天 的最新行的 management_area - 可租面积:同上,purpose_id.name == '可租' 的行的 rent_area - 不可租面积:同上,purpose_id.name != '可租' 的行的 management_area - 已租面积(物业):查询月内有执行中合同(actual_end_time >= 查询月底)的物业的可租面积 - 空置面积:管理类型为可租,但查询月内无执行中合同的物业的可租面积 """ # 解析查询月 date_start_str = params.get("date_start") if date_start_str: query_date = datetime.datetime.fromisoformat(date_start_str).date() else: query_date = datetime.date.today().replace(day=1) # 查询月底最后一天 last_day = calendar.monthrange(query_date.year, query_date.month)[1] month_end = query_date.replace(day=last_day) # 查询月第一天 month_start = query_date.replace(day=1) # 基础过滤条件 condition = params.get('condition', False) partner_id = _id_list(params.get('partner_id')) # 支持多选,转为列表 company_id = _id_list(params.get('company_id', False)) domain = [] if condition: domain.append(('tenure_type', '=', condition)) if partner_id: domain.append(('precinct_id', 'in', partner_id)) # 多选用 in if company_id: domain.append(('company_id', 'in', company_id)) properties = self.search(domain, limit=500) # 查询月内有执行中合同的物业 id 集合 # 合同 actual_end_time >= 查询月底 且 contact_state == 'ing' active_contracts = self.env['property.lease.contract'].search([ ('contact_state', '=', 'ing'), ('actual_end_time', '>=', month_end), ]) rented_property_ids = set() for contract in active_contracts: for line in contract.lease_ids: if line.address_id: rented_property_ids.add(line.address_id.id) result = [] for p in properties: # 从 manage_information_ids 按查询月筛选有效行 # 有效条件:start_time <= 月底 且 (end_date 为空 或 end_date >= 月底) valid_lines = p.manage_information_ids.filtered( lambda l: (not l.start_time or l.start_time <= month_end) and (not l.end_date or l.end_date >= month_end) ) if not valid_lines: continue # 取最新一条(start_time 最大的) current_line = sorted(valid_lines, key=lambda l: l.start_time or datetime.date.min, reverse=True)[0] purpose_name = current_line.purpose_id.name if current_line.purpose_id else '' management_area = current_line.management_area or 0.0 rent_area = current_line.rent_area or 0.0 if purpose_name == '可租' else 0.0 non_rentable_area = management_area if purpose_name != '可租' else 0.0 is_rented = p.id in rented_property_ids if purpose_name == '可租': rented_area = rent_area if is_rented else 0.0 vacant_area = rent_area if not is_rented else 0.0 else: rented_area = 0.0 vacant_area = 0.0 # 查询月内的执行中合同 contract_line = self.env['property.lease.contract.line'].search([ ('address_id', '=', p.id), ('lease_id.contact_state', '=', 'ing'), ('lease_id.actual_end_time', '>=', month_end), ], limit=1) contract_id = contract_line.lease_id if contract_line else False result.append({ "物业名称": p.name or "", "用途": purpose_name, "产权证号": p.title_deed or "", "产权面积": p.equity_area or 0.0, "管理面积": round(management_area, 4), "可租面积": round(rent_area, 4), "不可租面积": round(non_rentable_area, 4), "已租面积": round(rented_area, 4), "空置面积": round(vacant_area, 4), "租赁状态": "在租" if is_rented else ("空置" if purpose_name == '可租' else ""), "合同编号": { 'model': 'property.lease.contract', 'id': contract_id.id, 'display_name': contract_id.contract_code, } if contract_id else "", "承租方": { 'model': 'property.tenant.information', 'id': contract_id.lessee_id.id, 'display_name': contract_id.lessee_id.name, } if contract_id and contract_id.lessee_id else "", "单价": contract_id.unit_price if contract_id else 0, "租金": contract_id.rent if contract_id else 0, "所属公司": current_line.company_id.name if current_line.company_id else "", "管理员": current_line.admin_id.name if current_line.admin_id else "", }) return result