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 datetime import date, datetime, timedelta
25 from openerp.osv import fields, osv
26 from openerp.tools import float_compare, float_is_zero
27 from openerp.tools.translate import _
29 class hr_payslip(osv.osv):
33 _inherit = 'hr.payslip'
34 _description = 'Pay Slip'
37 '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."),
38 'journal_id': fields.many2one('account.journal', 'Salary Journal',states={'draft': [('readonly', False)]}, readonly=True, required=True),
39 'move_id': fields.many2one('account.move', 'Accounting Entry', readonly=True, copy=False),
42 def _get_default_journal(self, cr, uid, context=None):
43 model_data = self.pool.get('ir.model.data')
44 res = model_data.search(cr, uid, [('name', '=', 'expenses_journal')])
46 return model_data.browse(cr, uid, res[0]).res_id
50 'journal_id': _get_default_journal,
53 def create(self, cr, uid, vals, context=None):
56 if 'journal_id' in context:
57 vals.update({'journal_id': context.get('journal_id')})
58 return super(hr_payslip, self).create(cr, uid, vals, context=context)
60 def onchange_contract_id(self, cr, uid, ids, date_from, date_to, employee_id=False, contract_id=False, context=None):
61 contract_obj = self.pool.get('hr.contract')
62 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)
63 journal_id = contract_id and contract_obj.browse(cr, uid, contract_id, context=context).journal_id.id or False
64 res['value'].update({'journal_id': journal_id})
67 def cancel_sheet(self, cr, uid, ids, context=None):
68 move_pool = self.pool.get('account.move')
71 for slip in self.browse(cr, uid, ids, context=context):
73 move_ids.append(slip.move_id.id)
74 if slip.move_id.state == 'posted':
75 move_to_cancel.append(slip.move_id.id)
76 move_pool.button_cancel(cr, uid, move_to_cancel, context=context)
77 move_pool.unlink(cr, uid, move_ids, context=context)
78 return super(hr_payslip, self).cancel_sheet(cr, uid, ids, context=context)
80 def process_sheet(self, cr, uid, ids, context=None):
81 move_pool = self.pool.get('account.move')
82 period_pool = self.pool.get('account.period')
83 precision = self.pool.get('decimal.precision').precision_get(cr, uid, 'Payroll')
84 timenow = time.strftime('%Y-%m-%d')
86 for slip in self.browse(cr, uid, ids, context=context):
90 if not slip.period_id:
91 search_periods = period_pool.find(cr, uid, slip.date_to, context=context)
92 period_id = search_periods[0]
94 period_id = slip.period_id.id
96 default_partner_id = slip.employee_id.address_home_id.id
97 name = _('Payslip of %s') % (slip.employee_id.name)
102 'journal_id': slip.journal_id.id,
103 'period_id': period_id,
105 for line in slip.details_by_salary_rule_category:
106 amt = slip.credit_note and -line.total or line.total
107 if float_is_zero(amt, precision_digits=precision):
109 partner_id = line.salary_rule_id.register_id.partner_id and line.salary_rule_id.register_id.partner_id.id or default_partner_id
110 debit_account_id = line.salary_rule_id.account_debit.id
111 credit_account_id = line.salary_rule_id.account_credit.id
115 debit_line = (0, 0, {
118 '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,
119 'account_id': debit_account_id,
120 'journal_id': slip.journal_id.id,
121 'period_id': period_id,
122 'debit': amt > 0.0 and amt or 0.0,
123 'credit': amt < 0.0 and -amt or 0.0,
124 'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
125 'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
126 'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
128 line_ids.append(debit_line)
129 debit_sum += debit_line[2]['debit'] - debit_line[2]['credit']
131 if credit_account_id:
133 credit_line = (0, 0, {
136 '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,
137 'account_id': credit_account_id,
138 'journal_id': slip.journal_id.id,
139 'period_id': period_id,
140 'debit': amt < 0.0 and -amt or 0.0,
141 'credit': amt > 0.0 and amt or 0.0,
142 'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
143 'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
144 'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
146 line_ids.append(credit_line)
147 credit_sum += credit_line[2]['credit'] - credit_line[2]['debit']
149 if float_compare(credit_sum, debit_sum, precision_digits=precision) == -1:
150 acc_id = slip.journal_id.default_credit_account_id.id
152 raise osv.except_osv(_('Configuration Error!'),_('The Expense Journal "%s" has not properly configured the Credit Account!')%(slip.journal_id.name))
153 adjust_credit = (0, 0, {
154 'name': _('Adjustment Entry'),
157 'account_id': acc_id,
158 'journal_id': slip.journal_id.id,
159 'period_id': period_id,
161 'credit': debit_sum - credit_sum,
163 line_ids.append(adjust_credit)
165 elif float_compare(debit_sum, credit_sum, precision_digits=precision) == -1:
166 acc_id = slip.journal_id.default_debit_account_id.id
168 raise osv.except_osv(_('Configuration Error!'),_('The Expense Journal "%s" has not properly configured the Debit Account!')%(slip.journal_id.name))
169 adjust_debit = (0, 0, {
170 'name': _('Adjustment Entry'),
173 'account_id': acc_id,
174 'journal_id': slip.journal_id.id,
175 'period_id': period_id,
176 'debit': credit_sum - debit_sum,
179 line_ids.append(adjust_debit)
181 move.update({'line_id': line_ids})
182 move_id = move_pool.create(cr, uid, move, context=context)
183 self.write(cr, uid, [slip.id], {'move_id': move_id, 'period_id' : period_id}, context=context)
184 if slip.journal_id.entry_posted:
185 move_pool.post(cr, uid, [move_id], context=context)
186 return super(hr_payslip, self).process_sheet(cr, uid, [slip.id], context=context)
189 class hr_salary_rule(osv.osv):
190 _inherit = 'hr.salary.rule'
192 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
193 'account_tax_id':fields.many2one('account.tax.code', 'Tax Code'),
194 'account_debit': fields.many2one('account.account', 'Debit Account'),
195 'account_credit': fields.many2one('account.account', 'Credit Account'),
198 class hr_contract(osv.osv):
200 _inherit = 'hr.contract'
201 _description = 'Employee Contract'
203 'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
204 'journal_id': fields.many2one('account.journal', 'Salary Journal'),
207 class hr_payslip_run(osv.osv):
209 _inherit = 'hr.payslip.run'
210 _description = 'Payslip Run'
212 'journal_id': fields.many2one('account.journal', 'Salary Journal', states={'draft': [('readonly', False)]}, readonly=True, required=True),
215 def _get_default_journal(self, cr, uid, context=None):
216 model_data = self.pool.get('ir.model.data')
217 res = model_data.search(cr, uid, [('name', '=', 'expenses_journal')])
219 return model_data.browse(cr, uid, res[0]).res_id
223 'journal_id': _get_default_journal,
227 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: