mirror of
https://github.com/OCA/account-financial-tools.git
synced 2025-02-02 12:47:26 +02:00
[ADD] Check to forbid to delete or write account move line that are related to an invoice or bank statement
This commit is contained in:
@@ -49,6 +49,13 @@ Summary of constraints are:
|
||||
* Add a check on entries that user cannot provide a secondary currency
|
||||
if the same than the company one.
|
||||
|
||||
* Remove the possibility to modify or delete a move line related to an
|
||||
invoice or a bank statement, no matter what the status of the move
|
||||
(draft, validated or posted). This is usefule in standard context but
|
||||
moreover if you're using : account_default_draft_move. This way you ensure
|
||||
user cannot make mistake even in draft, he must pass through the
|
||||
parent object to make his modification.
|
||||
|
||||
""",
|
||||
'website': 'http://www.camptocamp.com',
|
||||
'data': [],
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
from openerp.osv import fields, orm
|
||||
|
||||
from openerp.osv import fields, orm, osv
|
||||
from openerp.tools.translate import _
|
||||
|
||||
class AccountMove(orm.Model):
|
||||
_inherit = "account.move"
|
||||
@@ -43,6 +43,53 @@ class AccountMove(orm.Model):
|
||||
class AccountMoveLine(orm.Model):
|
||||
_inherit = 'account.move.line'
|
||||
|
||||
def _check_invoice_related_move(self, cr, uid, ids, context=None):
|
||||
for line in self.browse(cr, uid, ids, context=context):
|
||||
if line.invoice:
|
||||
err_msg = _('Invoice name (id): %s (%s)') % (line.invoice.name, str(line.invoice.id))
|
||||
raise osv.except_osv(
|
||||
_('Error!'),
|
||||
_('You cannot do this on an entry generated by an invoice. You must '
|
||||
'change the related invoice directly.\n%s.') % err_msg)
|
||||
return True
|
||||
|
||||
def _check_statement_related_move(self, cr, uid, ids, context=None):
|
||||
for line in self.browse(cr, uid, ids, context=context):
|
||||
if line.statement_id:
|
||||
err_msg = _('Bank statement name (id): %s (%s)') % (line.statement_id.name, str(line.statement_id.id))
|
||||
raise osv.except_osv(
|
||||
_('Error!'),
|
||||
_('You cannot do this on an entry generated by a bank statement. '
|
||||
'You must change the related bank statement directly.\n%s.') % err_msg)
|
||||
return True
|
||||
|
||||
def unlink(self, cr, uid, ids, context=None, check=True):
|
||||
""" Add the verification of:
|
||||
- Is the move related to an invoice
|
||||
- Is the move related to a bank statement
|
||||
In that case, we forbid the move to be deleted even if draft. We should
|
||||
never delete directly a move line related or generated by another object.
|
||||
This is mandatory if you use the all move in draft (module: account_default_draft_move)
|
||||
"""
|
||||
if not context.get('from_parent_object', False):
|
||||
self._check_invoice_related_move(cr, uid, ids, context=context)
|
||||
self._check_statement_related_move(cr, uid, ids, context=context)
|
||||
return super(AccountMoveLine, self).unlink(cr, uid, ids, context=context, check=check)
|
||||
|
||||
def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
|
||||
""" Add the verification of:
|
||||
- Is the move related to an invoice
|
||||
- Is the move related to a bank statement
|
||||
In that case, we forbid the move to be modified even if draft. We should
|
||||
never update directly a move line related or generated by another object.
|
||||
This is mandatory if you use the all move in draft (module: account_default_draft_move)
|
||||
"""
|
||||
if not context.get('from_parent_object', False):
|
||||
self._check_invoice_related_move(cr, uid, ids, context=context)
|
||||
self._check_statement_related_move(cr, uid, ids, context=context)
|
||||
return super(AccountMoveLine, self).write(cr, uid, ids, vals,
|
||||
context=context, check=check, update_check=update_check)
|
||||
|
||||
def _check_currency_and_amount(self, cr, uid, ids, context=None):
|
||||
for l in self.browse(cr, uid, ids, context=context):
|
||||
if ((l.currency_id and not l.amount_currency) or
|
||||
@@ -82,3 +129,38 @@ class AccountMoveLine(orm.Model):
|
||||
['currency_id']
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
class AccountInvoice(orm.Model):
|
||||
_inherit = "account.invoice"
|
||||
|
||||
def action_cancel(self, cr, uid, ids, context=None):
|
||||
"""Override the method to add the key 'from_parent_object' in
|
||||
the context. This is to allow to delete move line related to invoice
|
||||
through the buton cancel."""
|
||||
if context is None:
|
||||
context = {}
|
||||
context['from_parent_object'] = True
|
||||
return super(AccountInvoice, self).action_cancel(cr, uid, ids, context=context)
|
||||
|
||||
|
||||
class AccountBankStatement(orm.Model):
|
||||
_inherit = "account.bank.statement"
|
||||
|
||||
def button_cancel(self, cr, uid, ids, context=None):
|
||||
"""Override the method to add the key 'from_parent_object' in
|
||||
the context. This is to allow to delete move line related to bank statement
|
||||
through the buton cancel."""
|
||||
if context is None:
|
||||
context = {}
|
||||
context['from_parent_object'] = True
|
||||
return super(AccountBankStatement, self).button_cancel(cr, uid, ids, context=context)
|
||||
|
||||
def create_move_from_st_line(self, cr, uid, st_line_id, company_currency_id, st_line_number, context=None):
|
||||
"""Add the from_parent_object key in context in order to be able to post the move."""
|
||||
if context is None:
|
||||
context = {}
|
||||
context['from_parent_object'] = True
|
||||
return super(AccountBankStatement, self).create_move_from_st_line(cr, uid,
|
||||
st_line_id, company_currency_id, st_line_number, context=context)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user