From 46fca553a0cfbc6e32ce6b56b9fb66d70edd4b57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= Date: Mon, 22 Jun 2026 17:25:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=81=9C=E8=BD=A6=E8=B4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parking_fee_current_month_receivable.py | 553 +++++++++++++++--- .../parking_fee_current_month_report.py | 185 +++--- .../models/parking_fee_yearly_report.py | 205 ++++--- yuthon_report/models/yuthon_dashboard.py | 4 +- 4 files changed, 703 insertions(+), 244 deletions(-) diff --git a/yuthon_report/models/parking_fee_current_month_receivable.py b/yuthon_report/models/parking_fee_current_month_receivable.py index 06d96eb1e..f9ce821c7 100644 --- a/yuthon_report/models/parking_fee_current_month_receivable.py +++ b/yuthon_report/models/parking_fee_current_month_receivable.py @@ -1,8 +1,12 @@ import datetime +import calendar +import logging from odoo import api, models from .report_utils import _id_list # partner_id 多选适配工具 +_logger = logging.getLogger(__name__) + class ParkingFeeCurrentMonthReceivable(models.Model): _inherit = 'property.accounts.receivable' @@ -10,108 +14,519 @@ class ParkingFeeCurrentMonthReceivable(models.Model): @api.model def get_parking_fee_current_month_receivable(self, params): """ - 停车费本月应收: - - 默认过滤当月应收日期;可用 date_start/date_end 覆盖 - - 默认管理类型名称含“车”,也可传 management_type_id 精确指定 - - 金额取应收 original_amount - """ - domain = [] + 停车费本月应收 = 当期应收 + 前期欠收 - 前期预收 + 应收类型只取"停车费",其余逻辑同经营收入板块的本月应收 - # 关键词 - keyword = params.get("keyword") - # summary_only:看板只取 stats 不构建 rows + ┌──────────────────────────────────────────────────────────┐ + │ 当期应收(+):property.accounts.receivable │ + │ due_date 在查询月内,management_type = '停车费' │ + │ 金额 = original_amount - discount_amount │ + ├──────────────────────────────────────────────────────────┤ + │ 前期欠收(+):两路数据源 │ + │ Part1: receivable.modify.line │ + │ receivable_id.due_date < 查询月 │ + │ receivable_id.management_type = '停车费' │ + │ reconciliation_date有值 且 所在月 > due_date月 │ + │ 且 >= 查询月 │ + │ 金额 = line.amount │ + │ Part2: property.accounts.receivable │ + │ is_reconciliation=False 且 due_date < 查询月 │ + │ 且 payout_date=False 且 management_type='停车费' │ + │ 且 id 不在 Part1 已覆盖的 │ + │ 金额 = submitted │ + ├──────────────────────────────────────────────────────────┤ + │ 前期预收(-):receivable.modify.line │ + │ receivable_id.management_type = '停车费' │ + │ reconciliation_date有值 且 < due_date(提前收款) │ + │ 且 reconciliation_date < 查询月第一天 │ + │ 且不在同一年同一月(跨月) │ + │ 金额 = line.amount │ + └──────────────────────────────────────────────────────────┘ + """ + + # ---------- 时间参数 ---------- + date_start = params.get("date_start") + date_end = params.get("date_end") summary_only = params.get("summary_only", False) + + if date_start: + query_month_start = datetime.datetime.fromisoformat(date_start).date() + else: + query_month_start = datetime.date.today().replace(day=1) + if date_end: + query_month_end = datetime.datetime.fromisoformat(date_end).date() + else: + # 查询月最后一天 + last_day = calendar.monthrange(query_month_start.year, query_month_start.month)[1] + query_month_end = query_month_start.replace(day=last_day) + + # ---------- 通用筛选参数 ---------- + keyword = params.get("keyword") + condition = params.get('condition') + partner_id = _id_list(params.get('partner_id')) + company_id = _id_list(params.get('company_id')) + property_id = params.get('property_id') + lessee_id = params.get('lessee_id') + contract_id = params.get('contract_id') + + # ---------- 停车费类型固定过滤 ---------- + # 只取应收类型 = '停车费' + parking_type = self.env['yuthon.management.type'].sudo().search([('name', '=', '停车费')], limit=1) + if not parking_type: + # 没有停车费类型记录,返回0 + _logger.warning("[PARKING_FEE] yuthon.management.type 中未找到'停车费'") + return {"rows": [], "stats": [ + {'name': '当期应收', 'amount': 0}, + {'name': '前期欠收', 'amount': 0}, + {'name': '前期预收', 'amount': 0}, + {'name': '本月应收', 'amount': 0}, + ]} + + # ================================================================ + # (+) 当期应收:property.accounts.receivable + # due_date 在查询月范围内,management_type = 停车费 + # ================================================================ + receivable_domain = [ + ('is_invalid', '!=', True), + ('due_date', '>=', query_month_start), + ('due_date', '<=', query_month_end), + ('management_type_id', '=', parking_type.id), + ] + # 通用筛选(accounts.receivable 自身字段,无 receivable_id. 前缀) if keyword: - domain += [ + receivable_domain += [ '|', '|', ('contract_id.contract_code', 'ilike', keyword), ('property_name', 'ilike', keyword), ('lessee_id.name', 'ilike', keyword), ] - - # 时间范围(默认当月) - today = datetime.date.today() - month_start = today.replace(day=1) - if month_start.month == 12: - month_end = month_start.replace(year=month_start.year + 1, month=1) - datetime.timedelta(days=1) - else: - month_end = month_start.replace(month=month_start.month + 1) - datetime.timedelta(days=1) - - date_start = params.get("date_start") - date_end = params.get("date_end") - start_dt = datetime.datetime.fromisoformat(date_start).date() if date_start else month_start - end_dt = datetime.datetime.fromisoformat(date_end).date() if date_end else month_end - - domain.append(("due_date", ">=", start_dt)) - domain.append(("due_date", "<=", end_dt)) - - # 其他过滤 - condition = params.get('condition') - partner_id = _id_list(params.get('partner_id')) # 支持多选,转为列表 - company_id = _id_list(params.get('company_id')) - management_type_id = params.get('management_type_id') - if condition: - domain.append(('tenure_type', '=', condition)) + receivable_domain.append(('tenure_type', '=', condition)) if partner_id: - domain.append(('precinct_id', 'in', partner_id)) # 多选用 in + receivable_domain.append(('precinct_id', 'in', partner_id)) if company_id: - domain.append(('company_id', 'in', company_id)) + receivable_domain.append(('company_id', 'in', company_id)) + if property_id: + receivable_domain.append(('property_ids', 'in', [property_id])) + if lessee_id: + receivable_domain.append(('lessee_id', '=', lessee_id)) + if contract_id: + receivable_domain.append(('contract_id', '=', contract_id)) - # 管理类型 - if management_type_id: - type_domain = domain + [('management_type_id', '=', management_type_id)] - else: - parking_types = self.env['yuthon.management.type'].search([('name', 'ilike', '车')]) - if parking_types: - type_domain = domain + [('management_type_id', 'in', parking_types.ids)] - else: - type_domain = domain[:] + receivables = self.env['property.accounts.receivable'].sudo().search( + receivable_domain, order="due_date asc") + # 当期应收金额 = original_amount - discount_amount + current_receivable_amount = sum( + (rec.original_amount or 0.0) - (rec.discount_amount or 0.0) + for rec in receivables + ) + _logger.info("[PARKING_FEE] 当期应收: %d 条, 金额=%.2f", len(receivables), current_receivable_amount) - receivables = self.search(type_domain, limit=2000, order="due_date asc") - if not receivables and type_domain != domain: - receivables = self.search(domain, limit=2000, order="due_date asc") + # ================================================================ + # (+) 前期欠收 Part1:receivable.modify.line + # receivable_id.due_date < 查询月 且 management_type = 停车费 + # reconciliation_date 有值 且 所在月 > due_date月 且 >= 查询月 + # ================================================================ + part1_domain = [ + ('receivable_id.due_date', '<', query_month_start), + ('receivable_id.is_invalid', '!=', True), + ('receivable_id.management_type_id', '=', parking_type.id), + ] + # 通用筛选(receivable_id. 前缀) + if keyword: + part1_domain += [ + '|', '|', + ('receivable_id.contract_id.contract_code', 'ilike', keyword), + ('receivable_id.property_name', 'ilike', keyword), + ('receivable_id.lessee_id.name', 'ilike', keyword), + ] + if condition: + part1_domain.append(('receivable_id.tenure_type', '=', condition)) + if partner_id: + part1_domain.append(('receivable_id.precinct_id', 'in', partner_id)) + if company_id: + part1_domain.append(('receivable_id.company_id', 'in', company_id)) + if property_id: + part1_domain.append(('receivable_id.property_ids', 'in', [property_id])) + if lessee_id: + part1_domain.append(('receivable_id.lessee_id', '=', lessee_id)) + if contract_id: + part1_domain.append(('receivable_id.contract_id', '=', contract_id)) - rows = [] - total_amount = 0.0 - # summary_only:跳过 rows 构建,直接算 stats + lines = self.env['receivable.modify.line'].sudo().search( + part1_domain, order="receivable_id asc, reconciliation_date asc") + # 内存过滤:reconciliation_date 有值 且 所在月 > due_date月 且 >= 查询月 + due_month_start = query_month_start.replace(day=1) + filtered_lines = lines.filtered( + lambda l: l.reconciliation_date and + l.reconciliation_date.replace(day=1) > l.receivable_id.due_date.replace(day=1) and + l.reconciliation_date.replace(day=1) >= due_month_start + ) + # 按应收账单聚合,记录已覆盖的 receivable_id(Part2 去重用) + covered_ids = set() + part1_amount = 0.0 + # Part1 明细(用于构建 rows) + part1_rec_map = {} + for line in filtered_lines: + rid = line.receivable_id.id + covered_ids.add(rid) + part1_amount += line.amount or 0.0 + # 明细聚合 + if rid not in part1_rec_map: + part1_rec_map[rid] = { + 'rec': line.receivable_id, + 'amount': 0.0, + 'payout_dates': [], + } + part1_rec_map[rid]['amount'] += line.amount or 0.0 + part1_rec_map[rid]['payout_dates'].append(line.reconciliation_date) + + _logger.info("[PARKING_FEE] 前期欠收Part1: %d 条modify.line, 金额=%.2f", len(filtered_lines), part1_amount) + + # ================================================================ + # (+) 前期欠收 Part2:property.accounts.receivable + # is_reconciliation=False 且 due_date < 查询月 且 payout_date=False + # management_type = 停车费 且 id 不在 Part1 的 covered_ids 中 + # ================================================================ + part2_domain = [ + ('is_reconciliation', '=', False), + ('due_date', '<', query_month_start), + ('payout_date', '=', False), + ('is_invalid', '!=', True), + ('management_type_id', '=', parking_type.id), + ] + if covered_ids: + part2_domain.append(('id', 'not in', list(covered_ids))) + # 通用筛选(accounts.receivable 自身字段) + if keyword: + part2_domain += [ + '|', '|', + ('contract_id.contract_code', 'ilike', keyword), + ('property_name', 'ilike', keyword), + ('lessee_id.name', 'ilike', keyword), + ] + if condition: + part2_domain.append(('tenure_type', '=', condition)) + if partner_id: + part2_domain.append(('precinct_id', 'in', partner_id)) + if company_id: + part2_domain.append(('company_id', 'in', company_id)) + if property_id: + part2_domain.append(('property_ids', 'in', [property_id])) + if lessee_id: + part2_domain.append(('lessee_id', '=', lessee_id)) + if contract_id: + part2_domain.append(('contract_id', '=', contract_id)) + + unreconciled_recs = self.env['property.accounts.receivable'].sudo().search( + part2_domain, order="due_date asc") + # Part2 明细聚合 + part2_rec_map = {} + part2_amount = 0.0 + for rec in unreconciled_recs: + rid = rec.id + part2_amount += rec.submitted or 0.0 + part2_rec_map[rid] = { + 'rec': rec, + 'amount': rec.submitted or 0.0, + 'payout_dates': [], + } + + _logger.info("[PARKING_FEE] 前期欠收Part2: %d 条accounts.receivable, 金额=%.2f", + len(unreconciled_recs), part2_amount) + + # ================================================================ + # (-) 前期预收:receivable.modify.line + # receivable_id.management_type = 停车费 + # reconciliation_date有值 且 < due_date(提前收款) + # 且 reconciliation_date < 查询月第一天(前期收的) + # 且不在同一年同一月(跨月) + # ================================================================ + pre_advance_domain = [ + ('reconciliation_date', '!=', False), + ('receivable_id.due_date', '>=', query_month_start), + ('receivable_id.is_invalid', '!=', True), + ('receivable_id.management_type_id', '=', parking_type.id), + ] + # 通用筛选(receivable_id. 前缀) + if keyword: + pre_advance_domain += [ + '|', '|', + ('receivable_id.contract_id.contract_code', 'ilike', keyword), + ('receivable_id.property_name', 'ilike', keyword), + ('receivable_id.lessee_id.name', 'ilike', keyword), + ] + if condition: + pre_advance_domain.append(('receivable_id.tenure_type', '=', condition)) + if partner_id: + pre_advance_domain.append(('receivable_id.precinct_id', 'in', partner_id)) + if company_id: + pre_advance_domain.append(('receivable_id.company_id', 'in', company_id)) + if property_id: + pre_advance_domain.append(('receivable_id.property_ids', 'in', [property_id])) + if lessee_id: + pre_advance_domain.append(('receivable_id.lessee_id', '=', lessee_id)) + if contract_id: + pre_advance_domain.append(('receivable_id.contract_id', '=', contract_id)) + + pre_lines = self.env['receivable.modify.line'].sudo().search( + pre_advance_domain, order="receivable_id asc, reconciliation_date asc") + # 内存过滤:三个条件必须同时满足 + pre_advance_amount = 0.0 + pre_advance_rec_map = {} + for line in pre_lines: + # 取收款日期(统一转为 date) + pay_date = line.reconciliation_date + if not pay_date: + continue + if hasattr(pay_date, 'date'): + pay_date = pay_date.date() + # 取应收日期 + due = line.receivable_id.due_date if line.receivable_id else None + if not due: + continue + # 条件1:提前收款(reconciliation_date < due_date) + if not (pay_date < due): + continue + # 条件2:跨月(不在同一年同一月) + if pay_date.year == due.year and pay_date.month == due.month: + continue + # 条件3:收款在查询月之前(前期预收) + if not (pay_date < query_month_start): + continue + rid = line.receivable_id.id + line_amount = line.amount or 0.0 + pre_advance_amount += line_amount + # 明细聚合 + if rid not in pre_advance_rec_map: + pre_advance_rec_map[rid] = { + 'rec': line.receivable_id, + 'amount': 0.0, + 'earliest_pay_date': pay_date, + } + pre_advance_rec_map[rid]['amount'] += line_amount + # 取最早收款日期 + if pay_date < pre_advance_rec_map[rid]['earliest_pay_date']: + pre_advance_rec_map[rid]['earliest_pay_date'] = pay_date + + _logger.info("[PARKING_FEE] 前期预收: 金额=%.2f", pre_advance_amount) + + # ================================================================ + # 合计:本月应收 = 当期应收 + 前期欠收(Part1+Part2) - 前期预收 + # ================================================================ + month_receivable = current_receivable_amount + part1_amount + part2_amount - pre_advance_amount + + # summary_only:看板只取 stats,不构建 rows if summary_only: - total_amount = sum(rec.original_amount or 0.0 for rec in receivables) - return {"rows": [], "stats": [{"name": "合计", "amount": round(total_amount, 2)}]} - for idx, rec in enumerate(receivables, start=1): - amount = rec.original_amount or 0.0 - total_amount += amount + return {"rows": [], "stats": [ + {'name': '当期应收', 'amount': round(current_receivable_amount, 2)}, + {'name': '前期欠收', 'amount': round(part1_amount + part2_amount, 2)}, + {'name': '前期预收', 'amount': round(pre_advance_amount, 2)}, + {'name': '本月应收', 'amount': round(month_receivable, 2)}, + ]} + + # ================================================================ + # 构建明细 rows + # ================================================================ + rows = [] + idx = 1 + + # 当期应收行 + for rec in receivables: + amount = round((rec.original_amount or 0.0) - (rec.discount_amount or 0.0), 2) + 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, + "类别": "当期应收", "合同编号": { 'model': 'property.lease.contract', - 'id': rec.contract_id.id, - 'display_name': rec.contract_id.contract_code, - } if rec.contract_id else "", - "物业名称": { - 'model': 'yuthon.property', - 'id': rec.contract_id.lease_ids[0].address_id.id, - 'display_name': rec.property_name, - } if rec.contract_id and rec.contract_id.lease_ids and rec.contract_id.lease_ids[0].address_id else rec.property_name or "", + '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 "", - "金额": round(amount, 2), - "应收金额": round((rec.original_amount or 0.0) - (rec.discount_amount or 0.0), 2), - "所属公司": rec.company_id.referred if rec.company_id and hasattr(rec.company_id, 'referred') else rec.company_id.name if rec.company_id else "", + "应收类型": '停车费', + "应收开始": 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 '', + "金额": amount, + "应收金额": amount, + "收款日期": '', + "所属公司": contract.lessor_id.name if contract and contract.lessor_id else "", "管理员": { 'model': 'res.users', 'id': rec.admin_id.id, 'display_name': rec.admin_id.name, } if rec.admin_id else "", }) + idx += 1 + + # 前期欠收 Part1 行(交晚了) + for rid, data in part1_rec_map.items(): + rec = data['rec'] + amount = round(data['amount'], 2) + contract = rec.contract_id + # 收款日期(多个用逗号分隔) + payout_dates = data.get('payout_dates', []) + payout_date_str = ', '.join( + [d.strftime('%Y-%m-%d') for d in sorted(set( + d.date() if hasattr(d, 'date') else d for d in payout_dates + ))] + ) if payout_dates else '' + # 物业穿透 + 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 '' + receivable_amount = round((rec.original_amount or 0.0) - (rec.discount_amount or 0.0), 2) + rows.append({ + "序号": idx, + "类别": "前期欠收", + "合同编号": { + '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 "", + "应收类型": '停车费', + "应收开始": 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 '', + "金额": amount, + "应收金额": receivable_amount, + "收款日期": payout_date_str, + "所属公司": contract.lessor_id.name if contract and contract.lessor_id else "", + "管理员": { + 'model': 'res.users', + 'id': rec.admin_id.id, + 'display_name': rec.admin_id.name, + } if rec.admin_id else "", + }) + idx += 1 + + # 前期欠收 Part2 行(从未收款) + for rid, data in part2_rec_map.items(): + rec = data['rec'] + amount = round(data['amount'], 2) + 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 '' + receivable_amount = round((rec.original_amount or 0.0) - (rec.discount_amount or 0.0), 2) + rows.append({ + "序号": idx, + "类别": "前期欠收", + "合同编号": { + '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 "", + "应收类型": '停车费', + "应收开始": 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 '', + "金额": amount, + "应收金额": receivable_amount, + "收款日期": '', + "所属公司": contract.lessor_id.name if contract and contract.lessor_id else "", + "管理员": { + 'model': 'res.users', + 'id': rec.admin_id.id, + 'display_name': rec.admin_id.name, + } if rec.admin_id else "", + }) + idx += 1 + + # 前期预收行(提前收款) + for rid, data in pre_advance_rec_map.items(): + rec = data['rec'] + amount = round(data['amount'], 2) + contract = rec.contract_id + earliest_pay_date = data.get('earliest_pay_date') + # 物业穿透 + 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 '' + receivable_amount = round((rec.original_amount or 0.0) - (rec.discount_amount or 0.0), 2) + rows.append({ + "序号": idx, + "类别": "前期预收", + "合同编号": { + '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 "", + "应收类型": '停车费', + "应收开始": 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 '', + "金额": -amount, # 预收为负值(减项) + "应收金额": receivable_amount, + "收款日期": earliest_pay_date.strftime('%Y-%m-%d') if earliest_pay_date else '', + "所属公司": contract.lessor_id.name if contract and contract.lessor_id else "", + "管理员": { + 'model': 'res.users', + 'id': rec.admin_id.id, + 'display_name': rec.admin_id.name, + } if rec.admin_id else "", + }) + idx += 1 return { "rows": rows, "stats": [ - {"name": "合计", "amount": round(total_amount, 2)}, + {'name': '当期应收', 'amount': round(current_receivable_amount, 2)}, + {'name': '前期欠收', 'amount': round(part1_amount + part2_amount, 2)}, + {'name': '前期预收', 'amount': round(pre_advance_amount, 2)}, + {'name': '本月应收', 'amount': round(month_receivable, 2)}, ], } - diff --git a/yuthon_report/models/parking_fee_current_month_report.py b/yuthon_report/models/parking_fee_current_month_report.py index 5a6951c26..9f662ea1c 100644 --- a/yuthon_report/models/parking_fee_current_month_report.py +++ b/yuthon_report/models/parking_fee_current_month_report.py @@ -1,123 +1,138 @@ import datetime +import calendar from odoo import api, models -from .report_utils import _id_list # partner_id 多选适配工具 +from .report_utils import _id_list class ParkingFeeCurrentMonthReport(models.Model): - _inherit = 'property.accounts.receivable' + _inherit = 'receivable.modify.line' @api.model def get_parking_fee_current_month_report(self, params): """ 停车费本月实收: - - 默认过滤当月收款日期,收款日期为空则用应收日期兜底 - - 默认管理类型名称含“车”,可通过 management_type_id 精确指定 - - 金额取已收 delivered1 + 取数逻辑:收款日期(reconciliation_date)在查询月内的所有停车费收款记录,金额取 amount + 应收类型只取"停车费",其余逻辑同实收总额(total_received_report) """ - domain = [] - - keyword = params.get("keyword") - # summary_only:看板只取 stats 不构建 rows summary_only = params.get("summary_only", False) - if keyword: + + # 停车费类型固定过滤:只取应收类型 = '停车费' + parking_type = self.env['yuthon.management.type'].sudo().search( + [('name', '=', '停车费')], limit=1) + + domain = [ + ('reconciliation_date', '!=', False), + ('receivable_id.is_invalid', '!=', True), + ] + # 固定只取停车费 + if parking_type: + domain.append(('receivable_id.management_type_id', '=', parking_type.id)) + + # 收款日期在查询月内 + if params.get('date_start'): + query_date = datetime.datetime.fromisoformat(params['date_start']).date() + month_start = query_date.replace(day=1) + last_day = calendar.monthrange(query_date.year, query_date.month)[1] + month_end = query_date.replace(day=last_day) domain += [ - '|', '|', - ('contract_id.contract_code', 'ilike', keyword), - ('property_name', 'ilike', keyword), - ('lessee_id.name', 'ilike', keyword), + ('reconciliation_date', '>=', month_start), + ('reconciliation_date', '<=', month_end), ] - # 当月时间范围 - today = datetime.date.today() - month_start = today.replace(day=1) - if month_start.month == 12: - month_end = month_start.replace(year=month_start.year + 1, month=1) - datetime.timedelta(days=1) - else: - month_end = month_start.replace(month=month_start.month + 1) - datetime.timedelta(days=1) - - date_start = params.get("date_start") - date_end = params.get("date_end") - start_dt = datetime.datetime.fromisoformat(date_start).date() if date_start else month_start - end_dt = datetime.datetime.fromisoformat(date_end).date() if date_end else month_end + if params.get('keyword'): + keyword = params['keyword'] + domain += ['|', '|', + ('receivable_id.lessee_id.name', 'ilike', keyword), + ('receivable_id.property_name', 'ilike', keyword), + ('receivable_id.contract_id.contract_code', 'ilike', keyword), + ] condition = params.get('condition') - partner_id = _id_list(params.get('partner_id')) # 支持多选,转为列表 + partner_id = _id_list(params.get('partner_id')) company_id = _id_list(params.get('company_id')) - management_type_id = params.get('management_type_id') + property_id = params.get('property_id') + lessee_id = params.get('lessee_id') + contract_id = params.get('contract_id') if condition: - domain.append(('tenure_type', '=', condition)) + domain.append(('receivable_id.tenure_type', '=', condition)) if partner_id: - domain.append(('precinct_id', 'in', partner_id)) # 多选用 in + domain.append(('receivable_id.precinct_id', 'in', partner_id)) if company_id: - domain.append(('company_id', 'in', company_id)) + domain.append(('receivable_id.company_id', 'in', company_id)) + if property_id: + domain.append(('receivable_id.property_ids', 'in', [property_id])) + if lessee_id: + domain.append(('receivable_id.lessee_id', '=', lessee_id)) + if contract_id: + domain.append(('receivable_id.contract_id', '=', contract_id)) - # 管理类型 - if management_type_id: - type_domain = domain + [('management_type_id', '=', management_type_id)] - else: - parking_types = self.env['yuthon.management.type'].search([('name', 'ilike', '车')]) - if parking_types: - type_domain = domain + [('management_type_id', 'in', parking_types.ids)] - else: - type_domain = domain[:] + lines = self.search(domain, order='reconciliation_date asc') - # 日期过滤:收款日期优先,收款为空则应收日期 - date_domain = [ - '|', - '&', ('payout_date', '>=', start_dt), ('payout_date', '<=', end_dt), - '&', ('payout_date', '=', False), ('due_date', '>=', start_dt), ('due_date', '<=', end_dt), - ] - - receivables = self.search(type_domain + date_domain, limit=2000, order="payout_date asc, due_date asc") - if not receivables: - receivables = self.search(type_domain, limit=2000, order="payout_date asc, due_date asc") - if not receivables and type_domain != domain: - receivables = self.search(domain + date_domain, limit=2000, order="payout_date asc, due_date asc") - if not receivables and type_domain != domain: - receivables = self.search(domain, limit=2000, order="payout_date asc, due_date asc") + if summary_only: + total_amount = sum(round(line.amount or 0.0, 2) for line in lines) + stats = [{'name': '合计', 'amount': round(total_amount, 2)}] + return {'rows': [], 'stats': stats} rows = [] total_amount = 0.0 - # summary_only:跳过 rows 构建,直接算 stats - if summary_only: - total_amount = sum(rec.delivered1 or 0.0 for rec in receivables) - return {"rows": [], "stats": [{"name": "合计", "amount": round(total_amount, 2)}]} - for idx, rec in enumerate(receivables, start=1): - amount = rec.delivered1 or 0.0 + for idx, line in enumerate(lines, start=1): + rec = line.receivable_id + contract = rec.contract_id if rec else None + amount = round(line.amount or 0.0, 2) total_amount += amount - rows.append({ - "序号": idx, - "合同编号": { - 'model': 'property.lease.contract', - 'id': rec.contract_id.id, - 'display_name': rec.contract_id.contract_code, - } if rec.contract_id else "", - "物业名称": { + + # 物业穿透 + 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': rec.contract_id.lease_ids[0].address_id.id, - 'display_name': rec.property_name, - } if rec.contract_id and rec.contract_id.lease_ids and rec.contract_id.lease_ids[0].address_id else rec.property_name or "", - "承租方名称": { + 'id': prop.id, + 'display_name': rec.property_name or prop.name, + } + elif rec: + property_val = rec.property_name or '' + + # 合同穿透 + contract_val = '' + if contract: + contract_val = { + 'model': 'property.lease.contract', + 'id': contract.id, + 'display_name': contract.contract_code or '', + } + + # 承租方穿透 + lessee_val = '' + if rec and rec.lessee_id: + lessee_val = { 'model': 'property.tenant.information', 'id': rec.lessee_id.id, 'display_name': rec.lessee_id.name, - } if rec.lessee_id else "", - "金额": round(amount, 2), - "应收金额": round((rec.original_amount or 0.0) - (rec.discount_amount or 0.0), 2), - "所属公司": rec.company_id.referred if rec.company_id and hasattr(rec.company_id, 'referred') else rec.company_id.name if rec.company_id else "", - "管理员": { + } + + # 应收金额 = original_amount - discount_amount + receivable_amount = round((rec.original_amount or 0.0) - (rec.discount_amount or 0.0), 2) + + rows.append({ + '序号': idx, + '物业名称': property_val, + '合同编号': contract_val, + '承租方名称': lessee_val, + '收款类型': '停车费', + '应收开始': rec.due_date.strftime('%Y-%m-%d') if rec and rec.due_date else '', + '应收结束': rec.due_date_end.strftime('%Y-%m-%d') if rec and rec.due_date_end else '', + '金额': amount, + '应收金额': receivable_amount, + '所属公司': contract.lessor_id.name if contract and contract.lessor_id else '', + '管理员': { 'model': 'res.users', 'id': rec.admin_id.id, 'display_name': rec.admin_id.name, - } if rec.admin_id else "", + } if rec.admin_id else '', }) - return { - "rows": rows, - "stats": [ - {"name": "合计", "amount": round(total_amount, 2)}, - ], - } - + stats = [{'name': '合计', 'amount': round(total_amount, 2)}] + return {'rows': rows, 'stats': stats} diff --git a/yuthon_report/models/parking_fee_yearly_report.py b/yuthon_report/models/parking_fee_yearly_report.py index 58c7735ab..f2dc032cb 100644 --- a/yuthon_report/models/parking_fee_yearly_report.py +++ b/yuthon_report/models/parking_fee_yearly_report.py @@ -1,128 +1,157 @@ +import calendar import datetime from odoo import api, models -from .report_utils import _id_list # partner_id 多选适配工具 +from .report_utils import _id_list class ParkingFeeYearlyReport(models.Model): - _inherit = 'property.accounts.receivable' + _inherit = 'receivable.modify.line' @api.model def get_parking_fee_yearly_report(self, params): """ - 停车费本年累计: - - 默认取当年1月1日至12月31日的停车费账单 - - 允许传入 date_start/date_end 覆盖 - - 金额取 original_amount + 停车费本年累计(数据源:receivable.modify.line): + - 查询范围:年初(1月1日)到查询月末 + 例如查3月 → 1月1日 ~ 3月31日;查6月 → 1月1日 ~ 6月30日 + - 用 reconciliation_date(收款行对账日期)判断已收 + - 取每行的 amount(已对金额) + - 应收类型只取"停车费" + 其余逻辑同累计收入(cum_income_report) """ - domain = [] + today = datetime.date.today() - # 关键词 - keyword = params.get("keyword") - # summary_only:看板只取 stats 不构建 rows + date_start = params.get("date_start") summary_only = params.get("summary_only", False) + # 取查询日所在年份和月份 + if date_start: + query_date = datetime.datetime.fromisoformat(date_start).date() + else: + query_date = today + + year = query_date.year + query_month = query_date.month + + # 查询范围:年初到查询月末 + year_start = datetime.date(year, 1, 1) + last_day = calendar.monthrange(year, query_month)[1] + query_month_end = datetime.date(year, query_month, last_day) + + # 停车费类型固定过滤:只取应收类型 = '停车费' + parking_type = self.env['yuthon.management.type'].sudo().search( + [('name', '=', '停车费')], limit=1) + + # 用 reconciliation_date(收款行自身的对账日期)做时间范围筛选 + domain = [ + ('reconciliation_date', '>=', year_start), + ('reconciliation_date', '<=', query_month_end), + ('receivable_id.is_invalid', '!=', True), + ] + # 固定只取停车费 + if parking_type: + domain.append(('receivable_id.management_type_id', '=', parking_type.id)) + + keyword = params.get("keyword") if keyword: domain += [ '|', '|', - ('contract_id.contract_code', 'ilike', keyword), - ('property_name', 'ilike', keyword), - ('lessee_id.name', 'ilike', keyword), + ('receivable_id.contract_id.contract_code', 'ilike', keyword), + ('receivable_id.property_name', 'ilike', keyword), + ('receivable_id.lessee_id.name', 'ilike', keyword), ] - # 时间范围(默认当年) - today = datetime.date.today() - year_start = datetime.date(today.year, 1, 1) - year_end = datetime.date(today.year, 12, 31) - date_start = params.get("date_start") - date_end = params.get("date_end") - start_dt = datetime.datetime.fromisoformat(date_start).date() if date_start else year_start - end_dt = datetime.datetime.fromisoformat(date_end).date() if date_end else year_end - - domain.append(("due_date", ">=", start_dt)) - domain.append(("due_date", "<=", end_dt)) - - # 过滤条件 condition = params.get('condition') - partner_id = _id_list(params.get('partner_id')) # 支持多选,转为列表 + partner_id = _id_list(params.get('partner_id')) company_id = _id_list(params.get('company_id')) - management_type_id = params.get('management_type_id') + property_id = params.get('property_id') + lessee_id = params.get('lessee_id') + contract_id = params.get('contract_id') if condition: - domain.append(('tenure_type', '=', condition)) + domain.append(('receivable_id.tenure_type', '=', condition)) if partner_id: - domain.append(('precinct_id', 'in', partner_id)) # 多选用 in + domain.append(('receivable_id.precinct_id', 'in', partner_id)) if company_id: - domain.append(('company_id', 'in', company_id)) + domain.append(('receivable_id.company_id', 'in', company_id)) + if property_id: + domain.append(('receivable_id.property_ids', 'in', [property_id])) + if lessee_id: + domain.append(('receivable_id.lessee_id', '=', lessee_id)) + if contract_id: + domain.append(('receivable_id.contract_id', '=', contract_id)) - if management_type_id: - domain.append(('management_type_id', '=', management_type_id)) - parking_domain = domain[:] - else: - # 默认匹配管理类型名称含“车”(停车/车位等),若无匹配则不过滤 - parking_types = self.env['yuthon.management.type'].search([('name', 'ilike', '车')]) - if parking_types: - parking_domain = domain + [('management_type_id', 'in', parking_types.ids)] - else: - parking_domain = domain[:] + lines = self.search(domain, order="reconciliation_date asc") - date_domain = [ - '|', - '&', ('payout_date', '>=', start_dt), ('payout_date', '<=', end_dt), - '&', ('payout_date', '=', False), ('due_date', '>=', start_dt), ('due_date', '<=', end_dt), - ] - - # 1) 按停车管理类型 + 日期 - receivables = self.search(parking_domain + date_domain, limit=3000, order="payout_date asc, due_date asc") - # 2) 无结果时,移除日期过滤 - if not receivables: - receivables = self.search(parking_domain, limit=3000, order="payout_date asc, due_date asc") - # 3) 仍无结果,移除停车管理类型过滤(仅保留日期) - if not receivables and parking_domain != domain: - receivables = self.search(domain + date_domain, limit=3000, order="payout_date asc, due_date asc") - # 4) 仍无结果,再移除日期,彻底兜底 - if not receivables and parking_domain != domain: - receivables = self.search(domain, limit=3000, order="payout_date asc, due_date asc") + if summary_only: + total_amount = sum(round(line.amount or 0.0, 2) for line in lines) + stats = [ + {'name': '记录总数', 'amount': len(lines)}, + {'name': '本年累计', 'amount': round(total_amount, 2)}, + ] + return {"rows": [], "stats": stats} rows = [] total_amount = 0.0 - # summary_only:跳过 rows 构建,直接算 stats - if summary_only: - total_amount = sum(rec.original_amount or 0.0 for rec in receivables) - return {"rows": [], "stats": [{"name": "合计", "amount": round(total_amount, 2)}]} - for idx, rec in enumerate(receivables, start=1): - amount = rec.original_amount or 0.0 + for idx, line in enumerate(lines, start=1): + rec = line.receivable_id + contract = rec.contract_id if rec else None + amount = round(line.amount or 0.0, 2) total_amount += amount - rows.append({ - "序号": idx, - "合同编号": { - 'model': 'property.lease.contract', - 'id': rec.contract_id.id, - 'display_name': rec.contract_id.contract_code, - } if rec.contract_id else "", - "物业名称": { + + # 物业穿透 + 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': rec.contract_id.lease_ids[0].address_id.id, - 'display_name': rec.property_name, - } if rec.contract_id and rec.contract_id.lease_ids and rec.contract_id.lease_ids[0].address_id else rec.property_name or "", - "承租方名称": { + 'id': prop.id, + 'display_name': rec.property_name or prop.name, + } + elif rec: + property_val = rec.property_name or '' + + # 合同穿透 + contract_val = '' + if contract: + contract_val = { + 'model': 'property.lease.contract', + 'id': contract.id, + 'display_name': contract.contract_code or '', + } + + # 承租方穿透 + lessee_val = '' + if rec and rec.lessee_id: + lessee_val = { 'model': 'property.tenant.information', 'id': rec.lessee_id.id, 'display_name': rec.lessee_id.name, - } if rec.lessee_id else "", - "金额": round(amount, 2), - "应收金额": round((rec.original_amount or 0.0) - (rec.discount_amount or 0.0), 2), - "所属公司": rec.company_id.referred if rec.company_id and hasattr(rec.company_id, 'referred') else rec.company_id.name if rec.company_id else "", - "管理员": { + } + + # 应收金额 = original_amount - discount_amount + receivable_amount = round((rec.original_amount or 0.0) - (rec.discount_amount or 0.0), 2) + + rows.append({ + '序号': idx, + '物业名称': property_val, + '合同编号': contract_val, + '承租方名称': lessee_val, + '收款类型': '停车费', + '应收开始': rec.due_date.strftime('%Y-%m-%d') if rec and rec.due_date else '', + '应收结束': rec.due_date_end.strftime('%Y-%m-%d') if rec and rec.due_date_end else '', + '金额': amount, + '应收金额': receivable_amount, + '所属公司': contract.lessor_id.name if contract and contract.lessor_id else '', + '管理员': { 'model': 'res.users', 'id': rec.admin_id.id, 'display_name': rec.admin_id.name, - } if rec.admin_id else "", + } if rec.admin_id else '', }) - return { - "rows": rows, - "stats": [ - {"name": "合计", "amount": round(total_amount, 2)}, - ], - } + stats = [ + {'name': '记录总数', 'amount': len(rows)}, + {'name': '本年累计', 'amount': round(total_amount, 2)}, + ] + return {"rows": rows, "stats": stats} diff --git a/yuthon_report/models/yuthon_dashboard.py b/yuthon_report/models/yuthon_dashboard.py index 12c04aa8c..5b74626cc 100644 --- a/yuthon_report/models/yuthon_dashboard.py +++ b/yuthon_report/models/yuthon_dashboard.py @@ -128,8 +128,8 @@ class YuthonDashboard(models.Model): overrides=penalty_year_cum_params ) - # 停车费 - parking_month_receivable = safe_get('property.accounts.receivable', 'get_parking_fee_current_month_receivable', 0) + # 停车费本月应收:stats = [当期应收, 前期欠收, 前期预收, 本月应收] → 取 index 3 + parking_month_receivable = safe_get('property.accounts.receivable', 'get_parking_fee_current_month_receivable', 3) parking_month_received = safe_get('property.accounts.receivable', 'get_parking_fee_current_month_report', 0) parking_year_cum = safe_get('property.accounts.receivable', 'get_parking_fee_yearly_report', 0)