formatting

This commit is contained in:
Frederik Jaeckel 2023-06-08 14:00:59 +02:00
parent 6c1fac8672
commit ad067475f8
13 changed files with 1126 additions and 709 deletions

245
line.py
View file

@ -85,7 +85,8 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
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.',
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']),
@ -97,7 +98,8 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
# performance
current_value = fields.Function(fields.Numeric(
string='Value',
help='Valuation of the investment based on the current stock market price.',
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',
@ -154,60 +156,69 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
SplitLine = pool.get('cashbook.split')
tab_line = cls.__table__()
tab_mvsp_counterpart = cls.__table__()
tab_mvsp_local = cls.__table__()
tab_mvmv_counterpart = cls.__table__()
tab_spmv_counterpart = cls.__table__()
tab_mv_spline = SplitLine.__table__()
cfg1 = AssetSetting.get_singleton()
gainloss_book = getattr(getattr(cfg1, 'gainloss_book', None), 'id', None)
gainloss_book = getattr(getattr(
cfg1, 'gainloss_book', None), 'id', None)
tab_assetline = cls.search([
('cashbook.btype.feature', '=', 'asset'),
], query=True)
query = tab_line.join(tab_assetline,
condition=(tab_assetline.id==tab_line.id),
query = tab_line.join(
tab_assetline,
condition=(tab_assetline.id == tab_line.id),
).join(tab_mvsp_counterpart,
# [MV-SP] transfer booking, select counterpart [1] - a split-booking
).join(
tab_mvsp_counterpart,
# [MV-SP] transfer booking,
# select counterpart [1] - a split-booking
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'])),
type_ = 'LEFT OUTER',
).join(tab_mv_spline,
# [MV-SP] line is linked to split-booking-line of counterpart [1]
((tab_line.reference == tab_mvsp_counterpart.id) |
(tab_line.id == tab_mvsp_counterpart.reference)) &
(tab_mvsp_counterpart.bookingtype.in_(['spin', 'spout'])),
type_='LEFT OUTER',
).join(
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) & \
(tab_mv_spline.booktransf == gainloss_book),
type_ = 'LEFT OUTER',
(tab_mv_spline.splittype == 'tr') & \
(tab_mv_spline.booktransf != None) & \
(tab_mv_spline.booktransf == gainloss_book),
type_='LEFT OUTER',
).join(tab_spmv_counterpart,
# [SP-MV] split booking, select counterpart [1] - a transfer-booking
).join(
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']) & \
(tab_spmv_counterpart.cashbook == gainloss_book),
type_ = 'LEFT OUTER',
((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,
).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']) & \
(tab_mvmv_counterpart.cashbook == gainloss_book),
type_ = 'LEFT OUTER',
((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(
tab_line.id,
(Coalesce(
tab_mvmv_counterpart.credit - tab_mvmv_counterpart.debit,
Case(
(tab_line.bookingtype == 'mvin', tab_mv_spline.amount),
(tab_line.bookingtype == 'mvout', tab_mv_spline.amount * Decimal('-1.0')),
(tab_line.bookingtype == 'mvout',
tab_mv_spline.amount * Decimal('-1.0')),
),
Case(
(tab_mvsp_counterpart.cashbook == gainloss_book,
@ -238,67 +249,79 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
cfg1 = AssetSetting.get_singleton()
fee_category = getattr(getattr(cfg1, 'fee_category', None), 'id', None)
dividend_category = getattr(getattr(cfg1, 'dividend_category', None), 'id', None)
dividend_category = getattr(getattr(
cfg1, 'dividend_category', None), 'id', None)
tab_assetline = cls.search([
('cashbook.btype.feature', '=', 'asset'),
], query=True)
query = tab_line.join(tab_assetline,
condition=(tab_assetline.id==tab_line.id),
).join(tab_inout_fee,
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 == fee_category),
type_ = 'LEFT OUTER',
).join(tab_inout_divi,
condition=(tab_inout_fee.id == tab_line.id) & \
tab_inout_fee.bookingtype.in_(['in', 'out']) & \
(tab_inout_fee.category != 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) & \
(tab_inout_divi.category == dividend_category),
type_ = 'LEFT OUTER',
condition=(tab_inout_divi.id == tab_line.id) & \
tab_inout_divi.bookingtype.in_(['in', 'out']) & \
(tab_inout_divi.category != None) & \
(tab_inout_divi.category == dividend_category),
type_='LEFT OUTER',
).join(tab_mv_counterpart,
# [MV] transfer booking, select counterpart [1] - a split-booking
).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]
((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 == fee_category),
type_ = 'LEFT OUTER',
).join(tab_mv_spline_divi,
# [MV] dividend-line is linked to split-booking-line of counterpart [1]
(tab_mv_spline_fee.splittype == 'cat') & \
(tab_mv_spline_fee.category != 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) & \
(tab_mv_spline_divi.category == dividend_category),
type_ = 'LEFT OUTER',
(tab_mv_spline_divi.splittype == 'cat') & \
(tab_mv_spline_divi.category != None) & \
(tab_mv_spline_divi.category == dividend_category),
type_='LEFT OUTER',
).join(tab_spline_fee,
).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) & \
(tab_spline_fee.category == fee_category),
type_ = 'LEFT OUTER',
).join(tab_spline_divi,
tab_line.bookingtype.in_(['spin', 'spout']) & \
(tab_spline_fee.splittype == 'cat') & \
(tab_spline_fee.category != 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) & \
(tab_spline_divi.category == dividend_category),
type_ = 'LEFT OUTER',
tab_line.bookingtype.in_(['spin', 'spout']) & \
(tab_spline_divi.splittype == 'cat') & \
(tab_spline_divi.category != None) & \
(tab_spline_divi.category == dividend_category),
type_='LEFT OUTER',
).select(
tab_line.id,
Sum(Coalesce(
@ -308,8 +331,10 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
# transfer = fee is positive
tab_mv_spline_fee.amount,
Case(
(tab_line.bookingtype == 'spin', tab_spline_fee.amount * Decimal('-1.0')),
(tab_line.bookingtype == 'spout', tab_spline_fee.amount),
(tab_line.bookingtype == 'spin',
tab_spline_fee.amount * Decimal('-1.0')),
(tab_line.bookingtype == 'spout',
tab_spline_fee.amount),
),
Decimal('0.0'),
)).as_('fee'),
@ -317,8 +342,10 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
tab_inout_divi.credit - tab_inout_divi.debit,
tab_mv_spline_divi.amount,
Case(
(tab_line.bookingtype == 'spin', tab_spline_divi.amount),
(tab_line.bookingtype == 'spout', tab_spline_divi.amount * Decimal('-1.0')),
(tab_line.bookingtype == 'spin',
tab_spline_divi.amount),
(tab_line.bookingtype == 'spout',
tab_spline_divi.amount * Decimal('-1.0')),
),
Decimal('0.0'),
)).as_('dividend'),
@ -380,7 +407,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
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}
result = {x: {y.id: None for y in lines} for x in names}
# read fee, dividend
name_set = set({'trade_fee', 'asset_dividend'}).intersection(set(names))
@ -408,7 +435,8 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
for record in records:
line = Line2(record[0])
result['asset_gainloss'][record[0]] = quantize_val(record[1], line)
result['asset_gainloss'][record[0]] = quantize_val(
record[1], line)
return result
def get_rec_name(self, name):
@ -416,10 +444,13 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
"""
recname = super(Line, self).get_rec_name(name)
if self.cashbook.feature == 'asset':
credit = self.quantity_credit if self.quantity_credit is not None else Decimal('0.0')
debit = self.quantity_debit if self.quantity_debit is not None else Decimal('0.0')
credit = self.quantity_credit \
if self.quantity_credit is not None else Decimal('0.0')
debit = self.quantity_debit \
if self.quantity_debit is not None else Decimal('0.0')
recname += '|%(quantity)s %(uom_symbol)s' % {
'quantity': Report.format_number(credit - debit,
'quantity': Report.format_number(
credit - debit,
lang=None, digits=self.quantity_digits),
'uom_symbol': getattr(self.quantity_uom, 'symbol', '-'),
}
@ -453,10 +484,12 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
cashbook = Cashbook.browse([id_cashbook])[0]
if isinstance(values, dict):
type_ = values.get('bookingtype', getattr(line, 'bookingtype', None))
type_ = values.get(
'bookingtype', getattr(line, 'bookingtype', None))
quantity = values.get('quantity', None)
else :
type_ = getattr(values, 'bookingtype', getattr(line, 'bookingtype', None))
else:
type_ = getattr(
values, 'bookingtype', getattr(line, 'bookingtype', None))
quantity = getattr(values, 'quantity', None)
if (type_ is not None) and (cashbook.feature == 'asset'):
@ -471,7 +504,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
'quantity_debit': quantity,
'quantity_credit': Decimal('0.0'),
})
else :
else:
raise ValueError('invalid "bookingtype"')
return result
@ -482,31 +515,35 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
"""
result = super(Line, cls).get_counterpart_values(
line,
splitline = splitline,
values = values
splitline=splitline,
values=values
)
line_uom = getattr(line.quantity_uom, 'id', None)
booktransf_uom = getattr(getattr(line.booktransf, 'quantity_uom', {}), 'id', None)
booktransf_uom = getattr(getattr(
line.booktransf, 'quantity_uom', {}), 'id', None)
if getattr(splitline, 'quantity', None) is not None:
# we add values to the counterpart of a splitbooking-line
asset_books = sum([
1 if splitline.feature == 'asset' else 0,
1 if getattr(splitline.booktransf, 'feature', '-') == 'asset' else 0,
1 if getattr(
splitline.booktransf, 'feature', '-') == 'asset' else 0,
])
diff_uom = False
if asset_books == 2:
diff_uom = (splitline.quantity_uom != splitline.booktransf.quantity_uom) and \
(splitline.quantity_uom is not None) and \
(splitline.booktransf.quantity_uom is not None)
diff_uom = (
splitline.quantity_uom !=
splitline.booktransf.quantity_uom) and \
(splitline.quantity_uom is not None) and \
(splitline.booktransf.quantity_uom is not None)
result.update({
'quantity': splitline.quantity_2nd_uom
if (asset_books == 2) and (diff_uom is True)
else splitline.quantity,
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:
@ -565,7 +602,8 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
if (self.quantity is not None) and \
(self.amount is not None) and \
(self.cashbook.current_rate is not None):
return (self.quantity * self.cashbook.current_rate -
return (
self.quantity * self.cashbook.current_rate -
self.amount).quantize(
Decimal(str(1/10**self.currency_digits)))
@ -579,8 +617,9 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
(self.amount is not None) and \
(self.amount != Decimal('0.0')) and \
(self.cashbook.current_rate is not None):
return (self.quantity * self.cashbook.current_rate *
Decimal('100.0') / self.amount - Decimal('100.0')
return (
self.quantity * self.cashbook.current_rate *
Decimal('100.0') / self.amount - Decimal('100.0')
).quantize(Decimal(str(1/10**self.currency_digits)))
@fields.depends('splitlines')