line: add fields 'Fee' + 'Dividend' - todos
This commit is contained in:
parent
43c9a1a2fa
commit
c70d2fef7a
12 changed files with 569 additions and 0 deletions
137
line.py
137
line.py
|
@ -4,12 +4,14 @@
|
|||
# full copyright notices and license terms.
|
||||
|
||||
from decimal import Decimal
|
||||
from sql.conditionals import Coalesce
|
||||
from trytond.model import fields
|
||||
from trytond.pool import PoolMeta, Pool
|
||||
from trytond.pyson import Eval, Or, If, And
|
||||
from trytond.exceptions import UserError
|
||||
from trytond.i18n import gettext
|
||||
from trytond.report import Report
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.modules.cashbook.line import STATES, DEPENDS
|
||||
from .mixin import SecondUomMixin
|
||||
|
||||
|
@ -108,6 +110,141 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
|
|||
}, depends=['currency_digits', 'feature']),
|
||||
'on_change_with_diff_percent')
|
||||
|
||||
trade_fee = fields.Function(fields.Numeric(string='Fee',
|
||||
readonly=True, digits=(16, Eval('currency_digits', 2)),
|
||||
states = {
|
||||
'invisible': Eval('feature', '') != 'asset',
|
||||
}, depends=['currency_digits', 'feature']),
|
||||
'get_yield_data')
|
||||
asset_dividend = fields.Function(fields.Numeric(string='Dividend',
|
||||
readonly=True, digits=(16, Eval('currency_digits', 2)),
|
||||
states = {
|
||||
'invisible': Eval('feature', '') != 'asset',
|
||||
}, depends=['currency_digits', 'feature']),
|
||||
'get_yield_data')
|
||||
asset_gainloss = fields.Function(fields.Numeric(string='Profit/Loss',
|
||||
readonly=True, digits=(16, Eval('currency_digits', 2)),
|
||||
states = {
|
||||
'invisible': Eval('feature', '') != 'asset',
|
||||
}, depends=['currency_digits', 'feature']),
|
||||
'get_yield_data')
|
||||
|
||||
@classmethod
|
||||
def get_yield_data_sql(cls):
|
||||
""" query for fee, dividend, gain/loss
|
||||
"""
|
||||
pool = Pool()
|
||||
AssetSetting = pool.get('cashbook.assetconf')
|
||||
SplitLine = pool.get('cashbook.split')
|
||||
tab_line = cls.__table__()
|
||||
tab_line1 = cls.__table__()
|
||||
tab_line2 = cls.__table__()
|
||||
tab_line_fee = cls.__table__()
|
||||
tab_line_divi = cls.__table__()
|
||||
tab_spline_fee = SplitLine.__table__()
|
||||
tab_spline_divi = SplitLine.__table__()
|
||||
|
||||
cfg1 = AssetSetting.get_singleton()
|
||||
|
||||
# local booked fee/dividend
|
||||
tab_inout = cls.search([
|
||||
('cashbook.btype.feature', '=', 'asset'),
|
||||
('bookingtype', 'in', ['in', 'out']),
|
||||
('category', '!=', None),
|
||||
], query=True)
|
||||
query_inout = tab_inout.join(tab_line_fee,
|
||||
condition=(tab_line_fee.id==tab_inout.id) & \
|
||||
(tab_line_fee.category == getattr(cfg1.fee_category, 'id', None)),
|
||||
type_ = 'LEFT OUTER',
|
||||
).join(tab_line_divi,
|
||||
condition=(tab_line_divi.id==tab_inout.id) & \
|
||||
(tab_line_divi.category == getattr(cfg1.dividend_category, 'id', None)),
|
||||
type_ = 'LEFT OUTER',
|
||||
).select(
|
||||
tab_inout.id,
|
||||
(tab_line_fee.credit - tab_line_fee.debit).as_('fee'),
|
||||
(tab_line_divi.credit - tab_line_divi.debit).as_('dividend'),
|
||||
)
|
||||
|
||||
# fee/dividend - in counterpart booking
|
||||
tab_mvinout = cls.search([
|
||||
('cashbook.btype.feature', '=', 'asset'),
|
||||
('bookingtype', 'in', ['mvin', 'mvout']),
|
||||
], query=True)
|
||||
query_mvinout = tab_mvinout.join(tab_line1,
|
||||
condition=tab_mvinout.id==tab_line1.id,
|
||||
).join(tab_line2,
|
||||
# current line is linked to split-booking-line of counterpart
|
||||
condition=((tab_line1.reference == tab_line2.id) | \
|
||||
(tab_line1.id == tab_line2.reference)) & \
|
||||
(tab_line2.bookingtype.in_(['spin', 'spout'])),
|
||||
).join(tab_spline_fee,
|
||||
# fee-line is linked to split-booking-line
|
||||
condition=(tab_spline_fee.line == tab_line2.id) & \
|
||||
(tab_spline_fee.splittype == 'cat') & \
|
||||
(tab_spline_fee.category != None) & \
|
||||
(tab_spline_fee.category == getattr(cfg1.fee_category, 'id', None)),
|
||||
type_ = 'LEFT OUTER',
|
||||
).join(tab_spline_divi,
|
||||
# dividend-line is linked to split-booking-line
|
||||
condition=(tab_spline_divi.line == tab_line2.id) & \
|
||||
(tab_spline_divi.splittype == 'cat') & \
|
||||
(tab_spline_divi.category != None) & \
|
||||
(tab_spline_divi.category == getattr(cfg1.dividend_category, 'id', None)),
|
||||
type_ = 'LEFT OUTER',
|
||||
).select(
|
||||
tab_line1.id,
|
||||
tab_spline_fee.amount.as_('fee'),
|
||||
tab_spline_divi.amount.as_('dividend'),
|
||||
)
|
||||
|
||||
# together
|
||||
query = tab_line.join(query_inout,
|
||||
condition=query_inout.id==tab_line.id,
|
||||
type_ = 'LEFT OUTER',
|
||||
).join(query_mvinout,
|
||||
condition=query_mvinout.id==tab_line.id,
|
||||
type_ = 'LEFT OUTER',
|
||||
).select(
|
||||
tab_line.id,
|
||||
Coalesce(query_inout.fee, query_mvinout.fee).as_('fee'),
|
||||
Coalesce(query_inout.dividend, query_mvinout.dividend).as_('dividend'),
|
||||
)
|
||||
return (tab_line, query)
|
||||
|
||||
@classmethod
|
||||
def get_yield_data(cls, lines, names):
|
||||
""" collect data for fee, dividend, gain/loss per line
|
||||
"""
|
||||
Line2 = Pool().get('cashbook.line')
|
||||
(tab_line, query) = cls.get_yield_data_sql()
|
||||
cursor = Transaction().connection.cursor()
|
||||
|
||||
def quantize_val(value, line):
|
||||
""" quantize...
|
||||
"""
|
||||
return (
|
||||
value or Decimal('0.0')
|
||||
).quantize(Decimal(str(1/10**line.currency_digits)))
|
||||
|
||||
result = {x:{y.id: None for y in lines} for x in names}
|
||||
query.where = tab_line.id.in_([x.id for x in lines])
|
||||
cursor.execute(*query)
|
||||
records = cursor.fetchall()
|
||||
|
||||
for record in records:
|
||||
line = Line2(record[0])
|
||||
values = {
|
||||
'trade_fee': quantize_val(record[1], line),
|
||||
'asset_dividend': quantize_val(record[2], line),
|
||||
'asset_gainloss': Decimal('0.0'), #quantize_val(record[3], line),
|
||||
}
|
||||
|
||||
for name in names:
|
||||
result[name][record[0]] = values[name]
|
||||
|
||||
return result
|
||||
|
||||
def get_rec_name(self, name):
|
||||
""" add quantities - if its a asset-cashbook
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue