From fd36a3f4ce682183133be888e4c15b75110d87bc Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Sun, 1 Jan 2023 20:34:51 +0100 Subject: [PATCH] =?UTF-8?q?line:=20funktion=20f=C3=BCr=20'balance'=20+=20t?= =?UTF-8?q?est=20reconciliation:=20rec=5Fname=20+=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- line.py | 15 +++- reconciliation.py | 24 +++++- reconciliation.xml | 5 ++ tests/__init__.py | 2 + tests/test_reconciliation.py | 152 +++++++++++++++++++++++++++++++++++ view/recon_list.xml | 11 +++ 6 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 tests/test_reconciliation.py create mode 100644 view/recon_list.xml diff --git a/line.py b/line.py index 493acfa..9d155cb 100644 --- a/line.py +++ b/line.py @@ -5,7 +5,7 @@ from decimal import Decimal from trytond.model import fields -from trytond.pool import PoolMeta +from trytond.pool import PoolMeta, Pool from trytond.pyson import Eval, Or, If from trytond.exceptions import UserError from trytond.i18n import gettext @@ -106,6 +106,19 @@ class Line(metaclass=PoolMeta): result['quantity'] = line.quantity return result + @fields.depends('id', 'date', 'cashbook', \ + '_parent_cashbook.id', 'reconciliation', \ + '_parent_reconciliation.start_quantity',\ + '_parent_reconciliation.state') + def on_change_with_quantity_balance(self, name=None): + """ get quantity-balance + """ + Line2 = Pool().get('cashbook.line') + return Line2.get_balance_of_line(self, + field_name='quantity', + credit_name='quantity_credit', + debit_name='quantity_debit') + @fields.depends('quantity', 'amount', 'currency_digits', 'quantity_digits') def on_change_with_asset_rate(self, name=None): """ get rate diff --git a/reconciliation.py b/reconciliation.py index fd4aefc..b7581e1 100644 --- a/reconciliation.py +++ b/reconciliation.py @@ -4,9 +4,10 @@ # full copyright notices and license terms. -from trytond.pool import PoolMeta +from trytond.pool import PoolMeta, Pool from trytond.model import fields from trytond.pyson import Eval +from trytond.report import Report from decimal import Decimal @@ -31,6 +32,20 @@ class Reconciliation(metaclass=PoolMeta): readonly=True, model_name='product.uom'), 'on_change_with_quantity_uom') + def get_rec_name(self, name): + """ add quantities - if its a asset-cashbook + """ + recname = super(Reconciliation, self).get_rec_name(name) + if self.cashbook.feature == 'asset': + recname += ' | %(start_quantity)s %(uom_symbol)s - %(end_quantity)s %(uom_symbol)s' % { + 'start_quantity': Report.format_number(self.start_quantity or 0.0, None, + digits=self.quantity_digits), + 'end_quantity': Report.format_number(self.end_quantity or 0.0, None, + digits=self.quantity_digits), + 'uom_symbol': self.quantity_uom.symbol, + } + return recname + @fields.depends('cashbook', '_parent_cashbook.quantity_uom') def on_change_with_quantity_uom(self, name=None): """ get quantity-unit of asset @@ -71,7 +86,11 @@ class Reconciliation(metaclass=PoolMeta): def get_values_wfcheck(cls, reconciliation): """ get values for 'to_write' in wf-check """ + Line = Pool().get('cashbook.line') + values = super(Reconciliation, cls).get_values_wfcheck(reconciliation) + if reconciliation.cashbook.feature != 'asset': + return values if reconciliation.predecessor: values['start_quantity'] = reconciliation.predecessor.end_quantity @@ -84,9 +103,10 @@ class Reconciliation(metaclass=PoolMeta): if len(values['lines']) != 1: raise ValueError('invalid number of values') + lines_records = Line.browse(values['lines'][0][1]) values['end_quantity'] += sum([ x.quantity_credit - x.quantity_debit - for x in values['lines'][0][1] + for x in lines_records ]) # add quantities of already linked lines diff --git a/reconciliation.xml b/reconciliation.xml index ad8a238..8e7477d 100644 --- a/reconciliation.xml +++ b/reconciliation.xml @@ -5,6 +5,11 @@ full copyright notices and license terms. --> + + cashbook.recon + + recon_list + cashbook.recon diff --git a/tests/__init__.py b/tests/__init__.py index 51d7cde..21885a8 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -5,6 +5,7 @@ import trytond.tests.test_tryton import unittest from trytond.modules.cashbook_investment.tests.test_book import CbInvTestCase +from trytond.modules.cashbook_investment.tests.test_reconciliation import ReconTestCase __all__ = ['suite'] @@ -12,6 +13,7 @@ __all__ = ['suite'] class CashbookInvestmentTestCase(\ CbInvTestCase,\ + ReconTestCase,\ ): 'Test cashbook-investment module' module = 'cashbook_investment' diff --git a/tests/test_reconciliation.py b/tests/test_reconciliation.py new file mode 100644 index 0000000..aebd8cb --- /dev/null +++ b/tests/test_reconciliation.py @@ -0,0 +1,152 @@ +# -*- 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.tests.test_tryton import ModuleTestCase, with_transaction +from trytond.pool import Pool +from trytond.transaction import Transaction +from trytond.exceptions import UserError +from datetime import date +from decimal import Decimal + + +class ReconTestCase(ModuleTestCase): + 'Test quantity reconciliation module' + module = 'cashbook_investment' + + @with_transaction() + def test_recon_set_start_quantity_by_cashbook(self): + """ set stat-quantity from cashbook-setting + """ + pool = Pool() + Book = pool.get('cashbook.book') + Reconciliation = pool.get('cashbook.recon') + BType = pool.get('cashbook.type') + + company = self.prep_company() + type_depot = self.prep_type('Depot', 'D') + BType.write(*[ + [type_depot], + { + 'feature': 'asset', + }]) + asset = self.prep_asset_item( + company=company, + product = self.prep_asset_product(name='Product 1')) + self.assertEqual(asset.symbol, 'usd/u') + + book, = Book.create([{ + 'name': 'Asset-Book', + 'btype': type_depot.id, + 'company': company.id, + 'currency': company.currency.id, + 'asset': asset.id, + 'quantity_uom': asset.uom.id, + 'start_date': date(2022, 5, 1), + 'number_sequ': self.prep_sequence().id, + 'reconciliations': [('create', [{ + 'date': date(2022, 5, 28), + 'date_from': date(2022, 5, 1), + 'date_to': date(2022, 5, 31), + }])], + }]) + self.assertEqual(book.name, 'Asset-Book') + self.assertEqual(book.reconciliations[0].feature, 'asset') + self.assertEqual(book.reconciliations[0].rec_name, + '05/01/2022 - 05/31/2022 | 0.00 usd - 0.00 usd [0] | 0.00 u - 0.00 u') + + Reconciliation.wfcheck(list(book.reconciliations)) + self.assertEqual(book.reconciliations[0].rec_name, + '05/01/2022 - 05/31/2022 | 0.00 usd - 0.00 usd [0] | 0.00 u - 0.00 u') + + @with_transaction() + def test_recon_set_start_quantity_by_predecessor(self): + """ set stat-quantity from end_amount of predecessor + """ + pool = Pool() + Book = pool.get('cashbook.book') + Lines = pool.get('cashbook.line') + Reconciliation = pool.get('cashbook.recon') + BType = pool.get('cashbook.type') + + company = self.prep_company() + type_depot = self.prep_type('Depot', 'D') + BType.write(*[ + [type_depot], + { + 'feature': 'asset', + }]) + asset = self.prep_asset_item( + company=company, + product = self.prep_asset_product(name='Product 1')) + self.assertEqual(asset.symbol, 'usd/u') + + category = self.prep_category(cattype='in') + party = self.prep_party() + book, = Book.create([{ + 'name': 'Asset-Book', + 'btype': type_depot.id, + 'company': company.id, + 'currency': company.currency.id, + 'asset': asset.id, + 'quantity_uom': asset.uom.id, + 'start_date': date(2022, 5, 1), + 'number_sequ': self.prep_sequence().id, + 'reconciliations': [('create', [{ + 'date': date(2022, 5, 28), + 'date_from': date(2022, 5, 1), + 'date_to': date(2022, 5, 31), + }])], + 'lines': [('create', [{ + 'date': date(2022, 5, 5), + 'bookingtype': 'in', + 'category': category.id, + 'description': 'Line 1', + 'amount': Decimal('5.0'), + 'quantity': Decimal('1.5'), + 'party': party.id, + }, { + 'date': date(2022, 5, 6), + 'bookingtype': 'in', + 'category': category.id, + 'description': 'Line 2', + 'party': party.id, + 'amount': Decimal('7.0'), + 'quantity': Decimal('2.5'), + },])], + }]) + self.assertEqual(book.name, 'Asset-Book') + self.assertEqual(len(book.reconciliations), 1) + self.assertEqual(book.reconciliations[0].rec_name, + '05/01/2022 - 05/31/2022 | 0.00 usd - 0.00 usd [0] | 0.00 u - 0.00 u') + self.assertEqual(len(book.reconciliations[0].lines), 0) + + Lines.wfcheck(list(book.lines)) + + self.assertEqual(book.lines[0].quantity_balance, Decimal('1.5')) + self.assertEqual(book.lines[1].quantity_balance, Decimal('4.0')) + + Reconciliation.wfcheck(list(book.reconciliations)) + + self.assertEqual(book.lines[0].quantity_balance, Decimal('1.5')) + self.assertEqual(book.lines[1].quantity_balance, Decimal('4.0')) + + self.assertEqual(book.reconciliations[0].state, 'check') + self.assertEqual(book.reconciliations[0].rec_name, + '05/01/2022 - 05/31/2022 | 0.00 usd - 12.00 usd [2] | 0.00 u - 4.00 u') + Reconciliation.wfdone(list(book.reconciliations)) + self.assertEqual(book.reconciliations[0].state, 'done') + + recons = Reconciliation.create([{ + 'cashbook': book.id, + 'date_from': date(2022, 5, 31), + 'date_to': date(2022, 6, 30), + }]) + self.assertEqual(recons[0].rec_name, + '05/31/2022 - 06/30/2022 | 0.00 usd - 0.00 usd [0] | 0.00 u - 0.00 u') + Reconciliation.wfcheck(recons) + self.assertEqual(recons[0].rec_name, + '05/31/2022 - 06/30/2022 | 12.00 usd - 12.00 usd [0] | 4.00 u - 4.00 u') + +# end ReconTestCase diff --git a/view/recon_list.xml b/view/recon_list.xml new file mode 100644 index 0000000..6fa1694 --- /dev/null +++ b/view/recon_list.xml @@ -0,0 +1,11 @@ + + + + + + + + +