# -*- 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, }]) 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, 'GainLoss | 0.00 usd | Open') 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', 'GainLoss', 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', 'date': date(2022, 5, 1), 'amount': Decimal('2.0'), 'quantity': Decimal('0.0'), 'category': as_cfg.fee_category.id, 'description': 'Fee', }, { 'bookingtype': 'in', 'date': date(2022, 5, 1), 'amount': Decimal('10.0'), 'quantity': Decimal('1.0'), 'category': as_cfg.dividend_category.id, 'description': 'reinvested dividend', }])], }]) # dividend, incoming + link to depot-account 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', [{ 'bookingtype': 'spin', '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'), }])], }])], }]) self.assertEqual(len(book_cash.lines), 1) Line.wfcheck([book_cash.lines[0]]) 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 | 5.00 usd | Open]|0.0000 u') self.assertEqual(book_asset.name, 'Depot') self.assertEqual(book_asset.rec_name, 'Depot | 8.00 usd | Open | 1.0000 u') self.assertEqual(book_asset.btype.rec_name, 'D - Depot') self.assertEqual(book_asset.state, 'open') self.assertEqual(book_asset.feature, 'asset') self.assertEqual(len(book_asset.lines), 3) # reference to dividend (1) self.assertEqual(book_asset.lines[0].rec_name, '05/01/2022|to|0.00 usd|Dividend [Cash | 5.00 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].asset_gainloss, Decimal('0.0')) # fee payed from asset-account self.assertEqual(book_asset.lines[1].rec_name, '05/01/2022|Exp|-2.00 usd|Fee [Fee]|0.0000 u') self.assertEqual(book_asset.lines[1].trade_fee, Decimal('-2.0')) self.assertEqual(book_asset.lines[1].asset_dividend, Decimal('0.0')) self.assertEqual(book_asset.lines[1].asset_gainloss, Decimal('0.0')) # dividend (2) added to asset-account self.assertEqual(book_asset.lines[2].rec_name, '05/01/2022|Rev|10.00 usd|reinvested dividend [Dividend]|1.0000 u') self.assertEqual(book_asset.lines[2].trade_fee, Decimal('0.0')) self.assertEqual(book_asset.lines[2].asset_dividend, Decimal('10.0')) self.assertEqual(book_asset.lines[2].asset_gainloss, Decimal('0.0')) # end YieldTestCase