[MERGE] Sync with trunk, until revision 8927
[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 from datetime import date, datetime, timedelta
24
25 from openerp.osv import fields, osv
26 from openerp.tools import config
27 from openerp.tools.translate import _
28
29 class hr_payslip(osv.osv):
30     '''
31     Pay Slip
32     '''
33     _inherit = 'hr.payslip'
34     _description = 'Pay Slip'
35
36     _columns = {
37         '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."),
38         'journal_id': fields.many2one('account.journal', 'Salary Journal',states={'draft': [('readonly', False)]}, readonly=True, required=True),
39         'move_id': fields.many2one('account.move', 'Accounting Entry', readonly=True),
40     }
41
42     def _get_default_journal(self, cr, uid, context=None):
43         model_data = self.pool.get('ir.model.data')
44         res = model_data.search(cr, uid, [('name', '=', 'expenses_journal')])
45         if res:
46             return model_data.browse(cr, uid, res[0]).res_id
47         return False
48
49     _defaults = {
50         'journal_id': _get_default_journal,
51     }
52
53     def copy(self, cr, uid, id, default=None, context=None):
54         if default is None:
55             default = {}
56         default['move_id'] = False
57         return super(hr_payslip, self).copy(cr, uid, id, default, context=context)
58
59     def create(self, cr, uid, vals, context=None):
60         if context is None:
61             context = {}
62         if 'journal_id' in context:
63             vals.update({'journal_id': context.get('journal_id')})
64         return super(hr_payslip, self).create(cr, uid, vals, context=context)
65
66     def onchange_contract_id(self, cr, uid, ids, date_from, date_to, employee_id=False, contract_id=False, context=None):
67         contract_obj = self.pool.get('hr.contract')
68         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)
69         journal_id = contract_id and contract_obj.browse(cr, uid, contract_id, context=context).journal_id.id or False
70         res['value'].update({'journal_id': journal_id})
71         return res
72
73     def cancel_sheet(self, cr, uid, ids, context=None):
74         move_pool = self.pool.get('account.move')
75         move_ids = []
76         move_to_cancel = []
77         for slip in self.browse(cr, uid, ids, context=context):
78             if slip.move_id:
79                 move_ids.append(slip.move_id.id)
80                 if slip.move_id.state == 'posted':
81                     move_to_cancel.append(slip.move_id.id)
82         move_pool.button_cancel(cr, uid, move_to_cancel, context=context)
83         move_pool.unlink(cr, uid, move_ids, context=context)
84         return super(hr_payslip, self).cancel_sheet(cr, uid, ids, context=context)
85
86     def process_sheet(self, cr, uid, ids, context=None):
87         move_pool = self.pool.get('account.move')
88         period_pool = self.pool.get('account.period')
89         timenow = time.strftime('%Y-%m-%d')
90
91         for slip in self.browse(cr, uid, ids, context=context):
92             line_ids = []
93             debit_sum = 0.0
94             credit_sum = 0.0
95             if not slip.period_id:
96                 search_periods = period_pool.find(cr, uid, slip.date_to, context=context)
97                 period_id = search_periods[0]
98             else:
99                 period_id = slip.period_id.id
100
101             default_partner_id = slip.employee_id.address_home_id.id
102             name = _('Payslip of %s') % (slip.employee_id.name)
103             move = {
104                 'narration': name,
105                 'date': timenow,
106                 'ref': slip.number,
107                 'journal_id': slip.journal_id.id,
108                 'period_id': period_id,
109             }
110             for line in slip.details_by_salary_rule_category:
111                 amt = slip.credit_note and -line.total or line.total
112                 partner_id = line.salary_rule_id.register_id.partner_id and line.salary_rule_id.register_id.partner_id.id or default_partner_id
113                 debit_account_id = line.salary_rule_id.account_debit.id
114                 credit_account_id = line.salary_rule_id.account_credit.id
115
116                 if debit_account_id:
117
118                     debit_line = (0, 0, {
119                     'name': line.name,
120                     'date': timenow,
121                     'partner_id': (line.salary_rule_id.register_id.partner_id or line.salary_rule_id.account_debit.type in ('receivable', 'payable')) and partner_id or False,
122                     'account_id': debit_account_id,
123                     'journal_id': slip.journal_id.id,
124                     'period_id': period_id,
125                     'debit': amt > 0.0 and amt or 0.0,
126                     'credit': amt < 0.0 and -amt or 0.0,
127                     'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
128                     'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
129                     'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
130                 })
131                     line_ids.append(debit_line)
132                     debit_sum += debit_line[2]['debit'] - debit_line[2]['credit']
133
134                 if credit_account_id:
135
136                     credit_line = (0, 0, {
137                     'name': line.name,
138                     'date': timenow,
139                     'partner_id': (line.salary_rule_id.register_id.partner_id or line.salary_rule_id.account_credit.type in ('receivable', 'payable')) and partner_id or False,
140                     'account_id': credit_account_id,
141                     'journal_id': slip.journal_id.id,
142                     'period_id': period_id,
143                     'debit': amt < 0.0 and -amt or 0.0,
144                     'credit': amt > 0.0 and amt or 0.0,
145                     'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
146                     'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
147                     'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
148                 })
149                     line_ids.append(credit_line)
150                     credit_sum += credit_line[2]['credit'] - credit_line[2]['debit']
151
152             if debit_sum > credit_sum:
153                 acc_id = slip.journal_id.default_credit_account_id.id
154                 if not acc_id:
155                     raise osv.except_osv(_('Configuration Error!'),_('The Expense Journal "%s" has not properly configured the Credit Account!')%(slip.journal_id.name))
156                 adjust_credit = (0, 0, {
157                     'name': _('Adjustment Entry'),
158                     'date': timenow,
159                     'partner_id': False,
160                     'account_id': acc_id,
161                     'journal_id': slip.journal_id.id,
162                     'period_id': period_id,
163                     'debit': 0.0,
164                     'credit': debit_sum - credit_sum,
165                 })
166                 line_ids.append(adjust_credit)
167
168             elif debit_sum < credit_sum:
169                 acc_id = slip.journal_id.default_debit_account_id.id
170                 if not acc_id:
171                     raise osv.except_osv(_('Configuration Error!'),_('The Expense Journal "%s" has not properly configured the Debit Account!')%(slip.journal_id.name))
172                 adjust_debit = (0, 0, {
173                     'name': _('Adjustment Entry'),
174                     'date': timenow,
175                     'partner_id': False,
176                     'account_id': acc_id,
177                     'journal_id': slip.journal_id.id,
178                     'period_id': period_id,
179                     'debit': credit_sum - debit_sum,
180                     'credit': 0.0,
181                 })
182                 line_ids.append(adjust_debit)
183             move.update({'line_id': line_ids})
184             move_id = move_pool.create(cr, uid, move, context=context)
185             self.write(cr, uid, [slip.id], {'move_id': move_id, 'period_id' : period_id}, context=context)
186             if slip.journal_id.entry_posted:
187                 move_pool.post(cr, uid, [move_id], context=context)
188         return super(hr_payslip, self).process_sheet(cr, uid, [slip.id], context=context)
189
190
191 class hr_salary_rule(osv.osv):
192     _inherit = 'hr.salary.rule'
193     _columns = {
194         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
195         'account_tax_id':fields.many2one('account.tax.code', 'Tax Code'),
196         'account_debit': fields.many2one('account.account', 'Debit Account'),
197         'account_credit': fields.many2one('account.account', 'Credit Account'),
198     }
199
200 class hr_contract(osv.osv):
201
202     _inherit = 'hr.contract'
203     _description = 'Employee Contract'
204     _columns = {
205         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
206         'journal_id': fields.many2one('account.journal', 'Salary Journal'),
207     }
208
209 class hr_payslip_run(osv.osv):
210
211     _inherit = 'hr.payslip.run'
212     _description = 'Payslip Run'
213     _columns = {
214         'journal_id': fields.many2one('account.journal', 'Salary Journal', states={'draft': [('readonly', False)]}, readonly=True, required=True),
215     }
216
217     def _get_default_journal(self, cr, uid, context=None):
218         model_data = self.pool.get('ir.model.data')
219         res = model_data.search(cr, uid, [('name', '=', 'expenses_journal')])
220         if res:
221             return model_data.browse(cr, uid, res[0]).res_id
222         return False
223
224     _defaults = {
225         'journal_id': _get_default_journal,
226     }
227
228
229 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: