From d7aaa5eacc864e1dd568cfe3dabc6371bbf1d53a Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Tue, 5 Mar 2024 22:19:22 +0100 Subject: [PATCH] Create booking from standard-cashbook to asset-cashbook --- planner.py | 47 ++++++++++++++++++++++++++++++++++------------- tests/planner.py | 20 ++++++++++++-------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/planner.py b/planner.py index 63273ff..5af2ca4 100644 --- a/planner.py +++ b/planner.py @@ -489,11 +489,43 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView): @classmethod def run_booking(cls, records): - """ do prepared booking + """ create planned bookings + + Args: + records (list): list of planned bokings """ pool = Pool() IrDate = pool.get('ir.date') Line = pool.get('cashbook.line') + Currency = pool.get('currency.currency') + Cashbook = pool.get('cashbook.book') + + def add_asset_values(aline): + """ compute quantity from rate of asset and + amount to invest + + Args: + aline (dict): prepared dictionary to create + cashbook-line-record + + Returns: + dict: dictionary to create cashbook-line record + """ + asset_book = Cashbook(aline['booktransf']) + + # add 2nd currency if currencies are different + aline = Line.add_2nd_currency(aline, asset_book.currency) + target_amount = aline.get('amount_2nd_currency', aline['amount']) + asset_rate = asset_book.asset.rate + with Transaction().set_context({'date': aline['date']}): + # convert rate of asset to currency of target-cashbook + asset_rate = Currency.compute( + asset_book.asset.currency, asset_rate, asset_book.currency, + round=False) + aline['quantity'] = Decimal('0.0') + if asset_rate: + aline['quantity'] = target_amount / asset_rate + return aline to_create = [] to_create_check = [] @@ -514,28 +546,17 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView): if record.booktransf: line['booktransf'] = record.booktransf.id if record.booktransf.feature == 'asset': - # add 2nd currency if currencies are different - line = Line.add_2nd_currency( - line, record.booktransf.currency) - print('-- line-1:', line) - if record.booktransf.current_rate: - line['quantity'] = (line.get( - 'amount_2nd_currency', record.amount) / - record.booktransf.asset.rate) + line.update(add_asset_values(line)) - print('-- line-2:', line) if record.wfcheck: to_create_check.append(line) else: to_create.append(line) if to_create_check: - print('-- to_create_check:', to_create_check) lines = Line.create(to_create_check) Line.wfcheck(lines) - if to_create: - print('-- to_create:', to_create) Line.create(to_create) @classmethod diff --git a/tests/planner.py b/tests/planner.py index e40b8d7..466a832 100644 --- a/tests/planner.py +++ b/tests/planner.py @@ -551,6 +551,8 @@ class PlannerTestCase(object): @with_transaction() def test_planner_cronjobs_booking_transfer_asset(self): """ create job, configure transfer-booking to asset-cashbook, + same currencies between cashbooks, + same units between asset and cashbook """ pool = Pool() Planner = pool.get('cashbook.planner') @@ -561,6 +563,7 @@ class PlannerTestCase(object): company = self.prep_company() with Transaction().set_context({'company': company.id}): job = self.prep_create_job() + # rate of asset = 12.5 usd asset_book = self.prep_planner_asset_book() self.assertEqual( asset_book.rec_name, @@ -573,14 +576,14 @@ class PlannerTestCase(object): Planner.write(*[ [job], { - 'name': 'Transfer to Book-2', + 'name': 'buy asset', 'amount': Decimal('10.0'), 'bookingtype': 'mvout', 'category': category.id, - 'subject': 'booking 10 usd --> 10.5 usd', + 'subject': 'invest 10.00 usd to buy 0.80 units', 'booktransf': asset_book.id, 'wfcheck': True}]) - self.assertEqual(job.rec_name, 'Transfer to Book-2') + self.assertEqual(job.rec_name, 'buy asset') self.assertEqual(job.cashbook.rec_name, 'Book 1 | 0.00 usd | Open') self.assertEqual( job.booktransf.rec_name, @@ -600,17 +603,18 @@ class PlannerTestCase(object): self.assertEqual(len(cashbook.lines), 1) self.assertEqual( cashbook.lines[0].rec_name, - "06/01/2022|to|-10.00 €|booking 10 usd --> 10.5 usd" + - " [Depot | 10.50 usd | Open]") + "06/01/2022|to|-10.00 usd|invest 10.00 usd to buy 0.80 " + + "units [Depot | 10.00 usd | Open | 0.8000 u]") self.assertEqual(cashbook.lines[0].state, 'check') asset_book, = Cashbook.browse([job.booktransf]) - self.assertEqual(asset_book.rec_name, 'Depot | 10.50 usd | Open') + self.assertEqual( + asset_book.rec_name, 'Depot | 10.00 usd | Open | 0.8000 u') self.assertEqual(len(asset_book.lines), 1) self.assertEqual( asset_book.lines[0].rec_name, - "06/01/2022|from|10.50 usd|booking 10 € --> 10.5 usd" + - " [Book 1 | -10.00 € | Open]") + "06/01/2022|from|10.00 usd|invest 10.00 usd to " + + "buy 0.80 units [Book 1 | -10.00 usd | Open]|0.8000 u") self.assertEqual(asset_book.lines[0].state, 'check') self.assertEqual(asset_book.lines[0].state, 'check')