[FIX] hr_payroll_account: misc fixes
[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
31 class contrib_register(osv.osv):
32     _inherit = 'hr.contribution.register'
33     _description = 'Contribution Register'
34
35     _columns = {
36         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
37     }
38 contrib_register()
39
40 #class account_move_line(osv.osv):
41 #
42 #    _inherit = 'account.move.line'
43 #    _columns = {
44 #        'slip_id': fields.many2one('hr.payslip', 'Payslip'),
45 #    }
46 #account_move_line()
47
48 class hr_payslip(osv.osv):
49     '''
50     Pay Slip
51     '''
52     _inherit = 'hr.payslip'
53     _description = 'Pay Slip'
54
55     _columns = {
56         '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."),
57         'journal_id': fields.many2one('account.journal', 'Expense Journal',states={'draft': [('readonly', False)]}, readonly=True, required=True),
58         #TOCHECK: should we have a link to account.move or account.move.line?
59         'move_id': fields.many2one('account.move', 'Accounting Entry', readonly=True),
60         #'move_line_ids':fields.one2many('account.move.line', 'slip_id', 'Accounting Lines', readonly=True),
61         #'account_move_ids': fields.many2many('account.move', 'payslip_move_rel', 'slip_id', 'move_id', 'Accounting Entries', readonly=True),
62     }
63
64     def onchange_contract_id(self, cr, uid, ids, date_from, date_to, employee_id=False, contract_id=False, context=None):
65         contract_obj = self.pool.get('hr.contract')
66         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)
67         journal_id = contract_id and contract_obj.browse(cr, uid, contract_id, context=context).journal_id.id or False
68         res['value'].update({'journal_id': journal_id})
69         return res
70
71     def cancel_sheet(self, cr, uid, ids, context=None):
72         move_pool = self.pool.get('account.move')
73         move_ids = []
74         move_to_cancel = []
75         for slip in self.browse(cr, uid, ids, context=context):
76             move_ids.append(slip.move_id.id)
77             if slip.move_id.state == 'posted':
78                 move_to_cancel.append(slip.move_id.id)
79         move_pool.button_cancel(cr, uid, move_to_cancel, context=context)
80         move_pool.unlink(cr, uid, move_ids, context=context)
81         return super(hr_payslip, self).cancel_sheet(cr, uid, ids, context=context)
82
83 #TODO: to correct
84     def process_sheet(self, cr, uid, ids, context=None):
85         move_pool = self.pool.get('account.move')
86         movel_pool = self.pool.get('account.move.line')
87         invoice_pool = self.pool.get('account.invoice')
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.search(cr, uid, [('date_start','<=',slip.date_from),('date_stop','>=',slip.date_to)], context=context)
97                 if not search_periods:
98                     raise osv.except_osv(_('Warning !'), _('Period is not defined for slip date %s') % slip.date)
99                 period_id = search_periods[0]
100             else:
101                 period_id = slip.period_id.id
102
103             name = _('Payslip of %s') % (slip.employee_id.name)
104             move = {
105                 'journal_id': slip.journal_id.id,
106                 'period_id': period_id,
107                 'date': timenow,
108                 'ref':slip.number,
109                 'narration': name
110             }
111             for line in slip.line_ids:
112                 amt = slip.credit_note and -line.total or line.total
113                 partner_id = False
114                 name = line.name
115                 debit_account_id = line.salary_rule_id.account_debit.id
116                 credit_account_id = line.salary_rule_id.account_credit.id
117                 debit_line = (0,0,{
118                     'name': line.name,
119                     'account_id': debit_account_id,
120                     'debit': amt > 0.0 and amt or 0.0,
121                     'credit': amt < 0.0 and -amt or 0.0,
122                     'date': timenow,
123                     'journal_id': slip.journal_id.id,
124                     'period_id': period_id,
125                 })
126                 credit_line = (0,0,{
127                     'date': timenow,
128                     'journal_id': slip.journal_id.id,
129                     'period_id': period_id,
130                     'name': name,
131                     'partner_id': partner_id,
132                     'account_id': credit_account_id,
133                     'debit': amt < 0.0 and -amt or 0.0,
134                     'credit': amt > 0.0 and amt or 0.0,
135                 })
136                 if debit_account_id:
137                     line_ids.append(debit_line)
138                     debit_sum += debit_line[2]['debit']
139                 if credit_account_id:
140                     line_ids.append(credit_line)
141                     credit_sum += credit_line[2]['credit']
142             
143             if debit_sum > credit_sum:
144                 adjust_credit = (0,0,{
145                     'date': timenow,
146                     'journal_id': slip.journal_id.id,
147                     'period_id': period_id,
148                     'name': _('Adjustment Entry'),
149                     'partner_id': partner_id,
150                     'account_id': slip.journal_id.default_credit_account_id.id,
151                     'debit': 0.0,
152                     'credit': debit_sum - credit_sum,
153                 })
154                 line_ids.append(adjust_credit)
155             elif debit_sum < credit_sum:
156                 adjust_debit = (0,0,{
157                     'date': timenow,
158                     'journal_id': slip.journal_id.id,
159                     'period_id': period_id,
160                     'name': _('Adjustment Entry'),
161                     'partner_id': partner_id,
162                     'account_id': slip.journal_id.default_debit_account_id.id,
163                     'debit': credit_sum - debit_sum,
164                     'credit': 0.0
165                 })
166                 line_ids.append(adjust_debit)
167                 
168             move.update({'line_id': line_ids})
169             move_id = move_pool.create(cr, uid, move, context=context)
170             self.write(cr, uid, [slip.id], {'move_id': move_id}, context=context)
171         return super(hr_payslip, self).process_sheet(cr, uid, [slip.id], context=context)
172
173 #TODO: to clean: the verofying doesn't do anything in the accounting..
174 #    def verify_sheet(self, cr, uid, ids, context=None):
175 #        move_pool = self.pool.get('account.move')
176 #        movel_pool = self.pool.get('account.move.line')
177 #        exp_pool = self.pool.get('hr.expense.expense')
178 #        fiscalyear_pool = self.pool.get('account.fiscalyear')
179 #        period_pool = self.pool.get('account.period')
180 #        property_pool = self.pool.get('ir.property')
181 #        payslip_pool = self.pool.get('hr.payslip.line')
182 #
183 #        for slip in self.browse(cr, uid, ids, context=context):
184 #            for line in slip.line_ids:
185 #                if line.category_id.name == 'Basic':
186 #                    basic_amt = line.total
187 #            if not slip.journal_id:
188 #                # Call super method to verify sheet if journal_id is not specified.
189 #                super(hr_payslip, self).verify_sheet(cr, uid, [slip.id], context=context)
190 #                continue
191 #            total_deduct = 0.0
192 #
193 #            line_ids = []
194 #            move_ids = []
195 #            partner = False
196 #            partner_id = False
197 #
198 #            if not slip.employee_id.bank_account_id:
199 #                raise osv.except_osv(_('Integrity Error !'), _('Please define bank account for %s !') % (slip.employee_id.name))
200 #
201 #            if not slip.employee_id.bank_account_id.partner_id:
202 #                raise osv.except_osv(_('Integrity Error !'), _('Please define partner in bank account for %s !') % (slip.employee_id.name))
203 #
204 #            partner = slip.employee_id.bank_account_id.partner_id
205 #            partner_id = slip.employee_id.bank_account_id.partner_id.id
206 #
207 #            period_id = False
208 #
209 #            if slip.period_id:
210 #                period_id = slip.period_id.id
211 #            else:
212 #                fiscal_year_ids = fiscalyear_pool.search(cr, uid, [], context=context)
213 #                if not fiscal_year_ids:
214 #                    raise osv.except_osv(_('Warning !'), _('Please define fiscal year for perticular contract'))
215 #                fiscal_year_objs = fiscalyear_pool.read(cr, uid, fiscal_year_ids, ['date_start','date_stop'], context=context)
216 #                year_exist = False
217 #                for fiscal_year in fiscal_year_objs:
218 #                    if ((fiscal_year['date_start'] <= slip.date_from) and (fiscal_year['date_stop'] >= slip.date_to)):
219 #                        year_exist = True
220 #                if not year_exist:
221 #                    raise osv.except_osv(_('Warning !'), _('Fiscal Year is not defined for slip date %s') % slip.date)
222 #                search_periods = period_pool.search(cr,uid,[('date_start','=',slip.date_from),('date_stop','=',slip.date_to)], context=context)
223 #                if not search_periods:
224 #                    raise osv.except_osv(_('Warning !'), _('Period is not defined for slip date %s') % slip.date)
225 #                period_id = search_periods[0]
226 #
227 #            move = {
228 #                'journal_id': slip.journal_id.id,
229 #                'period_id': period_id,
230 #                'date': slip.date_from,
231 #                'ref':slip.number,
232 #                'narration': slip.name
233 #            }
234 #            move_id = move_pool.create(cr, uid, move, context=context)
235 #            move_ids += [move_id]
236 #            self.create_voucher(cr, uid, [slip.id], slip.name, move_id)
237 #
238 #            if not slip.employee_id.salary_account.id:
239 #                raise osv.except_osv(_('Warning !'), _('Please define Salary Account for %s.') % slip.employee_id.name)
240 #
241 #            line = {
242 #                'move_id':move_id,
243 #                'name': "By Basic Salary / " + slip.employee_id.name,
244 #                'date': slip.date_from,
245 #                'account_id': slip.employee_id.salary_account.id,
246 #                'debit': basic_amt,
247 #                'credit': 0.0,
248 #                'journal_id': slip.journal_id.id,
249 #                'period_id': period_id,
250 #                'analytic_account_id': False,
251 #                'ref':slip.number
252 #            }
253 #            #Setting Analysis Account for Basic Salary
254 #            if slip.employee_id.analytic_account:
255 #                line['analytic_account_id'] = slip.employee_id.analytic_account.id
256 #
257 #            move_line_id = movel_pool.create(cr, uid, line, context=context)
258 #            line_ids += [move_line_id]
259 #
260 #            if not slip.employee_id.employee_account.id:
261 #                raise osv.except_osv(_('Warning !'), _('Please define Employee Payable Account for %s.') % slip.employee_id.name)
262 #
263 #            line = {
264 #                'move_id':move_id,
265 #                'name': "To Basic Payble Salary / " + slip.employee_id.name,
266 #                'partner_id': partner_id,
267 #                'date': slip.date_from,
268 #                'account_id': slip.employee_id.employee_account.id,
269 #                'debit': 0.0,
270 #                'credit': basic_amt,
271 #                'journal_id': slip.journal_id.id,
272 #                'period_id': period_id,
273 #                'ref':slip.number
274 #            }
275 #            line_ids += [movel_pool.create(cr, uid, line, context=context)]
276 #            for line in slip.line_ids:
277 #                if line.name == 'Net' or line.name == 'Gross' or line.name == 'Basic':
278 #                    continue
279 #                name = "[%s] - %s / %s" % (line.code, line.name, slip.employee_id.name)
280 #                amount = line.total
281 #                rec = {
282 #                    'move_id': move_id,
283 #                    'name': name,
284 #                    'date': slip.date_from,
285 #                    'account_id': line.account_id.id,
286 #                    'debit': 0.0,
287 #                    'credit': 0.0,
288 #                    'journal_id': slip.journal_id.id,
289 #                    'period_id': period_id,
290 #                    'analytic_account_id': False,
291 #                    'ref': slip.number,
292 #                    'quantity': 1
293 #                }
294 #
295 #                #Setting Analysis Account for Salary Slip Lines
296 #                if line.analytic_account_id:
297 #                    rec['analytic_account_id'] = line.analytic_account_id.id
298 #                if line.category_id.name == 'Allowance' :
299 #                    rec['debit'] = amount
300 #                    if not partner.property_account_payable:
301 #                        raise osv.except_osv(_('Integrity Error !'), _('Please Configure Partners Payable Account!!'))
302 #                    ded_rec = {
303 #                        'move_id': move_id,
304 #                        'name': name,
305 #                        'partner_id': partner_id,
306 #                        'date': slip.date_from,
307 #                        'account_id': partner.property_account_payable.id,
308 #                        'debit': 0.0,
309 #                        'quantity': 1,
310 #                        'credit': amount,
311 #                        'journal_id': slip.journal_id.id,
312 #                        'period_id': period_id,
313 #                        'ref': slip.number
314 #                    }
315 #                    line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
316 #                elif line.category_id.name  == 'Deduction':
317 #                    if not partner.property_account_receivable:
318 #                        raise osv.except_osv(_('Integrity Error !'), _('Please Configure Partners Receivable Account!!'))
319 #                    amount =  -(amount)
320 #                    rec['credit'] = amount
321 #                    total_deduct += amount
322 #                    ded_rec = {
323 #                        'move_id': move_id,
324 #                        'name': name,
325 #                        'partner_id': partner_id,
326 #                        'date': slip.date_from,
327 #                        'quantity': 1,
328 #                        'account_id': partner.property_account_receivable.id,
329 #                        'debit': amount,
330 #                        'credit': 0.0,
331 #                        'journal_id': slip.journal_id.id,
332 #                        'period_id': period_id,
333 #                        'ref': slip.number
334 #                    }
335 #                    line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
336 #                line_ids += [movel_pool.create(cr, uid, rec, context=context)]
337 #            adj_move_id = False
338 #            if total_deduct > 0:
339 #                move = {
340 #                    'journal_id': slip.journal_id.id,
341 #                    'period_id': period_id,
342 #                    'date': slip.date_from,
343 #                    'ref':slip.number,
344 #                    'narration': 'Adjustment: %s' % (slip.name)
345 #                }
346 #                adj_move_id = move_pool.create(cr, uid, move, context=context)
347 #                move_ids += [adj_move_id]
348 #                name = "Adjustment Entry - %s" % (slip.employee_id.name)
349 #                self.create_voucher(cr, uid, [slip.id], name, adj_move_id)
350 #
351 #                ded_rec = {
352 #                    'move_id': adj_move_id,
353 #                    'name': name,
354 #                    'partner_id': partner_id,
355 #                    'date': slip.date_from,
356 #                    'account_id': partner.property_account_receivable.id,
357 #                    'debit': 0.0,
358 #                    'quantity': 1,
359 #                    'credit': total_deduct,
360 #                    'journal_id': slip.journal_id.id,
361 #                    'period_id': period_id,
362 #                    'ref': slip.number
363 #                }
364 #                line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
365 #                cre_rec = {
366 #                    'move_id': adj_move_id,
367 #                    'name': name,
368 #                    'partner_id': partner_id,
369 #                    'date': slip.date_from,
370 #                    'account_id': partner.property_account_payable.id,
371 #                    'debit': total_deduct,
372 #                    'quantity': 1,
373 #                    'credit': 0.0,
374 #                    'journal_id': slip.journal_id.id,
375 #                    'period_id': period_id,
376 #                    'ref': slip.number
377 #                }
378 #                line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
379 #
380 #            rec = {
381 #                'state':'confirm',
382 #                'move_line_ids':[(6, 0,line_ids)],
383 #                'account_move_ids':[(6, 0, move_ids)]
384 #            }
385 #            if not slip.period_id:
386 #                rec['period_id'] = period_id
387 #
388 #            exp_ids = exp_pool.search(cr, uid, [('date_valid','>=',slip.date_from), ('date_valid','<=',slip.date_to), ('state','=','invoiced')], context=context)
389 #            self.write(cr, uid, [slip.id], rec, context=context)
390 #        return True
391 #
392 hr_payslip()
393
394 #TODO: remove, i don't think it's worth having that information on the payslip line rather than on the salary rule
395 #class hr_payslip_line(osv.osv):
396 #    _inherit = 'hr.payslip.line'
397 #    _columns = {
398 #        'account_id': fields.many2one('account.account', 'General Account'),
399 #        'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
400 #    }
401 #hr_payslip_line()
402
403 class hr_salary_rule(osv.osv):
404     _inherit = 'hr.salary.rule'
405     _columns = {
406         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
407         'account_tax_id':fields.many2one('account.tax.code', 'Tax Code'),
408         'account_debit': fields.many2one('account.account', 'Debit Account'),
409         'account_credit': fields.many2one('account.account', 'Credit Account'),
410     }
411 hr_salary_rule()
412
413 class hr_contract(osv.osv):
414
415     _inherit = 'hr.contract'
416     _description = 'Employee Contract'
417     _columns = {
418         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
419         'journal_id': fields.many2one('account.journal', 'Salary Journal'),
420     }
421 hr_contract()
422
423
424 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: