← Back to team overview

openerp-community-reviewer team mailing list archive

[Merge] lp:~acsone-openerp/account-analytic/account_analytic_required-test_suite-sbi into lp:account-analytic

 

Stéphane Bidoul (Acsone) has proposed merging lp:~acsone-openerp/account-analytic/account_analytic_required-test_suite-sbi into lp:account-analytic.

Requested reviews:
  Alexis de Lattre (alexis-via)
  Account Core Editors (account-core-editors)

For more details, see:
https://code.launchpad.net/~acsone-openerp/account-analytic/account_analytic_required-test_suite-sbi/+merge/216451

Add a test suite for account_analytic_required, and fix a small hole in the policy check (when changing analytic account after creating the move line, the policy would not be checked).
-- 
https://code.launchpad.net/~acsone-openerp/account-analytic/account_analytic_required-test_suite-sbi/+merge/216451
Your team Account Core Editors is requested to review the proposed merge of lp:~acsone-openerp/account-analytic/account_analytic_required-test_suite-sbi into lp:account-analytic.
=== modified file 'account_analytic_required/account.py'
--- account_analytic_required/account.py	2013-04-23 14:25:48 +0000
+++ account_analytic_required/account.py	2014-04-18 13:48:50 +0000
@@ -44,21 +44,29 @@
 class account_move_line(orm.Model):
     _inherit = "account.move.line"
 
-    def check_analytic_required(self, cr, uid, vals, context=None):
-        if 'account_id' in vals and (vals.get('debit', 0.0) != 0.0 or vals.get('credit', 0.0) != 0.0):
-            account = self.pool.get('account.account').browse(cr, uid, vals['account_id'], context=context)
-            if account.user_type.analytic_policy == 'always' and not vals.get('analytic_account_id', False):
-                raise orm.except_orm(_('Error :'), _("Analytic policy is set to 'Always' with account %s '%s' but the analytic account is missing in the account move line with label '%s'." % (account.code, account.name, vals.get('name', False))))
-            elif account.user_type.analytic_policy == 'never' and vals.get('analytic_account_id', False):
-                cur_analytic_account = self.pool.get('account.analytic.account').read(cr, uid, vals['analytic_account_id'], ['name', 'code'], context=context)
-                raise orm.except_orm(_('Error :'), _("Analytic policy is set to 'Never' with account %s '%s' but the account move line with label '%s' has an analytic account %s '%s'." % (account.code, account.name, vals.get('name', False), cur_analytic_account['code'], cur_analytic_account['name'])))
+    def check_analytic_required(self, cr, uid, ids, vals, context=None):
+        if 'account_id' in vals or 'analytic_account_id' in vals or \
+                'debit' in vals or 'credit' in vals:
+            if isinstance(ids, (int, long)):
+                ids = [ids]
+            for move_line in self.browse(cr, uid, ids, context):
+                if move_line.debit == 0 and move_line.credit == 0:
+                    continue
+                analytic_policy = move_line.account_id.user_type.analytic_policy
+                if analytic_policy == 'always' and not move_line.analytic_account_id:
+                    raise orm.except_orm(_('Error :'), _("Analytic policy is set to 'Always' with account %s '%s' but the analytic account is missing in the account move line with label '%s'." % (move_line.account_id.code, move_line.account_id.name, move_line.name)))
+                elif analytic_policy == 'never' and move_line.analytic_account_id:
+                    raise orm.except_orm(_('Error :'), _("Analytic policy is set to 'Never' with account %s '%s' but the account move line with label '%s' has an analytic account %s '%s'." % (move_line.account_id.code, move_line.account_id.name, move_line.name, move_line.analytic_account_id.code, move_line.analytic_account_id.name)))
         return True
 
     def create(self, cr, uid, vals, context=None, check=True):
-        self.check_analytic_required(cr, uid, vals, context=context)
-        return super(account_move_line, self).create(cr, uid, vals, context=context, check=check)
+        line_id = super(account_move_line, self).create(cr, uid, vals, context=context, check=check)
+        self.check_analytic_required(cr, uid, line_id, vals, context=context)
+        return line_id
 
     def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
-        self.check_analytic_required(cr, uid, vals, context=context)
-        return super(account_move_line, self).write(cr, uid, ids, vals, context=context, check=check, update_check=update_check)
+        res = super(account_move_line, self).write(cr, uid, ids, vals, context=context, check=check, update_check=update_check)
+        self.check_analytic_required(cr, uid, ids, vals, context=context)
+        return res
+
 

=== added directory 'account_analytic_required/tests'
=== added file 'account_analytic_required/tests/__init__.py'
--- account_analytic_required/tests/__init__.py	1970-01-01 00:00:00 +0000
+++ account_analytic_required/tests/__init__.py	2014-04-18 13:48:50 +0000
@@ -0,0 +1,30 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    Account analytic required module for OpenERP
+#    Copyright (C) 2014 Acsone (http://acsone.eu). All Rights Reserved
+#    @author Stéphane Bidoul <stephane.bidoul@xxxxxxxxx>
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+from . import test_account_analytic_required
+
+fast_suite = [
+]
+
+checks = [
+    test_account_analytic_required,
+]

=== added file 'account_analytic_required/tests/test_account_analytic_required.py'
--- account_analytic_required/tests/test_account_analytic_required.py	1970-01-01 00:00:00 +0000
+++ account_analytic_required/tests/test_account_analytic_required.py	2014-04-18 13:48:50 +0000
@@ -0,0 +1,125 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    Account analytic required module for OpenERP
+#    Copyright (C) 2014 Acsone (http://acsone.eu). All Rights Reserved
+#    @author Stéphane Bidoul <stephane.bidoul@xxxxxxxxx>
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+from datetime import datetime
+
+from openerp.tests import common
+from openerp.osv import orm
+
+
+class test_account_analytic_required(common.TransactionCase):
+
+    def setUp(self):
+        super(test_account_analytic_required, self).setUp()
+        self.account_obj = self.registry('account.account')
+        self.account_type_obj = self.registry('account.account.type')
+        self.move_obj = self.registry('account.move')
+        self.move_line_obj = self.registry('account.move.line')
+        self.analytic_account_obj = self.registry('account.analytic.account')
+        self.analytic_account_id = self.analytic_account_obj.create(
+            self.cr, self.uid, {'name': 'test aa', 'type': 'normal'})
+
+    def _create_move(self, with_analytic, amount=100):
+        date = datetime.now()
+        period_id = self.registry('account.period').find(
+            self.cr, self.uid, date,
+            context={'account_period_prefer_normal': True})[0]
+        move_vals = {
+            'journal_id': self.ref('account.sales_journal'),
+            'period_id': period_id,
+            'date': date,
+        }
+        move_id = self.move_obj.create(self.cr, self.uid, move_vals)
+        move_line_id = self.move_line_obj.create(
+            self.cr, self.uid,
+            {'move_id': move_id,
+             'name': '/',
+             'debit': 0,
+             'credit': amount,
+             'account_id': self.ref('account.a_sale'),
+             'analytic_account_id': self.analytic_account_id if with_analytic else False})
+        self.move_line_obj.create(
+            self.cr, self.uid,
+            {'move_id': move_id,
+             'name': '/',
+             'debit': amount,
+             'credit': 0,
+             'account_id': self.ref('account.a_recv')})
+        return move_line_id
+
+    def _set_analytic_policy(self, policy, aref='account.a_sale'):
+        account_type = self.account_obj.browse(self.cr, self.uid,
+                                               self.ref(aref)).user_type
+        self.account_type_obj.write(self.cr, self.uid, account_type.id,
+                                    {'analytic_policy': policy})
+
+    def test_optional(self):
+        self._create_move(with_analytic=False)
+        self._create_move(with_analytic=True)
+
+    def test_always_no_analytic(self):
+        self._set_analytic_policy('always')
+        with self.assertRaises(orm.except_orm):
+            self._create_move(with_analytic=False)
+
+    def test_always_no_analytic_0(self):
+        # accept missing analytic account when debit=credit=0
+        self._set_analytic_policy('always')
+        self._create_move(with_analytic=False, amount=0)
+
+    def test_always_with_analytic(self):
+        self._set_analytic_policy('always')
+        self._create_move(with_analytic=True)
+
+    def test_never_no_analytic(self):
+        self._set_analytic_policy('never')
+        self._create_move(with_analytic=False)
+
+    def test_never_with_analytic(self):
+        self._set_analytic_policy('never')
+        with self.assertRaises(orm.except_orm):
+            self._create_move(with_analytic=True)
+
+    def test_never_with_analytic_0(self):
+        # accept analytic when debit=credit=0
+        self._set_analytic_policy('never')
+        self._create_move(with_analytic=True, amount=0)
+
+    def test_always_remove_analytic(self):
+        # remove partner when policy is always
+        self._set_analytic_policy('always')
+        line_id = self._create_move(with_analytic=True)
+        with self.assertRaises(orm.except_orm):
+            self.move_line_obj.write(self.cr, self.uid, line_id,
+                                     {'analytic_account_id': False})
+
+    def test_change_account(self):
+        self._set_analytic_policy('always', aref='account.a_expense')
+        line_id = self._create_move(with_analytic=False)
+        # change account to a_pay with policy always but missing analytic_account
+        with self.assertRaises(orm.except_orm):
+            self.move_line_obj.write(self.cr, self.uid, line_id,
+                                     {'account_id': self.ref('account.a_expense')})
+        # change account to a_pay with policy always with partner -> ok
+        self.move_line_obj.write(self.cr, self.uid, line_id,
+                                 {'account_id': self.ref('account.a_expense'),
+                                  'analytic_account_id': self.analytic_account_id})