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, datetime, timedelta
26 from osv import fields, osv
27 from tools import config
28 from tools.translate import _
30 class hr_payslip(osv.osv):
34 _inherit = 'hr.payslip'
35 _description = 'Pay Slip'
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', 'Expense Journal',states={'draft': [('readonly', False)]}, readonly=True, required=True),
40 'move_id': fields.many2one('account.move', 'Accounting Entry', readonly=True),
43 def create(self, cr, uid, vals, context=None):
46 if 'journal_id' in context:
47 vals.update({'journal_id': context.get('journal_id')})
48 return super(hr_payslip, self).create(cr, uid, vals, context=context)
50 def onchange_contract_id(self, cr, uid, ids, date_from, date_to, employee_id=False, contract_id=False, context=None):
51 contract_obj = self.pool.get('hr.contract')
52 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)
53 journal_id = contract_id and contract_obj.browse(cr, uid, contract_id, context=context).journal_id.id or False
54 res['value'].update({'journal_id': journal_id})
57 def cancel_sheet(self, cr, uid, ids, context=None):
58 move_pool = self.pool.get('account.move')
61 for slip in self.browse(cr, uid, ids, context=context):
62 move_ids.append(slip.move_id.id)
63 if slip.move_id.state == 'posted':
64 move_to_cancel.append(slip.move_id.id)
65 move_pool.button_cancel(cr, uid, move_to_cancel, context=context)
66 move_pool.unlink(cr, uid, move_ids, context=context)
67 return super(hr_payslip, self).cancel_sheet(cr, uid, ids, context=context)
69 def process_sheet(self, cr, uid, ids, context=None):
70 move_pool = self.pool.get('account.move')
71 period_pool = self.pool.get('account.period')
72 timenow = time.strftime('%Y-%m-%d')
74 for slip in self.browse(cr, uid, ids, context=context):
78 if not slip.period_id:
79 search_periods = period_pool.find(cr, uid, slip.date_to, context=context)
80 period_id = search_periods[0]
82 period_id = slip.period_id.id
84 name = _('Payslip of %s') % (slip.employee_id.name)
89 'journal_id': slip.journal_id.id,
90 'period_id': period_id,
92 for line in slip.line_ids:
93 amt = slip.credit_note and -line.total or line.total
95 debit_account_id = line.salary_rule_id.account_debit.id
96 credit_account_id = line.salary_rule_id.account_credit.id
100 'partner_id': partner_id,
101 'account_id': debit_account_id,
102 'journal_id': slip.journal_id.id,
103 'period_id': period_id,
104 'debit': amt > 0.0 and amt or 0.0,
105 'credit': amt < 0.0 and -amt or 0.0,
106 'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
107 'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
108 'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
110 credit_line = (0, 0, {
113 'partner_id': partner_id,
114 'account_id': credit_account_id,
115 'journal_id': slip.journal_id.id,
116 'period_id': period_id,
117 'debit': amt < 0.0 and -amt or 0.0,
118 'credit': amt > 0.0 and amt or 0.0,
119 'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
120 'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
121 'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
124 line_ids.append(debit_line)
125 debit_sum += debit_line[2]['debit'] - debit_line[2]['credit']
126 if credit_account_id:
127 line_ids.append(credit_line)
128 credit_sum += credit_line[2]['credit'] - credit_line['debit']
130 if debit_sum > credit_sum:
131 adjust_credit = (0, 0, {
132 'name': _('Adjustment Entry'),
134 'partner_id': partner_id,
135 'account_id': slip.journal_id.default_credit_account_id.id,
136 'journal_id': slip.journal_id.id,
137 'period_id': period_id,
139 'credit': debit_sum - credit_sum,
141 line_ids.append(adjust_credit)
142 elif debit_sum < credit_sum:
143 adjust_debit = (0, 0, {
144 'name': _('Adjustment Entry'),
146 'partner_id': partner_id,
147 'account_id': slip.journal_id.default_debit_account_id.id,
148 'journal_id': slip.journal_id.id,
149 'period_id': period_id,
150 'debit': credit_sum - debit_sum,
153 line_ids.append(adjust_debit)
155 move.update({'line_id': line_ids})
156 move_id = move_pool.create(cr, uid, move, context=context)
157 self.write(cr, uid, [slip.id], {'move_id': move_id}, context=context)
158 if slip.journal_id.entry_posted:
159 move_pool.post(cr, uid, [move_id], context=context)
160 return super(hr_payslip, self).process_sheet(cr, uid, [slip.id], context=context)
164 class hr_salary_rule(osv.osv):
165 _inherit = 'hr.salary.rule'
167 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
168 'account_tax_id':fields.many2one('account.tax.code', 'Tax Code'),
169 'account_debit': fields.many2one('account.account', 'Debit Account'),
170 'account_credit': fields.many2one('account.account', 'Credit Account'),
174 class hr_contract(osv.osv):
176 _inherit = 'hr.contract'
177 _description = 'Employee Contract'
179 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
180 'journal_id': fields.many2one('account.journal', 'Salary Journal'),
184 class hr_payslip_run(osv.osv):
186 _inherit = 'hr.payslip.run'
187 _description = 'Payslip Run'
189 'journal_id': fields.many2one('account.journal', 'Expense Journal', states={'draft': [('readonly', False)]}, readonly=True, required=True),
194 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: