diff --git a/account_statement_cancel_line/statement.py b/account_statement_cancel_line/statement.py index 88e55126..f87fbd64 100644 --- a/account_statement_cancel_line/statement.py +++ b/account_statement_cancel_line/statement.py @@ -27,7 +27,7 @@ class Statement(orm.Model): """Bank Statement. - All logic is in the BankStatementLine + Minimal changes to allow cancelling single lines. """ @@ -38,3 +38,25 @@ class Statement(orm.Model): _defaults = { } + + def button_confirm_bank(self, cr, uid, ids, context=None): + """Change the state on the statement lines. Return super.""" + st_line_obj = self.pool['account.bank.statement.line'] + for st_data in self.read(cr, uid, ids, ['line_ids'], context=context): + st_line_obj.write(cr, uid, st_data['line_ids'], { + 'state': 'confirmed' + }, context=context) + + return super(Statement, self).button_confirm_bank( + cr, uid, ids, context) + + def button_cancel(self, cr, uid, ids, context=None): + """Change the state on the statement lines. Return super.""" + st_line_obj = self.pool['account.bank.statement.line'] + for st_data in self.read(cr, uid, ids, ['line_ids'], context=context): + st_line_obj.write(cr, uid, st_data['line_ids'], { + 'state': 'draft' + }, context=context) + + return super(Statement, self).button_cancel( + cr, uid, ids, context) diff --git a/account_statement_cancel_line/statement_line.py b/account_statement_cancel_line/statement_line.py index f0cf2226..539b46f5 100644 --- a/account_statement_cancel_line/statement_line.py +++ b/account_statement_cancel_line/statement_line.py @@ -52,6 +52,14 @@ class StatementLine(orm.Model): the bank-statement-reconcile branch does not). """ + if context is None: + context = {} + local_ctx = context.copy() + # if account_constraints is installed, we need to tell it that moves + # are being created by a statement, which is OK. + # The module tries to prevent direct changes to the moves created by + # bank statements. + local_ctx['from_parent_object'] = True statement_pool = self.pool.get('account.bank.statement') for st_line in self.browse(cr, uid, ids, context): @@ -64,13 +72,15 @@ class StatementLine(orm.Model): st_line_number = statement_pool.get_next_st_line_number( cr, uid, st_number, st_line, context) + # We pass the local_ctx so that account_constraints allows us to + # work on the moves generated by the bank statement statement_pool.create_move_from_st_line( cr, uid, st_line.id, curr_id, st_line_number, - context) + local_ctx) self.write(cr, uid, st_line.id, { 'state': 'confirmed' }, context) @@ -83,7 +93,17 @@ class StatementLine(orm.Model): module. """ + if context is None: + context = {} + local_ctx = context.copy() + # if account_constraints is installed, we need to tell it that moves + # are being created by a statement, which is OK. + # The module tries to prevent direct changes to the moves created by + # bank statements. + local_ctx['from_parent_object'] = True + move_pool = self.pool.get('account.move') + set_draft_ids = [] move_unlink_ids = [] # harvest ids for various actions @@ -91,21 +111,23 @@ class StatementLine(orm.Model): if st_line.state != 'confirmed': continue - for line in st_line.move_ids: + for move in st_line.move_ids: # We allow for people canceling and removing # the associated payments, which can lead to confirmed # statement lines without an associated move - move_unlink_ids.append(line.id) + move_unlink_ids.append(move.id) + # do we need to check that? + if move.state != 'draft': + raise orm.except_orm( + _('Confirmed Journal Entry'), + _('You cannot delete a confirmed Statement Line ' + 'associated to a Journal Entry that is posted.')) set_draft_ids.append(st_line.id) - if st_line.move_line.state != 'draft': - raise orm.except_orm( - _('Confirmed Journal Entry'), - _('You cannot delete a confirmed Statement Line ' - 'associated to a Journal Entry that is posted.')) move_pool.button_cancel( cr, uid, move_unlink_ids, context=context) - move_pool.unlink(cr, uid, move_unlink_ids, context=context) + + move_pool.unlink(cr, uid, move_unlink_ids, context=local_ctx) self.write( cr, uid, set_draft_ids, {'state': 'draft'}, context=context) return True