From 3278fc7e52878323c64784b3d791d7907f537cd2 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Sun, 30 Oct 2022 22:35:24 +0100 Subject: [PATCH] =?UTF-8?q?evaluation=20-=20currency=20filtert=20ungenutzt?= =?UTF-8?q?e=20w=C3=A4hrungen=20+=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 2 ++ currency.py | 62 ++++++++++++++++++++++++++++++++++++++++++++ evaluation.py | 6 +++-- tests/test_report.py | 57 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 currency.py diff --git a/__init__.py b/__init__.py index 660f817..f91263d 100644 --- a/__init__.py +++ b/__init__.py @@ -6,9 +6,11 @@ from trytond.pool import Pool from .evaluation import Evaluation, EvaluationCashbookRel, \ EvaluationTypeRel, EvaluationCurrencyRel +from .currency import Currency def register(): Pool.register( + Currency, Evaluation, EvaluationCashbookRel, EvaluationTypeRel, diff --git a/currency.py b/currency.py new file mode 100644 index 0000000..f6155e3 --- /dev/null +++ b/currency.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# This file is part of the cashbook-module from m-ds for Tryton. +# The COPYRIGHT file at the top level of this repository contains the +# full copyright notices and license terms. + +from trytond.model import fields +from trytond.transaction import Transaction +from trytond.pool import Pool, PoolMeta +from sql.aggregate import Count +from sql.conditionals import Case + + +class Currency(metaclass=PoolMeta): + __name__ = 'currency.currency' + + cashbook_hasbookings = fields.Function(fields.Boolean(string='Has Bookings', + readonly=True), 'on_change_with_cashbook_hasbookings', + searcher='search_cashbook_hasbookings') + + @fields.depends('id') + def on_change_with_cashbook_hasbookings(self, name=None): + """ result: True if there are bookings + """ + Lines = Pool().get('cashbook.line') + + with Transaction().set_context({ + '_check_access': True, + }): + if Lines.search_count([ + ('cashbook.currency.id', '=', self.id), + ]) > 0: + return True + return False + + @classmethod + def search_cashbook_hasbookings(cls, name, clause): + """ indicate existing bookings + """ + pool = Pool() + Currency2 = pool.get('currency.currency') + Cashbook = pool.get('cashbook.book') + Line = pool.get('cashbook.line') + tab_cur = Currency2.__table__() + tab_book = Cashbook.__table__() + tab_line = Line.__table__() + Operator = fields.SQL_OPERATORS[clause[1]] + + query = tab_book.join(tab_line, + condition=tab_book.id==tab_line.cashbook, + ).join(tab_cur, + condition=tab_cur.id==tab_book.currency, + ).select( + tab_cur.id, + having=Operator(Case( + (Count(tab_line.id) > 0, True), + else_ = False, + ), clause[2]), + group_by=[tab_cur.id,], + ) + return [('id', 'in', query)] + +# end Currency diff --git a/evaluation.py b/evaluation.py index 01efe91..d3c5591 100644 --- a/evaluation.py +++ b/evaluation.py @@ -73,12 +73,14 @@ class Evaluation(ModelSQL, ModelView): currencies = fields.Many2Many(string='Currencies', relation_name='cashbook_report.eval_currency', origin='evaluation', target='currency', + filter=[('cashbook_hasbookings', '=', True)], states={ 'invisible': Eval('dtype', '') != 'currencies', }, depends=['dtype']) - values = fields.One2Many(string='Values', field='evaluation', - model_name='cashbook_report.eval_book', readonly=True) + currency_values = fields.One2Many(string='Currency Values', + field='evaluation', readonly=True, + model_name='cashbook_report.eval_currency') @staticmethod def default_company(): diff --git a/tests/test_report.py b/tests/test_report.py index 69711c2..4e5fc4b 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -21,10 +21,21 @@ class ReportTestCase(CashbookTestCase): """ pool = Pool() Book = pool.get('cashbook.book') + ResUser = pool.get('res.user') + user_admin, = ResUser.search([]) type_cash = self.prep_type() type_bank = self.prep_type(name='Bank', short='BK') company = self.prep_company() + + ResUser.write(*[ + [user_admin], + { + 'companies': [('add', [company.id])], + 'company': company.id, + }]) + self.assertEqual(user_admin.company.id, company.id) + (usd, euro) = self.prep_2nd_currency(company) sequ_id = self.prep_sequence().id category = self.prep_category(cattype='in') @@ -113,6 +124,52 @@ class ReportTestCase(CashbookTestCase): self.assertEqual(books[2].balance, Decimal('23.0')) return books + @with_transaction() + def test_report_currency_hasbookings(self): + """ check detectpn of bookings @ currency + """ + pool = Pool() + Currency = pool.get('currency.currency') + Lines = pool.get('cashbook.line') + + books = self.prep_report_3books() + + company = self.prep_company() + with Transaction().set_context({ + 'company': company.id, + }): + self.assertEqual(len(books[0].lines), 2) + self.assertEqual(books[0].currency.code, 'usd') + self.assertEqual(len(books[1].lines), 2) + self.assertEqual(books[1].currency.code, 'usd') + self.assertEqual(len(books[2].lines), 2) + self.assertEqual(books[2].currency.code, 'EUR') + + euro, = Currency.search([('code', '=', 'EUR')]) + self.assertEqual(euro.cashbook_hasbookings, True) + self.assertEqual(Currency.search_count([ + ('cashbook_hasbookings', '=', True) + ]), 2) + + Lines.delete(books[2].lines) + self.assertEqual(euro.cashbook_hasbookings, False) + + self.assertEqual(Currency.search_count([ + ('cashbook_hasbookings', '=', True) + ]), 1) + + usd, = Currency.search([('code', '=', 'usd')]) + self.assertEqual(usd.cashbook_hasbookings, True) + + Lines.delete(books[0].lines) + self.assertEqual(usd.cashbook_hasbookings, True) + Lines.delete(books[1].lines) + self.assertEqual(usd.cashbook_hasbookings, False) + + self.assertEqual(Currency.search_count([ + ('cashbook_hasbookings', '=', True) + ]), 0) + @with_transaction() def test_report_dtype_update(self): """ check unlink of cashbooks/types/currenciews