From d8316664538959bf1aee17deb7ef6e6472dac201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 7 Nov 2018 19:01:14 +0100 Subject: [PATCH] Base journal cash report on account kind receivable and payable Since changeset d1d3a1c12158, journal has no more credit/debit accounts. The journal cash report is based now on all moves between receivable/payable and others. We clean also unused Model and method from the removal of the credit/debit accounts. issue7808 review68331002 --- CHANGELOG | 2 + journal.py | 88 ++++++++------------------------------ tests/scenario_reports.rst | 24 +++++++++++ 3 files changed, 44 insertions(+), 70 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index a9020e2b1a..34025a18c6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Base journal cash report on account kind receivable and payable + Version 5.0.0 - 2018-10-01 * Bug fixes (see mercurial logs for details) * Set party required on account payable and receivable of party diff --git a/journal.py b/journal.py index dc31f5deb6..58e2c7c89a 100644 --- a/journal.py +++ b/journal.py @@ -2,7 +2,6 @@ # this repository contains the full copyright notices and license terms. from decimal import Decimal -from sql import Null from sql.aggregate import Sum from trytond.model import ( @@ -69,13 +68,6 @@ class Journal( super(Journal, cls).__setup__() cls._order.insert(0, ('name', 'ASC')) - @classmethod - def multivalue_model(cls, field): - pool = Pool() - if field in {'credit_account', 'debit_account'}: - return pool.get('account.journal.account') - return super(Journal, cls).multivalue_model(field) - @classmethod def default_sequence(cls, **pattern): return None @@ -114,6 +106,8 @@ class Journal( pool = Pool() MoveLine = pool.get('account.move.line') Move = pool.get('account.move') + Account = pool.get('account.account') + Company = pool.get('company.company') context = Transaction().context cursor = Transaction().connection.cursor() @@ -122,32 +116,25 @@ class Journal( for name in ['debit', 'credit', 'balance']: result[name] = dict.fromkeys(ids, 0) + company_id = Transaction().context.get('company') + if not company_id: + return result + company = Company(company_id) + line = MoveLine.__table__() move = Move.__table__() - where = ((move.date >= context['start_date']) - & (move.date <= context['end_date'])) + account = Account.__table__() + where = ((move.date >= context.get('start_date')) + & (move.date <= context.get('end_date')) + & ~account.kind.in_(['receivable', 'payable']) + & (move.company == company.id)) for sub_journals in grouped_slice(journals): sub_journals = list(sub_journals) red_sql = reduce_ids(move.journal, [j.id for j in sub_journals]) - accounts = None - for journal in sub_journals: - credit_account = (journal.credit_account.id - if journal.credit_account else None) - debit_account = (journal.debit_account.id - if journal.debit_account else None) - clause = ((move.journal == journal.id) - & (((line.credit != Null) - & (line.account == credit_account)) - | ((line.debit != Null) - & (line.account == debit_account)))) - if accounts is None: - accounts = clause - else: - accounts |= clause - query = line.join(move, 'LEFT', condition=line.move == move.id + ).join(account, 'LEFT', condition=line.account == account.id ).select(move.journal, Sum(line.debit), Sum(line.credit), - where=where & red_sql & accounts, + where=where & red_sql, group_by=move.journal) cursor.execute(*query) for journal_id, debit, credit in cursor.fetchall(): @@ -156,9 +143,10 @@ class Journal( debit = Decimal(str(debit)) if not isinstance(credit, Decimal): credit = Decimal(str(credit)) - result['debit'][journal_id] = debit - result['credit'][journal_id] = credit - result['balance'][journal_id] = debit - credit + result['debit'][journal_id] = company.currency.round(debit) + result['credit'][journal_id] = company.currency.round(credit) + result['balance'][journal_id] = company.currency.round( + debit - credit) return result @@ -195,46 +183,6 @@ class JournalSequence(ModelSQL, CompanyValueMixin): parent='journal', fields=fields) -class JournalAccount(ModelSQL, CompanyValueMixin): - "Journal Account" - __name__ = 'account.journal.account' - journal = fields.Many2One( - 'account.journal', "Journal", ondelete='CASCADE', select=True) - credit_account = fields.Many2One( - 'account.account', "Default Credit Account", - domain=[ - ('kind', '!=', 'view'), - ('company', '=', Eval('company', -1)), - ], - depends=['company']) - debit_account = fields.Many2One( - 'account.account', "Default Debit Account", - domain=[ - ('kind', '!=', 'view'), - ('company', '=', Eval('company', -1)), - ], - depends=['company']) - - @classmethod - def __register__(cls, module_name): - TableHandler = backend.get('TableHandler') - exist = TableHandler.table_exist(cls._table) - - super(JournalAccount, cls).__register__(module_name) - - if not exist: - cls._migrate_property([], [], []) - - @classmethod - def _migrate_property(cls, field_names, value_names, fields): - field_names.extend(['credit_account', 'debit_account']) - value_names.extend(['credit_account', 'debit_account']) - fields.append('company') - migrate_property( - 'account.journal', field_names, cls, value_names, - parent='journal', fields=fields) - - class JournalCashContext(ModelView): 'Journal Cash Context' __name__ = 'account.journal.open_cash.context' diff --git a/tests/scenario_reports.rst b/tests/scenario_reports.rst index f2890ce4a9..749cda7195 100644 --- a/tests/scenario_reports.rst +++ b/tests/scenario_reports.rst @@ -68,6 +68,19 @@ Create a moves:: >>> line.party = party >>> move.save() + >>> move = Move() + >>> move.period = period + >>> move.journal = journal_cash + >>> move.date = period.start_date + >>> line = move.lines.new() + >>> line.account = cash + >>> line.debit = Decimal(10) + >>> line = move.lines.new() + >>> line.account = receivable + >>> line.credit = Decimal(10) + >>> line.party = party + >>> move.save() + Print some reports:: >>> GeneralLedgerAccount = Model.get('account.general_ledger.account') @@ -105,3 +118,14 @@ Print some reports:: >>> print_general_journal = Wizard('account.move.print_general_journal') >>> print_general_journal.execute('print_') + + >>> with config.set_context( + ... start_date=period.start_date, + ... end_date=period.end_date): + ... journal_cash = Journal(journal_cash.id) + >>> journal_cash.credit + Decimal('0.00') + >>> journal_cash.debit + Decimal('10.00') + >>> journal_cash.balance + Decimal('10.00')