[FIX] website_crm_partner_assign: Selecting 'All Grades' => exception
[odoo/odoo.git] / addons / hr_payroll / report / report_payslip_details.py
1 #-*- coding:utf-8 -*-
2
3 ##############################################################################
4 #
5 #    OpenERP, Open Source Management Solution
6 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
7 #    d$
8 #
9 #    This program is free software: you can redistribute it and/or modify
10 #    it under the terms of the GNU Affero General Public License as published by
11 #    the Free Software Foundation, either version 3 of the License, or
12 #    (at your option) any later version.
13 #
14 #    This program is distributed in the hope that it will be useful,
15 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #    GNU Affero General Public License for more details.
18 #
19 #    You should have received a copy of the GNU Affero General Public License
20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 ##############################################################################
23
24 from openerp.report import report_sxw
25 from openerp.tools import amount_to_text_en
26
27 class payslip_details_report(report_sxw.rml_parse):
28
29     def __init__(self, cr, uid, name, context):
30         super(payslip_details_report, self).__init__(cr, uid, name, context)
31         self.localcontext.update({
32             'get_details_by_rule_category': self.get_details_by_rule_category,
33             'get_lines_by_contribution_register': self.get_lines_by_contribution_register,
34         })
35
36     def get_details_by_rule_category(self, obj):
37         payslip_line = self.pool.get('hr.payslip.line')
38         rule_cate_obj = self.pool.get('hr.salary.rule.category')
39
40         def get_recursive_parent(rule_categories):
41             if not rule_categories:
42                 return []
43             if rule_categories[0].parent_id:
44                 rule_categories.insert(0, rule_categories[0].parent_id)
45                 get_recursive_parent(rule_categories)
46             return rule_categories
47
48         res = []
49         result = {}
50         ids = []
51
52         for id in range(len(obj)):
53             ids.append(obj[id].id)
54         if ids:
55             self.cr.execute('''SELECT pl.id, pl.category_id FROM hr_payslip_line as pl \
56                 LEFT JOIN hr_salary_rule_category AS rc on (pl.category_id = rc.id) \
57                 WHERE pl.id in %s \
58                 GROUP BY rc.parent_id, pl.sequence, pl.id, pl.category_id \
59                 ORDER BY pl.sequence, rc.parent_id''',(tuple(ids),))
60             for x in self.cr.fetchall():
61                 result.setdefault(x[1], [])
62                 result[x[1]].append(x[0])
63             for key, value in result.iteritems():
64                 rule_categories = rule_cate_obj.browse(self.cr, self.uid, [key])
65                 parents = get_recursive_parent(rule_categories)
66                 category_total = 0
67                 for line in payslip_line.browse(self.cr, self.uid, value):
68                     category_total += line.total
69                 level = 0
70                 for parent in parents:
71                     res.append({
72                         'rule_category': parent.name,
73                         'name': parent.name,
74                         'code': parent.code,
75                         'level': level,
76                         'total': category_total,
77                     })
78                     level += 1
79                 for line in payslip_line.browse(self.cr, self.uid, value):
80                     res.append({
81                         'rule_category': line.name,
82                         'name': line.name,
83                         'code': line.code,
84                         'total': line.total,
85                         'level': level
86                     })
87         return res
88
89     def get_lines_by_contribution_register(self, obj):
90         payslip_line = self.pool.get('hr.payslip.line')
91         result = {}
92         res = []
93
94         for id in range(len(obj)):
95             if obj[id].register_id:
96                 result.setdefault(obj[id].register_id.name, [])
97                 result[obj[id].register_id.name].append(obj[id].id)
98         for key, value in result.iteritems():
99             register_total = 0
100             for line in payslip_line.browse(self.cr, self.uid, value):
101                 register_total += line.total
102             res.append({
103                 'register_name': key,
104                 'total': register_total,
105             })
106             for line in payslip_line.browse(self.cr, self.uid, value):
107                 res.append({
108                     'name': line.name,
109                     'code': line.code,
110                     'quantity': line.quantity,
111                     'amount': line.amount,
112                     'total': line.total,
113                 })
114         return res
115
116 report_sxw.report_sxw('report.paylip.details', 'hr.payslip', 'hr_payroll/report/report_payslip_details.rml', parser=payslip_details_report)
117
118 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: