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