diff --git a/CHANGELOG b/CHANGELOG index 4a630eb76e..86b1407084 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add balance non-deferral wizard * Display write-off amount in reconciliation * Enforce same company for a chart of account * Change tax percentage into rate diff --git a/__init__.py b/__init__.py index 21f9eefe94..b6169992fc 100644 --- a/__init__.py +++ b/__init__.py @@ -15,6 +15,7 @@ from .party import * def register(): Pool.register( FiscalYear, + BalanceNonDeferralStart, CloseFiscalYearStart, TypeTemplate, Type, @@ -69,6 +70,7 @@ def register(): module='account', type_='model') Pool.register( OpenType, + BalanceNonDeferral, CloseFiscalYear, OpenChartAccount, PrintGeneralLedger, diff --git a/doc/index.rst b/doc/index.rst index 6a1d161c94..b7195bd848 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -21,6 +21,10 @@ year will close all the corresponding periods. - Post Move Sequence: The sequence to use for numbering moves in this fiscal year. +The *Balance Non-Deferral* wizard allow to create a move that will debit/credit +each non-deferral account in such way to have a balance equals to zero for the +fiscal year and debit/credit a counter part account. + Period ****** diff --git a/fiscalyear.py b/fiscalyear.py index e7a9e8c37a..dd9cfc2eff 100644 --- a/fiscalyear.py +++ b/fiscalyear.py @@ -2,13 +2,16 @@ #this repository contains the full copyright notices and license terms. from dateutil.relativedelta import relativedelta from trytond.model import ModelView, ModelSQL, fields -from trytond.wizard import Wizard, StateView, StateTransition, Button +from trytond.wizard import Wizard, StateView, StateTransition, StateAction, \ + Button from trytond.tools import datetime_strftime -from trytond.pyson import Eval, If +from trytond.pyson import Eval, If, PYSONEncoder from trytond.transaction import Transaction from trytond.pool import Pool -__all__ = ['FiscalYear', 'CloseFiscalYearStart', 'CloseFiscalYear'] +__all__ = ['FiscalYear', + 'BalanceNonDeferralStart', 'BalanceNonDeferral', + 'CloseFiscalYearStart', 'CloseFiscalYear'] STATES = { 'readonly': Eval('state') == 'close', @@ -306,6 +309,118 @@ class FiscalYear(ModelSQL, ModelView): }) +class BalanceNonDeferralStart(ModelView): + 'Balance Non-Deferral' + __name__ = 'account.fiscalyear.balance_non_deferral.start' + fiscalyear = fields.Many2One('account.fiscalyear', 'Fiscal Year', + required=True, domain=[('state', '=', 'open')]) + journal = fields.Many2One('account.journal', 'Journal', required=True, + domain=[ + ('type', '=', 'situation'), + ]) + period = fields.Many2One('account.period', 'Period', required=True, + domain=[ + ('fiscalyear', '=', Eval('fiscalyear')), + ('type', '=', 'adjustment'), + ], + depends=['fiscalyear']) + credit_account = fields.Many2One('account.account', 'Credit Account', + required=True, + domain=[ + ('kind', '!=', 'view'), + ('company', '=', Eval('context', {}).get('company', 0)), + ('deferral', '=', True), + ]) + debit_account = fields.Many2One('account.account', 'Debit Account', + required=True, + domain=[ + ('kind', '!=', 'view'), + ('company', '=', Eval('context', {}).get('company', 0)), + ('deferral', '=', True), + ]) + + +class BalanceNonDeferral(Wizard): + 'Balance Non-Deferral' + __name__ = 'account.fiscalyear.balance_non_deferral' + start = StateView('account.fiscalyear.balance_non_deferral.start', + 'account.fiscalyear_balance_non_deferral_start_view_form', [ + Button('Cancel', 'end', 'tryton-cancel'), + Button('Ok', 'balance', 'tryton-ok', default=True), + ]) + balance = StateAction('account.act_move_line_form') + + def get_move_line(self, account): + pool = Pool() + Line = pool.get('account.move.line') + if account.company.currency.is_zero(account.balance): + return + line = Line() + line.account = account + if account.balance >= 0: + line.credit = abs(account.balance) + line.debit = 0 + else: + line.credit = 0 + line.debit = abs(account.balance) + return line + + def get_counterpart_line(self, amount): + pool = Pool() + Line = pool.get('account.move.line') + if self.start.fiscalyear.company.currency.is_zero(amount): + return + line = Line() + if amount >= 0: + line.credit = abs(amount) + line.debit = 0 + line.account = self.start.credit_account + else: + line.credit = 0 + line.debit = abs(amount) + line.account = self.start.debit_account + return line + + def create_move(self): + pool = Pool() + Account = pool.get('account.account') + Move = pool.get('account.move') + + with Transaction().set_context(fiscalyear=self.start.fiscalyear.id, + date=None, cumulate=False): + accounts = Account.search([ + ('company', '=', self.start.fiscalyear.company.id), + ('deferral', '=', False), + ]) + lines = [] + for account in accounts: + line = self.get_move_line(account) + if line: + lines.append(line) + if not lines: + return + amount = sum(l.debit - l.credit for l in lines) + counter_part_line = self.get_counterpart_line(amount) + if counter_part_line: + lines.append(counter_part_line) + + move = Move() + move.period = self.start.period + move.journal = self.start.journal + move.date = self.start.period.start_date + move.origin = self.start.fiscalyear + move.lines = lines + move.save() + return move + + def do_balance(self, action): + self.create_move() + action['pyson_domain'] = PYSONEncoder().encode([ + ('origin', '=', str(self.start.fiscalyear)), + ]) + return action, {} + + class CloseFiscalYearStart(ModelView): 'Close Fiscal Year' __name__ = 'account.fiscalyear.close.start' diff --git a/fiscalyear.xml b/fiscalyear.xml index 1f175796aa..9ad5f0395a 100644 --- a/fiscalyear.xml +++ b/fiscalyear.xml @@ -57,6 +57,24 @@ this repository contains the full copyright notices and license terms. --> + + account.fiscalyear.balance_non_deferral.start + form + fiscalyear_balance_non_deferral_start_form + + + + Balance Non-Deferral + account.fiscalyear.balance_non_deferral + + + account.fiscalyear.close.start form @@ -67,7 +85,7 @@ this repository contains the full copyright notices and license terms. --> Close Fiscal Year account.fiscalyear.close - + diff --git a/move.py b/move.py index 594b112af6..bc87e990ad 100644 --- a/move.py +++ b/move.py @@ -156,7 +156,7 @@ class Move(ModelSQL, ModelView): @classmethod def _get_origin(cls): 'Return list of Model names for origin Reference' - return [] + return ['account.fiscalyear'] @classmethod def get_origin(cls): diff --git a/tests/test_account.py b/tests/test_account.py index 07bb330c17..46d5bdce37 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -40,6 +40,9 @@ class AccountTestCase(unittest.TestCase): self.move = POOL.get('account.move') self.journal = POOL.get('account.journal') self.account_type = POOL.get('account.account.type') + self.period = POOL.get('account.period') + self.balance_non_deferral = POOL.get( + 'account.fiscalyear.balance_non_deferral', type='wizard') def test0005views(self): ''' @@ -233,7 +236,7 @@ class AccountTestCase(unittest.TestCase): (Decimal(0), Decimal(100))) self.assertEqual(revenue.balance, Decimal(-100)) - # Close fiscalyear + # Balance non-deferral journal_sequence, = self.sequence.search([ ('code', '=', 'account.journal'), ]) @@ -243,6 +246,13 @@ class AccountTestCase(unittest.TestCase): 'type': 'situation', 'sequence': journal_sequence.id, }]) + period_closing, = self.period.create([{ + 'name': 'Closing', + 'start_date': fiscalyear.end_date, + 'end_date': fiscalyear.end_date, + 'fiscalyear': fiscalyear.id, + 'type': 'adjustment', + }]) type_equity, = self.account_type.search([ ('name', '=', 'Equity'), ]) @@ -253,28 +263,25 @@ class AccountTestCase(unittest.TestCase): 'parent': revenue.parent.id, 'kind': 'other', }]) - self.move.create([{ - 'period': fiscalyear.periods[-1].id, - 'journal': journal_closing.id, - 'date': fiscalyear.periods[-1].end_date, - 'lines': [ - ('create', [{ - 'account': revenue.id, - 'debit': Decimal(100), - }, { - 'account': expense.id, - 'credit': Decimal(30), - }, { - 'account': account_pl.id, - 'credit': Decimal('70'), - }]), - ], - }]) + + session_id = self.balance_non_deferral.create()[0] + balance_non_deferral = self.balance_non_deferral(session_id) + + balance_non_deferral.start.fiscalyear = fiscalyear + balance_non_deferral.start.journal = journal_closing + balance_non_deferral.start.period = period_closing + balance_non_deferral.start.credit_account = account_pl + balance_non_deferral.start.debit_account = account_pl + + balance_non_deferral._execute('balance') + moves = self.move.search([ ('state', '=', 'draft'), ('period.fiscalyear', '=', fiscalyear.id), ]) self.move.post(moves) + + # Close fiscalyear self.fiscalyear.close([fiscalyear]) # Check deferral diff --git a/view/fiscalyear_balance_non_deferral_start_form.xml b/view/fiscalyear_balance_non_deferral_start_form.xml new file mode 100644 index 0000000000..63346bc3b5 --- /dev/null +++ b/view/fiscalyear_balance_non_deferral_start_form.xml @@ -0,0 +1,16 @@ + + +
+