bank_statement_populate.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 fields, models, api
  7. class account_voucher_populate_statement(models.TransientModel):
  8. _name = "account.voucher.populate.statement"
  9. _description = "Account Voucher Populate Statement"
  10. journal_id = fields.Many2one(
  11. 'account.journal',
  12. 'Metodos de Pago',
  13. required=True,
  14. domain="[('type', '=', ['cash','bank'])]"
  15. )
  16. line_ids = fields.Many2many(
  17. 'account.voucher',
  18. 'account_voucher_line_rel_',
  19. 'voucher_id', 'line_id',
  20. 'Lineas de Pago',
  21. domain="[('journal_id', '=', journal_id), ('state', '=', 'posted'), ('bank_statement_line_ids', '=', False)]"
  22. )
  23. def get_statement_line_new(self, cr, uid, voucher, statement, context=None):
  24. # Override thi method to modifiy the new statement line to create
  25. ctx = context.copy()
  26. ctx['date'] = voucher.date
  27. amount = self.pool.get('res.currency').compute(cr, uid, voucher.currency_id.id,
  28. statement.currency.id, voucher.amount, context=ctx)
  29. sign = voucher.type == 'payment' and -1.0 or 1.0
  30. type = voucher.type == 'payment' and 'supplier' or 'customer'
  31. account_id = voucher.type == 'payment' and voucher.partner_id.property_account_payable.id or voucher.partner_id.property_account_receivable.id
  32. return {
  33. 'name': voucher.reference or voucher.number or '?',
  34. 'amount': sign * amount,
  35. 'type': type,
  36. 'partner_id': voucher.partner_id.id,
  37. 'account_id': account_id,
  38. 'statement_id': statement.id,
  39. 'ref': voucher.name,
  40. 'voucher_id': voucher.id,
  41. 'journal_entry_id': voucher.move_id.id,
  42. }
  43. def populate_statement(self, cr, uid, ids, context=None):
  44. statement_obj = self.pool.get('account.bank.statement')
  45. statement_line_obj = self.pool.get('account.bank.statement.line')
  46. voucher_obj = self.pool.get('account.voucher')
  47. if context is None:
  48. context = {}
  49. data = self.read(cr, uid, ids, [], context=context)[0]
  50. voucher_ids = data['line_ids']
  51. if not voucher_ids:
  52. return {'type': 'ir.actions.act_window_close'}
  53. statement = statement_obj.browse(
  54. cr, uid, context['active_id'], context=context)
  55. for voucher in voucher_obj.browse(cr, uid, voucher_ids, context=context):
  56. statement_line_obj.create(cr, uid,
  57. self.get_statement_line_new(cr, uid, voucher, statement, context=context), context=context)
  58. voucher_obj.write(
  59. cr, uid, voucher_ids, {'is_bank_voucher': True}, context=context)
  60. return {'type': 'ir.actions.act_window_close'}
  61. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: