formatting

This commit is contained in:
Frederik Jaeckel 2023-12-03 17:31:42 +01:00
parent 41e04482d4
commit 53f020b4d0
5 changed files with 150 additions and 224 deletions

184
line.py
View file

@ -14,21 +14,19 @@ from trytond.i18n import gettext
from trytond.report import Report
from trytond.transaction import Transaction
from trytond.modules.cashbook.line import STATES, DEPENDS
from trytond.modules.cashbook.const import DEF_NONE
from .mixin import SecondUomMixin
STATESQ1 = {
'invisible': And(
Eval('feature', '') != 'asset',
Eval('booktransf_feature', '') != 'asset',
),
Eval('booktransf_feature', '') != 'asset'),
'required': Or(
Eval('feature', '') == 'asset',
Eval('booktransf_feature', '') == 'asset',
),
Eval('booktransf_feature', '') == 'asset'),
'readonly': Or(
STATES['readonly'],
Eval('bookingtype', '').in_(['spin', 'spout']),
),
Eval('bookingtype', '').in_(['spin', 'spout'])),
}
DEPENDSQ1 = ['feature', 'booktransf_feature', 'quantity_digits', 'bookingtype']
DEPENDSQ1.extend(DEPENDS)
@ -38,14 +36,12 @@ STATESQ1B.update(STATESQ1)
STATESQ1B['invisible'] = And(
Eval('feature', '') != 'asset',
Eval('booktransf_feature', '') != 'asset',
Eval('splitline_has_quantity', False) == False,
)
~Eval('splitline_has_quantity', False))
STATESQ2 = {
'invisible': Eval('feature', '') != 'asset',
'required': Eval('feature', '') == 'asset',
}
'required': Eval('feature', '') == 'asset'}
DEPENDSQ2 = ['feature', 'quantity_digits', 'bookingtype']
@ -66,30 +62,26 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
states=STATESQ2, depends=DEPENDSQ2)
quantity_digits = fields.Function(fields.Integer(
string='Digits',
readonly=True, states={'invisible': True}),
string='Digits', readonly=True, states={'invisible': True}),
'on_change_with_quantity_digits')
quantity_uom = fields.Function(fields.Many2One(
string='Symbol',
readonly=True, model_name='product.uom'),
string='Symbol', readonly=True, model_name='product.uom'),
'on_change_with_quantity_uom')
asset_rate = fields.Function(fields.Numeric(
string='Rate', readonly=True,
digits=(16, If(
Eval('currency_digits', 2) > Eval('quantity_digits', 2),
Eval('currency_digits', 2), Eval('quantity_digits', 2))),
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'quantity_digits', 'feature']),
states={'invisible': Eval('feature', '') != 'asset'},
depends=['currency_digits', 'quantity_digits', 'feature']),
'on_change_with_asset_rate')
quantity_balance = fields.Function(fields.Numeric(
string='Quantity',
digits=(16, Eval('quantity_digits', 4)), readonly=True,
help='Number of shares in the cashbook up to the current ' +
'row if the default sort applies.',
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['quantity_digits', 'feature']),
states={'invisible': Eval('feature', '') != 'asset'},
depends=['quantity_digits', 'feature']),
'on_change_with_quantity_balance')
splitline_has_quantity = fields.Function(fields.Boolean(
string='has quantity', readonly=True, states={'invisible': True}),
@ -101,50 +93,42 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
help='Valuation of the investment based on the current ' +
'stock market price.',
readonly=True, digits=(16, Eval('currency_digits', 2)),
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']),
states={'invisible': Eval('feature', '') != 'asset'},
depends=['currency_digits', 'feature']),
'on_change_with_current_value')
diff_amount = fields.Function(fields.Numeric(
string='Difference',
help='Difference between acquisition value and current value',
readonly=True, digits=(16, Eval('currency_digits', 2)),
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']),
'on_change_with_diff_amount')
states={'invisible': Eval('feature', '') != 'asset'},
depends=['currency_digits', 'feature']), 'on_change_with_diff_amount')
diff_percent = fields.Function(fields.Numeric(
string='Percent',
help='percentage performance since acquisition',
readonly=True, digits=(16, Eval('currency_digits', 2)),
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']),
'on_change_with_diff_percent')
states={'invisible': Eval('feature', '') != 'asset'},
depends=['currency_digits', 'feature']), 'on_change_with_diff_percent')
trade_fee = fields.Function(fields.Numeric(
string='Fee',
help='Trading fee for the current booking line.',
readonly=True, digits=(16, Eval('currency_digits', 2)),
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']),
states={'invisible': Eval('feature', '') != 'asset'},
depends=['currency_digits', 'feature']),
'get_yield_data', searcher='search_trade_fee')
asset_dividend = fields.Function(fields.Numeric(
string='Dividend',
help='Dividend received at the current booking line.',
readonly=True, digits=(16, Eval('currency_digits', 2)),
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']),
states={'invisible': Eval('feature', '') != 'asset'},
depends=['currency_digits', 'feature']),
'get_yield_data', searcher='search_asset_dividend')
asset_gainloss = fields.Function(fields.Numeric(
string='Profit/Loss',
help='Profit or loss on sale on the current booking line.',
readonly=True, digits=(16, Eval('currency_digits', 2)),
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']),
states={'invisible': Eval('feature', '') != 'asset'},
depends=['currency_digits', 'feature']),
'get_yield_data', searcher='search_asset_gainloss')
@classmethod
@ -176,7 +160,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
tab_mvsp_counterpart,
# [MV-SP] transfer booking,
# select counterpart [1] - a split-booking
condition=tab_line.bookingtype.in_(['mvin', 'mvout']) & \
condition=tab_line.bookingtype.in_(['mvin', 'mvout']) &
((tab_line.reference == tab_mvsp_counterpart.id) |
(tab_line.id == tab_mvsp_counterpart.reference)) &
(tab_mvsp_counterpart.bookingtype.in_(['spin', 'spout'])),
@ -185,9 +169,9 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
tab_mv_spline,
# [MV-SP] line is linked to split-booking-line
# of counterpart [1]
condition=(tab_mv_spline.line == tab_mvsp_counterpart.id) & \
(tab_mv_spline.splittype == 'tr') & \
(tab_mv_spline.booktransf != None) & \
condition=(tab_mv_spline.line == tab_mvsp_counterpart.id) &
(tab_mv_spline.splittype == 'tr') &
(tab_mv_spline.booktransf != DEF_NONE) &
(tab_mv_spline.booktransf == gainloss_book),
type_='LEFT OUTER',
@ -195,20 +179,20 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
tab_spmv_counterpart,
# [SP-MV] split booking, select counterpart [1]
# - a transfer-booking
condition=tab_line.bookingtype.in_(['spin', 'spout']) & \
((tab_line.reference == tab_spmv_counterpart.id) | \
(tab_line.id == tab_spmv_counterpart.reference)) & \
tab_spmv_counterpart.bookingtype.in_(['mvin', 'mvout']) & \
condition=tab_line.bookingtype.in_(['spin', 'spout']) &
((tab_line.reference == tab_spmv_counterpart.id) |
(tab_line.id == tab_spmv_counterpart.reference)) &
tab_spmv_counterpart.bookingtype.in_(['mvin', 'mvout']) &
(tab_spmv_counterpart.cashbook == gainloss_book),
type_='LEFT OUTER',
).join(
tab_mvmv_counterpart,
# [MV-MV] transfer booking
condition=tab_line.bookingtype.in_(['mvin', 'mvout']) & \
((tab_mvmv_counterpart.reference == tab_line.id) | \
(tab_mvmv_counterpart.id == tab_line.reference)) & \
tab_mvmv_counterpart.bookingtype.in_(['mvin', 'mvout']) & \
condition=tab_line.bookingtype.in_(['mvin', 'mvout']) &
((tab_mvmv_counterpart.reference == tab_line.id) |
(tab_mvmv_counterpart.id == tab_line.reference)) &
tab_mvmv_counterpart.bookingtype.in_(['mvin', 'mvout']) &
(tab_mvmv_counterpart.cashbook == gainloss_book),
type_='LEFT OUTER',
).select(
@ -218,17 +202,14 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
Case(
(tab_line.bookingtype == 'mvin', tab_mv_spline.amount),
(tab_line.bookingtype == 'mvout',
tab_mv_spline.amount * Decimal('-1.0')),
),
tab_mv_spline.amount * Decimal('-1.0'))),
Case(
(tab_mvsp_counterpart.cashbook == gainloss_book,
tab_line.debit - tab_line.credit),
),
tab_line.debit - tab_line.credit)),
tab_spmv_counterpart.credit - tab_spmv_counterpart.debit,
Decimal('0.0'),
) * Decimal('-1.0')).as_('gainloss'),
tab_line.cashbook,
)
tab_line.cashbook)
return (tab_line, query)
@classmethod
@ -262,17 +243,17 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
).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) & \
condition=(tab_inout_fee.id == tab_line.id) &
tab_inout_fee.bookingtype.in_(['in', 'out']) &
(tab_inout_fee.category != DEF_NONE) &
(tab_inout_fee.category == fee_category),
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) & \
condition=(tab_inout_divi.id == tab_line.id) &
tab_inout_divi.bookingtype.in_(['in', 'out']) &
(tab_inout_divi.category != DEF_NONE) &
(tab_inout_divi.category == dividend_category),
type_='LEFT OUTER',
@ -280,46 +261,46 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
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)) & \
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) & \
condition=(tab_mv_spline_fee.line == tab_mv_counterpart.id) &
(tab_mv_spline_fee.splittype == 'cat') &
(tab_mv_spline_fee.category != DEF_NONE) &
(tab_mv_spline_fee.category == fee_category),
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) & \
condition=(tab_mv_spline_divi.line == tab_mv_counterpart.id) &
(tab_mv_spline_divi.splittype == 'cat') &
(tab_mv_spline_divi.category != DEF_NONE) &
(tab_mv_spline_divi.category == dividend_category),
type_='LEFT OUTER',
).join(
tab_spline_fee,
# [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) & \
condition=(tab_spline_fee.line == tab_line.id) &
tab_line.bookingtype.in_(['spin', 'spout']) &
(tab_spline_fee.splittype == 'cat') &
(tab_spline_fee.category != DEF_NONE) &
(tab_spline_fee.category == fee_category),
type_='LEFT OUTER',
).join(
tab_spline_divi,
# [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) & \
condition=(tab_spline_divi.line == tab_line.id) &
tab_line.bookingtype.in_(['spin', 'spout']) &
(tab_spline_divi.splittype == 'cat') &
(tab_spline_divi.category != DEF_NONE) &
(tab_spline_divi.category == dividend_category),
type_='LEFT OUTER',
).select(
@ -350,8 +331,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
Decimal('0.0'),
)).as_('dividend'),
tab_line.cashbook,
group_by=[tab_line.id, tab_line.cashbook],
)
group_by=[tab_line.id, tab_line.cashbook])
return (tab_line, query)
@classmethod
@ -363,8 +343,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
query = tab_query.select(
tab_query.id,
where=Operator(tab_query.fee, clause[2]),
)
where=Operator(tab_query.fee, clause[2]))
return [('id', 'in', query)]
@classmethod
@ -376,8 +355,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
query = tab_query.select(
tab_query.id,
where=Operator(tab_query.dividend, clause[2]),
)
where=Operator(tab_query.dividend, clause[2]))
return [('id', 'in', query)]
@classmethod
@ -389,8 +367,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
query = tab_query.select(
tab_query.id,
where=Operator(tab_query.gainloss, clause[2]),
)
where=Operator(tab_query.gainloss, clause[2]))
return [('id', 'in', query)]
@classmethod
@ -421,8 +398,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
line = Line2(record[0])
values = {
'trade_fee': quantize_val(record[1], line),
'asset_dividend': quantize_val(record[2], line),
}
'asset_dividend': quantize_val(record[2], line)}
for name in list(name_set):
result[name][record[0]] = values[name]
@ -452,8 +428,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
'quantity': Report.format_number(
credit - debit,
lang=None, digits=self.quantity_digits),
'uom_symbol': getattr(self.quantity_uom, 'symbol', '-'),
}
'uom_symbol': getattr(self.quantity_uom, 'symbol', '-')}
return recname
@classmethod
@ -516,8 +491,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
result = super(Line, cls).get_counterpart_values(
line,
splitline=splitline,
values=values
)
values=values)
line_uom = getattr(line.quantity_uom, 'id', None)
booktransf_uom = getattr(getattr(
@ -543,27 +517,23 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
if (asset_books == 2) and (diff_uom is True)
else splitline.quantity,
'quantity_2nd_uom': splitline.quantity
if (asset_books == 2) and (diff_uom is True) else None,
})
if (asset_books == 2) and (diff_uom is True) else None})
elif sum([1 if booktransf_uom is not None else 0,
1 if line_uom is not None else 0]) == 1:
# one of the related cashbooks only is asset-type
result.update({
'quantity': line.quantity,
'quantity_2nd_uom': None,
})
'quantity_2nd_uom': None})
elif sum([1 if booktransf_uom is not None else 0,
1 if line_uom is not None else 0]) == 2:
if line_uom == booktransf_uom:
result.update({
'quantity': line.quantity,
'quantity_2nd_uom': None,
})
'quantity_2nd_uom': None})
else:
result.update({
'quantity': line.quantity_2nd_uom,
'quantity_2nd_uom': line.quantity,
})
'quantity_2nd_uom': line.quantity})
return result
@fields.depends('amount', 'splitlines', 'quantity')
@ -728,8 +698,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
(line.quantity_debit is None):
raise UserError(gettext(
'cashbook_investment.msg_line_quantity_not_set',
linetxt=line.rec_name,
))
linetxt=line.rec_name))
# quantity and amount must with same sign
if (line.amount != Decimal('0.0')) and \
@ -739,8 +708,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
if amount_sign != quantity_sign:
raise UserError(gettext(
'cashbook_investment.msg_line_sign_mismatch',
linetxt=line.rec_name,
))
linetxt=line.rec_name))
@classmethod
def update_values_by_splitlines(cls, lines):
@ -753,7 +721,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
quantity = sum([
x.quantity or Decimal('0.0') for x in line.splitlines])
if (cnt1 > 0) and (quantity != line.quantity):
to_write.extend([[line], {'quantity': quantity, }])
to_write.extend([[line], {'quantity': quantity}])
return to_write
@classmethod