diff --git a/account_statement_cancel_line/__openerp__.py b/account_statement_cancel_line/__openerp__.py index a64d0b6f..84ed975d 100644 --- a/account_statement_cancel_line/__openerp__.py +++ b/account_statement_cancel_line/__openerp__.py @@ -31,6 +31,7 @@ 'account', 'account_statement_ext', 'account_default_draft_move', + 'account_statement_base_completion', ], 'description': """ Account Statement Cancel Line diff --git a/account_statement_cancel_line/statement.py b/account_statement_cancel_line/statement.py index f87fbd64..c01daf3b 100644 --- a/account_statement_cancel_line/statement.py +++ b/account_statement_cancel_line/statement.py @@ -33,12 +33,6 @@ class Statement(orm.Model): _inherit = "account.bank.statement" - _columns = { - } - - _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'] @@ -60,3 +54,18 @@ class Statement(orm.Model): return super(Statement, self).button_cancel( cr, uid, ids, context) + + def confirm_statement_from_lines(self, cr, uid, ids, context=None): + """If all lines are confirmed, so is the whole statement. + + Return True if we changed anything. + + """ + need_to_update_view = False + for statement in self.browse(cr, uid, ids, context=context): + if all(line.state == 'confirmed' for line in statement.line_ids): + self.write(cr, uid, [statement.id], { + 'state': 'confirm' + }, context=context) + need_to_update_view = True + return need_to_update_view diff --git a/account_statement_cancel_line/statement_line.py b/account_statement_cancel_line/statement_line.py index 539b46f5..7850f8bf 100644 --- a/account_statement_cancel_line/statement_line.py +++ b/account_statement_cancel_line/statement_line.py @@ -45,7 +45,7 @@ class StatementLine(orm.Model): } def confirm(self, cr, uid, ids, context=None): - """Confirm just one statement line, return true. + """Confirm just one statement line, return action. The module account_banking does have a similar method, but at the moment it uses a different logic (for example, it uses vouchers, where @@ -60,7 +60,9 @@ class StatementLine(orm.Model): # 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') + res = {} for st_line in self.browse(cr, uid, ids, context): if st_line.state != 'draft': @@ -84,10 +86,19 @@ class StatementLine(orm.Model): self.write(cr, uid, st_line.id, { 'state': 'confirmed' }, context) - return True + if statement_pool.confirm_statement_from_lines(cr, uid, [st.id], + context=context): + # to see that the state of the statement has changed, we need + # to update the whole view. Do that only if necessary. + res = { + 'type': 'ir.actions.client', + 'tag': 'reload', + } + + return res def cancel(self, cr, uid, ids, context=None): - """Cancel one statement line, return True. + """Cancel one statement line, return action. This is again similar to the method cancel in the account_banking module. @@ -102,9 +113,14 @@ class StatementLine(orm.Model): # bank statements. local_ctx['from_parent_object'] = True - move_pool = self.pool.get('account.move') + move_pool = self.pool['account.move'] + statement_pool = self.pool['account.bank.statement'] + + st_line_ids = [] + + # to avoid duplicates if all lines come from the same statement + statement_ids = set() - set_draft_ids = [] move_unlink_ids = [] # harvest ids for various actions for st_line in self.browse(cr, uid, ids, context): @@ -122,15 +138,24 @@ class StatementLine(orm.Model): _('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) + st_line_ids.append(st_line.id) + statement_ids.add(st_line.statement_id.id) move_pool.button_cancel( 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 + self.write(cr, uid, st_line_ids, { + 'state': 'draft', + 'already_completed': False + }, context=context) + # if we cancel one or more lines, the statement goes back to draft, too + statement_pool.write( + cr, uid, list(statement_ids), {'state': 'draft'}, context=context) + return { + 'type': 'ir.actions.client', + 'tag': 'reload', + } def unlink(self, cr, uid, ids, context=None): """Don't allow deletion of a confirmed statement line. Return super."""