16d88d69572462436606defad2b224a4ef6fecf1
[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
25 from openerp import pooler
26 from openerp.report import report_sxw
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         pool = pooler.get_pool(self.cr.dbname)
42         all_lines = []
43         for line in pool.get('account_followup.stat.by.partner').browse(self.cr, self.uid, ids):
44             if line not in all_lines:
45                 all_lines.append(line)
46         return all_lines
47
48     def _lines_get(self, stat_by_partner_line):
49         return self._lines_get_with_partner(stat_by_partner_line.partner_id, stat_by_partner_line.company_id.id)
50
51     def _lines_get_with_partner(self, partner, company_id):
52         pool = pooler.get_pool(self.cr.dbname)
53         moveline_obj = pool.get('account.move.line')
54         moveline_ids = moveline_obj.search(self.cr, self.uid, [
55                             ('partner_id', '=', partner.id),
56                             ('account_id.type', '=', 'receivable'),
57                             ('reconcile_id', '=', False),
58                             ('state', '!=', 'draft'),
59                             ('company_id', '=', company_id),
60                         ])
61
62         # lines_per_currency = {currency: [line data, ...], ...}
63         lines_per_currency = defaultdict(list)
64         for line in moveline_obj.browse(self.cr, self.uid, moveline_ids):
65             currency = line.currency_id or line.company_id.currency_id
66             line_data = {
67                 'name': line.move_id.name,
68                 'ref': line.ref,
69                 'date': line.date,
70                 'date_maturity': line.date_maturity,
71                 'balance': line.amount_currency if currency != line.company_id.currency_id else line.debit - line.credit,
72                 'blocked': line.blocked,
73                 'currency_id': currency,
74             }
75             lines_per_currency[currency].append(line_data)
76
77         return [{'line': lines} for lines in lines_per_currency.values()]
78
79     def _get_text(self, stat_line, followup_id, context=None):
80         if context is None:
81             context = {}
82         context.update({'lang': stat_line.partner_id.lang})
83         fp_obj = pooler.get_pool(self.cr.dbname).get('account_followup.followup')
84         fp_line = fp_obj.browse(self.cr, self.uid, followup_id, context=context).followup_line
85         if not fp_line:
86             raise osv.except_osv(_('Error!'),_("The followup plan defined for the current company does not have any followup action."))
87         #the default text will be the first fp_line in the sequence with a description.
88         default_text = ''
89         li_delay = []
90         for line in fp_line:
91             if not default_text and line.description:
92                 default_text = line.description
93             li_delay.append(line.delay)
94         li_delay.sort(reverse=True)
95         a = {}
96         #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
97         partner_line_ids = pooler.get_pool(self.cr.dbname).get('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)])
98         partner_max_delay = 0
99         partner_max_text = ''
100         for i in pooler.get_pool(self.cr.dbname).get('account.move.line').browse(self.cr, self.uid, partner_line_ids, context=context):
101             if i.followup_line_id.delay > partner_max_delay and i.followup_line_id.description:
102                 partner_max_delay = i.followup_line_id.delay
103                 partner_max_text = i.followup_line_id.description
104         text = partner_max_delay and partner_max_text or default_text
105         if text:
106             text = text % {
107                 'partner_name': stat_line.partner_id.name,
108                 'date': time.strftime('%Y-%m-%d'),
109                 'company_name': stat_line.company_id.name,
110                 'user_signature': pooler.get_pool(self.cr.dbname).get('res.users').browse(self.cr, self.uid, self.uid, context).signature or '',
111             }
112         return text
113
114 report_sxw.report_sxw('report.account_followup.followup.print',
115         'account_followup.stat.by.partner', 'addons/account_followup/report/account_followup_print.rml',
116         parser=report_rappel)
117
118 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: