2017-05-26 12:54:36 +02:00
|
|
|
# The COPYRIGHT file at the top level of this repository contains the full
|
|
|
|
# copyright notices and license terms.
|
|
|
|
from trytond.model import fields
|
2022-11-28 18:13:59 +01:00
|
|
|
from trytond.pool import Pool, PoolMeta
|
|
|
|
from trytond.transaction import Transaction
|
2022-12-07 12:13:45 +01:00
|
|
|
from trytond import backend
|
2017-06-20 16:31:22 +02:00
|
|
|
from . import party
|
2017-05-26 12:54:36 +02:00
|
|
|
|
|
|
|
|
2018-08-17 18:51:03 +02:00
|
|
|
class Bank(party.PartyCompanyMixin, metaclass=PoolMeta):
|
2017-06-20 16:31:22 +02:00
|
|
|
__name__ = "bank"
|
2017-05-26 12:54:36 +02:00
|
|
|
|
|
|
|
|
2018-08-17 18:51:03 +02:00
|
|
|
class BankAccount(metaclass=PoolMeta):
|
2017-06-20 16:31:22 +02:00
|
|
|
__name__ = 'bank.account'
|
2017-11-24 14:51:35 +01:00
|
|
|
companies = fields.Function(fields.One2Many('company.company', None,
|
|
|
|
'Companies'), 'get_companies', searcher='search_companies')
|
2022-11-28 18:13:59 +01:00
|
|
|
owners_by_companies = fields.Function(
|
|
|
|
fields.Many2Many('bank.account-party.party', 'account', 'owner',
|
|
|
|
'Owners'), 'get_owners_by_companies', setter='set_owners_by_companies')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, vlist):
|
|
|
|
# allow create bank accounts from party when not select owners
|
|
|
|
with Transaction().set_context(_check_access=False):
|
|
|
|
return super(BankAccount, cls).create(vlist)
|
2017-05-26 12:54:36 +02:00
|
|
|
|
2017-11-24 14:51:35 +01:00
|
|
|
def get_companies(self, name):
|
|
|
|
if self.bank:
|
|
|
|
return [c.id for c in self.bank.party.companies]
|
2017-05-26 12:54:36 +02:00
|
|
|
|
|
|
|
@classmethod
|
2017-11-24 14:51:35 +01:00
|
|
|
def search_companies(cls, name, clause):
|
|
|
|
return [('bank.party.companies',) + tuple(clause[1:])]
|
2022-11-28 18:13:59 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_owners_by_companies(cls, records, name):
|
|
|
|
PartyCompany = Pool().get('party.company.rel')
|
|
|
|
|
|
|
|
user_company = Transaction().context.get('company')
|
|
|
|
|
|
|
|
res = dict((x.id, None) for x in records)
|
|
|
|
for record in records:
|
|
|
|
owners = []
|
|
|
|
|
|
|
|
for owner in record.owners:
|
|
|
|
owner_companies = [o.company for o in PartyCompany.search([
|
|
|
|
('party', '=', owner),
|
|
|
|
])]
|
|
|
|
if not owner_companies:
|
|
|
|
owners.append(owner)
|
|
|
|
continue
|
|
|
|
for company in owner_companies:
|
|
|
|
if company.id == user_company:
|
|
|
|
owners.append(owner)
|
|
|
|
break
|
|
|
|
res[record.id] = [o.id for o in owners]
|
|
|
|
return res
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_owners_by_companies(cls, records, name, value):
|
|
|
|
cls.write(records, {'owners': value})
|
2022-12-07 16:07:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BankAccountNumber(metaclass=PoolMeta):
|
|
|
|
__name__ = 'bank.account.number'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __register__(cls, module_name):
|
|
|
|
cursor = Transaction().connection.cursor()
|
|
|
|
|
|
|
|
# since issue11198 fill or create bank from IBAN and enforce uniqueness
|
|
|
|
# replace default sql_constrain (keep same name) that always return false
|
|
|
|
exist = backend.TableHandler.table_exist(cls._table)
|
|
|
|
if exist and backend.name == 'postgresql':
|
|
|
|
cursor.execute("ALTER TABLE bank_account_number DROP CONSTRAINT "
|
|
|
|
"IF EXISTS bank_account_number_number_iban_exclude")
|
|
|
|
cursor.execute("ALTER table bank_account_number add constraint "
|
|
|
|
"bank_account_number_number_iban_exclude check (type != 'x')")
|
|
|
|
|
|
|
|
super(BankAccountNumber, cls).__register__(module_name)
|