bank accont number: add field 'company_owned'
This commit is contained in:
parent
9bff1f142b
commit
0165d56115
3 changed files with 147 additions and 0 deletions
97
bank.py
Normal file
97
bank.py
Normal file
|
@ -0,0 +1,97 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# This file is part of the edocument-module for Tryton from m-ds.de.
|
||||
# The COPYRIGHT file at the top level of this repository contains the
|
||||
# full copyright notices and license terms.
|
||||
|
||||
|
||||
from sql.conditionals import Case
|
||||
from trytond.pool import PoolMeta, Pool
|
||||
from trytond.model import fields
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
DEF_NONE = None
|
||||
|
||||
|
||||
class AccountNumber(metaclass=PoolMeta):
|
||||
__name__ = 'bank.account.number'
|
||||
|
||||
company_owned = fields.Function(fields.Boolean(
|
||||
string='Number belongs to Company',
|
||||
readonly=True),
|
||||
'get_company_owned',
|
||||
searcher='searcher_company_owned')
|
||||
|
||||
@classmethod
|
||||
def get_company_owned_sql(cls):
|
||||
""" get sql to search for bank acconts owned by company-party
|
||||
"""
|
||||
pool = Pool()
|
||||
Account = pool.get('bank.account')
|
||||
Number = pool.get('bank.account.number')
|
||||
Owners = pool.get('bank.account-party.party')
|
||||
Company = pool.get('company.company')
|
||||
context = Transaction().context
|
||||
|
||||
tab_acc = Account.__table__()
|
||||
tab_owner = Owners.__table__()
|
||||
tab_num = Number.__table__()
|
||||
|
||||
company_id = context.get('company', -1)
|
||||
party_id = -1
|
||||
if company_id and company_id > 0:
|
||||
party_id = Company(company_id).party.id
|
||||
|
||||
query = tab_num.join(
|
||||
tab_acc,
|
||||
condition=tab_num.account == tab_acc.id,
|
||||
).join(
|
||||
tab_owner,
|
||||
condition=tab_owner.account == tab_acc.id,
|
||||
).select(
|
||||
tab_num.id.as_('number'),
|
||||
Case(
|
||||
(tab_owner.owner == party_id, True),
|
||||
else_=False,
|
||||
).as_('owned'))
|
||||
return (tab_num, query)
|
||||
|
||||
@classmethod
|
||||
def searcher_company_owned(cls, name, clause):
|
||||
""" search in owned by party
|
||||
|
||||
Args:
|
||||
name (str): field name
|
||||
clause (list): search domain
|
||||
|
||||
Returns:
|
||||
list: updated search domain
|
||||
"""
|
||||
Operator = fields.SQL_OPERATORS[clause[1]]
|
||||
(tab_num, query) = cls.get_company_owned_sql()
|
||||
|
||||
query = query.select(
|
||||
query.number,
|
||||
where=Operator(query.owned, clause[2]))
|
||||
return [('id', 'in', query)]
|
||||
|
||||
@classmethod
|
||||
def get_company_owned(cls, records, names):
|
||||
""" get list of bank account numbers owned by company
|
||||
"""
|
||||
cursor = Transaction().connection.cursor()
|
||||
|
||||
result = {x: {y.id: False for y in records} for x in names}
|
||||
|
||||
(tab_num, query) = cls.get_company_owned_sql()
|
||||
query.where = tab_num.id.in_([x.id for x in records])
|
||||
cursor.execute(*query)
|
||||
lines = cursor.fetchall()
|
||||
|
||||
for line in lines:
|
||||
values = {'company_owned': line[1]}
|
||||
|
||||
for name in names:
|
||||
result[name][line[0]] = values.get(name)
|
||||
return result
|
||||
|
||||
# end AccountNumber
|
Loading…
Add table
Add a link
Reference in a new issue