2023-02-11 23:09:56 +00:00
|
|
|
# -*- 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,
|
2023-02-16 22:12:48 +00:00
|
|
|
'start_date': date(2022, 5, 1),
|
2023-02-11 23:09:56 +00:00
|
|
|
}])
|
|
|
|
|
|
|
|
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,
|
|
|
|
}])
|
2023-02-16 22:12:48 +00:00
|
|
|
self.assertEqual(as_cfg.fee_category.rec_name, fee)
|
2023-02-11 23:09:56 +00:00
|
|
|
self.assertEqual(as_cfg.fee_category.cattype, 'out')
|
|
|
|
|
2023-02-16 22:12:48 +00:00
|
|
|
self.assertEqual(as_cfg.dividend_category.rec_name, dividend)
|
2023-02-11 23:09:56 +00:00
|
|
|
self.assertEqual(as_cfg.dividend_category.cattype, 'in')
|
|
|
|
|
|
|
|
self.assertEqual(as_cfg.gainloss_book.rec_name,
|
2023-02-16 22:12:48 +00:00
|
|
|
'%s | 0.00 usd | Open' % gainloss)
|
2023-02-11 23:09:56 +00:00
|
|
|
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()
|
2023-02-16 22:12:48 +00:00
|
|
|
as_cfg = self.prep_yield_config('Fee', 'Dividend', 'Profit-Loss', company)
|
2023-02-11 23:09:56 +00:00
|
|
|
|
|
|
|
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', [{
|
2023-02-12 21:01:07 +00:00
|
|
|
'bookingtype': 'out', # [BK01]
|
2023-02-11 23:09:56 +00:00
|
|
|
'date': date(2022, 5, 1),
|
|
|
|
'amount': Decimal('2.0'),
|
|
|
|
'quantity': Decimal('0.0'),
|
|
|
|
'category': as_cfg.fee_category.id,
|
|
|
|
'description': 'Fee',
|
|
|
|
}, {
|
2023-02-12 21:01:07 +00:00
|
|
|
'bookingtype': 'in', # [BK02]
|
2023-02-11 23:09:56 +00:00
|
|
|
'date': date(2022, 5, 1),
|
|
|
|
'amount': Decimal('10.0'),
|
|
|
|
'quantity': Decimal('1.0'),
|
|
|
|
'category': as_cfg.dividend_category.id,
|
|
|
|
'description': 'reinvested dividend',
|
2023-02-12 21:01:07 +00:00
|
|
|
}, {
|
|
|
|
'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'),
|
|
|
|
}])],
|
2023-02-11 23:09:56 +00:00
|
|
|
}])],
|
|
|
|
}])
|
|
|
|
|
|
|
|
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', [{
|
2023-02-14 21:27:25 +00:00
|
|
|
# dividend, incoming + link to depot-account
|
2023-02-12 21:01:07 +00:00
|
|
|
'bookingtype': 'spin', # [BK05]
|
2023-02-11 23:09:56 +00:00
|
|
|
'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'),
|
|
|
|
}])],
|
2023-02-14 21:27:25 +00:00
|
|
|
}, {
|
|
|
|
# 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'),
|
|
|
|
}])],
|
2023-02-11 23:09:56 +00:00
|
|
|
}])],
|
|
|
|
}])
|
2023-02-14 21:27:25 +00:00
|
|
|
self.assertEqual(len(book_cash.lines), 2)
|
|
|
|
Line.wfcheck(book_cash.lines)
|
2023-02-11 23:09:56 +00:00
|
|
|
|
|
|
|
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,
|
2023-02-14 21:27:25 +00:00
|
|
|
'05/01/2022|to|0.00 usd|Dividend [Cash | -7.50 usd | Open]|0.0000 u')
|
2023-02-11 23:09:56 +00:00
|
|
|
|
2023-02-14 21:27:25 +00:00
|
|
|
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')
|
2023-02-11 23:09:56 +00:00
|
|
|
self.assertEqual(book_asset.btype.rec_name, 'D - Depot')
|
2023-02-14 21:27:25 +00:00
|
|
|
self.assertEqual(len(book_asset.lines), 6)
|
2023-02-11 23:09:56 +00:00
|
|
|
|
2023-02-12 21:01:07 +00:00
|
|
|
# reference to dividend (1) [BK05]
|
2023-02-11 23:09:56 +00:00
|
|
|
self.assertEqual(book_asset.lines[0].rec_name,
|
2023-02-14 21:27:25 +00:00
|
|
|
'05/01/2022|to|0.00 usd|Dividend [Cash | -7.50 usd | Open]|0.0000 u')
|
2023-02-11 23:09:56 +00:00
|
|
|
self.assertEqual(book_asset.lines[0].trade_fee, Decimal('0.0'))
|
|
|
|
self.assertEqual(book_asset.lines[0].asset_dividend, Decimal('5.0'))
|
2023-02-12 21:01:07 +00:00
|
|
|
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)
|
2023-02-11 23:09:56 +00:00
|
|
|
|
2023-02-14 21:27:25 +00:00
|
|
|
# reference to fee [BK06]
|
2023-02-11 23:09:56 +00:00
|
|
|
self.assertEqual(book_asset.lines[1].rec_name,
|
2023-02-14 21:27:25 +00:00
|
|
|
'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'))
|
2023-02-11 23:09:56 +00:00
|
|
|
self.assertEqual(book_asset.lines[1].asset_dividend, Decimal('0.0'))
|
2023-02-14 21:27:25 +00:00
|
|
|
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)
|
2023-02-11 23:09:56 +00:00
|
|
|
|
2023-02-14 21:27:25 +00:00
|
|
|
# fee payed from asset-account [BK01]
|
2023-02-11 23:09:56 +00:00
|
|
|
self.assertEqual(book_asset.lines[2].rec_name,
|
2023-02-14 21:27:25 +00:00
|
|
|
'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,
|
2023-02-11 23:09:56 +00:00
|
|
|
'05/01/2022|Rev|10.00 usd|reinvested dividend [Dividend]|1.0000 u')
|
2023-02-14 21:27:25 +00:00
|
|
|
self.assertEqual(book_asset.lines[3].trade_fee, Decimal('0.0'))
|
|
|
|
self.assertEqual(book_asset.lines[3].asset_dividend, Decimal('10.0'))
|
2023-02-12 21:01:07 +00:00
|
|
|
|
|
|
|
# dividend as split.booking on asset-account, two lines [BK03]
|
2023-02-14 21:27:25 +00:00
|
|
|
self.assertEqual(book_asset.lines[4].rec_name,
|
2023-02-12 21:01:07 +00:00
|
|
|
'05/01/2022|Rev/Sp|7.00 usd|Dividend [-]|0.0000 u')
|
2023-02-14 21:27:25 +00:00
|
|
|
self.assertEqual(book_asset.lines[4].trade_fee, Decimal('0.0'))
|
|
|
|
self.assertEqual(book_asset.lines[4].asset_dividend, Decimal('7.0'))
|
2023-02-12 21:01:07 +00:00
|
|
|
|
|
|
|
# fee as split.booking on asset-account [BK04]
|
2023-02-14 21:27:25 +00:00
|
|
|
self.assertEqual(book_asset.lines[5].rec_name,
|
2023-02-12 21:01:07 +00:00
|
|
|
'05/01/2022|Exp/Sp|-1.50 usd|Fee [-]|0.0000 u')
|
2023-02-14 21:27:25 +00:00
|
|
|
self.assertEqual(book_asset.lines[5].trade_fee, Decimal('-1.5'))
|
|
|
|
self.assertEqual(book_asset.lines[5].asset_dividend, Decimal('0.0'))
|
2023-02-12 21:01:07 +00:00
|
|
|
|
2023-02-14 21:27:25 +00:00
|
|
|
lines = Line.search([('trade_fee', '=', Decimal('-1.5'))])
|
2023-02-12 21:01:07 +00:00
|
|
|
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')
|
2023-02-11 23:09:56 +00:00
|
|
|
|
2023-02-16 22:12:48 +00:00
|
|
|
@with_transaction()
|
|
|
|
def test_yield_gainloss_spout(self):
|
2023-02-18 19:55:04 +00:00
|
|
|
""" check out-booking, split with fee and profit (1)
|
2023-02-16 22:12:48 +00:00
|
|
|
"""
|
|
|
|
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')
|
|
|
|
|
|
|
|
book_cash, = Cashbook.create([{
|
|
|
|
'name': 'Cash',
|
2023-02-15 21:34:52 +00:00
|
|
|
'btype': type_cash.id,
|
|
|
|
'company': company.id,
|
|
|
|
'currency': company.currency.id,
|
|
|
|
'number_sequ': self.prep_sequence().id,
|
|
|
|
'start_date': date(2022, 5, 1),
|
|
|
|
}])
|
|
|
|
|
2023-02-16 22:12:48 +00:00
|
|
|
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,
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'Initial (1)',
|
2023-02-16 22:12:48 +00:00
|
|
|
}, ])],
|
|
|
|
}])
|
|
|
|
|
|
|
|
# add counter-account for profit or loss of
|
|
|
|
# depot-account
|
|
|
|
book_gainloss = as_cfg.gainloss_book
|
2023-02-15 21:34:52 +00:00
|
|
|
# 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',
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'all out (1)',
|
2023-02-15 21:34:52 +00:00
|
|
|
'splitlines': [('create', [{
|
|
|
|
'splittype': 'tr',
|
|
|
|
'booktransf': book_cash.id,
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'sale with 40% profit (1)',
|
2023-02-15 21:34:52 +00:00
|
|
|
'quantity': Decimal('3.0'),
|
|
|
|
'amount': Decimal('30.4'),
|
|
|
|
}, {
|
|
|
|
'splittype': 'cat',
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'trade fee (1)',
|
2023-02-15 21:34:52 +00:00
|
|
|
'category': as_cfg.fee_category.id,
|
|
|
|
'amount': Decimal('2.5'),
|
|
|
|
'quantity': Decimal('0.0'),
|
|
|
|
}, {
|
|
|
|
'splittype': 'tr',
|
|
|
|
'booktransf': book_gainloss.id,
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'profit of sale (1)',
|
2023-02-15 21:34:52 +00:00
|
|
|
'amount': Decimal('-9.4'),
|
|
|
|
'quantity': Decimal('0.0'),
|
|
|
|
}])],
|
|
|
|
}])
|
|
|
|
|
|
|
|
self.assertEqual(len(lines), 1)
|
|
|
|
self.assertEqual(lines[0].rec_name,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|Exp/Sp|-23.50 usd|all out (1) [-]|-3.0000 u')
|
2023-02-15 21:34:52 +00:00
|
|
|
Line.wfcheck(lines)
|
|
|
|
|
|
|
|
self.assertEqual(lines[0].rec_name,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|Exp/Sp|-23.50 usd|all out (1) [-]|-3.0000 u')
|
2023-02-15 21:34:52 +00:00
|
|
|
self.assertEqual(len(lines[0].splitlines), 3)
|
2023-02-16 15:59:50 +00:00
|
|
|
self.assertEqual(lines[0].reference, None)
|
|
|
|
self.assertEqual(len(lines[0].references), 2)
|
|
|
|
self.assertEqual(lines[0].references[0].rec_name,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|from|30.40 usd|sale with 40% profit (1) [Depot | 0.00 usd | Open | 0.0000 u]')
|
2023-02-16 15:59:50 +00:00
|
|
|
self.assertEqual(lines[0].references[1].rec_name,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|from|-9.40 usd|profit of sale (1) [Depot | 0.00 usd | Open | 0.0000 u]')
|
2023-02-16 15:59:50 +00:00
|
|
|
|
2023-02-18 19:55:04 +00:00
|
|
|
self.assertEqual(lines[0].asset_gainloss, Decimal('9.4'))
|
2023-02-15 21:34:52 +00:00
|
|
|
self.assertEqual(lines[0].asset_dividend, Decimal('0.0'))
|
2023-02-16 15:59:50 +00:00
|
|
|
self.assertEqual(lines[0].trade_fee, Decimal('-2.5'))
|
2023-02-15 21:34:52 +00:00
|
|
|
|
|
|
|
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
|
2023-02-18 19:55:04 +00:00
|
|
|
lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))])
|
2023-02-15 21:34:52 +00:00
|
|
|
self.assertEqual(len(lines), 1)
|
|
|
|
self.assertEqual(lines[0].rec_name,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|Exp/Sp|-23.50 usd|all out (1) [-]|-3.0000 u')
|
2023-02-16 22:12:48 +00:00
|
|
|
|
|
|
|
@with_transaction()
|
2023-02-18 19:55:04 +00:00
|
|
|
def test_yield_gainloss_spin(self):
|
|
|
|
""" check in-booking, split with profit (2)
|
2023-02-16 22:12:48 +00:00
|
|
|
"""
|
|
|
|
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')
|
|
|
|
|
2023-02-18 19:55:04 +00:00
|
|
|
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 (2)',
|
|
|
|
}, ])],
|
|
|
|
}])
|
|
|
|
|
|
|
|
# add counter-account for profit or loss of
|
|
|
|
# depot-account
|
|
|
|
book_gainloss = as_cfg.gainloss_book
|
|
|
|
|
|
|
|
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_cash.id,
|
|
|
|
'date': date(2022, 5, 2),
|
|
|
|
'bookingtype': 'spin',
|
|
|
|
'description': 'all out (2)',
|
|
|
|
'splitlines': [('create', [{
|
|
|
|
'splittype': 'tr',
|
|
|
|
'booktransf': book_asset.id,
|
|
|
|
'description': 'sale with 40% profit (2)',
|
|
|
|
'quantity': Decimal('3.0'),
|
|
|
|
'amount': Decimal('30.4'),
|
|
|
|
}, ])],
|
|
|
|
}, {
|
|
|
|
'cashbook': book_gainloss.id,
|
|
|
|
'date': date(2022, 5, 2),
|
|
|
|
'bookingtype': 'spin',
|
|
|
|
'description': 'profit (2)',
|
|
|
|
'splitlines': [('create', [{
|
|
|
|
'splittype': 'tr',
|
|
|
|
'booktransf': book_asset.id,
|
|
|
|
'description': 'profit of sale (2)',
|
|
|
|
'amount': Decimal('-9.4'),
|
|
|
|
'quantity': Decimal('0.0'),
|
|
|
|
}])],
|
|
|
|
}])
|
|
|
|
|
|
|
|
self.assertEqual(len(lines), 2)
|
|
|
|
Line.wfcheck(lines)
|
|
|
|
|
|
|
|
self.assertEqual(lines[0].rec_name,
|
|
|
|
'05/02/2022|Rev/Sp|30.40 usd|all out (2) [-]')
|
|
|
|
self.assertEqual(len(lines[0].splitlines), 1)
|
|
|
|
self.assertEqual(lines[0].reference, None)
|
|
|
|
self.assertEqual(len(lines[0].references), 1)
|
|
|
|
self.assertEqual(lines[0].references[0].rec_name,
|
|
|
|
'05/02/2022|to|-30.40 usd|sale with 40% profit (2) [Cash | 30.40 usd | Open]|-3.0000 u')
|
|
|
|
|
|
|
|
self.assertEqual(lines[0].asset_gainloss, None)
|
|
|
|
self.assertEqual(lines[0].asset_dividend, None)
|
|
|
|
self.assertEqual(lines[0].trade_fee, None)
|
|
|
|
self.assertEqual(lines[0].references[0].asset_gainloss, Decimal('0.0'))
|
|
|
|
self.assertEqual(lines[0].references[0].asset_dividend, Decimal('0.0'))
|
|
|
|
self.assertEqual(lines[0].references[0].trade_fee, Decimal('0.0'))
|
|
|
|
|
|
|
|
self.assertEqual(lines[1].rec_name,
|
|
|
|
'05/02/2022|Rev/Sp|-9.40 usd|profit (2) [-]')
|
|
|
|
self.assertEqual(len(lines[1].splitlines), 1)
|
|
|
|
self.assertEqual(lines[1].reference, None)
|
|
|
|
self.assertEqual(len(lines[1].references), 1)
|
|
|
|
self.assertEqual(lines[1].references[0].rec_name,
|
|
|
|
'05/02/2022|to|9.40 usd|profit of sale (2) [Profit-Loss | -9.40 usd | Open]|0.0000 u')
|
|
|
|
|
|
|
|
self.assertEqual(lines[1].asset_gainloss, None)
|
|
|
|
self.assertEqual(lines[1].asset_dividend, None)
|
|
|
|
self.assertEqual(lines[1].trade_fee, None)
|
|
|
|
self.assertEqual(lines[1].references[0].asset_gainloss, Decimal('9.4'))
|
|
|
|
self.assertEqual(lines[1].references[0].asset_dividend, Decimal('0.0'))
|
|
|
|
self.assertEqual(lines[1].references[0].trade_fee, Decimal('0.0'))
|
|
|
|
|
|
|
|
self.assertEqual(book_asset.rec_name, 'Depot | 2.50 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|to|9.40 usd|profit of sale (2) [Profit-Loss | -9.40 usd | Open]|0.0000 u')
|
|
|
|
|
|
|
|
@with_transaction()
|
|
|
|
def test_yield_gainloss_spin2(self):
|
|
|
|
""" check in-booking, split with profit (5)
|
|
|
|
"""
|
|
|
|
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')
|
|
|
|
|
|
|
|
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,
|
2023-02-16 22:12:48 +00:00
|
|
|
'company': company.id,
|
2023-02-18 19:55:04 +00:00
|
|
|
'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 (2)',
|
|
|
|
}, ])],
|
|
|
|
}])
|
|
|
|
|
|
|
|
# add counter-account for profit or loss of
|
|
|
|
# depot-account
|
|
|
|
book_gainloss = as_cfg.gainloss_book
|
|
|
|
|
|
|
|
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_cash.id,
|
|
|
|
'date': date(2022, 5, 2),
|
|
|
|
'bookingtype': 'spout',
|
|
|
|
'description': 'guv (5)',
|
|
|
|
'splitlines': [('create', [{
|
|
|
|
'splittype': 'tr',
|
|
|
|
'booktransf': book_asset.id,
|
|
|
|
'description': 'profit/loss (5)',
|
|
|
|
'quantity': Decimal('-3.0'),
|
|
|
|
'amount': Decimal('-23.5'),
|
|
|
|
}, {
|
|
|
|
'splittype': 'tr',
|
|
|
|
'booktransf': book_gainloss.id,
|
|
|
|
'description': 'profit/loss (5)',
|
|
|
|
'amount': Decimal('-9.4'),
|
|
|
|
}, ])],
|
2023-02-16 22:12:48 +00:00
|
|
|
}])
|
|
|
|
|
2023-02-18 19:55:04 +00:00
|
|
|
self.assertEqual(len(lines), 1)
|
|
|
|
Line.wfcheck(lines)
|
|
|
|
|
|
|
|
self.assertEqual(lines[0].rec_name,
|
|
|
|
'05/02/2022|Exp/Sp|32.90 usd|guv (5) [-]')
|
|
|
|
self.assertEqual(len(lines[0].splitlines), 2)
|
|
|
|
self.assertEqual(lines[0].splitlines[0].rec_name,
|
|
|
|
'Exp/Sp|-23.50 usd|profit/loss (5) [Depot | 0.00 usd | Open | 0.0000 u]')
|
|
|
|
self.assertEqual(lines[0].splitlines[1].rec_name,
|
|
|
|
'Exp/Sp|-9.40 usd|profit/loss (5) [Profit-Loss | -9.40 usd | Open]')
|
|
|
|
|
|
|
|
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|-23.50 usd|profit/loss (5) [Cash | 32.90 usd | Open]|-3.0000 u')
|
|
|
|
self.assertEqual(lines[0].references[1].rec_name,
|
|
|
|
'05/02/2022|from|-9.40 usd|profit/loss (5) [Cash | 32.90 usd | Open]')
|
|
|
|
|
|
|
|
self.assertEqual(lines[0].asset_gainloss, None)
|
|
|
|
self.assertEqual(lines[0].asset_dividend, None)
|
|
|
|
self.assertEqual(lines[0].trade_fee, None)
|
|
|
|
self.assertEqual(lines[0].references[0].asset_gainloss, Decimal('9.4'))
|
|
|
|
self.assertEqual(lines[0].references[0].asset_dividend, Decimal('0.0'))
|
|
|
|
self.assertEqual(lines[0].references[0].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')
|
|
|
|
|
|
|
|
# check searcher
|
|
|
|
lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))])
|
|
|
|
self.assertEqual(len(lines), 1)
|
|
|
|
self.assertEqual(lines[0].rec_name,
|
|
|
|
'05/02/2022|from|-23.50 usd|profit/loss (5) [Cash | 32.90 usd | Open]|-3.0000 u')
|
|
|
|
|
|
|
|
@with_transaction()
|
|
|
|
def test_yield_gainloss_mvout(self):
|
|
|
|
""" check out-booking, transfer with profit-account, fee
|
|
|
|
2x transfer, 1x category, 3x transfers (3)
|
|
|
|
"""
|
|
|
|
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')
|
|
|
|
|
2023-02-16 22:12:48 +00:00
|
|
|
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,
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'Initial (3)',
|
2023-02-16 22:12:48 +00:00
|
|
|
}, ])],
|
|
|
|
}])
|
|
|
|
|
|
|
|
# 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,
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'sale with 40% profit (3)',
|
2023-02-16 22:12:48 +00:00
|
|
|
'quantity': Decimal('3.0'),
|
|
|
|
'amount': Decimal('30.4'),
|
|
|
|
}, {
|
|
|
|
'cashbook': book_asset.id,
|
|
|
|
'date': date(2022, 5, 2),
|
|
|
|
'bookingtype': 'out',
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'trade fee (3)',
|
2023-02-16 22:12:48 +00:00
|
|
|
'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',
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'profit of sale (3)',
|
2023-02-16 22:12:48 +00:00
|
|
|
'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,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|to|-30.40 usd|sale with 40% profit (3) [Cash | 30.40 usd | Open]|-3.0000 u')
|
2023-02-16 22:12:48 +00:00
|
|
|
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,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|Exp|-2.50 usd|trade fee (3) [Fee]|0.0000 u')
|
2023-02-16 22:12:48 +00:00
|
|
|
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,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|from|9.40 usd|profit of sale (3) [Profit-Loss | -9.40 usd | Open]|0.0000 u')
|
|
|
|
self.assertEqual(lines[2].asset_gainloss, Decimal('9.4'))
|
2023-02-16 22:12:48 +00:00
|
|
|
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')
|
2023-02-17 09:20:06 +00:00
|
|
|
self.assertEqual(book_cash.rec_name, 'Cash | 30.40 usd | Open')
|
|
|
|
|
|
|
|
# check searcher
|
2023-02-18 19:55:04 +00:00
|
|
|
lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))])
|
2023-02-17 09:20:06 +00:00
|
|
|
self.assertEqual(len(lines), 1)
|
|
|
|
|
|
|
|
@with_transaction()
|
|
|
|
def test_yield_gainloss_mv_sp(self):
|
|
|
|
""" check out-booking, transfer with profit-account, fee
|
2023-02-18 19:55:04 +00:00
|
|
|
2x transfer, 1x category, 1x transfer to splitbooking (4)
|
2023-02-17 09:20:06 +00:00
|
|
|
"""
|
|
|
|
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')
|
|
|
|
|
|
|
|
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,
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'Initial (4)',
|
2023-02-17 09:20:06 +00:00
|
|
|
}, ])],
|
|
|
|
}])
|
|
|
|
|
|
|
|
# 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_cash.id,
|
|
|
|
'date': date(2022, 5, 2),
|
|
|
|
'bookingtype': 'spout', # negate transaction, because the
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'all out (4)', # category 'as_cfg.fee_category' is for
|
2023-02-17 09:20:06 +00:00
|
|
|
'splitlines': [('create', [{ # out-only
|
|
|
|
'splittype': 'tr',
|
|
|
|
'booktransf': book_asset.id,
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'sale with 40% profit (4)',
|
2023-02-17 09:20:06 +00:00
|
|
|
'quantity': Decimal('-3.0'),
|
|
|
|
'amount': Decimal('-32.9'), # profit + fee
|
|
|
|
}, {
|
|
|
|
'splittype': 'cat',
|
|
|
|
'category': as_cfg.fee_category.id,
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'trade fee (4)',
|
2023-02-17 09:20:06 +00:00
|
|
|
'amount': Decimal('2.5'), # fee
|
|
|
|
}])],
|
|
|
|
}, {
|
|
|
|
'cashbook': book_asset.id,
|
|
|
|
'date': date(2022, 5, 2),
|
|
|
|
'bookingtype': 'mvin',
|
|
|
|
'booktransf': book_gainloss.id,
|
|
|
|
'amount': Decimal('9.4'),
|
|
|
|
'quantity': Decimal('0.0'),
|
2023-02-18 19:55:04 +00:00
|
|
|
'description': 'gainloss_mv_sp (4)',
|
2023-02-17 09:20:06 +00:00
|
|
|
}])
|
|
|
|
|
|
|
|
self.assertEqual(len(lines), 2)
|
|
|
|
Line.wfcheck(lines)
|
|
|
|
|
|
|
|
self.assertEqual(lines[0].rec_name,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|Exp/Sp|30.40 usd|all out (4) [-]')
|
2023-02-17 09:20:06 +00:00
|
|
|
self.assertEqual(lines[0].asset_gainloss, None) # non-asset cashbook
|
|
|
|
self.assertEqual(lines[0].asset_dividend, None)
|
|
|
|
self.assertEqual(lines[0].trade_fee, None)
|
|
|
|
self.assertEqual(lines[0].reference, None)
|
|
|
|
self.assertEqual(len(lines[0].references), 1)
|
|
|
|
self.assertEqual(lines[0].references[0].rec_name,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|from|-32.90 usd|sale with 40% profit (4) [Cash | 30.40 usd | Open]|-3.0000 u')
|
2023-02-17 09:20:06 +00:00
|
|
|
self.assertEqual(lines[0].references[0].asset_gainloss, Decimal('0.0'))
|
|
|
|
self.assertEqual(lines[0].references[0].asset_dividend, Decimal('0.0'))
|
|
|
|
self.assertEqual(lines[0].references[0].trade_fee, Decimal('2.5'))
|
|
|
|
|
|
|
|
self.assertEqual(lines[1].rec_name,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|from|9.40 usd|gainloss_mv_sp (4) [Profit-Loss | -9.40 usd | Open]|0.0000 u')
|
|
|
|
self.assertEqual(lines[1].asset_gainloss, Decimal('9.4'))
|
2023-02-17 09:20:06 +00:00
|
|
|
self.assertEqual(lines[1].asset_dividend, Decimal('0.0'))
|
|
|
|
self.assertEqual(lines[1].trade_fee, Decimal('0.0'))
|
|
|
|
self.assertEqual(lines[1].reference, None)
|
|
|
|
self.assertEqual(len(lines[1].references), 1)
|
|
|
|
self.assertEqual(lines[1].references[0].rec_name,
|
2023-02-18 19:55:04 +00:00
|
|
|
'05/02/2022|to|-9.40 usd|gainloss_mv_sp (4) [Depot | 0.00 usd | Open | 0.0000 u]')
|
2023-02-17 09:20:06 +00:00
|
|
|
|
|
|
|
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, 'Cash | 30.40 usd | Open')
|
2023-02-16 22:12:48 +00:00
|
|
|
|
|
|
|
# check searcher
|
2023-02-18 19:55:04 +00:00
|
|
|
lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))])
|
2023-02-16 22:12:48 +00:00
|
|
|
self.assertEqual(len(lines), 1)
|
2023-02-15 21:34:52 +00:00
|
|
|
|
2023-02-11 23:09:56 +00:00
|
|
|
# end YieldTestCase
|