From 0165d56115347cb7508780ddcd204881b3c8bafc Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Tue, 10 Dec 2024 12:24:45 +0100 Subject: [PATCH] bank accont number: add field 'company_owned' --- __init__.py | 2 + bank.py | 97 +++++++++++++++++++++++++++++++++++++++++ tests/test_edocument.py | 48 ++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 bank.py diff --git a/__init__.py b/__init__.py index d20da9a..9d78ad7 100644 --- a/__init__.py +++ b/__init__.py @@ -5,11 +5,13 @@ from trytond.pool import Pool from .edocument import XRechnung, FacturX +from .bank import AccountNumber from .party import PartyConfiguration, Party def register(): Pool.register( + AccountNumber, XRechnung, FacturX, Party, diff --git a/bank.py b/bank.py new file mode 100644 index 0000000..1d6f923 --- /dev/null +++ b/bank.py @@ -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 diff --git a/tests/test_edocument.py b/tests/test_edocument.py index eaa75df..4ff38b7 100644 --- a/tests/test_edocument.py +++ b/tests/test_edocument.py @@ -11,6 +11,7 @@ from datetime import date from trytond.tests.test_tryton import ModuleTestCase, with_transaction from trytond.pool import Pool from trytond.modules.edocument_uncefact.tests.test_module import get_invoice +from trytond.modules.company.tests import create_company, set_company from trytond.exceptions import UserError @@ -18,6 +19,53 @@ class EdocTestCase(ModuleTestCase): 'Test e-rechnung module' module = 'edocument_xrechnung' + @with_transaction() + def test_xrechn_bank_account_owned(self): + """ check field 'company_owned' on bank.account.number + """ + pool = Pool() + BankAccount = pool.get('bank.account') + AccountNumber = pool.get('bank.account.number') + Bank = pool.get('bank') + Party = pool.get('party.party') + + company = create_company() + with set_company(company): + bank_party, = Party.create([{ + 'name': 'Bank 123', + 'addresses': [('create', [{}])]}]) + customer_party, = Party.create([{ + 'name': 'Someone', + 'addresses': [('create', [{}])]}]) + bank, = Bank.create([{'party': bank_party.id}]) + + acc_company, acc_other, = BankAccount.create([ + { + 'bank': bank.id, + 'owners': [('add', [company.party.id])], + 'numbers': [('create', [ + {'type': 'iban', 'number': 'DE02300209000106531065'}])] + }, { + 'bank': bank.id, + 'owners': [('add', [customer_party.id])], + 'numbers': [('create', [ + {'type': 'iban', 'number': 'DE02200505501015871393'}])] + }]) + self.assertEqual(len(acc_company.numbers), 1) + self.assertEqual(acc_company.numbers[0].company_owned, True) + self.assertEqual(len(acc_other.numbers), 1) + self.assertEqual(acc_other.numbers[0].company_owned, False) + + company_numbers = AccountNumber.search( + [('company_owned', '=', True)]) + self.assertEqual(len(company_numbers), 1) + self.assertEqual(company_numbers[0].id, acc_company.numbers[0].id) + + other_numbers = AccountNumber.search( + [('company_owned', '=', False)]) + self.assertEqual(len(other_numbers), 1) + self.assertEqual(other_numbers[0].id, acc_other.numbers[0].id) + @with_transaction() def test_xrechn_check_validator(self): """ check validation of optional route-id