Merge lp:~jgrandguillaume-c2c/account-financial-tools/account-constraints-adding into lp:~account-core-editors/account-financial-tools/6.1

Proposed by Joël Grand-Guillaume @ camptocamp
Status: Rejected
Rejected by: Guewen Baconnier @ Camptocamp
Proposed branch: lp:~jgrandguillaume-c2c/account-financial-tools/account-constraints-adding
Merge into: lp:~account-core-editors/account-financial-tools/6.1
Diff against target: 182 lines (+137/-1)
2 files modified
account_constraints/__openerp__.py (+8/-0)
account_constraints/account_constraints.py (+129/-1)
To merge this branch: bzr merge lp:~jgrandguillaume-c2c/account-financial-tools/account-constraints-adding
Reviewer Review Type Date Requested Status
Alexandre Fayolle - camptocamp resubmitting from elsewhere with fixes Disapprove
Review via email: mp+144141@code.launchpad.net

Description of the change

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.

I had to use monkey patching here cause a method hasn't the context in args :(

To post a comment you must log in.
Revision history for this message
Alexandre Fayolle - camptocamp (alexandre-fayolle-c2c) wrote :

I can't fix the various typos in this MP, so I'm resubmitting it using a branch owned by the camptocamp account.

review: Disapprove (resubmitting from elsewhere with fixes)
Revision history for this message
Alexandre Fayolle - camptocamp (alexandre-fayolle-c2c) wrote :
Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

> The new MP is at https://code.launchpad.net/~camptocamp/account-financial-
> tools/account-constraints-adding/+merge/144241

I changed the status of the proposal to 'Rejected' to reflect that.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'account_constraints/__openerp__.py'
--- account_constraints/__openerp__.py 2013-01-16 09:16:56 +0000
+++ account_constraints/__openerp__.py 2013-01-21 15:52:21 +0000
@@ -67,6 +67,14 @@
67* Forbid to remove the reconcile on opening entries. We introduce a new67* Forbid to remove the reconcile on opening entries. We introduce a new
68 boolean field to distinguish the reconciliations made by the closing process68 boolean field to distinguish the reconciliations made by the closing process
69 from others.69 from others.
70
71* Remove the possibility to modify or delete a move line related to an
72 invoice or a bank statement, no matter what the status of the move
73 (draft, validated or posted). This is usefule in standard context but
74 moreover if you're using : account_default_draft_move. This way you ensure
75 user cannot make mistake even in draft, he must pass through the
76 parent object to make his modification.
77
70 """,78 """,
71 'website': 'http://www.camptocamp.com',79 'website': 'http://www.camptocamp.com',
72 'init_xml': [],80 'init_xml': [],
7381
=== modified file 'account_constraints/account_constraints.py'
--- account_constraints/account_constraints.py 2013-01-16 09:28:38 +0000
+++ account_constraints/account_constraints.py 2013-01-21 15:52:21 +0000
@@ -20,6 +20,46 @@
2020
21from openerp.osv import fields, osv, orm21from openerp.osv import fields, osv, orm
22from openerp.tools.translate import _22from openerp.tools.translate import _
23from openerp.addons.account_invoice import account_invoice as original_invoice_model
24
25def action_cancel(self, cr, uid, ids, *args):
26 """
27 Override the whole function in order to pass the context in it as we need it
28 in the move to know if this is allowed or not. The trouble was in the original
29 method, there was context = {} that just scratch the whole context content...
30 I marked chages with # --- start changes / end changes
31 """
32 # ---- Start of changes
33 # context = {} # TODO: Use context from arguments
34 context = args[-1] if args and isinstance(args[-1], dict) else {}
35 # ---- End of changes
36 account_move_obj = self.pool.get('account.move')
37 invoices = self.read(cr, uid, ids, ['move_id', 'payment_ids'])
38 move_ids = [] # ones that we will need to remove
39 for i in invoices:
40 if i['move_id']:
41 move_ids.append(i['move_id'][0])
42 if i['payment_ids']:
43 account_move_line_obj = self.pool.get('account.move.line')
44 pay_ids = account_move_line_obj.browse(cr, uid, i['payment_ids'])
45 for move_line in pay_ids:
46 if move_line.reconcile_partial_id and move_line.reconcile_partial_id.line_partial_ids:
47 raise osv.except_osv(_('Error !'), _('You can not cancel an invoice which is partially paid! You need to unreconcile related payment entries first!'))
48
49 # First, set the invoices as cancelled and detach the move ids
50 self.write(cr, uid, ids, {'state':'cancel', 'move_id':False})
51 if move_ids:
52 # second, invalidate the move(s)
53 account_move_obj.button_cancel(cr, uid, move_ids, context=context)
54 # delete the move this invoice was pointing to
55 # Note that the corresponding move_lines and move_reconciles
56 # will be automatically deleted too
57 account_move_obj.unlink(cr, uid, move_ids, context=context)
58 self._log_event(cr, uid, ids, -1.0, 'Cancel Invoice')
59 return True
60
61# Monkey patch the original method in order to parse the context !
62original_invoice_model.action_cancel = action_cancel
2363
2464
25class AccountAccount(orm.Model):65class AccountAccount(orm.Model):
@@ -91,6 +131,53 @@
91class AccountMoveLine(orm.Model):131class AccountMoveLine(orm.Model):
92 _inherit = "account.move.line"132 _inherit = "account.move.line"
93133
134 def _check_invoice_related_move(self, cr, uid, ids, context=None):
135 for line in self.browse(cr, uid, ids, context=context):
136 if line.invoice:
137 err_msg = _('Invoice name (id): %s (%s)') % (line.invoice.name, str(line.invoice.id))
138 raise osv.except_osv(
139 _('Error!'),
140 _('You cannot do this on an entry generated by an invoice. You must '
141 'change the related invoice directly.\n%s.') % err_msg)
142 return True
143
144 def _check_statement_related_move(self, cr, uid, ids, context=None):
145 for line in self.browse(cr, uid, ids, context=context):
146 if line.statement_id:
147 err_msg = _('Bank statement name (id): %s (%s)') % (line.statement_id.name, str(line.statement_id.id))
148 raise osv.except_osv(
149 _('Error!'),
150 _('You cannot do this on an entry generated by a bank statement. '
151 'You must change the related bank statement directly.\n%s.') % err_msg)
152 return True
153
154 def unlink(self, cr, uid, ids, context=None, check=True):
155 """ Add the verification of:
156 - Is the move related to an invoice
157 - Is the move related to a bank statement
158 In that case, we forbid the move to be deleted even if draft. We should
159 never delete directly a move line related or generated by another object.
160 This is mandatory if you use the all move in draft (module: account_default_draft_move)
161 """
162 if not context.get('from_parent_object', False):
163 self._check_invoice_related_move(cr, uid, ids, context=context)
164 self._check_statement_related_move(cr, uid, ids, context=context)
165 return super(AccountMoveLine, self).unlink(cr, uid, ids, context=context, check=check)
166
167 def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
168 """ Add the verification of:
169 - Is the move related to an invoice
170 - Is the move related to a bank statement
171 In that case, we forbid the move to be modified even if draft. We should
172 never update directly a move line related or generated by another object.
173 This is mandatory if you use the all move in draft (module: account_default_draft_move)
174 """
175 if not context.get('from_parent_object', False):
176 self._check_invoice_related_move(cr, uid, ids, context=context)
177 self._check_statement_related_move(cr, uid, ids, context=context)
178 return super(AccountMoveLine, self).write(cr, uid, ids, vals,
179 context=context, check=check, update_check=update_check)
180
94 def _remove_move_reconcile(self, cr, uid, move_ids=None,181 def _remove_move_reconcile(self, cr, uid, move_ids=None,
95 opening_reconciliation=False, context=None):182 opening_reconciliation=False, context=None):
96 """Redefine the whole method to add the kwarg opening_reconciliation."""183 """Redefine the whole method to add the kwarg opening_reconciliation."""
@@ -238,11 +325,27 @@
238class Invoice(orm.Model):325class Invoice(orm.Model):
239 _inherit = 'account.invoice'326 _inherit = 'account.invoice'
240327
328 def action_move_create(self, cr, uid, ids, context=None):
329 """Override the method to add the key 'from_parent_object' in
330 the context."""
331 if context is None:
332 context = {}
333 context['from_parent_object'] = True
334 return super(Invoice,self).action_move_create(cr, uid, ids, context=context)
335
241 # Forbid to cancel an invoice if the related move lines have already been336 # Forbid to cancel an invoice if the related move lines have already been
242 # used in a payment order. The risk is that importing the payment line337 # used in a payment order. The risk is that importing the payment line
243 # in the bank statement will result in a crash cause no more move will338 # in the bank statement will result in a crash cause no more move will
244 # be found in the payment line339 # be found in the payment line + Override the method to add the key 'from_parent_object' in
340 # the context.
245 def action_cancel(self, cr, uid, ids, *args):341 def action_cancel(self, cr, uid, ids, *args):
342 """
343 As the signature of the original function isn't good, we
344 append context at the end of *args.
345 """
346 # Hack to take the last args...
347 context = args[-1] if args and isinstance(args[-1], dict) else {}
348 context['from_parent_object'] = True
246 payment_line_obj = self.pool.get('payment.line')349 payment_line_obj = self.pool.get('payment.line')
247 for inv in self.browse(cr, uid, ids, *args):350 for inv in self.browse(cr, uid, ids, *args):
248 pl_line_ids = False351 pl_line_ids = False
@@ -259,4 +362,29 @@
259 "imported in a payment order. Remove it from the "362 "imported in a payment order. Remove it from the "
260 "following payment order : %s." % payment_order_name)363 "following payment order : %s." % payment_order_name)
261 )364 )
365 args = list(args)
366 args.append(context)
262 return super(Invoice, self).action_cancel(cr, uid, ids, *args)367 return super(Invoice, self).action_cancel(cr, uid, ids, *args)
368
369
370class AccountBankStatement(orm.Model):
371 _inherit = "account.bank.statement"
372
373 def button_cancel(self, cr, uid, ids, context=None):
374 """Override the method to add the key 'from_parent_object' in
375 the context. This is to allow to delete move line related to bank statement
376 through the buton cancel."""
377 if context is None:
378 context = {}
379 context['from_parent_object'] = True
380 return super(AccountBankStatement, self).button_cancel(cr, uid, ids, context=context)
381
382 def create_move_from_st_line(self, cr, uid, st_line_id, company_currency_id, st_line_number, context=None):
383 """Add the from_parent_object key in context in order to be able to post the move."""
384 if context is None:
385 context = {}
386 context['from_parent_object'] = True
387 return super(AccountBankStatement, self).create_move_from_st_line(cr, uid,
388 st_line_id, company_currency_id, st_line_number, context=context)
389
390

Subscribers

People subscribed via source and target branches