[imp] statement_cancel_line: if all lines are confirmed, confirm the statement as well

This commit is contained in:
Leonardo Pistone
2014-01-24 14:37:09 +01:00
parent 27fbae9020
commit 47e4dcb8f8
3 changed files with 50 additions and 15 deletions

View File

@@ -31,6 +31,7 @@
'account', 'account',
'account_statement_ext', 'account_statement_ext',
'account_default_draft_move', 'account_default_draft_move',
'account_statement_base_completion',
], ],
'description': """ 'description': """
Account Statement Cancel Line Account Statement Cancel Line

View File

@@ -33,12 +33,6 @@ class Statement(orm.Model):
_inherit = "account.bank.statement" _inherit = "account.bank.statement"
_columns = {
}
_defaults = {
}
def button_confirm_bank(self, cr, uid, ids, context=None): def button_confirm_bank(self, cr, uid, ids, context=None):
"""Change the state on the statement lines. Return super.""" """Change the state on the statement lines. Return super."""
st_line_obj = self.pool['account.bank.statement.line'] st_line_obj = self.pool['account.bank.statement.line']
@@ -60,3 +54,18 @@ class Statement(orm.Model):
return super(Statement, self).button_cancel( return super(Statement, self).button_cancel(
cr, uid, ids, context) 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

View File

@@ -45,7 +45,7 @@ class StatementLine(orm.Model):
} }
def confirm(self, cr, uid, ids, context=None): 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 The module account_banking does have a similar method, but at the
moment it uses a different logic (for example, it uses vouchers, where 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 # The module tries to prevent direct changes to the moves created by
# bank statements. # bank statements.
local_ctx['from_parent_object'] = True local_ctx['from_parent_object'] = True
statement_pool = self.pool.get('account.bank.statement') statement_pool = self.pool.get('account.bank.statement')
res = {}
for st_line in self.browse(cr, uid, ids, context): for st_line in self.browse(cr, uid, ids, context):
if st_line.state != 'draft': if st_line.state != 'draft':
@@ -84,10 +86,19 @@ class StatementLine(orm.Model):
self.write(cr, uid, st_line.id, { self.write(cr, uid, st_line.id, {
'state': 'confirmed' 'state': 'confirmed'
}, context) }, 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): 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 This is again similar to the method cancel in the account_banking
module. module.
@@ -102,9 +113,14 @@ class StatementLine(orm.Model):
# bank statements. # bank statements.
local_ctx['from_parent_object'] = True 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 = [] move_unlink_ids = []
# harvest ids for various actions # harvest ids for various actions
for st_line in self.browse(cr, uid, ids, context): for st_line in self.browse(cr, uid, ids, context):
@@ -122,15 +138,24 @@ class StatementLine(orm.Model):
_('Confirmed Journal Entry'), _('Confirmed Journal Entry'),
_('You cannot delete a confirmed Statement Line ' _('You cannot delete a confirmed Statement Line '
'associated to a Journal Entry that is posted.')) '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( move_pool.button_cancel(
cr, uid, move_unlink_ids, context=context) cr, uid, move_unlink_ids, context=context)
move_pool.unlink(cr, uid, move_unlink_ids, context=local_ctx) move_pool.unlink(cr, uid, move_unlink_ids, context=local_ctx)
self.write( self.write(cr, uid, st_line_ids, {
cr, uid, set_draft_ids, {'state': 'draft'}, context=context) 'state': 'draft',
return True '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): def unlink(self, cr, uid, ids, context=None):
"""Don't allow deletion of a confirmed statement line. Return super.""" """Don't allow deletion of a confirmed statement line. Return super."""