fix: deny invalid 'date' in context
This commit is contained in:
parent
610532f6a5
commit
dbc570bdbf
1 changed files with 56 additions and 34 deletions
90
book.py
90
book.py
|
@ -11,6 +11,7 @@ from trytond.transaction import Transaction
|
|||
from trytond.pool import Pool
|
||||
from trytond.report import Report
|
||||
from decimal import Decimal
|
||||
from datetime import date
|
||||
from sql.aggregate import Sum
|
||||
from sql.conditionals import Case, Coalesce
|
||||
from sql.functions import CurrentDate
|
||||
|
@ -274,6 +275,14 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
|
|||
context = Transaction().context
|
||||
|
||||
query_date = context.get('date', IrDate.today())
|
||||
|
||||
# deny invalid date in context
|
||||
if isinstance(query_date, str):
|
||||
try :
|
||||
dt1 = date.fromisoformat(query_date)
|
||||
except :
|
||||
query_date = IrDate.today()
|
||||
|
||||
query = tab_book.join(tab_line,
|
||||
condition=tab_book.id==tab_line.cashbook,
|
||||
).select(
|
||||
|
@ -336,49 +345,62 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
|
|||
Book2 = pool.get('cashbook.book')
|
||||
Currency = pool.get('currency.currency')
|
||||
Company = pool.get('company.company')
|
||||
IrDate = pool.get('ir.date')
|
||||
tab_book = Book2.__table__()
|
||||
tab_comp = Company.__table__()
|
||||
(tab_line, tab2) = cls.get_balance_of_cashbook_sql()
|
||||
cursor = Transaction().connection.cursor()
|
||||
context = Transaction().context
|
||||
|
||||
result = {x:{y.id: Decimal('0.0') for y in cashbooks} for x in names}
|
||||
|
||||
# deny invalid date in context
|
||||
query_date = context.get('date', IrDate.today())
|
||||
if isinstance(query_date, str):
|
||||
try :
|
||||
dt1 = date.fromisoformat(query_date)
|
||||
except :
|
||||
query_date = IrDate.today()
|
||||
|
||||
# query balances of cashbooks and sub-cashbooks
|
||||
tab_subids = sub_ids_hierarchical('cashbook.book')
|
||||
query = tab_book.join(tab_subids,
|
||||
condition=tab_book.id==tab_subids.parent,
|
||||
).join(tab_comp,
|
||||
condition=tab_book.company==tab_comp.id,
|
||||
).join(tab_line,
|
||||
condition=tab_line.cashbook==AnyInArray(tab_subids.subids),
|
||||
).select(
|
||||
tab_book.id,
|
||||
tab_book.currency.as_('to_currency'),
|
||||
tab_line.currency.as_('from_currency'),
|
||||
tab_comp.currency.as_('company_currency'),
|
||||
Sum(tab_line.balance).as_('balance'),
|
||||
Sum(tab_line.balance_all).as_('balance_all'),
|
||||
group_by=[tab_book.id, tab_line.currency, tab_comp.currency],
|
||||
where=tab_book.id.in_([x.id for x in cashbooks]),
|
||||
)
|
||||
cursor.execute(*query)
|
||||
records = cursor.fetchall()
|
||||
with Transaction().set_context({
|
||||
'date': query_date,
|
||||
}):
|
||||
(tab_line, tab2) = cls.get_balance_of_cashbook_sql()
|
||||
tab_subids = sub_ids_hierarchical('cashbook.book')
|
||||
query = tab_book.join(tab_subids,
|
||||
condition=tab_book.id==tab_subids.parent,
|
||||
).join(tab_comp,
|
||||
condition=tab_book.company==tab_comp.id,
|
||||
).join(tab_line,
|
||||
condition=tab_line.cashbook==AnyInArray(tab_subids.subids),
|
||||
).select(
|
||||
tab_book.id,
|
||||
tab_book.currency.as_('to_currency'),
|
||||
tab_line.currency.as_('from_currency'),
|
||||
tab_comp.currency.as_('company_currency'),
|
||||
Sum(tab_line.balance).as_('balance'),
|
||||
Sum(tab_line.balance_all).as_('balance_all'),
|
||||
group_by=[tab_book.id, tab_line.currency, tab_comp.currency],
|
||||
where=tab_book.id.in_([x.id for x in cashbooks]),
|
||||
)
|
||||
cursor.execute(*query)
|
||||
records = cursor.fetchall()
|
||||
|
||||
for record in records:
|
||||
values = {
|
||||
'balance': Currency.compute(
|
||||
record[2], record[4], record[1],
|
||||
),
|
||||
'balance_all': Currency.compute(
|
||||
record[2], record[5], record[1],
|
||||
),
|
||||
'balance_ref': Currency.compute(
|
||||
record[2], record[5], record[3],
|
||||
),
|
||||
}
|
||||
for record in records:
|
||||
values = {
|
||||
'balance': Currency.compute(
|
||||
record[2], record[4], record[1],
|
||||
),
|
||||
'balance_all': Currency.compute(
|
||||
record[2], record[5], record[1],
|
||||
),
|
||||
'balance_ref': Currency.compute(
|
||||
record[2], record[5], record[3],
|
||||
),
|
||||
}
|
||||
|
||||
for name in names:
|
||||
result[name][record[0]] += values[name]
|
||||
for name in names:
|
||||
result[name][record[0]] += values[name]
|
||||
return result
|
||||
|
||||
@fields.depends('btype')
|
||||
|
|
Loading…
Reference in a new issue