diff --git a/addons/account/models/company.py b/addons/account/models/company.py index 4535f0da68d..40128bb2f4a 100644 --- a/addons/account/models/company.py +++ b/addons/account/models/company.py @@ -227,6 +227,7 @@ class ResCompany(models.Model): def _validate_fiscalyear_lock(self, values): if values.get('fiscalyear_lock_date'): + nb_draft_entries = self.env['account.move'].search([ ('company_id', 'in', self.ids), ('state', '=', 'draft'), @@ -234,6 +235,15 @@ class ResCompany(models.Model): if nb_draft_entries: raise ValidationError(_('There are still unposted entries in the period you want to lock. You should either post or delete them.')) + has_unreconciled_statement_lines = self.env['account.bank.statement.line'].search_count([ + ('company_id', 'in', self.ids), + ('is_reconciled', '=', False), + ]) + if has_unreconciled_statement_lines: + raise ValidationError(_( + "There are still unreconciled bank statement lines in the period you want to lock." + "You should either reconcile or delete them.")) + def _get_user_fiscal_lock_date(self): """Get the fiscal lock date for this company depending on the user""" self.ensure_one() diff --git a/addons/account/tests/test_account_move_entry.py b/addons/account/tests/test_account_move_entry.py index c4198a1eecb..aba886a1929 100644 --- a/addons/account/tests/test_account_move_entry.py +++ b/addons/account/tests/test_account_move_entry.py @@ -142,6 +142,23 @@ class TestAccountMove(AccountTestInvoicingCommon): # The date has been changed to the first valid date. self.assertEqual(copy_move.date, copy_move.company_id.fiscalyear_lock_date + relativedelta(days=1)) + def test_misc_fiscalyear_lock_date_2(self): + self.test_move.post() + + # Create a bank statement to get a balance in the suspense account. + statement = self.env['account.bank.statement'].create({ + 'journal_id': self.company_data['default_journal_bank'].id, + 'date': '2016-01-01', + 'line_ids': [ + (0, 0, {'payment_ref': 'test', 'amount': 10.0}) + ], + }) + statement.button_post() + + # You can't lock the fiscal year if there is some unreconciled statement. + with self.assertRaises(ValidationError), self.cr.savepoint(): + self.test_move.company_id.fiscalyear_lock_date = fields.Date.from_string('2017-01-01') + def test_misc_tax_lock_date_1(self): self.test_move.post()