175 lines
9.1 KiB
Python
175 lines
9.1 KiB
Python
from odoo import models, fields, api
|
|
from odoo.exceptions import UserError, ValidationError
|
|
from dateutil.relativedelta import relativedelta
|
|
from datetime import datetime, timedelta, date
|
|
|
|
|
|
class ElectricWaterWizard(models.TransientModel):
|
|
_name = 'electric.water.wizard'
|
|
_description = '电水表推送弹窗'
|
|
|
|
push_date = fields.Date(string='推送日期', default=fields.date.today())
|
|
meter_ids = fields.Many2many('property.meter.archives', string='电表库')
|
|
water_ids = fields.Many2many('property.meter.water', string='水表库')
|
|
|
|
def confirm(self):
|
|
push_date = self.push_date
|
|
month_start = push_date.replace(day=1)
|
|
if push_date.month == 12:
|
|
month_end = push_date.replace(year=push_date.year + 1, month=1, day=1) - timedelta(days=1)
|
|
else:
|
|
month_end = push_date.replace(month=push_date.month + 1, day=1) - timedelta(days=1)
|
|
push_date_minus_1month = (self.push_date - relativedelta(months=1))
|
|
if self.meter_ids:
|
|
is_acc = False
|
|
for meter in self.meter_ids:
|
|
if meter.start_time:
|
|
if meter.start_time > self.push_date:
|
|
continue
|
|
if meter.end_time:
|
|
if meter.end_time and meter.end_time < self.push_date:
|
|
continue
|
|
first_property = False
|
|
if meter.yuthon_property_ids:
|
|
for prop in meter.yuthon_property_ids:
|
|
# 检查这个房产是否存在合同
|
|
contract_line = self.env['property.lease.contract.line'].search([
|
|
('address_id', '=', prop.id),
|
|
('lease_id.actual_start_time', '<', push_date_minus_1month),
|
|
('lease_id.actual_end_time', '>=', push_date_minus_1month),
|
|
], limit=1)
|
|
if contract_line:
|
|
first_property = prop
|
|
break
|
|
# ======================================================================
|
|
|
|
if first_property:
|
|
contract_line = self.env['property.lease.contract.line'].search([
|
|
('address_id', '=', first_property.id),
|
|
('lease_id.actual_start_time', '<', push_date_minus_1month),
|
|
('lease_id.actual_end_time', '>=', push_date_minus_1month),
|
|
], limit=1)
|
|
if contract_line.lease_id:
|
|
if contract_line.lease_id.actual_start_time.year == self.push_date.year and contract_line.lease_id.actual_start_time.month == self.push_date.month:
|
|
is_acc = True
|
|
else:
|
|
contract_line = False
|
|
|
|
electric_id = self.env['property.electric.fee.line'].search([
|
|
('property_meter_archives_id', '=', meter.id),
|
|
('contract_id', '!=', False),
|
|
], limit=1)
|
|
if electric_id:
|
|
is_fix_loss = electric_id.is_fix_loss
|
|
is_fix_price = electric_id.is_fix_price
|
|
else:
|
|
is_fix_loss = 'yes'
|
|
is_fix_price = 'no'
|
|
|
|
# 查找上个月的记录
|
|
last_month_line = meter.line_ids.search([('meter_date', '<', self.push_date)]).sorted('meter_date')[-1]
|
|
self_month_line = meter.line_ids.search([
|
|
('meter_date', '>=', month_start),
|
|
('meter_date', '<=', month_end),
|
|
('archives_id', '=', meter.id)
|
|
])
|
|
if not self_month_line:
|
|
meter.line_ids.create({
|
|
'archives_id': meter.id,
|
|
'is_acc': is_acc,
|
|
'is_fix_loss': is_fix_loss,
|
|
'is_fix_price': is_fix_price,
|
|
'last_meter': last_month_line.last_meter,
|
|
'last_read': last_month_line.last_read,
|
|
'last_dosage': last_month_line.last_dosage,
|
|
'meter_date': self.push_date,
|
|
'epitope_values_id': meter.epitope_values_id.id,
|
|
'lessee_id': meter.lessee_id.id,
|
|
'yuthon_property_ids': [(6, 0, meter.yuthon_property_ids.ids)],
|
|
'current_meter': 0,
|
|
'unit_number': meter.unit_number,
|
|
'gauge_magnification': meter.gauge_magnification,
|
|
'last_reading_time': last_month_line.meter_date,
|
|
'type': meter.type,
|
|
'archives_state': meter.archives_state,
|
|
'maximum_count': meter.maximum_count,
|
|
'contract_id': contract_line.lease_id.id if contract_line and contract_line.lease_id else False,
|
|
})
|
|
|
|
# 处理水表推送
|
|
if self.water_ids:
|
|
is_acc = False
|
|
for water in self.water_ids:
|
|
if water.start_time:
|
|
if water.start_time > self.push_date:
|
|
continue
|
|
if water.end_time:
|
|
if water.end_time and water.end_time < self.push_date:
|
|
continue
|
|
|
|
# ====================== 水表:找到第一个有合同的房产 ======================
|
|
first_property = False
|
|
if water.yuthon_property_ids:
|
|
for prop in water.yuthon_property_ids:
|
|
# 检查这个房产是否存在合同
|
|
contract_line = self.env['property.lease.contract.line'].search([
|
|
('address_id', '=', prop.id),
|
|
('lease_id.actual_start_time', '<', push_date_minus_1month),
|
|
('lease_id.actual_end_time', '>=', push_date_minus_1month),
|
|
], limit=1)
|
|
if contract_line:
|
|
first_property = prop
|
|
break
|
|
# ======================================================================
|
|
|
|
if first_property:
|
|
push_date_minus_1month = (self.push_date - relativedelta(months=1))
|
|
contract_line = self.env['property.lease.contract.line'].search([
|
|
('address_id', '=', first_property.id),
|
|
('lease_id.actual_start_time', '<', push_date_minus_1month),
|
|
('lease_id.actual_end_time', '>=', push_date_minus_1month),
|
|
], limit=1)
|
|
if contract_line.lease_id:
|
|
if contract_line.lease_id.actual_start_time.year == self.push_date.year and contract_line.lease_id.actual_start_time.month == self.push_date.month:
|
|
is_acc = True
|
|
else:
|
|
contract_line = False
|
|
|
|
water_id = self.env['property.water.fee.line'].search([('property_meter_water_id', '=', water.id),
|
|
('contract_id', '!=', False),
|
|
], limit=1)
|
|
if water_id:
|
|
is_fix_loss = water_id.is_fix_loss
|
|
is_fix_price = water_id.is_fix_price
|
|
else:
|
|
is_fix_loss = 'yes'
|
|
is_fix_price = 'no'
|
|
|
|
last_month_line = water.line_ids.search([('meter_date', '<', self.push_date)]).sorted('meter_date')[-1]
|
|
self_month_line = water.line_ids.search([
|
|
('meter_date', '>=', month_start),
|
|
('meter_date', '<=', month_end),
|
|
('water_id', '=', water.id)
|
|
])
|
|
if not self_month_line:
|
|
water.line_ids.create({
|
|
'water_id': water.id,
|
|
'is_fix_loss': is_fix_loss,
|
|
'is_fix_price': is_fix_price,
|
|
'last_meter': last_month_line.last_meter,
|
|
'last_read': last_month_line.last_read,
|
|
'last_dosage': last_month_line.last_dosage,
|
|
'meter_date': self.push_date,
|
|
'epitope_values_id': water.epitope_values_id.id,
|
|
'lessee_id': water.lessee_id.id,
|
|
'yuthon_property_ids': [(6, 0, water.yuthon_property_ids.ids)],
|
|
'current_meter': 0,
|
|
'unit_number': water.unit_number,
|
|
'gauge_magnification': water.gauge_magnification,
|
|
'last_reading_time': last_month_line.meter_date,
|
|
'type': water.type,
|
|
'water_state': water.water_state,
|
|
'is_acc': is_acc,
|
|
'maximum_count': water.maximum_count,
|
|
'contract_id': contract_line.lease_id.id if contract_line and contract_line.lease_id else False,
|
|
}) |