account.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. # For copyright and license notices, see __openerp__.py file in module root
  4. # directory
  5. ##############################################################################
  6. from openerp import models, fields, api, _
  7. from openerp.exceptions import Warning
  8. class account_bank_statement(models.Model):
  9. _inherit = 'account.bank.statement'
  10. def button_cancel(self, cr, uid, ids, context=None):
  11. context['cancel_from_statement'] = True
  12. return super(account_bank_statement, self).button_cancel(
  13. cr, uid, ids, context=context)
  14. class account_bank_statement_line(models.Model):
  15. _inherit = 'account.bank.statement.line'
  16. voucher_id = fields.Many2one('account.voucher', 'Voucher', readonly=True)
  17. def cancel(self, cr, uid, ids, context=None):
  18. # if we are canceling the statement then we dont raise the warning
  19. if context.get('cancel_from_statement', False):
  20. none_voucher_ids = self.search(
  21. cr, uid,
  22. [('id', 'in', ids), ('voucher_id', '=', False)])
  23. return super(account_bank_statement_line, self).cancel(
  24. cr, uid, none_voucher_ids, context)
  25. for line in self.browse(cr, uid, ids, context):
  26. if line.voucher_id:
  27. raise Warning(
  28. _("You can not cancel a line that has been imported from a Voucher, you should cancel the voucher first"))
  29. return super(account_bank_statement_line, self).cancel(
  30. cr, uid, ids, context)
  31. account_move_obj = self.pool.get('account.move')
  32. move_ids = []
  33. for line in self.browse(cr, uid, ids, context=context):
  34. if line.journal_entry_id:
  35. move_ids.append(line.journal_entry_id.id)
  36. for aml in line.journal_entry_id.line_id:
  37. if aml.reconcile_id:
  38. move_lines = [l.id for l in aml.reconcile_id.line_id]
  39. move_lines.remove(aml.id)
  40. self.pool.get('account.move.reconcile').unlink(
  41. cr, uid, [aml.reconcile_id.id], context=context)
  42. if len(move_lines) >= 2:
  43. self.pool.get('account.move.line').reconcile_partial(
  44. cr, uid, move_lines, 'auto', context=context)
  45. if move_ids:
  46. account_move_obj.button_cancel(cr, uid, move_ids, context=context)
  47. account_move_obj.unlink(cr, uid, move_ids, context)
  48. def unlink(self, cr, uid, ids, context=None):
  49. line_voucher_ids = self.search(
  50. cr, uid,
  51. [('id', 'in', ids), ('voucher_id', '!=', False)])
  52. # First remove journal_entry_id id in order to avoid constraint
  53. self.write(cr, uid, line_voucher_ids, {'journal_entry_id': False})
  54. return super(account_bank_statement_line, self).unlink(
  55. cr, uid, ids, context=context)
  56. class account_voucher(models.Model):
  57. _inherit = 'account.voucher'
  58. bank_statement_line_ids = fields.One2many(
  59. 'account.bank.statement.line', 'voucher_id', string="Statement Lines")
  60. @api.multi
  61. def cancel_voucher(self):
  62. # run with sudo because some users may not have access to statement line
  63. if self.sudo().bank_statement_line_ids.statement_id.state == 'confirm':
  64. raise Warning(
  65. _("You can not cancel a voucher that is linked to a confirm bank statement"))
  66. else:
  67. super(account_voucher, self).cancel_voucher()
  68. return self.sudo().bank_statement_line_ids.unlink()