[IMP] account: unique description for wizard model
[odoo/odoo.git] / addons / account / wizard / account_reconcile_partner_process.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (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 Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import time
23
24 from osv import osv, fields
25
26 class account_partner_reconcile_process(osv.osv_memory):
27     _name = 'account.partner.reconcile.process'
28     _description = 'Reconcilation Process partner by partner'
29
30     def _get_to_reconcile(self, cr, uid, context=None):
31         cr.execute("""
32                   SELECT p_id FROM (SELECT l.partner_id as p_id, SUM(l.debit) AS debit, SUM(l.credit) AS credit
33                                     FROM account_move_line AS l LEFT JOIN account_account a ON (l.account_id = a.id)
34                                                                 LEFT JOIN res_partner p ON (p.id = l.partner_id)
35                                     WHERE a.reconcile = 't'
36                                     AND l.reconcile_id IS NULL
37                                     AND  (%s >  to_char(p.last_reconciliation_date, 'YYYY-MM-DD') OR  p.last_reconciliation_date IS NULL )
38                                     AND  l.state <> 'draft'
39                                     GROUP BY l.partner_id) AS tmp
40                               WHERE debit > 0
41                               AND credit > 0
42                 """,(time.strftime('%Y-%m-%d'),)
43         )
44         return len(map(lambda x: x[0], cr.fetchall())) - 1
45
46     def _get_today_reconciled(self, cr, uid, context=None):
47         cr.execute(
48                 "SELECT l.partner_id " \
49                 "FROM account_move_line AS l LEFT JOIN res_partner p ON (p.id = l.partner_id) " \
50                 "WHERE l.reconcile_id IS NULL " \
51                 "AND %s =  to_char(p.last_reconciliation_date, 'YYYY-MM-DD') " \
52                 "AND l.state <> 'draft' " \
53                 "GROUP BY l.partner_id ",(time.strftime('%Y-%m-%d'),)
54         )
55         return len(map(lambda x: x[0], cr.fetchall())) + 1
56
57     def _get_partner(self, cr, uid, context=None):
58         move_line_obj = self.pool.get('account.move.line')
59
60         partner = move_line_obj.get_next_partner_only(cr, uid, offset=1, context=context)
61         if not partner:
62             return False
63         return partner[0]
64
65     def data_get(self, cr, uid, to_reconcile, today_reconciled, context=None):
66         return {'progress': (100 / (float(to_reconcile + today_reconciled) or 1.0)) * today_reconciled}
67
68     def default_get(self, cr, uid, fields, context=None):
69         res = super(account_partner_reconcile_process, self).default_get(cr, uid, fields, context=context)
70         if 'to_reconcile' in res and 'today_reconciled' in res:
71             data = self.data_get(cr, uid, res['to_reconcile'], res['today_reconciled'], context)
72             res.update(data)
73         return res
74
75     def next_partner(self, cr, uid, ids, context=None):
76         if context is None:
77             context = {}
78         move_line_obj = self.pool.get('account.move.line')
79         res_partner_obj = self.pool.get('res.partner')
80
81         partner_id = move_line_obj.read(cr, uid, context['active_id'], ['partner_id'])['partner_id']
82         if partner_id:
83             res_partner_obj.write(cr, uid, partner_id[0], {'last_reconciliation_date': time.strftime('%Y-%m-%d')}, context)
84         #TODO: we have to find a way to update the context of the current tab (we could open a new tab with the context but it's not really handy)
85         #TODO: remove that comments when the client side dev is done
86         return {'type': 'ir.actions.act_window_close'}
87
88     _columns = {
89         'to_reconcile': fields.float('Remaining Partners', readonly=True, help='This is the remaining partners for who you should check if there is something to reconcile or not. This figure already count the current partner as reconciled.'),
90         'today_reconciled': fields.float('Partners Reconciled Today', readonly=True, help='This figure depicts the total number of partners that have gone throught the reconciliation process today. The current partner is counted as already processed.'),
91         'progress': fields.float('Progress', readonly=True, help='Shows you the progress made today on the reconciliation process. Given by \nPartners Reconciled Today \ (Remaining Partners + Partners Reconciled Today)'),
92         'next_partner_id': fields.many2one('res.partner', 'Next Partner to Reconcile', readonly=True, help='This field shows you the next partner that will be automatically chosen by the system to go through the reconciliation process, based on the latest day it have been reconciled.'), # TODO: remove the readonly=True when teh client side will allow to update the context of existing tab, so that the user can change this value if he doesn't want to follow openerp proposal
93     }
94
95     _defaults = {
96         'to_reconcile': _get_to_reconcile,
97         'today_reconciled': _get_today_reconciled,
98         'next_partner_id': _get_partner,
99     }
100
101 account_partner_reconcile_process()
102
103 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: