100 lines
4.2 KiB
Python
100 lines
4.2 KiB
Python
from odoo import models, fields, api
|
|
from datetime import datetime, date, timedelta
|
|
|
|
|
|
class HydropowerReport(models.Model):
|
|
_name = 'hydropower.report'
|
|
_description = '水电表应收明细表'
|
|
_auto = False
|
|
|
|
property_name = fields.Char(string='房屋名称')
|
|
lessee_name = fields.Char(string='业户名称')
|
|
water_fee = fields.Float(string='水费')
|
|
water_period = fields.Char(string='水费期间')
|
|
electric_fee = fields.Float(string='电费')
|
|
electric_period = fields.Char(string='电费期间')
|
|
total = fields.Float(string='小计')
|
|
|
|
@api.model
|
|
def get_hydropower_data(self, params):
|
|
"""获取水电表数据,接收 params 字典"""
|
|
date_start = params.get('date_start')
|
|
date_end = params.get('date_end')
|
|
|
|
start_date = datetime.fromisoformat(date_start).date() if date_start else date.today().replace(day=1)
|
|
end_date = datetime.fromisoformat(date_end).date() if date_end else date.today()
|
|
|
|
# 直接以 property.accounts.receivable 为数据源
|
|
# 按收款日期(payout_date)过滤居水费/电费记录
|
|
water_records = self.env['property.accounts.receivable'].search([
|
|
('payout_date', '>=', start_date),
|
|
('payout_date', '<=', end_date),
|
|
('management_type_id.name', '=', '水费'),
|
|
('is_invalid', '!=', True),
|
|
])
|
|
|
|
electric_records = self.env['property.accounts.receivable'].search([
|
|
('payout_date', '>=', start_date),
|
|
('payout_date', '<=', end_date),
|
|
('management_type_id.name', '=', '电费'),
|
|
('is_invalid', '!=', True),
|
|
])
|
|
|
|
# 整合数据,按物业(合同关联的第一个物业)聊合
|
|
property_dict = {}
|
|
|
|
for rec in water_records:
|
|
prop_name = rec.property_name or ''
|
|
lessee_name = rec.lessee_id.name if rec.lessee_id else (rec.contract_id.lessee_id.name if rec.contract_id else '')
|
|
key = prop_name or str(rec.id)
|
|
if key not in property_dict:
|
|
property_dict[key] = {
|
|
'房屋名称': prop_name,
|
|
'业户名称': lessee_name,
|
|
'水费': 0.0,
|
|
'水费期间': rec.charging_range or '',
|
|
'电费': 0.0,
|
|
'电费期间': '',
|
|
'小计': 0.0
|
|
}
|
|
property_dict[key]['水费'] += rec.original_amount or 0.0
|
|
if rec.charging_range:
|
|
property_dict[key]['水费期间'] = rec.charging_range
|
|
|
|
for rec in electric_records:
|
|
prop_name = rec.property_name or ''
|
|
lessee_name = rec.lessee_id.name if rec.lessee_id else (rec.contract_id.lessee_id.name if rec.contract_id else '')
|
|
key = prop_name or str(rec.id)
|
|
if key not in property_dict:
|
|
property_dict[key] = {
|
|
'房屋名称': prop_name,
|
|
'业户名称': lessee_name,
|
|
'水费': 0.0,
|
|
'水费期间': '',
|
|
'电费': 0.0,
|
|
'电费期间': rec.charging_range or '',
|
|
'小计': 0.0
|
|
}
|
|
property_dict[key]['电费'] += rec.original_amount or 0.0
|
|
if rec.charging_range:
|
|
property_dict[key]['电费期间'] = rec.charging_range
|
|
|
|
rows = []
|
|
total_water = 0.0
|
|
total_electric = 0.0
|
|
for idx, (key, value) in enumerate(property_dict.items(), start=1):
|
|
value['小计'] = round(value['水费'] + value['电费'], 2)
|
|
value['水费'] = round(value['水费'], 2)
|
|
value['电费'] = round(value['电费'], 2)
|
|
total_water += value['水费']
|
|
total_electric += value['电费']
|
|
rows.append(value)
|
|
|
|
stats = [
|
|
{'name': '记录数', 'amount': len(rows)},
|
|
{'name': '水费合计', 'amount': round(total_water, 2)},
|
|
{'name': '电费合计', 'amount': round(total_electric, 2)},
|
|
{'name': '合计', 'amount': round(total_water + total_electric, 2)},
|
|
]
|
|
return {'rows': rows, 'stats': stats}
|