[IMP] website crawler log show query/s
[odoo/odoo.git] / addons / account_followup / report / account_followup_print.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 from collections import defaultdict
24 from openerp.osv import osv
25 from openerp.report import report_sxw
26
27
28 class report_rappel(report_sxw.rml_parse):
29     _name = "account_followup.report.rappel"
30
31     def __init__(self, cr, uid, name, context=None):
32         super(report_rappel, self).__init__(cr, uid, name, context=context)
33         self.localcontext.update({
34             'time': time,
35             'ids_to_objects': self._ids_to_objects,
36             'getLines': self._lines_get,
37             'get_text': self._get_text
38         })
39
40     def _ids_to_objects(self, ids):
41         all_lines = []
42         for line in self.pool['account_followup.stat.by.partner'].browse(self.cr, self.uid, ids):
43             if line not in all_lines:
44                 all_lines.append(line)
45         return all_lines
46
47     def _lines_get(self, stat_by_partner_line):
48         return self._lines_get_with_partner(stat_by_partner_line.partner_id, stat_by_partner_line.company_id.id)
49
50     def _lines_get_with_partner(self, partner, company_id):
51         moveline_obj = self.pool['account.move.line']
52         moveline_ids = moveline_obj.search(self.cr, self.uid, [
53                             ('partner_id', '=', partner.id),
54                             ('account_id.type', '=', 'receivable'),
55                             ('reconcile_id', '=', False),
56                             ('state', '!=', 'draft'),
57                             ('company_id', '=', company_id),
58                         ])
59
60         # lines_per_currency = {currency: [line data, ...], ...}
61         lines_per_currency = defaultdict(list)
62         for line in moveline_obj.browse(self.cr, self.uid, moveline_ids):
63             currency = line.currency_id or line.company_id.currency_id
64             line_data = {
65                 'name': line.move_id.name,
66                 'ref': line.ref,
67                 'date': line.date,
68                 'date_maturity': line.date_maturity,
69                 'balance': line.amount_currency if currency != line.company_id.currency_id else line.debit - line.credit,
70                 'blocked': line.blocked,
71                 'currency_id': currency,
72             }
73             lines_per_currency[currency].append(line_data)
74
75         return [{'line': lines} for lines in lines_per_currency.values()]
76
77     def _get_text(self, stat_line, followup_id, context=None):
78         if context is None:
79             context = {}
80         context.update({'lang': stat_line.partner_id.lang})
81         fp_obj = self.pool['account_followup.followup']
82         fp_line = fp_obj.browse(self.cr, self.uid, followup_id, context=context).followup_line
83         if not fp_line:
84             raise osv.except_osv(_('Error!'),_("The followup plan defined for the current company does not have any followup action."))
85         #the default text will be the first fp_line in the sequence with a description.
86         default_text = ''
87         li_delay = []
88         for line in fp_line:
89             if not default_text and line.description:
90                 default_text = line.description
91             li_delay.append(line.delay)
92         li_delay.sort(reverse=True)
93         a = {}
94         #look into the lines of the partner that already have a followup level, and take the description of the higher level for which it is available
95         partner_line_ids = self.pool['account.move.line'].search(self.cr, self.uid, [('partner_id','=',stat_line.partner_id.id),('reconcile_id','=',False),('company_id','=',stat_line.company_id.id),('blocked','=',False),('state','!=','draft'),('debit','!=',False),('account_id.type','=','receivable'),('followup_line_id','!=',False)])
96         partner_max_delay = 0
97         partner_max_text = ''
98         for i in self.pool['account.move.line'].browse(self.cr, self.uid, partner_line_ids, context=context):
99             if i.followup_line_id.delay > partner_max_delay and i.followup_line_id.description:
100                 partner_max_delay = i.followup_line_id.delay
101                 partner_max_text = i.followup_line_id.description
102         text = partner_max_delay and partner_max_text or default_text
103         if text:
104             text = text % {
105                 'partner_name': stat_line.partner_id.name,
106                 'date': time.strftime('%Y-%m-%d'),
107                 'company_name': stat_line.company_id.name,
108                 'user_signature': self.pool['res.users'].browse(self.cr, self.uid, self.uid, context).signature or '',
109             }
110         return text
111
112
113 class report_followup(osv.AbstractModel):
114     _name = 'report.account_followup.report_followup'
115     _inherit = 'report.abstract_report'
116     _template = 'account_followup.report_followup'
117     _wrapped_report_class = report_rappel
118
119 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: