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