# -*- 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 decimal import Decimal from datetime import date from trytond.pool import Pool from trytond.transaction import Transaction from trytond.tests.test_tryton import ModuleTestCase, with_transaction class YieldTestCase(ModuleTestCase): 'Test yield calculation module' module = 'cashbook_investment' def prep_yield_config(self, fee, dividend, gainloss, company): """ add config for yield-calculation fee: name of fee-category, dividend: name of fee-category, gainloss: name of cashbook for gain/loss booking """ pool = Pool() Category = pool.get('cashbook.category') Cashbook = pool.get('cashbook.book') AssetConf = pool.get('cashbook.assetconf') fee_cat = Category.search([('name', '=', fee)]) if len(fee_cat) > 0: fee_cat = fee_cat[0] else : fee_cat, = Category.create([{ 'name': fee, 'company': company.id, 'cattype': 'out', }]) dividend_cat = Category.search([('name', '=', dividend)]) if len(dividend_cat) > 0: dividend_cat = dividend_cat[0] else : dividend_cat, = Category.create([{ 'name': dividend, 'company': company.id, 'cattype': 'in', }]) gainloss_book = Cashbook.search([('name', '=', gainloss)]) if len(gainloss_book) > 0: gainloss_book = gainloss_book[0] else : types = self.prep_type() gainloss_book, = Cashbook.create([{ 'name': gainloss, 'btype': types.id, 'company': company.id, 'currency': company.currency.id, 'number_sequ': self.prep_sequence().id, 'start_date': date(2022, 5, 1), }]) as_cfg = None with Transaction().set_context({ 'company': company.id, }): as_cfg, = AssetConf.create([{ 'fee_category': fee_cat.id, 'dividend_category': dividend_cat.id, 'gainloss_book': gainloss_book.id, }]) self.assertEqual(as_cfg.fee_category.rec_name, fee) self.assertEqual(as_cfg.fee_category.cattype, 'out') self.assertEqual(as_cfg.dividend_category.rec_name, dividend) self.assertEqual(as_cfg.dividend_category.cattype, 'in') self.assertEqual(as_cfg.gainloss_book.rec_name, '%s | 0.00 usd | Open' % gainloss) return as_cfg @with_transaction() def test_yield_config(self): """ check config """ pool = Pool() AssetConf = pool.get('cashbook.assetconf') company = self.prep_company() as_cfg = self.prep_yield_config('Fee', 'Dividend', 'GainLoss', company) self.assertEqual(as_cfg.fee_category.rec_name, 'Fee') self.assertEqual(as_cfg.dividend_category.rec_name, 'Dividend') self.assertEqual(as_cfg.gainloss_book.rec_name, 'GainLoss | 0.00 usd | Open') @with_transaction() def test_yield_fee_dividend_gainloss(self): """ add cashbook, categories, bookings for fees, dididend... """ pool = Pool() Cashbook = pool.get('cashbook.book') BType = pool.get('cashbook.type') Category = pool.get('cashbook.category') Line = pool.get('cashbook.line') company = self.prep_company() as_cfg = self.prep_yield_config('Fee', 'Dividend', 'Profit-Loss', company) type_depot = self.prep_type('Depot', 'D') type_cash = self.prep_type('Cash', 'C') 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_office, = Category.create([{ 'name': 'Office', 'company': company.id, }]) book_asset, = Cashbook.create([{ 'name': 'Depot', 'btype': type_depot.id, 'company': company.id, 'currency': company.currency.id, 'number_sequ': self.prep_sequence().id, 'asset': asset.id, 'quantity_uom': asset.uom.id, 'start_date': date(2022, 5, 1), 'lines': [('create', [{ 'bookingtype': 'out', # [BK01] 'date': date(2022, 5, 1), 'amount': Decimal('2.0'), 'quantity': Decimal('0.0'), 'category': as_cfg.fee_category.id, 'description': 'Fee', }, { 'bookingtype': 'in', # [BK02] 'date': date(2022, 5, 1), 'amount': Decimal('10.0'), 'quantity': Decimal('1.0'), 'category': as_cfg.dividend_category.id, 'description': 'reinvested dividend', }, { 'bookingtype': 'spin', # [BK03] 'date': date(2022, 5, 1), 'description': 'Dividend', 'splitlines': [('create', [{ 'description': 'Dividend', 'splittype': 'cat', 'category': as_cfg.dividend_category.id, 'amount': Decimal('5.0'), 'quantity': Decimal('0.0'), }, { 'description': 'Dividend (2)', 'splittype': 'cat', 'category': as_cfg.dividend_category.id, 'amount': Decimal('2.0'), 'quantity': Decimal('0.0'), }])], }, { 'bookingtype': 'spout', # [BK04] 'date': date(2022, 5, 1), 'description': 'Fee', 'splitlines': [('create', [{ 'description': 'Fee', 'splittype': 'cat', 'category': as_cfg.fee_category.id, 'amount': Decimal('1.5'), 'quantity': Decimal('0.0'), }])], }])], }]) book_cash, = Cashbook.create([{ 'name': 'Cash', 'btype': type_cash.id, 'company': company.id, 'currency': company.currency.id, 'number_sequ': self.prep_sequence().id, 'start_date': date(2022, 5, 1), 'lines': [('create', [{ # dividend, incoming + link to depot-account 'bookingtype': 'spin', # [BK05] 'date': date(2022, 5, 1), 'description': 'Dividend', 'splitlines': [('create', [{ 'description': 'Dividend', 'splittype': 'cat', 'category': as_cfg.dividend_category.id, 'amount': Decimal('5.0'), }, { 'description': 'Dividend', 'splittype': 'tr', 'booktransf': book_asset.id, 'amount': Decimal('0.0'), 'quantity': Decimal('0.0'), }])], }, { # buy shares with trade fee, outgoing + link to depot-account 'bookingtype': 'spout', # [BK06] 'date': date(2022, 5, 1), 'description': '2x shares, trade fee', 'splitlines': [('create', [{ 'description': '2x Shares', 'splittype': 'tr', 'booktransf': book_asset.id, 'amount': Decimal('10.0'), 'quantity': Decimal('2.0'), }, { 'description': 'Trade Fee', 'splittype': 'cat', 'category': as_cfg.fee_category.id, 'amount': Decimal('2.5'), }])], }])], }]) self.assertEqual(len(book_cash.lines), 2) Line.wfcheck(book_cash.lines) self.assertEqual(book_cash.lines[0].rec_name, '05/01/2022|Rev/Sp|5.00 usd|Dividend [-]') self.assertEqual(book_cash.lines[0].reference, None) self.assertEqual(len(book_cash.lines[0].references), 1) self.assertEqual(book_cash.lines[0].references[0].rec_name, '05/01/2022|to|0.00 usd|Dividend [Cash | -7.50 usd | Open]|0.0000 u') self.assertEqual(book_cash.lines[1].rec_name, '05/01/2022|Exp/Sp|-12.50 usd|2x shares, trade fee [-]') self.assertEqual(book_cash.lines[1].reference, None) self.assertEqual(len(book_cash.lines[1].references), 1) self.assertEqual(book_cash.lines[1].references[0].rec_name, '05/01/2022|from|10.00 usd|2x Shares [Cash | -7.50 usd | Open]|2.0000 u') self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') self.assertEqual(book_asset.btype.rec_name, 'D - Depot') self.assertEqual(len(book_asset.lines), 6) # reference to dividend (1) [BK05] self.assertEqual(book_asset.lines[0].rec_name, '05/01/2022|to|0.00 usd|Dividend [Cash | -7.50 usd | Open]|0.0000 u') self.assertEqual(book_asset.lines[0].trade_fee, Decimal('0.0')) self.assertEqual(book_asset.lines[0].asset_dividend, Decimal('5.0')) self.assertEqual(book_asset.lines[0].reference.rec_name, '05/01/2022|Rev/Sp|5.00 usd|Dividend [-]') self.assertEqual(len(book_asset.lines[0].references), 0) # reference to fee [BK06] self.assertEqual(book_asset.lines[1].rec_name, '05/01/2022|from|10.00 usd|2x Shares [Cash | -7.50 usd | Open]|2.0000 u') self.assertEqual(book_asset.lines[1].trade_fee, Decimal('2.5')) self.assertEqual(book_asset.lines[1].asset_dividend, Decimal('0.0')) self.assertEqual(book_asset.lines[1].reference.rec_name, '05/01/2022|Exp/Sp|-12.50 usd|2x shares, trade fee [-]') self.assertEqual(len(book_asset.lines[1].references), 0) # fee payed from asset-account [BK01] self.assertEqual(book_asset.lines[2].rec_name, '05/01/2022|Exp|-2.00 usd|Fee [Fee]|0.0000 u') self.assertEqual(book_asset.lines[2].trade_fee, Decimal('-2.0')) self.assertEqual(book_asset.lines[2].asset_dividend, Decimal('0.0')) # dividend (2) added to asset-account [BK02] self.assertEqual(book_asset.lines[3].rec_name, '05/01/2022|Rev|10.00 usd|reinvested dividend [Dividend]|1.0000 u') self.assertEqual(book_asset.lines[3].trade_fee, Decimal('0.0')) self.assertEqual(book_asset.lines[3].asset_dividend, Decimal('10.0')) # dividend as split.booking on asset-account, two lines [BK03] self.assertEqual(book_asset.lines[4].rec_name, '05/01/2022|Rev/Sp|7.00 usd|Dividend [-]|0.0000 u') self.assertEqual(book_asset.lines[4].trade_fee, Decimal('0.0')) self.assertEqual(book_asset.lines[4].asset_dividend, Decimal('7.0')) # fee as split.booking on asset-account [BK04] self.assertEqual(book_asset.lines[5].rec_name, '05/01/2022|Exp/Sp|-1.50 usd|Fee [-]|0.0000 u') self.assertEqual(book_asset.lines[5].trade_fee, Decimal('-1.5')) self.assertEqual(book_asset.lines[5].asset_dividend, Decimal('0.0')) lines = Line.search([('trade_fee', '=', Decimal('-1.5'))]) self.assertEqual(len(lines), 1) self.assertEqual(lines[0].rec_name, '05/01/2022|Exp/Sp|-1.50 usd|Fee [-]|0.0000 u') lines = Line.search([('asset_dividend', '=', Decimal('7.0'))]) self.assertEqual(len(lines), 1) self.assertEqual(lines[0].rec_name, '05/01/2022|Rev/Sp|7.00 usd|Dividend [-]|0.0000 u') @with_transaction() def test_yield_gainloss_spout(self): """ check out-booking, split with fee and progit """ pool = Pool() Cashbook = pool.get('cashbook.book') BType = pool.get('cashbook.type') Category = pool.get('cashbook.category') Line = pool.get('cashbook.line') company = self.prep_company() as_cfg = self.prep_yield_config('Fee', 'Dividend', 'Profit-Loss', company) type_depot = self.prep_type('Depot', 'D') type_cash = self.prep_type('Cash', 'C') 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_office, = Category.create([{ 'name': 'Office', 'company': company.id, }]) book_cash, = Cashbook.create([{ 'name': 'Cash', 'btype': type_cash.id, 'company': company.id, 'currency': company.currency.id, 'number_sequ': self.prep_sequence().id, 'start_date': date(2022, 5, 1), }]) book_asset, = Cashbook.create([{ 'name': 'Depot', 'btype': type_depot.id, 'company': company.id, 'currency': company.currency.id, 'number_sequ': self.prep_sequence().id, 'asset': asset.id, 'quantity_uom': asset.uom.id, 'start_date': date(2022, 5, 1), 'lines': [('create', [{ 'bookingtype': 'in', 'date': date(2022, 5, 1), 'amount': Decimal('23.50'), 'quantity': Decimal('3.0'), 'category': as_cfg.dividend_category.id, 'description': 'Initial', }, ])], }]) # add counter-account for profit or loss of # depot-account book_gainloss = as_cfg.gainloss_book # sale all shares with profit and fee # buy: 23.50 # sale: 32.90 (+40%) # fee: 2.50 # asset (buy amount): 23.50 # booking: asset -> cash: - 30.40 # asset -> (category) fee: - 2.50 # asset <- gain-loss: 9.40 # ------- # 0.00 self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') self.assertEqual(book_gainloss.rec_name, 'Profit-Loss | 0.00 usd | Open') lines = Line.create([{ 'cashbook': book_asset.id, 'date': date(2022, 5, 2), 'bookingtype': 'spout', 'description': 'all out', 'splitlines': [('create', [{ 'splittype': 'tr', 'booktransf': book_cash.id, 'description': 'sale with 40% profit', 'quantity': Decimal('3.0'), 'amount': Decimal('30.4'), }, { 'splittype': 'cat', 'description': 'trade fee', 'category': as_cfg.fee_category.id, 'amount': Decimal('2.5'), 'quantity': Decimal('0.0'), }, { 'splittype': 'tr', 'booktransf': book_gainloss.id, 'description': 'profit of sale', 'amount': Decimal('-9.4'), 'quantity': Decimal('0.0'), }])], }]) self.assertEqual(len(lines), 1) self.assertEqual(lines[0].rec_name, '05/02/2022|Exp/Sp|-23.50 usd|all out [-]|-3.0000 u') Line.wfcheck(lines) self.assertEqual(lines[0].rec_name, '05/02/2022|Exp/Sp|-23.50 usd|all out [-]|-3.0000 u') self.assertEqual(len(lines[0].splitlines), 3) self.assertEqual(lines[0].reference, None) self.assertEqual(len(lines[0].references), 2) self.assertEqual(lines[0].references[0].rec_name, '05/02/2022|from|30.40 usd|sale with 40% profit [Depot | 0.00 usd | Open | 0.0000 u]') self.assertEqual(lines[0].references[1].rec_name, '05/02/2022|from|-9.40 usd|profit of sale [Depot | 0.00 usd | Open | 0.0000 u]') self.assertEqual(lines[0].asset_gainloss, Decimal('-9.4')) self.assertEqual(lines[0].asset_dividend, Decimal('0.0')) self.assertEqual(lines[0].trade_fee, Decimal('-2.5')) self.assertEqual(book_asset.rec_name, 'Depot | 0.00 usd | Open | 0.0000 u') # negative amount on profit/loss-account means success self.assertEqual(book_gainloss.rec_name, 'Profit-Loss | -9.40 usd | Open') # check searcher lines = Line.search([('asset_gainloss', '=', Decimal('-9.4'))]) self.assertEqual(len(lines), 1) self.assertEqual(lines[0].rec_name, '05/02/2022|Exp/Sp|-23.50 usd|all out [-]|-3.0000 u') @with_transaction() def test_yield_gainloss_mvout(self): """ check out-booking, transfer with profit-account, fee 2x transfer, 1x category """ pool = Pool() Cashbook = pool.get('cashbook.book') BType = pool.get('cashbook.type') Category = pool.get('cashbook.category') Line = pool.get('cashbook.line') company = self.prep_company() as_cfg = self.prep_yield_config('Fee', 'Dividend', 'Profit-Loss', company) type_depot = self.prep_type('Depot', 'D') type_cash = self.prep_type('Cash', 'C') 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_office, = Category.create([{ 'name': 'Office', 'company': company.id, }]) book_cash, = Cashbook.create([{ 'name': 'Cash', 'btype': type_cash.id, 'company': company.id, 'currency': company.currency.id, 'number_sequ': self.prep_sequence().id, 'start_date': date(2022, 5, 1), }]) book_asset, = Cashbook.create([{ 'name': 'Depot', 'btype': type_depot.id, 'company': company.id, 'currency': company.currency.id, 'number_sequ': self.prep_sequence().id, 'asset': asset.id, 'quantity_uom': asset.uom.id, 'start_date': date(2022, 5, 1), 'lines': [('create', [{ 'bookingtype': 'in', 'date': date(2022, 5, 1), 'amount': Decimal('23.50'), 'quantity': Decimal('3.0'), 'category': as_cfg.dividend_category.id, 'description': 'Initial', }, ])], }]) # add counter-account for profit or loss of # depot-account book_gainloss = as_cfg.gainloss_book # sale all shares with profit and fee # buy: 23.50 # sale: 32.90 (+40%) # fee: 2.50 # asset (buy amount): 23.50 # booking: asset -> cash: - 30.40 # asset -> (category) fee: - 2.50 # asset <- gain-loss: 9.40 # ------- # 0.00 self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') self.assertEqual(book_gainloss.rec_name, 'Profit-Loss | 0.00 usd | Open') lines = Line.create([{ 'cashbook': book_asset.id, 'date': date(2022, 5, 2), 'bookingtype': 'mvout', 'booktransf': book_cash.id, 'description': 'sale with 40% profit', 'quantity': Decimal('3.0'), 'amount': Decimal('30.4'), }, { 'cashbook': book_asset.id, 'date': date(2022, 5, 2), 'bookingtype': 'out', 'description': 'trade fee', 'category': as_cfg.fee_category.id, 'quantity': Decimal('0.0'), 'amount': Decimal('2.5'), }, { 'cashbook': book_asset.id, 'date': date(2022, 5, 2), 'bookingtype': 'mvin', 'description': 'profit of sale', 'booktransf': book_gainloss.id, 'quantity': Decimal('0.0'), 'amount': Decimal('9.4'), }]) self.assertEqual(len(lines), 3) Line.wfcheck(lines) self.assertEqual(lines[0].rec_name, '05/02/2022|to|-30.40 usd|sale with 40% profit [Cash | 30.40 usd | Open]|-3.0000 u') self.assertEqual(lines[0].asset_gainloss, Decimal('0.0')) self.assertEqual(lines[0].asset_dividend, Decimal('0.0')) self.assertEqual(lines[0].trade_fee, Decimal('0.0')) self.assertEqual(lines[1].rec_name, '05/02/2022|Exp|-2.50 usd|trade fee [Fee]|0.0000 u') self.assertEqual(lines[1].asset_gainloss, Decimal('0.0')) self.assertEqual(lines[1].asset_dividend, Decimal('0.0')) self.assertEqual(lines[1].trade_fee, Decimal('-2.5')) self.assertEqual(lines[2].rec_name, '05/02/2022|from|9.40 usd|profit of sale [Profit-Loss | -9.40 usd | Open]|0.0000 u') self.assertEqual(lines[2].asset_gainloss, Decimal('-9.4')) self.assertEqual(lines[2].asset_dividend, Decimal('0.0')) self.assertEqual(lines[2].trade_fee, Decimal('0.0')) self.assertEqual(book_asset.rec_name, 'Depot | 0.00 usd | Open | 0.0000 u') self.assertEqual(book_gainloss.rec_name, 'Profit-Loss | -9.40 usd | Open') self.assertEqual(book_cash.rec_name, 'Profit-Loss | 30.40 usd | Open') # check searcher lines = Line.search([('asset_gainloss', '=', Decimal('-9.4'))]) self.assertEqual(len(lines), 1) # end YieldTestCase