89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
from odoo import models, fields, api, _, SUPERUSER_ID
|
||
from odoo.exceptions import ValidationError, UserError
|
||
import xlrd
|
||
import base64
|
||
import datetime
|
||
from datetime import datetime
|
||
import pandas as pd
|
||
|
||
|
||
class PropertyContractWizard(models.Model):
|
||
_name = 'property.management.wizard'
|
||
_description = '批量导入物业'
|
||
|
||
|
||
property_ids = fields.Many2many('yuthon.property', string='物业')
|
||
property_management_id = fields.Many2one('property.management', string='关联物业变更')
|
||
|
||
def confirm(self):
|
||
if not self.property_ids:
|
||
raise UserError("请选择物业!")
|
||
|
||
today = fields.Date.today()
|
||
first_record = self.property_ids[0]
|
||
|
||
# 获取第一条记录的有效管理信息
|
||
valid_manage = first_record.manage_information_ids.filtered(
|
||
lambda m: m.start_time and m.start_time <= today and
|
||
(not m.end_date or m.end_date >= today)
|
||
)
|
||
if not valid_manage:
|
||
raise UserError(f"{first_record.name} 该物业没有找到有效的管理信息!")
|
||
# 参考值集合(tenure_type, company_id, admin_id, precinct_id, purpose_id)
|
||
reference_purpose_id = valid_manage[0].purpose_id.id
|
||
reference_values = (
|
||
first_record.tenure_type,
|
||
first_record.company_id.id,
|
||
first_record.admin_id.id,
|
||
first_record.precinct_id.id,
|
||
reference_purpose_id
|
||
)
|
||
|
||
# 检查所有记录的属性是否与第一条记录一致
|
||
for record in self.property_ids[1:]:
|
||
# 获取当前记录的有效管理信息
|
||
current_valid_manage = record.manage_information_ids.filtered(
|
||
lambda m: m.start_time and m.start_time <= today and
|
||
(not m.end_date or m.end_date >= today)
|
||
)
|
||
|
||
if not current_valid_manage:
|
||
raise UserError(f"{record.name} 该物业没有找到有效的管理信息!")
|
||
|
||
current_purpose_id = current_valid_manage[0].purpose_id.id
|
||
# 当前记录的值集合
|
||
current_values = (
|
||
record.tenure_type,
|
||
record.company_id.id,
|
||
record.admin_id.id,
|
||
record.precinct_id.id,
|
||
current_purpose_id
|
||
)
|
||
# 如果有任何属性不一致,抛出统一错误
|
||
if current_values != reference_values:
|
||
raise UserError("所选记录的公司、管理类型、权属类型、管理员和所属片区必须完全一样!")
|
||
# 准备批量创建的数据
|
||
property_list = []
|
||
for record in self.property_ids:
|
||
# 查找当前物业的有效管理信息
|
||
valid_manage = record.manage_information_ids.filtered(
|
||
lambda m: m.start_time and m.start_time <= today and
|
||
(not m.end_date or m.end_date >= today)
|
||
)
|
||
manage_id = valid_manage[0].id if valid_manage else False
|
||
property_list.append((0, 0, {
|
||
'property_id': record.id,
|
||
'purpose_id': record.purpose_id.id,
|
||
'property_management_id': self.property_management_id.id,
|
||
'information_line_id': manage_id,
|
||
}))
|
||
|
||
# 一次性更新主记录
|
||
self.property_management_id.write({
|
||
'company_id': first_record.company_id.id,
|
||
'purpose_id': reference_purpose_id,
|
||
'admin_id_all': first_record.admin_id.id,
|
||
'precinct_id': first_record.precinct_id.id,
|
||
'tenure_type': first_record.tenure_type,
|
||
'management_line2_ids': property_list,
|
||
}) |