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 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 General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
24 from datetime import date, datetime, timedelta
26 from osv import fields, osv
27 from tools import config
28 from tools.translate import _
30 def prev_bounds(cdate=False):
31 when = date.fromtimestamp(time.mktime(time.strptime(cdate,"%Y-%m-%d")))
32 this_first = date(when.year, when.month, 1)
33 month = when.month + 1
38 next_month = date(year, month, 1)
39 prev_end = next_month - timedelta(days=1)
40 return this_first, prev_end
42 class hr_payroll_structure(osv.osv):
43 _inherit = 'hr.payroll.structure'
44 _description = 'Salary Structure'
47 'account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
49 hr_payroll_structure()
51 class hr_employee(osv.osv):
55 _inherit = 'hr.employee'
56 _description = 'Employee'
59 'property_bank_account': fields.property(
62 relation='account.account',
63 string="Bank Account",
65 domain="[('type', '=', 'liquidity')]",
67 help="Select Bank Account from where Salary Expanse will be Paid"),
68 'salary_account':fields.property(
71 relation='account.account',
72 string="Salary Account",
74 domain="[('type', '=', 'other')]",
76 help="Expanse account when Salary Expanse will be recorded"),
77 'employee_account':fields.property(
80 relation='account.account',
81 string="Employee Account",
83 domain="[('type', '=', 'other')]",
85 help="Employee Payable Account"),
86 'analytic_account':fields.property(
87 'account.analytic.account',
89 relation='account.analytic.account',
90 string="Analytic Account",
93 help="Analytic Account for Salary Analysis"),
97 class payroll_register(osv.osv):
98 _inherit = 'hr.payroll.register'
99 _description = 'Payroll Register'
102 'journal_id': fields.many2one('account.journal', 'Expanse Journal'),
103 'bank_journal_id': fields.many2one('account.journal', 'Bank Journal'),
104 'period_id': fields.many2one('account.period', 'Force Period', domain=[('state','<>','done')], help="Keep empty to use the period of the validation(Payslip) date."),
107 def compute_sheet(self, cr, uid, ids, context={}):
108 emp_pool = self.pool.get('hr.employee')
109 slip_pool = self.pool.get('hr.payslip')
110 func_pool = self.pool.get('hr.payroll.structure')
111 slip_line_pool = self.pool.get('hr.payslip.line')
112 wf_service = netsvc.LocalService("workflow")
113 vals = self.browse(cr, uid, ids)[0]
114 emp_ids = emp_pool.search(cr, uid, [])
116 for emp in emp_pool.browse(cr, uid, emp_ids):
117 old_slips = slip_pool.search(cr, uid, [('employee_id','=', emp.id), ('date','=',vals.date)])
119 slip_pool.write(cr, uid, old_slips, {'register_id':ids[0]})
120 for sid in old_slips:
121 wf_service.trg_validate(uid, 'hr.payslip', sid, 'compute_sheet', cr)
124 'employee_id':emp.id,
126 'register_id':ids[0],
129 'journal_id':vals.journal_id.id,
130 'bank_journal_id':vals.bank_journal_id.id
132 slip_id = slip_pool.create(cr, uid, res)
133 wf_service.trg_validate(uid, 'hr.payslip', slip_id, 'compute_sheet', cr)
135 number = self.pool.get('ir.sequence').get(cr, uid, 'salary.register')
136 self.write(cr, uid, ids, {'state':'draft', 'number':number})
141 class payroll_advice(osv.osv):
142 _inherit = 'hr.payroll.advice'
143 _description = 'Bank Advice Note'
146 'account_id': fields.many2one('account.account', 'Account'),
150 class contrib_register(osv.osv):
151 _inherit = 'hr.contibution.register'
152 _description = 'Contribution Register'
154 def _total_contrib(self, cr, uid, ids, field_names, arg, context={}):
155 line_pool = self.pool.get('hr.contibution.register.line')
156 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]
157 fiscalyear_id = self.pool.get('account.period').browse(cr, uid, period_id).fiscalyear_id
159 for cur in self.browse(cr, uid, ids):
160 current = line_pool.search(cr, uid, [('period_id','=',period_id),('register_id','=',cur.id)])
161 years = line_pool.search(cr, uid, [('period_id.fiscalyear_id','=',fiscalyear_id.id), ('register_id','=',cur.id)])
165 for i in line_pool.browse(cr, uid, current):
166 e_month += i.emp_deduction
167 c_month += i.comp_deduction
171 for j in line_pool.browse(cr, uid, years):
172 e_year += i.emp_deduction
173 c_year += i.comp_deduction
176 'monthly_total_by_emp':e_month,
177 'monthly_total_by_comp':c_month,
178 'yearly_total_by_emp':e_year,
179 'yearly_total_by_comp':c_year
184 'account_id': fields.many2one('account.account', 'Account'),
185 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
186 'yearly_total_by_emp': fields.function(_total_contrib, method=True, multi='dc', store=True, string='Total By Employee', digits=(16, 4)),
187 'yearly_total_by_comp': fields.function(_total_contrib, method=True, multi='dc', store=True, string='Total By Company', digits=(16, 4)),
191 class contrib_register_line(osv.osv):
192 _inherit = 'hr.contibution.register.line'
193 _description = 'Contribution Register Line'
196 'period_id': fields.many2one('account.period', 'Period'),
198 contrib_register_line()
200 class hr_holidays_status(osv.osv):
201 _inherit = 'hr.holidays.status'
203 'account_id': fields.many2one('account.account', 'Account'),
204 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
208 class hr_payslip(osv.osv):
212 _inherit = 'hr.payslip'
213 _description = 'Pay Slip'
216 'journal_id': fields.many2one('account.journal', 'Expanse Journal'),
217 'bank_journal_id': fields.many2one('account.journal', 'Bank Journal'),
218 'move_ids':fields.one2many('hr.payslip.account.move', 'slip_id', 'Accounting vouchers'),
219 'move_line_ids':fields.many2many('account.move.line', 'payslip_lines_rel', 'slip_id', 'line_id', 'Accounting Lines', readonly=True),
220 'move_payment_ids':fields.many2many('account.move.line', 'payslip_payment_rel', 'slip_id', 'payment_id', 'Payment Lines', readonly=True),
221 'period_id': fields.many2one('account.period', 'Force Period', domain=[('state','<>','done')], help="Keep empty to use the period of the validation(Payslip) date."),
224 def create_voucher(self, cr, uid, ids, name, voucher, sequence=5):
225 slip_move = self.pool.get('hr.payslip.account.move')
233 slip_move.create(cr, uid, res)
235 def cancel_sheet(self, cr, uid, ids, context=None):
236 move_pool = self.pool.get('account.move')
237 slip_move = self.pool.get('hr.payslip.account.move')
241 for slip in self.browse(cr, uid, ids, context=context):
242 for line in slip.move_ids:
243 move_ids.append(line.id)
245 if line.move_id.state == 'posted':
246 move_pool.button_cancel(cr, uid [line.move_id.id], context)
247 move_pool.unlink(cr, uid, [line.move_id.id], context=context)
249 slip_move.unlink(cr, uid, move_ids, context=context)
250 self.write(cr, uid, ids, {'state':'cancel'}, context=context)
253 def process_sheet(self, cr, uid, ids, context=None):
254 move_pool = self.pool.get('account.move')
255 movel_pool = self.pool.get('account.move.line')
256 invoice_pool = self.pool.get('account.invoice')
257 fiscalyear_pool = self.pool.get('account.fiscalyear')
258 period_pool = self.pool.get('account.period')
262 for slip in self.browse(cr, uid, ids, context=context):
268 partner = slip.employee_id.bank_account_id.partner_id
269 partner_id = partner.id
271 fiscal_year_ids = fiscalyear_pool.search(cr, uid, [], context=context)
272 if not fiscal_year_ids:
273 raise osv.except_osv(_('Warning !'), _('Please define fiscal year for perticular contract'))
274 fiscal_year_objs = fiscalyear_pool.read(cr, uid, fiscal_year_ids, ['date_start','date_stop'], context=context)
276 for fiscal_year in fiscal_year_objs:
277 if ((fiscal_year['date_start'] <= slip.date) and (fiscal_year['date_stop'] >= slip.date)):
280 raise osv.except_osv(_('Warning !'), _('Fiscal Year is not defined for slip date %s'%slip.date))
281 search_periods = period_pool.search(cr, uid, [('date_start','<=',slip.date),('date_stop','>=',slip.date)], context=context)
282 if not search_periods:
283 raise osv.except_osv(_('Warning !'), _('Period is not defined for slip date %s'%slip.date))
284 period_id = search_periods[0]
285 name = 'Payment of Salary to %s' % (slip.employee_id.name)
287 'journal_id': slip.bank_journal_id.id,
288 'period_id': period_id,
290 'type':'bank_pay_voucher',
294 move_id = move_pool.create(cr, uid, move, context=context)
295 self.create_voucher(cr, uid, [slip.id], name, move_id)
297 name = "To %s account" % (slip.employee_id.name)
302 'account_id': slip.employee_id.property_bank_account.id,
304 'credit': slip.total_pay,
305 'journal_id': slip.journal_id.id,
306 'period_id': period_id,
309 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
310 name = "By %s account" % (slip.employee_id.property_bank_account.name)
314 'partner_id': partner_id,
316 'account_id': partner.property_account_payable.id,
317 'debit': slip.total_pay,
319 'journal_id': slip.journal_id.id,
320 'period_id': period_id,
323 line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
325 other_pay = slip.other_pay
326 #Process all Reambuse Entries
327 for line in slip.line_ids:
328 if line.type == 'otherpay' and line.expanse_id.invoice_id:
329 if not line.expanse_id.invoice_id.move_id:
330 raise osv.except_osv(_('Warning !'), _('Please Confirm all Expense Invoice appear for Reimbursement'))
331 invids = [line.expanse_id.invoice_id.id]
333 acc_id = slip.bank_journal_id.default_credit_account_id and slip.bank_journal_id.default_credit_account_id.id
334 period_id = slip.period_id.id
335 journal_id = slip.bank_journal_id.id
336 name = '[%s]-%s' % (slip.number, line.name)
337 invoice_pool.pay_and_reconcile(cr, uid, invids, amount, acc_id, period_id, journal_id, False, period_id, False, context, name)
339 #TODO: link this account entries to the Payment Lines also Expanse Entries to Account Lines
340 l_ids = movel_pool.search(cr, uid, [('name','=',name)], context=context)
343 l_ids = movel_pool.search(cr, uid, [('invoice','=',line.expanse_id.invoice_id.id)], context=context)
346 #Process for Other payment if any
347 other_move_id = False
348 if slip.other_pay > 0:
349 narration = 'Payment of Other Payeble amounts to %s' % (slip.employee_id.name)
351 'journal_id': slip.bank_journal_id.id,
352 'period_id': period_id,
354 'type':'bank_pay_voucher',
356 'narration': narration
358 other_move_id = move_pool.create(cr, uid, move, context=context)
359 self.create_voucher(cr, uid, [slip.id], narration, move_id)
361 name = "To %s account" % (slip.employee_id.name)
363 'move_id':other_move_id,
366 'account_id':slip.employee_id.property_bank_account.id,
369 'journal_id':slip.journal_id.id,
370 'period_id':period_id,
373 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
374 name = "By %s account" % (slip.employee_id.property_bank_account.name)
376 'move_id':other_move_id,
378 'partner_id':partner_id,
380 'account_id':partner.property_account_payable.id,
383 'journal_id':slip.journal_id.id,
384 'period_id':period_id,
387 line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
391 'move_payment_ids':[(6, 0, line_ids)],
394 self.write(cr, uid, [slip.id], rec, context=context)
395 for exp_id in exp_ids:
396 self.write(cr, uid, [slip.id], {'move_line_ids':[(4, exp_id)]}, context=context)
400 def account_check_sheet(self, cr, uid, ids, context=None):
403 self.write(cr, uid, ids, {'state':'accont_check'}, context=context)
406 def hr_check_sheet(self, cr, uid, ids, context=None):
409 self.write(cr, uid, ids, {'state':'hr_check'}, context=context)
412 def verify_sheet(self, cr, uid, ids, context=None):
413 move_pool = self.pool.get('account.move')
414 movel_pool = self.pool.get('account.move.line')
415 exp_pool = self.pool.get('hr.expense.expense')
416 fiscalyear_pool = self.pool.get('account.fiscalyear')
417 period_pool = self.pool.get('account.period')
418 property_pool = self.pool.get('ir.property')
419 payslip_pool = self.pool.get('hr.payslip.line')
423 for slip in self.browse(cr, uid, ids, context=context):
430 if not slip.employee_id.bank_account_id:
431 raise osv.except_osv(_('Integrity Error !'), _('Please defined bank account for %s !' % (slip.employee_id.name)))
433 if not slip.employee_id.bank_account_id.partner_id:
434 raise osv.except_osv(_('Integrity Error !'), _('Please defined partner in bank account for %s !' % (slip.employee_id.name)))
436 partner = slip.employee_id.bank_account_id.partner_id
437 partner_id = slip.employee_id.bank_account_id.partner_id.id
442 period_id = slip.period_id.id
444 fiscal_year_ids = fiscalyear_pool.search(cr, uid, [], context=context)
445 if not fiscal_year_ids:
446 raise osv.except_osv(_('Warning !'), _('Please define fiscal year for perticular contract'))
447 fiscal_year_objs = fiscalyear_pool.read(cr, uid, fiscal_year_ids, ['date_start','date_stop'], context=context)
449 for fiscal_year in fiscal_year_objs:
450 if ((fiscal_year['date_start'] <= slip.date) and (fiscal_year['date_stop'] >= slip.date)):
453 raise osv.except_osv(_('Warning !'), _('Fiscal Year is not defined for slip date %s'%slip.date))
454 search_periods = period_pool.search(cr,uid,[('date_start','<=',slip.date),('date_stop','>=',slip.date)], context=context)
455 if not search_periods:
456 raise osv.except_osv(_('Warning !'), _('Period is not defined for slip date %s'%slip.date))
457 period_id = search_periods[0]
460 'journal_id': slip.journal_id.id,
461 'period_id': period_id,
464 'narration': slip.name
466 move_id = move_pool.create(cr, uid, move, context=context)
467 self.create_voucher(cr, uid, [slip.id], slip.name, move_id)
471 'name': "By Basic Salary / " + slip.employee_id.name,
473 'account_id': slip.employee_id.salary_account.id,
476 'quantity':slip.working_days,
477 'journal_id': slip.journal_id.id,
478 'period_id': period_id,
479 'analytic_account_id': False,
483 #Setting Analysis Account for Basic Salary
484 if slip.employee_id.analytic_account:
485 line['analytic_account_id'] = slip.employee_id.analytic_account.id
487 move_line_id = movel_pool.create(cr, uid, line, context=context)
488 line_ids += [move_line_id]
492 'name': "To Basic Paysble Salary / " + slip.employee_id.name,
493 'partner_id': partner_id,
495 'account_id': slip.employee_id.employee_account.id,
497 'quantity':slip.working_days,
498 'credit': slip.basic,
499 'journal_id': slip.journal_id.id,
500 'period_id': period_id,
503 line_ids += [movel_pool.create(cr, uid, line, context=context)]
505 for line in slip.line_ids:
506 name = "[%s] - %s / %s" % (line.code, line.name, slip.employee_id.name)
509 if line.type == 'leaves':
516 'account_id': line.account_id.id,
519 'journal_id': slip.journal_id.id,
520 'period_id': period_id,
521 'analytic_account_id': False,
526 #Setting Analysis Account for Salary Slip Lines
527 if line.analytic_account_id:
528 rec['analytic_account_id'] = line.analytic_account_id.id
530 rec['analytic_account_id'] = slip.deg_id.account_id.id
532 if line.type == 'allowance' or line.type == 'otherpay':
533 rec['debit'] = amount
534 if not partner.property_account_payable:
535 raise osv.except_osv(_('Integrity Error !'), _('Please Configure Partners Payable Account!!'))
539 'partner_id': partner_id,
541 'account_id': partner.property_account_payable.id,
545 'journal_id': slip.journal_id.id,
546 'period_id': period_id,
549 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
550 elif line.type == 'deduction' or line.type == 'otherdeduct':
551 if not partner.property_account_receivable:
552 raise osv.except_osv(_('Integrity Error !'), _('Please Configure Partners Receivable Account!!'))
553 rec['credit'] = amount
554 total_deduct += amount
558 'partner_id': partner_id,
561 'account_id': partner.property_account_receivable.id,
564 'journal_id': slip.journal_id.id,
565 'period_id': period_id,
568 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
570 line_ids += [movel_pool.create(cr, uid, rec, context=context)]
573 # for contrib in line.category_id.contribute_ids:
574 # _log.debug("%s %s %s %s %s", contrib.name, contrub.code, contrub.amount_type, contrib.contribute_per, line.total)
579 'journal_id': slip.journal_id.id,
580 'period_id': period_id,
583 'narration': 'Adjustment: %s' % (slip.name)
585 adj_move_id = move_pool.create(cr, uid, move, context=context)
586 name = "Adjustment Entry - %s" % (slip.employee_id.name)
587 self.create_voucher(cr, uid, [slip.id], name, adj_move_id)
590 'move_id': adj_move_id,
592 'partner_id': partner_id,
594 'account_id': partner.property_account_receivable.id,
597 'credit': total_deduct,
598 'journal_id': slip.journal_id.id,
599 'period_id': period_id,
602 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
604 'move_id': adj_move_id,
606 'partner_id': partner_id,
608 'account_id': partner.property_account_payable.id,
609 'debit': total_deduct,
612 'journal_id': slip.journal_id.id,
613 'period_id': period_id,
616 line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
620 'move_line_ids':[(6, 0,line_ids)],
622 if not slip.period_id:
623 rec['period_id'] = period_id
625 dates = prev_bounds(slip.date)
626 exp_ids = exp_pool.search(cr, uid, [('date_valid','>=',dates[0]), ('date_valid','<=',dates[1]), ('state','=','invoiced')], context=context)
628 acc = property_pool.get(cr, uid, 'property_account_expense_categ', 'product.category')
629 for exp in exp_pool.browse(cr, uid, exp_ids, context=context):
634 'category_id':exp.category_id.id,
640 payslip_pool.create(cr, uid, exp_res, context=context)
642 self.write(cr, uid, [slip.id], rec, context=context)
648 class hr_payslip_line(osv.osv):
649 _inherit = 'hr.payslip.line'
651 'account_id': fields.many2one('account.account', 'General Account'),
652 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
656 class account_move_link_slip(osv.osv):
658 Account Move Link to Pay Slip
660 _name = 'hr.payslip.account.move'
661 _description = 'Account Move Link to Pay Slip'
663 'name':fields.char('Name', size=256, required=True, readonly=False),
664 'move_id':fields.many2one('account.move', 'Expense Entries', required=False, readonly=True),
665 'slip_id':fields.many2one('hr.payslip', 'Pay Slip', required=False),
666 'sequence': fields.integer('Sequence'),
668 account_move_link_slip()
670 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: