field trade_fee + asset_dividend ok + searcher + test
This commit is contained in:
parent
c70d2fef7a
commit
d820fc7109
2 changed files with 163 additions and 76 deletions
161
line.py
161
line.py
|
@ -5,6 +5,7 @@
|
|||
|
||||
from decimal import Decimal
|
||||
from sql.conditionals import Coalesce
|
||||
from sql.aggregate import Sum
|
||||
from trytond.model import fields
|
||||
from trytond.pool import PoolMeta, Pool
|
||||
from trytond.pyson import Eval, Or, If, And
|
||||
|
@ -115,19 +116,19 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
|
|||
states = {
|
||||
'invisible': Eval('feature', '') != 'asset',
|
||||
}, depends=['currency_digits', 'feature']),
|
||||
'get_yield_data')
|
||||
'get_yield_data', searcher='search_trade_fee')
|
||||
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')
|
||||
'get_yield_data', searcher='search_asset_dividend')
|
||||
# ~ 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):
|
||||
|
@ -137,81 +138,119 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
|
|||
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_inout_fee = cls.__table__()
|
||||
tab_inout_divi = cls.__table__()
|
||||
tab_mv_counterpart = cls.__table__()
|
||||
tab_mv_spline_fee = SplitLine.__table__()
|
||||
tab_mv_spline_divi = SplitLine.__table__()
|
||||
tab_spline_fee = SplitLine.__table__()
|
||||
tab_spline_divi = SplitLine.__table__()
|
||||
|
||||
cfg1 = AssetSetting.get_singleton()
|
||||
|
||||
# local booked fee/dividend
|
||||
tab_inout = cls.search([
|
||||
tab_assetline = 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'])),
|
||||
query = tab_line.join(tab_assetline,
|
||||
condition=(tab_assetline.id==tab_line.id),
|
||||
).join(tab_inout_fee,
|
||||
# [INOUT] fee, local booked
|
||||
condition=(tab_inout_fee.id==tab_line.id) & \
|
||||
tab_inout_fee.bookingtype.in_(['in', 'out']) & \
|
||||
(tab_inout_fee.category != None) & \
|
||||
(tab_inout_fee.category == getattr(cfg1.fee_category, 'id', None)),
|
||||
type_ = 'LEFT OUTER',
|
||||
).join(tab_inout_divi,
|
||||
# [INOUT] dividend, local booked
|
||||
condition=(tab_inout_divi.id==tab_line.id) & \
|
||||
tab_inout_divi.bookingtype.in_(['in', 'out']) & \
|
||||
(tab_inout_divi.category != None) & \
|
||||
(tab_inout_divi.category == getattr(cfg1.dividend_category, 'id', None)),
|
||||
type_ = 'LEFT OUTER',
|
||||
|
||||
).join(tab_mv_counterpart,
|
||||
# [MV] transfer booking, select counterpart [1] - a split-booking
|
||||
condition=tab_line.bookingtype.in_(['mvin', 'mvout']) & \
|
||||
((tab_line.reference == tab_mv_counterpart.id) | \
|
||||
(tab_line.id == tab_mv_counterpart.reference)) & \
|
||||
(tab_mv_counterpart.bookingtype.in_(['spin', 'spout'])),
|
||||
type_ = 'LEFT OUTER',
|
||||
).join(tab_mv_spline_fee,
|
||||
# [MV] fee-line is linked to split-booking-line of counterpart [1]
|
||||
condition=(tab_mv_spline_fee.line == tab_mv_counterpart.id) & \
|
||||
(tab_mv_spline_fee.splittype == 'cat') & \
|
||||
(tab_mv_spline_fee.category != None) & \
|
||||
(tab_mv_spline_fee.category == getattr(cfg1.fee_category, 'id', None)),
|
||||
type_ = 'LEFT OUTER',
|
||||
).join(tab_mv_spline_divi,
|
||||
# [MV] dividend-line is linked to split-booking-line of counterpart [1]
|
||||
condition=(tab_mv_spline_divi.line == tab_mv_counterpart.id) & \
|
||||
(tab_mv_spline_divi.splittype == 'cat') & \
|
||||
(tab_mv_spline_divi.category != None) & \
|
||||
(tab_mv_spline_divi.category == getattr(cfg1.dividend_category, 'id', None)),
|
||||
type_ = 'LEFT OUTER',
|
||||
|
||||
).join(tab_spline_fee,
|
||||
# fee-line is linked to split-booking-line
|
||||
condition=(tab_spline_fee.line == tab_line2.id) & \
|
||||
# [SP] fee, split booking
|
||||
condition=(tab_spline_fee.line == tab_line.id) & \
|
||||
tab_line.bookingtype.in_(['spin', 'spout']) & \
|
||||
(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) & \
|
||||
# [SP] dividend, split booking
|
||||
condition=(tab_spline_divi.line == tab_line.id) & \
|
||||
tab_line.bookingtype.in_(['spin', 'spout']) & \
|
||||
(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'),
|
||||
Sum(Coalesce(
|
||||
tab_inout_fee.credit - tab_inout_fee.debit,
|
||||
tab_mv_spline_fee.amount,
|
||||
tab_spline_fee.amount,
|
||||
Decimal('0.0'),
|
||||
)).as_('fee'),
|
||||
Sum(Coalesce(
|
||||
tab_inout_divi.credit - tab_inout_divi.debit,
|
||||
tab_mv_spline_divi.amount,
|
||||
tab_spline_divi.amount,
|
||||
Decimal('0.0'),
|
||||
)).as_('dividend'),
|
||||
group_by=[tab_line.id],
|
||||
)
|
||||
return (tab_line, query)
|
||||
|
||||
@classmethod
|
||||
def search_trade_fee(cls, name, clause):
|
||||
""" search for fees
|
||||
"""
|
||||
Operator = fields.SQL_OPERATORS[clause[1]]
|
||||
(tab_line, tab_query) = cls.get_yield_data_sql()
|
||||
|
||||
query = tab_query.select(
|
||||
tab_query.id,
|
||||
where=Operator(tab_query.fee, clause[2]),
|
||||
)
|
||||
return [('id', 'in', query)]
|
||||
|
||||
@classmethod
|
||||
def search_asset_dividend(cls, name, clause):
|
||||
""" search for dividends
|
||||
"""
|
||||
Operator = fields.SQL_OPERATORS[clause[1]]
|
||||
(tab_line, tab_query) = cls.get_yield_data_sql()
|
||||
|
||||
query = tab_query.select(
|
||||
tab_query.id,
|
||||
where=Operator(tab_query.dividend, clause[2]),
|
||||
)
|
||||
return [('id', 'in', query)]
|
||||
|
||||
@classmethod
|
||||
def get_yield_data(cls, lines, names):
|
||||
""" collect data for fee, dividend, gain/loss per line
|
||||
|
@ -237,12 +276,10 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
|
|||
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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue