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