[IMP]account_analytic_analysis: set ids intes of id for write bcoz in project there...
[odoo/odoo.git] / addons / hr_payroll_account / hr_payroll_account.py
1 #-*- coding:utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    d$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22 import time
23 from openerp import netsvc
24 from datetime import date, datetime, timedelta
25
26 from openerp.osv import fields, osv
27 from openerp.tools import config
28 from openerp.tools.translate import _
29
30 class hr_payslip(osv.osv):
31     '''
32     Pay Slip
33     '''
34     _inherit = 'hr.payslip'
35     _description = 'Pay Slip'
36
37     _columns = {
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),
41     }
42
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')])
46         if res:
47             return model_data.browse(cr, uid, res[0]).res_id
48         return False
49
50     _defaults = {
51         'journal_id': _get_default_journal,
52     }
53
54     def copy(self, cr, uid, id, default=None, context=None):
55         if default is None:
56             default = {}
57         default['move_id'] = False
58         return super(hr_payslip, self).copy(cr, uid, id, default, context=context)
59
60     def create(self, cr, uid, vals, context=None):
61         if context is None:
62             context = {}
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)
66
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})
72         return res
73
74     def cancel_sheet(self, cr, uid, ids, context=None):
75         move_pool = self.pool.get('account.move')
76         move_ids = []
77         move_to_cancel = []
78         for slip in self.browse(cr, uid, ids, context=context):
79             if slip.move_id:
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)
86
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         timenow = time.strftime('%Y-%m-%d')
91
92         for slip in self.browse(cr, uid, ids, context=context):
93             line_ids = []
94             debit_sum = 0.0
95             credit_sum = 0.0
96             if not slip.period_id:
97                 search_periods = period_pool.find(cr, uid, slip.date_to, context=context)
98                 period_id = search_periods[0]
99             else:
100                 period_id = slip.period_id.id
101
102             default_partner_id = slip.employee_id.address_home_id.id
103             name = _('Payslip of %s') % (slip.employee_id.name)
104             move = {
105                 'narration': name,
106                 'date': timenow,
107                 'ref': slip.number,
108                 'journal_id': slip.journal_id.id,
109                 'period_id': period_id,
110             }
111             for line in slip.details_by_salary_rule_category:
112                 amt = slip.credit_note and -line.total or line.total
113                 partner_id = line.salary_rule_id.register_id.partner_id and line.salary_rule_id.register_id.partner_id.id or default_partner_id
114                 debit_account_id = line.salary_rule_id.account_debit.id
115                 credit_account_id = line.salary_rule_id.account_credit.id
116
117                 if debit_account_id:
118
119                     debit_line = (0, 0, {
120                     'name': line.name,
121                     'date': timenow,
122                     '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,
123                     'account_id': debit_account_id,
124                     'journal_id': slip.journal_id.id,
125                     'period_id': period_id,
126                     'debit': amt > 0.0 and amt or 0.0,
127                     'credit': amt < 0.0 and -amt or 0.0,
128                     'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
129                     'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
130                     'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
131                 })
132                     line_ids.append(debit_line)
133                     debit_sum += debit_line[2]['debit'] - debit_line[2]['credit']
134
135                 if credit_account_id:
136
137                     credit_line = (0, 0, {
138                     'name': line.name,
139                     'date': timenow,
140                     '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,
141                     'account_id': credit_account_id,
142                     'journal_id': slip.journal_id.id,
143                     'period_id': period_id,
144                     'debit': amt < 0.0 and -amt or 0.0,
145                     'credit': amt > 0.0 and amt or 0.0,
146                     'analytic_account_id': line.salary_rule_id.analytic_account_id and line.salary_rule_id.analytic_account_id.id or False,
147                     'tax_code_id': line.salary_rule_id.account_tax_id and line.salary_rule_id.account_tax_id.id or False,
148                     'tax_amount': line.salary_rule_id.account_tax_id and amt or 0.0,
149                 })
150                     line_ids.append(credit_line)
151                     credit_sum += credit_line[2]['credit'] - credit_line[2]['debit']
152
153             if debit_sum > credit_sum:
154                 acc_id = slip.journal_id.default_credit_account_id.id
155                 if not acc_id:
156                     raise osv.except_osv(_('Configuration Error!'),_('The Expense Journal "%s" has not properly configured the Credit Account!')%(slip.journal_id.name))
157                 adjust_credit = (0, 0, {
158                     'name': _('Adjustment Entry'),
159                     'date': timenow,
160                     'partner_id': False,
161                     'account_id': acc_id,
162                     'journal_id': slip.journal_id.id,
163                     'period_id': period_id,
164                     'debit': 0.0,
165                     'credit': debit_sum - credit_sum,
166                 })
167                 line_ids.append(adjust_credit)
168
169             elif debit_sum < credit_sum:
170                 acc_id = slip.journal_id.default_debit_account_id.id
171                 if not acc_id:
172                     raise osv.except_osv(_('Configuration Error!'),_('The Expense Journal "%s" has not properly configured the Debit Account!')%(slip.journal_id.name))
173                 adjust_debit = (0, 0, {
174                     'name': _('Adjustment Entry'),
175                     'date': timenow,
176                     'partner_id': False,
177                     'account_id': acc_id,
178                     'journal_id': slip.journal_id.id,
179                     'period_id': period_id,
180                     'debit': credit_sum - debit_sum,
181                     'credit': 0.0,
182                 })
183                 line_ids.append(adjust_debit)
184             move.update({'line_id': line_ids})
185             move_id = move_pool.create(cr, uid, move, context=context)
186             self.write(cr, uid, [slip.id], {'move_id': move_id, 'period_id' : period_id}, context=context)
187             if slip.journal_id.entry_posted:
188                 move_pool.post(cr, uid, [move_id], context=context)
189         return super(hr_payslip, self).process_sheet(cr, uid, [slip.id], context=context)
190
191 hr_payslip()
192
193 class hr_salary_rule(osv.osv):
194     _inherit = 'hr.salary.rule'
195     _columns = {
196         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
197         'account_tax_id':fields.many2one('account.tax.code', 'Tax Code'),
198         'account_debit': fields.many2one('account.account', 'Debit Account'),
199         'account_credit': fields.many2one('account.account', 'Credit Account'),
200     }
201 hr_salary_rule()
202
203 class hr_contract(osv.osv):
204
205     _inherit = 'hr.contract'
206     _description = 'Employee Contract'
207     _columns = {
208         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
209         'journal_id': fields.many2one('account.journal', 'Salary Journal'),
210     }
211 hr_contract()
212
213 class hr_payslip_run(osv.osv):
214
215     _inherit = 'hr.payslip.run'
216     _description = 'Payslip Run'
217     _columns = {
218         'journal_id': fields.many2one('account.journal', 'Salary Journal', states={'draft': [('readonly', False)]}, readonly=True, required=True),
219     }
220
221     def _get_default_journal(self, cr, uid, context=None):
222         model_data = self.pool.get('ir.model.data')
223         res = model_data.search(cr, uid, [('name', '=', 'expenses_journal')])
224         if res:
225             return model_data.browse(cr, uid, res[0]).res_id
226         return False
227
228     _defaults = {
229         'journal_id': _get_default_journal,
230     }
231
232 hr_payslip_run()
233
234 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: