2
0

[IMP] refactor account_partner_required

* extention points for policies
* use constraints for readbility and robustness
This commit is contained in:
Stéphane Bidoul 2014-09-25 11:08:50 +02:00 committed by Alexis de Lattre
parent b4047063e1
commit a6fd61c129

View File

@ -27,12 +27,21 @@ from openerp.tools.translate import _
class account_account_type(orm.Model): class account_account_type(orm.Model):
_inherit = "account.account.type" _inherit = "account.account.type"
def _get_policies(self, cr, uid, context=None):
"""This is the method to be inherited for adding policies"""
return [('optional', 'Optional'),
('always', 'Always'),
('never', 'Never')]
def __get_policies(self, cr, uid, context=None):
""" Call method which can be inherited """
return self._get_policies(cr, uid, context=context)
_columns = { _columns = {
'partner_policy': fields.selection( 'partner_policy': fields.selection(
[('optional', 'Optional'), __get_policies,
('always', 'Always'),
('never', 'Never')],
'Policy for partner field', 'Policy for partner field',
required=True,
help="Set the policy for the partner field : if you select " help="Set the policy for the partner field : if you select "
"'Optional', the accountant is free to put a partner " "'Optional', the accountant is free to put a partner "
"on an account move line with this type of account ; " "on an account move line with this type of account ; "
@ -50,47 +59,41 @@ class account_account_type(orm.Model):
class account_move_line(orm.Model): class account_move_line(orm.Model):
_inherit = "account.move.line" _inherit = "account.move.line"
def check_partner_required(self, cr, uid, ids, vals, context=None): def _get_partner_policy(self, cr, uid, account, context=None):
if 'account_id' in vals or 'partner_id' in vals or \ """ Extension point to obtain analytic policy for an account """
'debit' in vals or 'credit' in vals: return account.user_type.partner_policy
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
policy = move_line.account_id.user_type.partner_policy
if policy == 'always' and not move_line.partner_id:
raise orm.except_orm(_('Error :'),
_("Partner policy is set to 'Always' "
"with account %s '%s' but the "
"partner is missing in the account "
"move line with label '%s'." %
(move_line.account_id.code,
move_line.account_id.name,
move_line.name)))
elif policy == 'never' and move_line.partner_id:
raise orm.except_orm(_('Error :'),
_("Partner policy is set to 'Never' "
"with account %s '%s' but the "
"account move line with label '%s' "
"has a partner '%s'." %
(move_line.account_id.code,
move_line.account_id.name,
move_line.name,
move_line.partner_id.name)))
def create(self, cr, uid, vals, context=None, check=True): def _check_partner_required_msg(self, cr, uid, ids, context=None):
line_id = super(account_move_line, self).create(cr, uid, vals, for move_line in self.browse(cr, uid, ids, context):
context=context, if move_line.debit == 0 and move_line.credit == 0:
check=check) continue
self.check_partner_required(cr, uid, line_id, vals, context=context) policy = self._get_partner_policy(cr, uid,
return line_id move_line.account_id,
context=context)
if policy == 'always' and not move_line.partner_id:
return _("Partner policy is set to 'Always' "
"with account %s '%s' but the "
"partner is missing in the account "
"move line with label '%s'." %
(move_line.account_id.code,
move_line.account_id.name,
move_line.name))
elif policy == 'never' and move_line.partner_id:
return _("Partner policy is set to 'Never' "
"with account %s '%s' but the "
"account move line with label '%s' "
"has a partner '%s'." %
(move_line.account_id.code,
move_line.account_id.name,
move_line.name,
move_line.partner_id.name))
def write(self, cr, uid, ids, vals, context=None, check=True, def _check_partner_required(self, cr, uid, ids, context=None):
update_check=True): return not self._check_partner_required_msg(cr, uid, ids,
res = super(account_move_line, self).write(cr, uid, ids, vals, context=context)
context=context,
check=check, _constraints = [
update_check=update_check) (_check_partner_required,
self.check_partner_required(cr, uid, ids, vals, context=context) _check_partner_required_msg,
return res ['partner_id']),
]