account_bank_voucher_import.py 3.1 KB

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