8e4e73458f220be72f2a283d50386410af7632ee
[odoo/odoo.git] / addons / hr_payroll_account / hr_payroll_account.py
1 #-*- coding:utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    d$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22 import time
23 import netsvc
24 from datetime import date, datetime, timedelta
25
26 from osv import fields, osv
27 from tools import config
28 from tools.translate import _
29
30 class hr_payslip(osv.osv):
31     '''
32     Pay Slip
33     '''
34     _inherit = 'hr.payslip'
35     _description = 'Pay Slip'
36
37     _columns = {
38         'period_id': fields.many2one('account.period', 'Force Period',states={'draft': [('readonly', False)]}, readonly=True, domain=[('state','<>','done')], help="Keep empty to use the period of the validation(Payslip) date."),
39         'journal_id': fields.many2one('account.journal', 'Expense Journal',states={'draft': [('readonly', False)]}, readonly=True, required=True),
40         'move_id': fields.many2one('account.move', 'Accounting Entry', readonly=True),
41     }
42     
43     def create(self, cr, uid, vals, context=None):
44         if context is None:
45             context = {}
46         if 'journal_id' in context:
47             vals.update({'journal_id': context.get('journal_id')})
48         return super(hr_payslip, self).create(cr, uid, vals, context=context)
49
50     def onchange_contract_id(self, cr, uid, ids, date_from, date_to, employee_id=False, contract_id=False, context=None):
51         contract_obj = self.pool.get('hr.contract')
52         res = super(hr_payslip, self).onchange_contract_id(cr, uid, ids, date_from=date_from, date_to=date_to, employee_id=employee_id, contract_id=contract_id, context=context)
53         journal_id = contract_id and contract_obj.browse(cr, uid, contract_id, context=context).journal_id.id or False
54         res['value'].update({'journal_id': journal_id})
55         return res
56
57     def cancel_sheet(self, cr, uid, ids, context=None):
58         move_pool = self.pool.get('account.move')
59         move_ids = []
60         move_to_cancel = []
61         for slip in self.browse(cr, uid, ids, context=context):
62             move_ids.append(slip.move_id.id)
63             if slip.move_id.state == 'posted':
64                 move_to_cancel.append(slip.move_id.id)
65         move_pool.button_cancel(cr, uid, move_to_cancel, context=context)
66         move_pool.unlink(cr, uid, move_ids, context=context)
67         return super(hr_payslip, self).cancel_sheet(cr, uid, ids, context=context)
68
69     def process_sheet(self, cr, uid, ids, context=None):
70         move_pool = self.pool.get('account.move')
71         period_pool = self.pool.get('account.period')
72         timenow = time.strftime('%Y-%m-%d')
73
74         for slip in self.browse(cr, uid, ids, context=context):
75             line_ids = []
76             debit_sum = 0.0
77             credit_sum = 0.0
78             if not slip.period_id:
79                 search_periods = period_pool.find(cr, uid, slip.date_to, context=context)
80                 period_id = search_periods[0]
81             else:
82                 period_id = slip.period_id.id
83
84             name = _('Payslip of %s') % (slip.employee_id.name)
85             move = {
86                 'narration': name,
87                 'date': timenow,
88                 'ref': slip.number,
89                 'journal_id': slip.journal_id.id,
90                 'period_id': period_id,
91             }
92             for line in slip.line_ids:
93                 amt = slip.credit_note and -line.total or line.total
94                 partner_id = False
95                 debit_account_id = line.salary_rule_id.account_debit.id
96                 credit_account_id = line.salary_rule_id.account_credit.id
97                 debit_line = (0, 0, {
98                     'name': line.name,
99                     'date': timenow,
100                     'partner_id': partner_id,
101                     'account_id': debit_account_id,
102                     'journal_id': slip.journal_id.id,
103                     'period_id': period_id,
104                     'debit': amt > 0.0 and amt or 0.0,
105                     'credit': amt < 0.0 and -amt or 0.0,
106                     'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
107                     'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
108                     'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
109                 })
110                 credit_line = (0, 0, {
111                     'name': line.name,
112                     'date': timenow,
113                     'partner_id': partner_id,
114                     'account_id': credit_account_id,
115                     'journal_id': slip.journal_id.id,
116                     'period_id': period_id,
117                     'debit': amt < 0.0 and -amt or 0.0,
118                     'credit': amt > 0.0 and amt or 0.0,
119                     'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
120                     'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
121                     'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
122                 })
123                 if debit_account_id:
124                     line_ids.append(debit_line)
125                     debit_sum += debit_line[2]['debit'] - debit_line[2]['credit']
126                 if credit_account_id:
127                     line_ids.append(credit_line)
128                     credit_sum += credit_line[2]['credit'] - credit_line['debit']
129
130             if debit_sum > credit_sum:
131                 adjust_credit = (0, 0, {
132                     'name': _('Adjustment Entry'),
133                     'date': timenow,
134                     'partner_id': partner_id,
135                     'account_id': slip.journal_id.default_credit_account_id.id,
136                     'journal_id': slip.journal_id.id,
137                     'period_id': period_id,
138                     'debit': 0.0,
139                     'credit': debit_sum - credit_sum,
140                 })
141                 line_ids.append(adjust_credit)
142             elif debit_sum < credit_sum:
143                 adjust_debit = (0, 0, {
144                     'name': _('Adjustment Entry'),
145                     'date': timenow,
146                     'partner_id': partner_id,
147                     'account_id': slip.journal_id.default_debit_account_id.id,
148                     'journal_id': slip.journal_id.id,
149                     'period_id': period_id,
150                     'debit': credit_sum - debit_sum,
151                     'credit': 0.0,
152                 })
153                 line_ids.append(adjust_debit)
154
155             move.update({'line_id': line_ids})
156             move_id = move_pool.create(cr, uid, move, context=context)
157             self.write(cr, uid, [slip.id], {'move_id': move_id}, context=context)
158             if slip.journal_id.entry_posted:
159                 move_pool.post(cr, uid, [move_id], context=context)
160         return super(hr_payslip, self).process_sheet(cr, uid, [slip.id], context=context)
161
162 hr_payslip()
163
164 class hr_salary_rule(osv.osv):
165     _inherit = 'hr.salary.rule'
166     _columns = {
167         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
168         'account_tax_id':fields.many2one('account.tax.code', 'Tax Code'),
169         'account_debit': fields.many2one('account.account', 'Debit Account'),
170         'account_credit': fields.many2one('account.account', 'Credit Account'),
171     }
172 hr_salary_rule()
173
174 class hr_contract(osv.osv):
175
176     _inherit = 'hr.contract'
177     _description = 'Employee Contract'
178     _columns = {
179         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
180         'journal_id': fields.many2one('account.journal', 'Salary Journal'),
181     }
182 hr_contract()
183
184 class hr_payslip_run(osv.osv):
185
186     _inherit = 'hr.payslip.run'
187     _description = 'Payslip Run'
188     _columns = {
189         'journal_id': fields.many2one('account.journal', 'Expense Journal', states={'draft': [('readonly', False)]}, readonly=True, required=True),
190     }
191
192 hr_payslip_run()
193
194 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: