account_bank_voucher_import.py 3.1 KB

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