You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
4.0 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Nicolas Bessi.
  5. # Copyright Camptocamp SA 2011
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import fields, orm
  22. class AccountMoveLine(orm.Model):
  23. """Overriding Account move line in order to add last_rec_date.
  24. Last rec date is the date of the last reconciliation (full or partial)
  25. account move line"""
  26. _inherit = 'account.move.line'
  27. def init(self, cr):
  28. # We do not want to catch error as if sql is not run it will give
  29. # invalid data
  30. cr.execute("UPDATE account_move_line as acm "
  31. " SET last_rec_date ="
  32. " (SELECT date from account_move_line"
  33. " WHERE reconcile_id = acm.reconcile_id"
  34. " AND reconcile_id IS NOT NULL"
  35. " ORDER BY date DESC LIMIT 1)"
  36. " WHERE last_rec_date is null;")
  37. cr.execute("UPDATE account_move_line as acm "
  38. " SET last_rec_date ="
  39. " (SELECT date from account_move_line"
  40. " WHERE reconcile_partial_id"
  41. " = acm.reconcile_partial_id"
  42. " AND reconcile_partial_id IS NOT NULL"
  43. " ORDER BY date DESC LIMIT 1)"
  44. " WHERE last_rec_date is null;")
  45. def _get_move_line_from_line_rec(self, cr, uid, ids, context=None):
  46. moves = []
  47. for reconcile in self.pool['account.move.reconcile'].browse(
  48. cr, uid, ids, context=context):
  49. for move_line in reconcile.line_partial_ids:
  50. moves.append(move_line.id)
  51. for move_line in reconcile.line_id:
  52. moves.append(move_line.id)
  53. return list(set(moves))
  54. def _get_last_rec_date(self, cursor, uid, ids, name, args, context=None):
  55. if not isinstance(ids, list):
  56. ids = [ids]
  57. res = {}
  58. for line in self.browse(cursor, uid, ids, context):
  59. res[line.id] = {'last_rec_date': False}
  60. rec = line.reconcile_id or line.reconcile_partial_id or False
  61. if rec:
  62. # we use cursor in order to gain some perfs
  63. cursor.execute('SELECT date from account_move_line'
  64. ' WHERE reconcile_id = %s'
  65. ' OR reconcile_partial_id = %s'
  66. ' ORDER BY date DESC LIMIT 1 ',
  67. (rec.id, rec.id))
  68. res_set = cursor.fetchone()
  69. if res_set:
  70. res[line.id] = {'last_rec_date': res_set[0]}
  71. return res
  72. _columns = {
  73. 'last_rec_date': fields.function(
  74. _get_last_rec_date,
  75. method=True,
  76. string='Last reconciliation date',
  77. store={'account.move.line': (lambda self, cr, uid, ids, c={}: ids,
  78. ['date'], 20),
  79. 'account.move.reconcile': (_get_move_line_from_line_rec,
  80. None, 20)},
  81. type='date',
  82. multi='all',
  83. help="the date of the last reconciliation (full or partial) \
  84. account move line"),
  85. }