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