Files
gzth/yuthon_property/wizard/property_water_wizard.py
T
2025-10-15 18:46:32 +08:00

148 lines
5.4 KiB
Python

# -*- coding: utf-8 -*-
from odoo import models, fields, api, _, SUPERUSER_ID
from odoo.exceptions import ValidationError, UserError
from pypinyin import pinyin, Style
import xlrd
import base64
import datetime
from datetime import datetime
from datetime import datetime, timedelta
class PropertyWaterWizard(models.Model):
_name = 'property.water.wizard'
excel_file = fields.Binary(string="上传Excel表")
excel_name = fields.Char(string="文件名")
def confirm(self):
"""
导入excel文件,按行读取数据并处理,将1-9月数据写入明细行
"""
if not self.excel_file:
raise ValidationError('请上传文件。')
try:
book = xlrd.open_workbook(file_contents=base64.decodebytes(self.excel_file))
sh = book.sheet_by_index(0)
except Exception as e:
raise ValidationError(f'文件解析错误: {str(e)}')
for rx in range(1, sh.nrows):
row = sh.row(rx)
for month in range(1, 10):
self.property_water_data(row, month)
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'message': f'成功导入 {sh.nrows - 1} 条水表数据及明细',
'type': 'success',
'sticky': False,
}
}
def property_water_data(self, row, month):
epitope_values = row[0].value
name = row[3].value # 仪表编号
unit_number = row[1].value
installation_location = row[2].value
# 仪表倍率
gauge_magnification = float(row[5].value) if row[5].value else None
# 最大位数
maximum_count = float(row[6].value) if row[6].value else None
# 类型转换
type_dict = {'公共': 'public', '收费': 'fees', '': ''}
meter_type = type_dict.get(row[7].value)
current_read_index = month + 9
meter_date = datetime(2025, month, 15)
current_read = None
if len(row) > current_read_index and row[current_read_index].value:
try:
current_read = float(row[current_read_index].value)
except ValueError:
current_read = None
property_id = None
if installation_location:
search_value = installation_location.strip()
try:
if float(search_value) == int(float(search_value)):
search_value = str(int(float(search_value)))
except (ValueError, TypeError):
pass
property_search = self.env['yuthon.property'].search([
('name', 'ilike', f'%{search_value}%')
], limit=1)
if property_search:
property_id = property_search
excel_start_date = datetime(1899, 12, 30)
if row[19].value:
start_date = excel_start_date + timedelta(days=row[19].value)
else:
start_date = None
if row[20].value:
end_date = excel_start_date + timedelta(days=row[20].value)
else:
end_date = None
# 处理物业信息(模糊匹配以及小数为.0的)
if type(name) == float:
name = int(name)
water = self.env['property.meter.water'].search([
('name', 'ilike', f'%{name}%')
], limit=1)
epitope_values_id = self.env['yuthon.epitope.values'].search([('name', '=', epitope_values)], limit=1)
if not epitope_values_id:
epitope_values_id = self.env['yuthon.epitope.values'].create({'name': epitope_values,
'type': 'ammeter'})
if not water:
water = self.env['property.meter.water'].create({
'name': name,
'epitope_values': epitope_values,
'epitope_values_id':epitope_values_id.id,
'start_time': start_date,
'end_time': end_date,
'unit_number': unit_number,
'installation_location': installation_location,
'type': meter_type,
'gauge_magnification': gauge_magnification,
'maximum_count': maximum_count,
'yuthon_property_ids': [(4, property_id.id)] if property_id else [],
})
else:
water.write({
'epitope_values': epitope_values,
'start_time': start_date,
'end_time': end_date,
'unit_number': unit_number,
'installation_location': installation_location,
'type': meter_type,
'gauge_magnification': gauge_magnification,
'maximum_count': maximum_count,
'yuthon_property_ids': [(4, property_id.id)] if property_id else [],
})
line_vals = {
'initial_value': row[9].value,
'meter_date': meter_date,
'water_id': water.id,
'unit_number': unit_number,
'type': meter_type,
'gauge_magnification': gauge_magnification or 1,
'maximum_count': maximum_count,
'current_read': current_read,
'last_reading_time': meter_date,
'yuthon_property_ids': [(4, property_id.id)] if property_id else [],
}
water.write({'line_ids': [(0, 0, line_vals)]})