~a-camilli/openforce/6.1

« back to all changes in this revision

Viewing changes to account_financial_report_webkit/account_move_line.py

  • Committer: a.camilli at yahoo
  • Date: 2013-03-13 18:29:12 UTC
  • Revision ID: a.camilli@yahoo.it-20130313182912-nlfk385plxo4w4a1
new

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 osv import fields, osv
 
23
from tools.translate import _
 
24
 
 
25
 
 
26
class AccountMoveLine(osv.osv):
 
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
                   
 
50
    def _get_move_line_from_line_rec(self, cr, uid, ids, context=None):
 
51
        moves = []
 
52
        for reconcile in self.pool.get('account.move.reconcile').browse(cr, uid, ids, context=context):
 
53
            for move_line in reconcile.line_partial_ids:
 
54
                moves.append(move_line.id)
 
55
            for move_line in reconcile.line_id:
 
56
                moves.append(move_line.id)
 
57
        return list(set(moves))
 
58
                   
 
59
    def _get_last_rec_date(self, cursor, uid, ids, name, args, context=None):
 
60
        if not isinstance(ids, list):
 
61
            ids = [ids]
 
62
        res = {}
 
63
        for line in self.browse(cursor, uid, ids, context):
 
64
            res[line.id] = {'last_rec_date': False}
 
65
            rec = line.reconcile_id or line.reconcile_partial_id or False
 
66
            if rec:
 
67
                # we use cursor in order to gain some perfs
 
68
                cursor.execute('SELECT date from account_move_line where'
 
69
                              ' reconcile_id = %s OR reconcile_partial_id = %s'
 
70
                              ' ORDER BY date DESC LIMIT 1 ',
 
71
                              (rec.id, rec.id))
 
72
                res_set = cursor.fetchone()
 
73
                if res_set:
 
74
                    res[line.id] = {'last_rec_date': res_set[0]}
 
75
        return res
 
76
 
 
77
    _columns = {
 
78
                'last_rec_date': fields.function(_get_last_rec_date,
 
79
                     method=True,
 
80
                     string='Last reconciliation date',
 
81
                     store={'account.move.line': (lambda self, cr, uid, ids, c={}: ids, ['date'], 20),
 
82
                            'account.move.reconcile': (_get_move_line_from_line_rec, None ,20)},
 
83
                     type='date',
 
84
                     multi='all',
 
85
                     help="the date of the last reconciliation (full or partial) account move line"),
 
86
 
 
87
                }
 
88
 
 
89
AccountMoveLine()