book: view zeigt in listansicht bei unterkonten in fremdwährung

den korrekten wert,
book-form: feld für saldo in unternehmens-währung
This commit is contained in:
Frederik Jaeckel 2022-10-02 15:04:10 +02:00
parent 4a7ee23e2c
commit 59dfb94bee
7 changed files with 238 additions and 13 deletions

91
book.py
View file

@ -11,8 +11,10 @@ from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.report import Report
from decimal import Decimal
from sql import Literal
from sql.aggregate import Sum
from sql.conditionals import Case
from sql.conditionals import Case, Coalesce
from sql.functions import CurrentDate
from .model import order_name_hierarchical
@ -103,15 +105,29 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
balance = fields.Function(fields.Numeric(string='Balance', readonly=True,
digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), 'on_change_with_balance')
currency = fields.Many2One(string='Currency',
model_name='currency.currency',
balance_ref = fields.Function(fields.Numeric(string='Balance (Ref.)',
help='Balance in company currency',
readonly=True, digits=(16, Eval('company_currency_digits', 2)),
states={
'invisible': ~Bool(Eval('company_currency')),
}, depends=['company_currency_digits', 'company_currency']),
'on_change_with_balance_ref')
company_currency = fields.Function(fields.Many2One(readonly=True,
string='Company Currency', states={'invisible': True},
model_name='currency.currency'),
'on_change_with_company_currency')
company_currency_digits = fields.Function(fields.Integer(
string='Currency Digits (Ref.)', readonly=True),
'on_change_with_currency_digits')
currency = fields.Many2One(string='Currency', select=True,
model_name='currency.currency', readonly=True,
states={
'readonly': Or(
STATES2['readonly'],
Bool(Eval('lines', [])),
),
'invisible': STATES2['invisible'],
'required': ~STATES2['invisible'],
}, depends=DEPENDS2+['lines'])
currency_digits = fields.Function(fields.Integer(string='Currency Digits',
readonly=True), 'on_change_with_currency_digits')
@ -255,36 +271,95 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
else:
return 2
@fields.depends('company', 'currency', 'btype')
def on_change_with_company_currency(self, name=None):
""" get company-currency if its different from current
cashbook-currency, disable if book is a view
"""
if self.company:
if self.currency:
if self.btype:
if self.company.currency.id != self.currency.id:
return self.company.currency.id
@fields.depends('id')
def on_change_with_balance(self, name=None):
""" compute balance
"""
pool = Pool()
Book2 = pool.get('cashbook.book')
Book3 = pool.get('cashbook.book')
Line = pool.get('cashbook.line')
Currency = pool.get('currency.currency')
tab_line = Line.__table__()
tab_book = Book3.__table__()
cursor = Transaction().connection.cursor()
# select cashbook-lines from current cashbook and below
line_query = Line.search([
('cashbook.id', 'in', Book2.search([
('parent', 'child_of', [self.id]),
], query=True)),
], query=True)
query = line_query.join(tab_line,
# sum lines by currency
bal_by_currency = line_query.join(tab_line,
condition=tab_line.id==line_query.id,
).join(tab_book,
condition=tab_book.id==tab_line.cashbook,
).select(
Sum(tab_line.credit - tab_line.debit).as_('balance'),
tab_book.currency,
group_by=[tab_book.currency],
)
if self.id:
total = Decimal('0.0')
cursor.execute(*bal_by_currency)
balance_lines = cursor.fetchall()
for line in balance_lines:
(balance, id_currency) = line
total += Currency.compute(
Currency(id_currency), # from
balance,
self.currency, # to
)
return total
@fields.depends('company', 'currency', 'id', 'btype')
def on_change_with_balance_ref(self, name=None):
""" balance converted to company-currency
"""
pool = Pool()
Line = pool.get('cashbook.line')
tab_line = Line.__table__()
cursor = Transaction().connection.cursor()
if self.btype is None:
return None
query = tab_line.select(
Sum(tab_line.credit - tab_line.debit),
where = tab_line.cashbook == self.id,
)
if self.id:
balance = Decimal('0.0')
cursor.execute(*query)
result = cursor.fetchone()
balance = Decimal('0.0')
if result:
if result[0] is not None:
balance += result[0]
return balance
if self.currency:
return self.currency.compute(
self.currency, # from
balance,
self.company.currency # to
)
@classmethod
@ModelView.button