2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
24 from datetime import date, timedelta
26 from osv import fields, osv
27 from tools.translate import _
29 def prev_bounds(cdate=False):
30 when = date.fromtimestamp(time.mktime(time.strptime(cdate,"%Y-%m-%d")))
31 this_first = date(when.year, when.month, 1)
32 month = when.month + 1
37 next_month = date(year, month, 1)
38 prev_end = next_month - timedelta(days=1)
39 return this_first, prev_end
41 class hr_payroll_structure(osv.osv):
42 _inherit = 'hr.payroll.structure'
43 _description = 'Salary Structure'
46 'account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
48 hr_payroll_structure()
50 class hr_employee(osv.osv):
54 _inherit = 'hr.employee'
55 _description = 'Employee'
58 'property_bank_account': fields.property(
61 relation='account.account',
62 string="Bank Account",
64 domain="[('type', '=', 'liquidity')]",
66 help="Select Bank Account from where Salary Expense will be Paid, to be used for payslip verification."),
67 'salary_account':fields.property(
70 relation='account.account',
71 string="Salary Account",
73 domain="[('type', '=', 'other')]",
75 help="Expense account when Salary Expense will be recorded"),
76 'employee_account':fields.property(
79 relation='account.account',
80 string="Employee Account",
82 domain="[('type', '=', 'other')]",
84 help="Employee Payable Account"),
85 'analytic_account':fields.property(
86 'account.analytic.account',
88 relation='account.analytic.account',
89 string="Analytic Account",
92 help="Analytic Account for Salary Analysis"),
96 class payroll_register(osv.osv):
97 _inherit = 'hr.payroll.register'
98 _description = 'Payroll Register'
101 'journal_id': fields.many2one('account.journal', 'Expense Journal'),
102 'bank_journal_id': fields.many2one('account.journal', 'Bank Journal'),
103 'period_id': fields.many2one('account.period', 'Force Period', domain=[('state','<>','done')], help="Keep empty to use the period of the validation(Payslip) date."),
106 def compute_sheet(self, cr, uid, ids, context=None):
107 emp_pool = self.pool.get('hr.employee')
108 slip_pool = self.pool.get('hr.payslip')
109 wf_service = netsvc.LocalService("workflow")
110 vals = self.browse(cr, uid, ids, context=context)[0]
111 emp_ids = emp_pool.search(cr, uid, [])
113 for emp in emp_pool.browse(cr, uid, emp_ids, context=context):
114 old_slips = slip_pool.search(cr, uid, [('employee_id','=', emp.id), ('date','=',vals.date)])
116 slip_pool.write(cr, uid, old_slips, {'register_id':ids[0]})
117 for sid in old_slips:
118 wf_service.trg_validate(uid, 'hr.payslip', sid, 'compute_sheet', cr)
121 'employee_id':emp.id,
123 'register_id':ids[0],
126 'journal_id':vals.journal_id.id,
127 'bank_journal_id':vals.bank_journal_id.id
129 slip_id = slip_pool.create(cr, uid, res)
130 wf_service.trg_validate(uid, 'hr.payslip', slip_id, 'compute_sheet', cr)
132 number = self.pool.get('ir.sequence').get(cr, uid, 'salary.register')
133 self.write(cr, uid, ids, {'state':'draft', 'number':number})
138 class payroll_advice(osv.osv):
139 _inherit = 'hr.payroll.advice'
140 _description = 'Bank Advice Note'
143 'account_id': fields.many2one('account.account', 'Account'),
147 class contrib_register(osv.osv):
148 _inherit = 'hr.contibution.register'
149 _description = 'Contribution Register'
151 def _total_contrib(self, cr, uid, ids, field_names, arg, context=None):
152 line_pool = self.pool.get('hr.contibution.register.line')
153 period_id = self.pool.get('account.period').search(cr,uid,[('date_start','<=',time.strftime('%Y-%m-%d')),('date_stop','>=',time.strftime('%Y-%m-%d'))])[0]
154 fiscalyear_id = self.pool.get('account.period').browse(cr, uid, period_id, context=context).fiscalyear_id
156 for cur in self.browse(cr, uid, ids, context=context):
157 current = line_pool.search(cr, uid, [('period_id','=',period_id),('register_id','=',cur.id)])
158 years = line_pool.search(cr, uid, [('period_id.fiscalyear_id','=',fiscalyear_id.id), ('register_id','=',cur.id)])
162 for i in line_pool.browse(cr, uid, current, context=context):
163 e_month += i.emp_deduction
164 c_month += i.comp_deduction
168 for j in line_pool.browse(cr, uid, years, context=context):
169 e_year += i.emp_deduction
170 c_year += i.comp_deduction
173 'monthly_total_by_emp':e_month,
174 'monthly_total_by_comp':c_month,
175 'yearly_total_by_emp':e_year,
176 'yearly_total_by_comp':c_year
181 'account_id': fields.many2one('account.account', 'Account'),
182 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
183 'yearly_total_by_emp': fields.function(_total_contrib, method=True, multi='dc', store=True, string='Total By Employee', digits=(16, 4)),
184 'yearly_total_by_comp': fields.function(_total_contrib, method=True, multi='dc', store=True, string='Total By Company', digits=(16, 4)),
188 class contrib_register_line(osv.osv):
189 _inherit = 'hr.contibution.register.line'
190 _description = 'Contribution Register Line'
193 'period_id': fields.many2one('account.period', 'Period'),
195 contrib_register_line()
197 class hr_holidays_status(osv.osv):
198 _inherit = 'hr.holidays.status'
200 'account_id': fields.many2one('account.account', 'Account'),
201 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
205 class hr_payslip(osv.osv):
209 _inherit = 'hr.payslip'
210 _description = 'Pay Slip'
213 'journal_id': fields.many2one('account.journal', 'Expense Journal'),
214 'bank_journal_id': fields.many2one('account.journal', 'Bank Journal'),
215 'move_ids':fields.one2many('hr.payslip.account.move', 'slip_id', 'Accounting vouchers'),
216 'move_line_ids':fields.many2many('account.move.line', 'payslip_lines_rel', 'slip_id', 'line_id', 'Accounting Lines', readonly=True),
217 'move_payment_ids':fields.many2many('account.move.line', 'payslip_payment_rel', 'slip_id', 'payment_id', 'Payment Lines', readonly=True),
218 'period_id': fields.many2one('account.period', 'Force Period', domain=[('state','<>','done')], help="Keep empty to use the period of the validation(Payslip) date."),
221 def create_voucher(self, cr, uid, ids, name, voucher, sequence=5):
222 slip_move = self.pool.get('hr.payslip.account.move')
230 slip_move.create(cr, uid, res)
232 def cancel_sheet(self, cr, uid, ids, context=None):
233 move_pool = self.pool.get('account.move')
234 slip_move = self.pool.get('hr.payslip.account.move')
236 for slip in self.browse(cr, uid, ids, context=context):
237 for line in slip.move_ids:
238 move_ids.append(line.id)
240 if line.move_id.state == 'posted':
241 move_pool.button_cancel(cr, uid [line.move_id.id], context)
242 move_pool.unlink(cr, uid, [line.move_id.id], context=context)
244 slip_move.unlink(cr, uid, move_ids, context=context)
245 self.write(cr, uid, ids, {'state':'cancel'}, context=context)
248 def process_sheet(self, cr, uid, ids, context=None):
249 move_pool = self.pool.get('account.move')
250 movel_pool = self.pool.get('account.move.line')
251 invoice_pool = self.pool.get('account.invoice')
252 fiscalyear_pool = self.pool.get('account.fiscalyear')
253 period_pool = self.pool.get('account.period')
255 for slip in self.browse(cr, uid, ids, context=context):
256 if not slip.bank_journal_id or not slip.journal_id:
257 # Call super method to process sheet if journal_id or bank_journal_id are not specified.
258 super(hr_payslip, self).process_sheet(cr, uid, [slip.id], context=context)
265 partner = slip.employee_id.bank_account_id.partner_id
266 partner_id = partner.id
268 fiscal_year_ids = fiscalyear_pool.search(cr, uid, [], context=context)
269 if not fiscal_year_ids:
270 raise osv.except_osv(_('Warning !'), _('Please define fiscal year for perticular contract'))
271 fiscal_year_objs = fiscalyear_pool.read(cr, uid, fiscal_year_ids, ['date_start','date_stop'], context=context)
273 for fiscal_year in fiscal_year_objs:
274 if ((fiscal_year['date_start'] <= slip.date) and (fiscal_year['date_stop'] >= slip.date)):
277 raise osv.except_osv(_('Warning !'), _('Fiscal Year is not defined for slip date %s') % slip.date)
278 search_periods = period_pool.search(cr, uid, [('date_start','<=',slip.date),('date_stop','>=',slip.date)], context=context)
279 if not search_periods:
280 raise osv.except_osv(_('Warning !'), _('Period is not defined for slip date %s') % slip.date)
281 period_id = search_periods[0]
282 name = 'Payment of Salary to %s' % (slip.employee_id.name)
284 'journal_id': slip.bank_journal_id.id,
285 'period_id': period_id,
287 'type':'bank_pay_voucher',
291 move_id = move_pool.create(cr, uid, move, context=context)
292 self.create_voucher(cr, uid, [slip.id], name, move_id)
294 name = "To %s account" % (slip.employee_id.name)
296 if not slip.employee_id.property_bank_account.id:
297 raise osv.except_osv(_('Warning !'), _('Employee Bank Account is not defined for %s') % slip.employee_id.name)
303 'account_id': slip.employee_id.property_bank_account.id,
305 'credit': slip.total_pay,
306 'journal_id': slip.journal_id.id,
307 'period_id': period_id,
310 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
311 name = "By %s account" % (slip.employee_id.property_bank_account.name)
315 'partner_id': partner_id,
317 'account_id': partner.property_account_payable.id,
318 'debit': slip.total_pay,
320 'journal_id': slip.journal_id.id,
321 'period_id': period_id,
324 line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
326 other_pay = slip.other_pay
327 #Process all Reambuse Entries
328 for line in slip.line_ids:
329 if line.type == 'otherpay':
331 acc_id = slip.bank_journal_id.default_credit_account_id and slip.bank_journal_id.default_credit_account_id.id
332 period_id = slip.period_id.id
333 journal_id = slip.bank_journal_id.id
334 name = '[%s]-%s' % (slip.number, line.name)
336 #TODO: link this account entries to the Payment Lines also Expense Entries to Account Lines
337 l_ids = movel_pool.search(cr, uid, [('name','=',name)], context=context)
340 #Process for Other payment if any
341 other_move_id = False
342 if slip.other_pay > 0:
343 narration = 'Payment of Other Payeble amounts to %s' % (slip.employee_id.name)
345 'journal_id': slip.bank_journal_id.id,
346 'period_id': period_id,
348 'type':'bank_pay_voucher',
350 'narration': narration
352 other_move_id = move_pool.create(cr, uid, move, context=context)
353 self.create_voucher(cr, uid, [slip.id], narration, move_id)
355 name = "To %s account" % (slip.employee_id.name)
357 'move_id':other_move_id,
360 'account_id':slip.employee_id.property_bank_account.id,
363 'journal_id':slip.journal_id.id,
364 'period_id':period_id,
367 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
368 name = "By %s account" % (slip.employee_id.property_bank_account.name)
370 'move_id':other_move_id,
372 'partner_id':partner_id,
374 'account_id':partner.property_account_payable.id,
377 'journal_id':slip.journal_id.id,
378 'period_id':period_id,
381 line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
385 'move_payment_ids':[(6, 0, line_ids)],
388 self.write(cr, uid, [slip.id], rec, context=context)
389 for exp_id in exp_ids:
390 self.write(cr, uid, [slip.id], {'move_line_ids':[(4, exp_id)]}, context=context)
394 def account_check_sheet(self, cr, uid, ids, context=None):
395 self.write(cr, uid, ids, {'state':'accont_check'}, context=context)
398 def hr_check_sheet(self, cr, uid, ids, context=None):
399 self.write(cr, uid, ids, {'state':'hr_check'}, context=context)
402 def verify_sheet(self, cr, uid, ids, context=None):
403 move_pool = self.pool.get('account.move')
404 movel_pool = self.pool.get('account.move.line')
405 fiscalyear_pool = self.pool.get('account.fiscalyear')
406 period_pool = self.pool.get('account.period')
408 for slip in self.browse(cr, uid, ids, context=context):
409 if not slip.journal_id:
410 # Call super method to verify sheet if journal_id is not specified.
411 super(hr_payslip, self).verify_sheet(cr, uid, [slip.id], context=context)
419 if not slip.employee_id.bank_account_id:
420 raise osv.except_osv(_('Configuration Error !'), _('Please define bank account for %s !') % (slip.employee_id.name))
422 if not slip.employee_id.bank_account_id.partner_id:
423 raise osv.except_osv(_('Configuration Error !'), _('Please define partner in bank account for %s !') % (slip.employee_id.name))
425 partner = slip.employee_id.bank_account_id.partner_id
426 partner_id = slip.employee_id.bank_account_id.partner_id.id
431 period_id = slip.period_id.id
433 fiscal_year_ids = fiscalyear_pool.search(cr, uid, [], context=context)
434 if not fiscal_year_ids:
435 raise osv.except_osv(_('Warning !'), _('Please define fiscal year for perticular contract'))
436 fiscal_year_objs = fiscalyear_pool.read(cr, uid, fiscal_year_ids, ['date_start','date_stop'], context=context)
438 for fiscal_year in fiscal_year_objs:
439 if ((fiscal_year['date_start'] <= slip.date) and (fiscal_year['date_stop'] >= slip.date)):
442 raise osv.except_osv(_('Warning !'), _('Fiscal Year is not defined for slip date %s') % slip.date)
443 search_periods = period_pool.search(cr,uid,[('date_start','<=',slip.date),('date_stop','>=',slip.date)], context=context)
444 if not search_periods:
445 raise osv.except_osv(_('Warning !'), _('Period is not defined for slip date %s') % slip.date)
446 period_id = search_periods[0]
449 'journal_id': slip.journal_id.id,
450 'period_id': period_id,
453 'narration': slip.name
455 move_id = move_pool.create(cr, uid, move, context=context)
456 self.create_voucher(cr, uid, [slip.id], slip.name, move_id)
458 if not slip.employee_id.salary_account.id:
459 raise osv.except_osv(_('Warning !'), _('Please define Salary Account for %s.') % slip.employee_id.name)
463 'name': "By Basic Salary / " + slip.employee_id.name,
465 'account_id': slip.employee_id.salary_account.id,
468 'quantity':slip.working_days,
469 'journal_id': slip.journal_id.id,
470 'period_id': period_id,
471 'analytic_account_id': False,
474 #Setting Analysis Account for Basic Salary
475 if slip.employee_id.analytic_account:
476 line['analytic_account_id'] = slip.employee_id.analytic_account.id
478 move_line_id = movel_pool.create(cr, uid, line, context=context)
479 line_ids += [move_line_id]
481 if not slip.employee_id.employee_account.id:
482 raise osv.except_osv(_('Warning !'), _('Please define Employee Payable Account for %s.') % slip.employee_id.name)
486 'name': "To Basic Payable Salary / " + slip.employee_id.name,
487 'partner_id': partner_id,
489 'account_id': slip.employee_id.employee_account.id,
491 'quantity':slip.working_days,
492 'credit': slip.basic,
493 'journal_id': slip.journal_id.id,
494 'period_id': period_id,
498 line_ids += [movel_pool.create(cr, uid, line, context=context)]
500 for line in slip.line_ids:
501 name = "[%s] - %s / %s" % (line.code, line.name, slip.employee_id.name)
504 if line.type == 'leaves':
511 'account_id': line.account_id.id,
514 'journal_id': slip.journal_id.id,
515 'period_id': period_id,
516 'analytic_account_id': False,
521 #Setting Analysis Account for Salary Slip Lines
522 if line.analytic_account_id:
523 rec['analytic_account_id'] = line.analytic_account_id.id
526 raise osv.except_osv(_('Configuration Error !'), _("Payslip cannot be approved due to one of the following reasons: \n 1. The Structure line %s has not been linked with an analytic account. \n Or \n 2. Payslip for %s is missing the configuration of Designation from 'Accounting Details") % (line.name, slip.employee_id.name))
528 rec['analytic_account_id'] = slip.deg_id.account_id.id
530 if line.type == 'allowance' or line.type == 'otherpay':
531 rec['debit'] = amount
532 if not partner.property_account_payable:
533 raise osv.except_osv(_('Configuration Error !'), _('Please Configure Partners Payable Account!!'))
537 'partner_id': partner_id,
539 'account_id': partner.property_account_payable.id,
543 'journal_id': slip.journal_id.id,
544 'period_id': period_id,
547 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
548 elif line.type == 'deduction' or line.type == 'otherdeduct':
549 if not partner.property_account_receivable:
550 raise osv.except_osv(_('Configuration Error !'), _('Please Configure Partners Receivable Account!!'))
551 rec['credit'] = amount
552 total_deduct += amount
556 'partner_id': partner_id,
559 'account_id': partner.property_account_receivable.id,
562 'journal_id': slip.journal_id.id,
563 'period_id': period_id,
566 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
568 line_ids += [movel_pool.create(cr, uid, rec, context=context)]
571 # for contrib in line.category_id.contribute_ids:
572 # _log.debug("%s %s %s %s %s", contrib.name, contrub.code, contrub.amount_type, contrib.contribute_per, line.total)
577 'journal_id': slip.journal_id.id,
578 'period_id': period_id,
581 'narration': 'Adjustment: %s' % (slip.name)
583 adj_move_id = move_pool.create(cr, uid, move, context=context)
584 name = "Adjustment Entry - %s" % (slip.employee_id.name)
585 self.create_voucher(cr, uid, [slip.id], name, adj_move_id)
588 'move_id': adj_move_id,
590 'partner_id': partner_id,
592 'account_id': partner.property_account_receivable.id,
595 'credit': total_deduct,
596 'journal_id': slip.journal_id.id,
597 'period_id': period_id,
600 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
602 'move_id': adj_move_id,
604 'partner_id': partner_id,
606 'account_id': partner.property_account_payable.id,
607 'debit': total_deduct,
610 'journal_id': slip.journal_id.id,
611 'period_id': period_id,
614 line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
618 'move_line_ids':[(6, 0,line_ids)],
620 if not slip.period_id:
621 rec['period_id'] = period_id
623 self.write(cr, uid, [slip.id], rec, context=context)
628 class hr_payslip_line(osv.osv):
629 _inherit = 'hr.payslip.line'
631 'account_id': fields.many2one('account.account', 'General Account'),
632 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
636 class account_move_link_slip(osv.osv):
638 Account Move Link to Pay Slip
640 _name = 'hr.payslip.account.move'
641 _description = 'Account Move Link to Pay Slip'
643 'name':fields.char('Name', size=256, required=True, readonly=False),
644 'move_id':fields.many2one('account.move', 'Expense Entries', required=False, readonly=True),
645 'slip_id':fields.many2one('hr.payslip', 'Pay Slip', required=False),
646 'sequence': fields.integer('Sequence'),
648 account_move_link_slip()
650 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: