119 lines
4.6 KiB
Python
119 lines
4.6 KiB
Python
import calendar
|
|
import datetime
|
|
|
|
from odoo import api, models
|
|
|
|
|
|
class CumIncomeReport(models.Model):
|
|
_inherit = 'receivable.modify.line'
|
|
|
|
@api.model
|
|
def get_cum_income_report(self, params):
|
|
"""
|
|
累计收入报表(数据源:receivable.modify.line):
|
|
- 查询范围:年初(1月1日)到查询月末(非全年)
|
|
例如查3月 → 1月1日 ~ 3月31日;查6月 → 1月1日 ~ 6月30日
|
|
- 用 reconciliation_date(收款行对账日期)判断已收
|
|
- 取每行的 amount(已对金额)
|
|
- 排除押金和违约金类型
|
|
"""
|
|
today = datetime.date.today()
|
|
|
|
date_start = params.get("date_start")
|
|
# 取查询日所在年份和月份
|
|
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)
|
|
|
|
# 用 reconciliation_date(收款行自身的对账日期)做时间范围筛选
|
|
domain = [
|
|
('reconciliation_date', '>=', year_start),
|
|
('reconciliation_date', '<=', query_month_end),
|
|
('receivable_id.is_invalid', '!=', True),
|
|
]
|
|
|
|
keyword = params.get("keyword")
|
|
if keyword:
|
|
domain += [
|
|
'|', '|',
|
|
('receivable_id.contract_id.contract_code', 'ilike', keyword),
|
|
('receivable_id.property_name', 'ilike', keyword),
|
|
('receivable_id.lessee_id.name', 'ilike', keyword),
|
|
]
|
|
|
|
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(('receivable_id.tenure_type', '=', condition))
|
|
if partner_id:
|
|
domain.append(('receivable_id.precinct_id', '=', partner_id))
|
|
if company_id:
|
|
domain.append(('receivable_id.company_id', '=', company_id))
|
|
if management_type_id:
|
|
domain.append(('receivable_id.management_type_id', '=', management_type_id))
|
|
else:
|
|
domain.append(('receivable_id.management_type_id.name', 'not in', ['押金', '违约金']))
|
|
|
|
lines = self.search(domain, order="receivable_id asc")
|
|
|
|
rows = []
|
|
total_amount = 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
|
|
|
|
# 物业穿透
|
|
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 ''
|
|
|
|
# 应收金额 = original_amount - discount_amount
|
|
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 "",
|
|
"承租方名称": {
|
|
'model': 'property.tenant.information',
|
|
'id': rec.lessee_id.id,
|
|
'display_name': rec.lessee_id.name,
|
|
} if rec and rec.lessee_id else "",
|
|
"物业名称": property_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,
|
|
})
|
|
|
|
stats = [
|
|
{'name': '记录总数', 'amount': len(rows)},
|
|
{'name': '累计收入', 'amount': round(total_amount, 2)},
|
|
]
|
|
|
|
return {"rows": rows, "stats": stats}
|