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.

89 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 osv import fields, osv
  22. from tools.translate import _
  23. class AccountMoveLine(osv.osv):
  24. """Overriding Account move line in order to add last_rec_date.
  25. Last rec date is the date of the last reconciliation (full or partial) 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 invalid data
  29. cr.execute("UPDATE account_move_line as acm "
  30. " SET last_rec_date ="
  31. " (SELECT date from account_move_line"
  32. " WHERE reconcile_id = acm.reconcile_id"
  33. " AND reconcile_id IS NOT NULL"
  34. " ORDER BY date DESC LIMIT 1)"
  35. " WHERE last_rec_date is null;")
  36. cr.execute("UPDATE account_move_line as acm "
  37. " SET last_rec_date ="
  38. " (SELECT date from account_move_line"
  39. " WHERE reconcile_partial_id = acm.reconcile_partial_id"
  40. " AND reconcile_partial_id IS NOT NULL"
  41. " ORDER BY date DESC LIMIT 1)"
  42. " WHERE last_rec_date is null;")
  43. def _get_move_line_from_line_rec(self, cr, uid, ids, context=None):
  44. moves = []
  45. for reconcile in self.pool.get('account.move.reconcile').browse(cr, uid, ids, context=context):
  46. for move_line in reconcile.line_partial_ids:
  47. moves.append(move_line.id)
  48. for move_line in reconcile.line_id:
  49. moves.append(move_line.id)
  50. return list(set(moves))
  51. def _get_last_rec_date(self, cursor, uid, ids, name, args, context=None):
  52. if not isinstance(ids, list):
  53. ids = [ids]
  54. res = {}
  55. for line in self.browse(cursor, uid, ids, context):
  56. res[line.id] = {'last_rec_date': False}
  57. rec = line.reconcile_id or line.reconcile_partial_id or False
  58. if rec:
  59. # we use cursor in order to gain some perfs
  60. cursor.execute('SELECT date from account_move_line where'
  61. ' reconcile_id = %s OR reconcile_partial_id = %s'
  62. ' ORDER BY date DESC LIMIT 1 ',
  63. (rec.id, rec.id))
  64. res_set = cursor.fetchone()
  65. if res_set:
  66. res[line.id] = {'last_rec_date': res_set[0]}
  67. return res
  68. _columns = {
  69. 'last_rec_date': fields.function(_get_last_rec_date,
  70. method=True,
  71. string='Last reconciliation date',
  72. store={'account.move.line': (lambda self, cr, uid, ids, c={}: ids, ['date'], 20),
  73. 'account.move.reconcile': (_get_move_line_from_line_rec, None ,20)},
  74. type='date',
  75. multi='all',
  76. help="the date of the last reconciliation (full or partial) account move line"),
  77. }
  78. AccountMoveLine()