From 09ec640e15b92ad50ba5e40b4eb91acef2602884 Mon Sep 17 00:00:00 2001 From: Joel Grand-Guillaume Date: Mon, 21 Jan 2013 14:37:36 +0100 Subject: [PATCH 1/6] [ADD] Check to forbid to delete or write account move line that are related to an invoice or bank statement --- account_constraints/__openerp__.py | 7 ++ account_constraints/account_constraints.py | 86 +++++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/account_constraints/__openerp__.py b/account_constraints/__openerp__.py index 222b3e365..33de72c9f 100644 --- a/account_constraints/__openerp__.py +++ b/account_constraints/__openerp__.py @@ -48,6 +48,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', diff --git a/account_constraints/account_constraints.py b/account_constraints/account_constraints.py index 861005daa..d1ecead4e 100644 --- a/account_constraints/account_constraints.py +++ b/account_constraints/account_constraints.py @@ -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) + From 117fa448dd539ede006d9997bcb900f71d85837e Mon Sep 17 00:00:00 2001 From: Joel Grand-Guillaume Date: Mon, 21 Jan 2013 15:34:47 +0100 Subject: [PATCH 2/6] [FIX] Add the key 'from_parent_object' in context when creating move from invoice. --- account_constraints/account_constraints.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/account_constraints/account_constraints.py b/account_constraints/account_constraints.py index d1ecead4e..22c6f550e 100644 --- a/account_constraints/account_constraints.py +++ b/account_constraints/account_constraints.py @@ -143,6 +143,14 @@ class AccountInvoice(orm.Model): context['from_parent_object'] = True return super(AccountInvoice, self).action_cancel(cr, uid, ids, context=context) + def action_move_create(self, cr, uid, ids, context=None): + """Override the method to add the key 'from_parent_object' in + the context.""" + if context is None: + context = {} + context['from_parent_object'] = True + return super(AccountInvoice,self).action_move_create(cr, uid, ids, context=context) + class AccountBankStatement(orm.Model): _inherit = "account.bank.statement" From f35e1ba6c7b50647e2f5bb0476488364dd9ac115 Mon Sep 17 00:00:00 2001 From: Joel Grand-Guillaume Date: Mon, 21 Jan 2013 17:07:58 +0100 Subject: [PATCH 3/6] [FIX] Typo regarding MP comments --- account_constraints/__openerp__.py | 6 +++--- account_constraints/account_constraints.py | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/account_constraints/__openerp__.py b/account_constraints/__openerp__.py index 33de72c9f..80009f2bb 100644 --- a/account_constraints/__openerp__.py +++ b/account_constraints/__openerp__.py @@ -51,9 +51,9 @@ Summary of constraints are: * 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 + (draft, validated or posted). This is useful in a standard context but + even more if you're using `account_default_draft_move`. This way you ensure + that the user cannot make mistakes even in draft state, he must pass through the parent object to make his modification. """, diff --git a/account_constraints/account_constraints.py b/account_constraints/account_constraints.py index 22c6f550e..0b1155380 100644 --- a/account_constraints/account_constraints.py +++ b/account_constraints/account_constraints.py @@ -48,7 +48,7 @@ class AccountMoveLine(orm.Model): if line.invoice: err_msg = _('Invoice name (id): %s (%s)') % (line.invoice.name, str(line.invoice.id)) raise osv.except_osv( - _('Error!'), + _('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 @@ -58,18 +58,19 @@ class AccountMoveLine(orm.Model): 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!'), + _('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: + """ Add the following checks: - 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) + This is mandatory if you use the module setting all moves 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) @@ -77,12 +78,13 @@ class AccountMoveLine(orm.Model): 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: + """ Add the following checks: - 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) + This is mandatory if you use the module setting all moves 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) @@ -137,7 +139,7 @@ class AccountInvoice(orm.Model): 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.""" + through the cancel button.""" if context is None: context = {} context['from_parent_object'] = True @@ -158,7 +160,7 @@ class AccountBankStatement(orm.Model): 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.""" + through the cancel button.""" if context is None: context = {} context['from_parent_object'] = True From 2dfaecdbdaa1b52ea202db85deb80da8b5229a73 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Mon, 28 Jan 2013 10:14:31 +0100 Subject: [PATCH 4/6] [FIX] copy the context to avoid to propagate the 'from_parent_object' key to callers' context --- account_constraints/account_constraints.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/account_constraints/account_constraints.py b/account_constraints/account_constraints.py index 0b1155380..40e320920 100644 --- a/account_constraints/account_constraints.py +++ b/account_constraints/account_constraints.py @@ -142,6 +142,8 @@ class AccountInvoice(orm.Model): through the cancel button.""" if context is None: context = {} + else: + context = context.copy() context['from_parent_object'] = True return super(AccountInvoice, self).action_cancel(cr, uid, ids, context=context) @@ -150,6 +152,8 @@ class AccountInvoice(orm.Model): the context.""" if context is None: context = {} + else: + context = context.copy() context['from_parent_object'] = True return super(AccountInvoice,self).action_move_create(cr, uid, ids, context=context) @@ -163,6 +167,8 @@ class AccountBankStatement(orm.Model): through the cancel button.""" if context is None: context = {} + else: + context = context.copy() context['from_parent_object'] = True return super(AccountBankStatement, self).button_cancel(cr, uid, ids, context=context) @@ -170,6 +176,8 @@ class AccountBankStatement(orm.Model): """Add the from_parent_object key in context in order to be able to post the move.""" if context is None: context = {} + else: + context = context.copy() 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) From f3a40b250bb168fec39e394f31955a5b1a50435d Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Mon, 28 Jan 2013 10:15:30 +0100 Subject: [PATCH 5/6] [FIX] initialize context to a dict if it is None as we want to get a key --- account_constraints/account_constraints.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/account_constraints/account_constraints.py b/account_constraints/account_constraints.py index 40e320920..14f2628a3 100644 --- a/account_constraints/account_constraints.py +++ b/account_constraints/account_constraints.py @@ -72,6 +72,8 @@ class AccountMoveLine(orm.Model): This is mandatory if you use the module setting all moves in draft (module: account_default_draft_move) """ + if context is None: + context = {} 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) @@ -86,6 +88,8 @@ class AccountMoveLine(orm.Model): This is mandatory if you use the module setting all moves in draft (module: account_default_draft_move) """ + if context is None: + context = {} 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) From 9d306b0ff07bfb89bd63718fb290c059ccc3ed52 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Mon, 28 Jan 2013 10:18:10 +0100 Subject: [PATCH 6/6] [FIX] cut some long lines, remove trailing whitespaces, indentation --- account_constraints/account_constraints.py | 68 +++++++++++++--------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/account_constraints/account_constraints.py b/account_constraints/account_constraints.py index 14f2628a3..63a0912c6 100644 --- a/account_constraints/account_constraints.py +++ b/account_constraints/account_constraints.py @@ -21,6 +21,7 @@ from openerp.osv import fields, orm, osv from openerp.tools.translate import _ + class AccountMove(orm.Model): _inherit = "account.move" @@ -46,9 +47,10 @@ class AccountMoveLine(orm.Model): 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)) + err_msg = _('Invoice name (id): %s (%s)') % + (line.invoice.name, str(line.invoice.id)) raise osv.except_osv( - _('Error'), + _('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 @@ -56,22 +58,25 @@ class AccountMoveLine(orm.Model): 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)) + err_msg = _('Bank statement name (id): %s (%s)') % + (line.statement_id.name, str(line.statement_id.id)) raise osv.except_osv( - _('Error'), + _('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 following checks: - - 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 module setting all moves in draft - (module: account_default_draft_move) - """ + + - 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 module setting + all moves in draft (module: account_default_draft_move) + """ if context is None: context = {} if not context.get('from_parent_object', False): @@ -81,13 +86,15 @@ class AccountMoveLine(orm.Model): def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True): """ Add the following checks: - - 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 module setting all moves in draft - (module: account_default_draft_move) - """ + + - 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 module + setting all moves in draft (module: account_default_draft_move) + """ if context is None: context = {} if not context.get('from_parent_object', False): @@ -139,11 +146,12 @@ class AccountMoveLine(orm.Model): 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 cancel button.""" + the context. This is to allow to delete move line related to + invoice through the cancel button. + """ if context is None: context = {} else: @@ -167,8 +175,9 @@ class AccountBankStatement(orm.Model): 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 cancel button.""" + the context. This is to allow to delete move line related to + bank statement through the cancel button. + """ if context is None: context = {} else: @@ -176,13 +185,16 @@ class AccountBankStatement(orm.Model): 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.""" + 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 = {} else: context = context.copy() 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) - + return super(AccountBankStatement, self).create_move_from_st_line( + cr, uid, st_line_id, company_currency_id, + st_line_number, context=context)