129 lines
5.8 KiB
Python
129 lines
5.8 KiB
Python
from odoo import models, fields, api, _, SUPERUSER_ID
|
||
from odoo.exceptions import UserError, ValidationError
|
||
|
||
|
||
class BindingInformationWizard(models.Model):
|
||
_name = 'binding.information.wizard'
|
||
_description = '绑定信息弹窗'
|
||
|
||
bank_id = fields.Many2one('property.bank.statements',string="银行")
|
||
property_ids = fields.Many2many('yuthon.property', string='物业')
|
||
contract_ids = fields.Many2many('property.lease.contract',string="租赁合同审批")
|
||
available_property_ids = fields.Many2many(
|
||
'yuthon.property',
|
||
'binding_information_wizard_available_property_rel',
|
||
'wizard_id',
|
||
'property_id',
|
||
compute='_compute_available_records',
|
||
string='可用物业ID',
|
||
store=True
|
||
)
|
||
available_contract_ids = fields.Many2many(
|
||
'property.lease.contract',
|
||
'binding_information_wizard_available_contract_rel',
|
||
'wizard_id',
|
||
'contract_id',
|
||
compute='_compute_available_records',
|
||
string='可用合同ID',
|
||
store = True
|
||
)
|
||
|
||
@api.onchange('property_ids')
|
||
def onchange_property_ids(self):
|
||
if not self.property_ids:
|
||
return
|
||
new_contract_ids = []
|
||
for property_id in self.property_ids:
|
||
# 获取物业关联的合同明细行
|
||
for line in property_id.property_contract_ids:
|
||
if line.lease_contract_id:
|
||
new_contract_ids.append(line.lease_contract_id.id)
|
||
|
||
# 过滤掉已选择的合同,避免重复
|
||
new_contract_ids = list(set(new_contract_ids) - set(self.contract_ids.ids))
|
||
if new_contract_ids:
|
||
self.contract_ids = [(4, contract_id) for contract_id in new_contract_ids]
|
||
|
||
@api.onchange('contract_ids')
|
||
def onchange_contract_ids(self):
|
||
if not self.contract_ids:
|
||
return
|
||
new_property_ids = []
|
||
for contract_id in self.contract_ids:
|
||
# 获取合同关联的物业明细行
|
||
for line in contract_id.lease_ids:
|
||
if line.address_id:
|
||
new_property_ids.append(line.address_id.id)
|
||
# 过滤掉已选择的物业,避免重复
|
||
new_property_ids = list(set(new_property_ids) - set(self.property_ids.ids))
|
||
# 追加新物业(使用操作码4)
|
||
if new_property_ids:
|
||
self.property_ids = [(4, property_id) for property_id in new_property_ids]
|
||
|
||
|
||
@api.onchange('bank_id')
|
||
def _onchange_bank_id(self):
|
||
"""计算可用的物业和合同ID"""
|
||
for record in self:
|
||
# 搜索未对账的应收记录
|
||
unreconciled_records = self.env['property.accounts.receivable'].search([
|
||
('is_reconciliation', '=', False)
|
||
])
|
||
contract_ids = list(set(record.contract_id.id for record in unreconciled_records if record.contract_id))
|
||
|
||
# 设置计算字段值
|
||
record.available_contract_ids = [(6, 0, contract_ids)] if contract_ids else [(5,)]
|
||
|
||
def confirm(self):
|
||
if self.bank_id.name:
|
||
bank_ids = self.env['property.bank.statements'].search([('name', '=', self.bank_id.name)])
|
||
for bank_id in bank_ids:
|
||
if self.property_ids:
|
||
new_properties = self.property_ids - bank_id.property_ids
|
||
if new_properties:
|
||
bank_id.write({
|
||
'property_ids': [(4, prop.id) for prop in new_properties]
|
||
})
|
||
if self.contract_ids:
|
||
new_contracts = self.contract_ids - bank_id.contract_ids
|
||
if new_contracts:
|
||
bank_id.write({
|
||
'contract_ids': [(4, cont.id) for cont in new_contracts]
|
||
})
|
||
|
||
# 根据银行账号查找承租方信息
|
||
bank_account = self.env['property.bank.line'].search([
|
||
('bank', '=', self.bank_id.name),
|
||
('tenant_id.name', '!=', False),
|
||
])
|
||
if bank_account:
|
||
# 获取已存在的合同明细行的合同ID
|
||
existing_contract_ids = bank_account.tenant_id.property_contract_ids.mapped('lease_contract_id.id')
|
||
all_contracts = self.contract_ids | self.bank_id.contract_ids
|
||
# 过滤出需要新增的合同(排除已存在的)
|
||
new_contracts = all_contracts.filtered(lambda c: c.id not in existing_contract_ids)
|
||
for contract in new_contracts:
|
||
self.env['property.tenant.line'].sudo().create({
|
||
'tenant_id': bank_account.tenant_id.id,
|
||
'lease_contract_id': contract.id,
|
||
})
|
||
else:
|
||
tenant = self.env['property.tenant.information'].create({
|
||
'name': self.bank_id.name_account,
|
||
})
|
||
self.env['property.bank.line'].create({
|
||
'bank': self.bank_id.name,
|
||
'name': self.bank_id.bank_branch,
|
||
'user': self.bank_id.name_account,
|
||
'tenant_id': tenant.id,
|
||
})
|
||
existing_contract_ids = tenant.property_contract_ids.mapped('lease_contract_id.id')
|
||
# 过滤出需要新增的合同(排除已存在的)
|
||
new_contracts = self.contract_ids.filtered(lambda c: c.id not in existing_contract_ids)
|
||
for contract in new_contracts:
|
||
self.env['property.tenant.line'].sudo().create({
|
||
'tenant_id': tenant.id,
|
||
'lease_contract_id': contract.id,
|
||
})
|
||
else:
|
||
raise UserError('请先输入该户名银行账号,才能绑定') |