Files
gzth/yuthon_property/wizard/property_meter_wizard.py
T
2025-09-16 11:16:38 +08:00

94 lines
3.3 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
class PropertyMeterWizard(models.Model):
_name = 'property.meter.wizard'
excel_file = fields.Binary(string="上传Excel表")
excel_name = fields.Char(string="文件名")
def confirm(self):
"""
导入excel文件,按行读取数据并处理
"""
if not self.excel_file:
raise ValidationError('请上传文件。')
book = xlrd.open_workbook(file_contents=base64.decodebytes(self.excel_file))
sh = book.sheet_by_index(0)
for rx in range(1, sh.nrows):
row = sh.row(rx)
last_read = 0
for month in range(1, 10):
last_read = self.property_meter_data(row, last_read, month)
def property_meter_data(self, row, last_read, month):
epitope_values = row[0].value
name = row[3].value
type_dict = {'水表': 'water', '电表': 'electric', '电表(电子表)': 'smart', '': ''}
unit_number = row[1].value
installation_location = row[2].value
if row[5].value:
gauge_magnification = float(row[5].value)
else:
gauge_magnification = None
maximum_count = float(row[6].value) if row[6].value else None
type_dict = {'公共': 'public', '收费': 'fees', '': ''}
type = type_dict.get(row[7].value)
property_id = None
if row[2].value:
property_search = self.env['yuthon.property'].search([('name', '=', row[2].value)])
if property_search:
property_id = property_search[0]
current_read_index = month + 9 if month < 9 else 9
meter_date = None
current_read = None
# 1-8月份设置正常数据,9月份留空
if month < 9:
meter_date = datetime(2025, month, 15)
current_read = row[current_read_index].value
is_push = month < 9 # 9月份依然保持is_push为False
additional_data = {
'name': name,
'epitope_values': epitope_values,
'maximum_count': maximum_count,
'unit_number': unit_number,
'installation_location': installation_location,
'type':type,
'gauge_magnification': gauge_magnification,
'meter_date': meter_date,
'current_read': current_read,
'last_read': last_read,
'is_push': is_push,
'yuthon_property_ids': [(4, property_id.id)] if property_id else [],
}
# 处理数据逻辑...
combined_data = {**additional_data}
meter_id = self.env['property.meter.archives'].create(combined_data)
if property_id:
clear_entries = [(5,)]
new_meter_entry = (0, 0, {
'property_meter_archives_id': meter_id.id,
})
updated_meters = clear_entries + [new_meter_entry]
property_id.write({
'is_electricity_meter': True,
'energy_meter_ids': updated_meters
})
if month < 9 and current_read is not None:
return current_read
else:
return last_read