From f4950c5033ac821d80c89edb0e46c682ea7f0a2d Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Mon, 31 Oct 2022 18:48:20 +0100 Subject: [PATCH] =?UTF-8?q?evaluation=20-=20werte=20f=C3=BCr=20currency+te?= =?UTF-8?q?st?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- evaluation.py | 41 ++++++++++++++++++++++++++++++++++++++--- tests/test_report.py | 9 +++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/evaluation.py b/evaluation.py index 16077c1..33aa59e 100644 --- a/evaluation.py +++ b/evaluation.py @@ -310,7 +310,7 @@ class EvaluationTypeRel(RelFieldsMixin, ModelSQL): @fields.depends('evaluation', 'eval_currency', 'currency_digits', 'dtype') def on_change_with_balance(self, name=None): - """ get balanceof bookings in cashbooks by 'type', + """ get balance of bookings in cashbooks by 'type', converted to currency of evaluation """ pool = Pool() @@ -389,9 +389,44 @@ class EvaluationCurrencyRel(RelFieldsMixin, ModelSQL): if self.currency: return self.currency.rec_name + @fields.depends('evaluation', 'eval_currency', 'currency_digits', 'currency') def on_change_with_balance(self, name=None): - """ balance of cashbook + """ get balance of bookings in cashbooks by 'currency', + converted to currency of evaluation """ - return None + pool = Pool() + Lines = pool.get('cashbook.line') + Currency = pool.get('currency.currency') + tab_line = Lines.__table__() + cursor = Transaction().connection.cursor() + + if (self.evaluation is None) or (self.currency is None) or \ + (self.eval_currency is None) or (self.currency_digits is None): + return None + + lines = Lines.search([ + ('cashbook.currency.id', '=', self.currency.id), + ('cashbook.state', '=', 'open'), + ('cashbook.owner.id', '=', Transaction().user), + ], query=True) + + query = lines.join(tab_line, condition=lines.id==tab_line.id, + ).select( + Sum(tab_line.credit - tab_line.debit).as_('balance'), + ) + cursor.execute(*query) + balances = cursor.fetchall() + + total_amount = Decimal('0.0') + for balance in balances: + (bal1,) = balance + + total_amount += Currency.compute( + self.currency, + bal1, + self.eval_currency, + ) + exp = Decimal(Decimal(1) / 10 ** self.currency_digits) + return total_amount.quantize(exp) # end EvaluationCurrencyRel diff --git a/tests/test_report.py b/tests/test_report.py index de924a8..d5fceb6 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -398,7 +398,16 @@ class ReportTestCase(CashbookTestCase): self.assertEqual(evaluation.posted, False) self.assertEqual(evaluation.maincolor, 'default') self.assertEqual(evaluation.bgcolor, '#ffffc0') + self.assertEqual(evaluation.currency.code, 'EUR') self.assertEqual(len(evaluation.currencies), 2) + self.assertEqual(evaluation.currencies[0].code, 'EUR') + self.assertEqual(evaluation.currencies[1].code, 'usd') + + self.assertEqual(len(evaluation.currency_values), 2) + self.assertEqual(evaluation.currency_values[0].name, 'Euro') + self.assertEqual(evaluation.currency_values[0].balance, Decimal('23.0')) + self.assertEqual(evaluation.currency_values[1].name, 'usd') + self.assertEqual(evaluation.currency_values[1].balance, Decimal('35.71')) # end ReportTestCase