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 ##############################################################################
23 from openerp import netsvc
24 from datetime import date, datetime, timedelta
26 from openerp.osv import fields, osv
27 from openerp.tools import float_compare, float_is_zero
28 from openerp.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', 'Salary Journal',states={'draft': [('readonly', False)]}, readonly=True, required=True),
40 'move_id': fields.many2one('account.move', 'Accounting Entry', readonly=True),
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')])
47 return model_data.browse(cr, uid, res[0]).res_id
51 'journal_id': _get_default_journal,
54 def copy(self, cr, uid, id, default=None, context=None):
57 default['move_id'] = False
58 return super(hr_payslip, self).copy(cr, uid, id, default, context=context)
60 def create(self, cr, uid, vals, context=None):
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)
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})
74 def cancel_sheet(self, cr, uid, ids, context=None):
75 move_pool = self.pool.get('account.move')
78 for slip in self.browse(cr, uid, ids, context=context):
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)
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')
93 for slip in self.browse(cr, uid, ids, context=context):
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]
102 period_id = slip.period_id.id
104 default_partner_id = slip.employee_id.address_home_id.id
105 name = _('Payslip of %s') % (slip.employee_id.name)
110 'journal_id': slip.journal_id.id,
111 'period_id': period_id,
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):
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
123 debit_line = (0, 0, {
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,
136 line_ids.append(debit_line)
137 debit_sum += debit_line[2]['debit'] - debit_line[2]['credit']
139 if credit_account_id:
141 credit_line = (0, 0, {
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,
154 line_ids.append(credit_line)
155 credit_sum += credit_line[2]['credit'] - credit_line[2]['debit']
157 if float_compare(credit_sum, debit_sum, precision_digits=precision) == -1:
158 acc_id = slip.journal_id.default_credit_account_id.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'),
165 'account_id': acc_id,
166 'journal_id': slip.journal_id.id,
167 'period_id': period_id,
169 'credit': debit_sum - credit_sum,
171 line_ids.append(adjust_credit)
173 elif float_compare(debit_sum, credit_sum, precision_digits=precision) == -1:
174 acc_id = slip.journal_id.default_debit_account_id.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'),
181 'account_id': acc_id,
182 'journal_id': slip.journal_id.id,
183 'period_id': period_id,
184 'debit': credit_sum - debit_sum,
187 line_ids.append(adjust_debit)
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)
198 class hr_salary_rule(osv.osv):
199 _inherit = 'hr.salary.rule'
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'),
208 class hr_contract(osv.osv):
210 _inherit = 'hr.contract'
211 _description = 'Employee Contract'
213 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
214 'journal_id': fields.many2one('account.journal', 'Salary Journal'),
218 class hr_payslip_run(osv.osv):
220 _inherit = 'hr.payslip.run'
221 _description = 'Payslip Run'
223 'journal_id': fields.many2one('account.journal', 'Salary Journal', states={'draft': [('readonly', False)]}, readonly=True, required=True),
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')])
230 return model_data.browse(cr, uid, res[0]).res_id
234 'journal_id': _get_default_journal,
239 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: