[IMP] hr_payroll_account: improved code for the verify_sheet function
[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 import netsvc
24 from datetime import date, datetime, timedelta
25
26 from osv import fields, osv
27 from tools import config
28 from tools.translate import _
29
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
34     year = when.year
35     if month > 12:
36         month = 1
37         year += 1
38     next_month = date(year, month, 1)
39     prev_end = next_month - timedelta(days=1)
40     return this_first, prev_end
41
42 class hr_payroll_structure(osv.osv):
43     _inherit = 'hr.payroll.structure'
44     _description = 'Salary Structure'
45
46     _columns = {
47         'account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
48     }
49 hr_payroll_structure()
50
51 class hr_employee(osv.osv):
52     '''
53     Employee
54     '''
55     _inherit = 'hr.employee'
56     _description = 'Employee'
57
58     _columns = {
59         'property_bank_account': fields.property(
60             'account.account',
61             type='many2one',
62             relation='account.account',
63             string="Bank Account",
64             method=True,
65             domain="[('type', '=', 'liquidity')]",
66             view_load=True,
67             help="Select Bank Account from where Salary Expense will be Paid, to be used for payslip verification."),
68         'salary_account':fields.property(
69             'account.account',
70             type='many2one',
71             relation='account.account',
72             string="Salary Account",
73             method=True,
74             domain="[('type', '=', 'other')]",
75             view_load=True,
76             help="Expense account when Salary Expense will be recorded"),
77         'employee_account':fields.property(
78             'account.account',
79             type='many2one',
80             relation='account.account',
81             string="Employee Account",
82             method=True,
83             domain="[('type', '=', 'other')]",
84             view_load=True,
85             help="Employee Payable Account"),
86         'analytic_account':fields.property(
87             'account.analytic.account',
88             type='many2one',
89             relation='account.analytic.account',
90             string="Analytic Account",
91             method=True,
92             view_load=True,
93             help="Analytic Account for Salary Analysis"),
94     }
95 hr_employee()
96
97 #class payroll_register(osv.osv):
98 #    _inherit = 'hr.payroll.register'
99 #    _description = 'Payroll Register'
100 #
101 #    _columns = {
102 #        'journal_id': fields.many2one('account.journal', 'Expense 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."),
105 #    }
106 #
107 #    def compute_sheet(self, cr, uid, ids, context=None):
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, context=context)[0]
114 #        emp_ids = emp_pool.search(cr, uid, [])
115 #
116 #        for emp in emp_pool.browse(cr, uid, emp_ids, context=context):
117 #            old_slips = slip_pool.search(cr, uid, [('employee_id','=', emp.id), ('date','=',vals.date)])
118 #            if old_slips:
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)
122 #            else:
123 #                res = {
124 #                    'employee_id':emp.id,
125 #                    'basic':0.0,
126 #                    'register_id':ids[0],
127 #                    'name':vals.name,
128 #                    'date':vals.date,
129 #                    'journal_id':vals.journal_id.id,
130 #                    'bank_journal_id':vals.bank_journal_id.id
131 #                }
132 #                slip_id = slip_pool.create(cr, uid, res)
133 #                wf_service.trg_validate(uid, 'hr.payslip', slip_id, 'compute_sheet', cr)
134 #
135 #        number = self.pool.get('ir.sequence').get(cr, uid, 'salary.register')
136 #        self.write(cr, uid, ids, {'state':'draft', 'number':number})
137 #        return True
138 #
139 #payroll_register()
140
141 #class payroll_advice(osv.osv):
142 #    _inherit = 'hr.payroll.advice'
143 #    _description = 'Bank Advice Note'
144 #
145 #    _columns = {
146 #        'account_id': fields.many2one('account.account', 'Account'),
147 #    }
148 #payroll_advice()
149
150 class contrib_register(osv.osv):
151     _inherit = 'hr.contribution.register'
152     _description = 'Contribution Register'
153
154     def _total_contrib(self, cr, uid, ids, field_names, arg, context=None):
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, context=context).fiscalyear_id
158         res = {}
159 #        for cur in self.browse(cr, uid, ids, context=context):
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)])
162 #
163 #            e_month = 0.0
164 #            c_month = 0.0
165 #            for i in line_pool.browse(cr, uid, current, context=context):
166 #                e_month += i.emp_deduction
167 #                c_month += i.comp_deduction
168 #
169 #            e_year = 0.0
170 #            c_year = 0.0
171 #            for j in line_pool.browse(cr, uid, years, context=context):
172 #                e_year += i.emp_deduction
173 #                c_year += i.comp_deduction
174 #
175 #            res[cur.id]={
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
180 #            }
181         return res
182
183     _columns = {
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)),
188     }
189 contrib_register()
190
191 #class contrib_register_line(osv.osv):
192 #    _inherit = 'hr.contibution.register.line'
193 #    _description = 'Contribution Register Line'
194 #
195 #    _columns = {
196 #        'period_id': fields.many2one('account.period', 'Period'),
197 #    }
198 #contrib_register_line()
199
200 #class hr_holidays_status(osv.osv):
201 #    _inherit = 'hr.holidays.status'
202 #    _columns = {
203 #        'account_id': fields.many2one('account.account', 'Account'),
204 #        'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
205 #    }
206 #hr_holidays_status()
207
208 class hr_payslip(osv.osv):
209     '''
210     Pay Slip
211     '''
212     _inherit = 'hr.payslip'
213     _description = 'Pay Slip'
214
215     _columns = {
216         'journal_id': fields.many2one('account.journal', 'Expense 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."),
222     }
223     
224     def get_payslip_lines(self, cr, uid, contract_ids, payslip_id, context):
225         result = super(hr_payslip, self).get_payslip_lines(cr, uid, contract_ids, payslip_id, context)
226         structure_ids = self.pool.get('hr.contract').get_all_structures(cr, uid, contract_ids, context=context)
227         #get the rules of the structure and thier children
228         rule_ids = self.pool.get('hr.payroll.structure').get_all_rules(cr, uid, structure_ids, context=context)
229         #run the rules by sequence
230         sorted_rule_ids = [id for id, sequence in sorted(rule_ids, key=lambda x:x[1])]
231
232         for rule in self.pool.get('hr.salary.rule').browse(cr, uid, sorted_rule_ids, context=context):
233             for value in result:
234                 if value['salary_rule_id'] == rule.id:
235                     value['account_id'] = rule.account_debit.id,
236         return result
237
238     def create_voucher(self, cr, uid, ids, name, voucher, sequence=5):
239         slip_move = self.pool.get('hr.payslip.account.move')
240         for slip in ids:
241             res = {
242                 'slip_id':slip,
243                 'move_id':voucher,
244                 'sequence':sequence,
245                 'name':name
246             }
247             slip_move.create(cr, uid, res)
248
249     def cancel_sheet(self, cr, uid, ids, context=None):
250         move_pool = self.pool.get('account.move')
251         slip_move = self.pool.get('hr.payslip.account.move')
252         move_ids = []
253         for slip in self.browse(cr, uid, ids, context=context):
254             for line in slip.move_ids:
255                 move_ids.append(line.id)
256                 if line.move_id:
257                     if line.move_id.state == 'posted':
258                         move_pool.button_cancel(cr, uid [line.move_id.id], context)
259                     move_pool.unlink(cr, uid, [line.move_id.id], context=context)
260
261         slip_move.unlink(cr, uid, move_ids, context=context)
262         self.write(cr, uid, ids, {'state':'cancel'}, context=context)
263         return True
264
265     def process_sheet(self, cr, uid, ids, context=None):
266         move_pool = self.pool.get('account.move')
267         movel_pool = self.pool.get('account.move.line')
268         invoice_pool = self.pool.get('account.invoice')
269         fiscalyear_pool = self.pool.get('account.fiscalyear')
270         period_pool = self.pool.get('account.period')
271
272         for slip in self.browse(cr, uid, ids, context=context):
273             if not slip.bank_journal_id or not slip.journal_id:
274                 # Call super method to process sheet if journal_id or bank_journal_id are not specified.
275                 super(hr_payslip, self).process_sheet(cr, uid, [slip.id], context=context)
276                 continue
277             line_ids = []
278             partner = False
279             partner_id = False
280             exp_ids = []
281
282             partner = slip.employee_id.bank_account_id.partner_id
283             partner_id = partner.id
284
285             fiscal_year_ids = fiscalyear_pool.search(cr, uid, [], context=context)
286             if not fiscal_year_ids:
287                 raise osv.except_osv(_('Warning !'), _('Please define fiscal year for perticular contract'))
288             fiscal_year_objs = fiscalyear_pool.read(cr, uid, fiscal_year_ids, ['date_start','date_stop'], context=context)
289             year_exist = False
290             for fiscal_year in fiscal_year_objs:
291                 if ((fiscal_year['date_start'] <= slip.date) and (fiscal_year['date_stop'] >= slip.date)):
292                     year_exist = True
293             if not year_exist:
294                 raise osv.except_osv(_('Warning !'), _('Fiscal Year is not defined for slip date %s') % slip.date)
295             search_periods = period_pool.search(cr, uid, [('date_start','<=',slip.date),('date_stop','>=',slip.date)], context=context)
296             if not search_periods:
297                 raise osv.except_osv(_('Warning !'), _('Period is not defined for slip date %s') % slip.date)
298             period_id = search_periods[0]
299             name = 'Payment of Salary to %s' % (slip.employee_id.name)
300             move = {
301                 'journal_id': slip.bank_journal_id.id,
302                 'period_id': period_id,
303                 'date': slip.date,
304                 'type':'bank_pay_voucher',
305                 'ref':slip.number,
306                 'narration': name
307             }
308             move_id = move_pool.create(cr, uid, move, context=context)
309             self.create_voucher(cr, uid, [slip.id], name, move_id)
310
311             name = "To %s account" % (slip.employee_id.name)
312
313             if not slip.employee_id.property_bank_account.id:
314                 raise osv.except_osv(_('Warning !'), _('Employee Bank Account is not defined for %s') % slip.employee_id.name)
315
316             ded_rec = {
317                 'move_id': move_id,
318                 'name': name,
319                 'date': slip.date,
320                 'account_id': slip.employee_id.property_bank_account.id,
321                 'debit': 0.0,
322                 'credit': slip.total_pay,
323                 'journal_id': slip.journal_id.id,
324                 'period_id': period_id,
325                 'ref': slip.number
326             }
327             line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
328             name = "By %s account" % (slip.employee_id.property_bank_account.name)
329             cre_rec = {
330                 'move_id': move_id,
331                 'name': name,
332                 'partner_id': partner_id,
333                 'date': slip.date,
334                 'account_id': partner.property_account_payable.id,
335                 'debit': slip.total_pay,
336                 'credit': 0.0,
337                 'journal_id': slip.journal_id.id,
338                 'period_id': period_id,
339                 'ref': slip.number
340             }
341             line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
342
343             other_pay = slip.other_pay
344             #Process all Reambuse Entries
345             for line in slip.line_ids:
346                 if line.type == 'otherpay' and line.expanse_id.invoice_id:
347                     if not line.expanse_id.invoice_id.move_id:
348                         raise osv.except_osv(_('Warning !'), _('Please Confirm all Expense Invoice appear for Reimbursement'))
349                     invids = [line.expanse_id.invoice_id.id]
350                     amount = line.total
351                     acc_id = slip.bank_journal_id.default_credit_account_id and slip.bank_journal_id.default_credit_account_id.id
352                     period_id = slip.period_id.id
353                     journal_id = slip.bank_journal_id.id
354                     name = '[%s]-%s' % (slip.number, line.name)
355                     invoice_pool.pay_and_reconcile(cr, uid, invids, amount, acc_id, period_id, journal_id, False, period_id, False, context, name)
356                     other_pay -= amount
357                     #TODO: link this account entries to the Payment Lines also Expense Entries to Account Lines
358                     l_ids = movel_pool.search(cr, uid, [('name','=',name)], context=context)
359                     line_ids += l_ids
360
361                     l_ids = movel_pool.search(cr, uid, [('invoice','=',line.expanse_id.invoice_id.id)], context=context)
362                     exp_ids += l_ids
363
364             #Process for Other payment if any
365             other_move_id = False
366             if slip.other_pay > 0:
367                 narration = 'Payment of Other Payeble amounts to %s' % (slip.employee_id.name)
368                 move = {
369                     'journal_id': slip.bank_journal_id.id,
370                     'period_id': period_id,
371                     'date': slip.date,
372                     'type':'bank_pay_voucher',
373                     'ref':slip.number,
374                     'narration': narration
375                 }
376                 other_move_id = move_pool.create(cr, uid, move, context=context)
377                 self.create_voucher(cr, uid, [slip.id], narration, move_id)
378
379                 name = "To %s account" % (slip.employee_id.name)
380                 ded_rec = {
381                     'move_id':other_move_id,
382                     'name':name,
383                     'date':slip.date,
384                     'account_id':slip.employee_id.property_bank_account.id,
385                     'debit': 0.0,
386                     'credit': other_pay,
387                     'journal_id':slip.journal_id.id,
388                     'period_id':period_id,
389                     'ref':slip.number
390                 }
391                 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
392                 name = "By %s account" % (slip.employee_id.property_bank_account.name)
393                 cre_rec = {
394                     'move_id':other_move_id,
395                     'name':name,
396                     'partner_id':partner_id,
397                     'date':slip.date,
398                     'account_id':partner.property_account_payable.id,
399                     'debit': other_pay,
400                     'credit':0.0,
401                     'journal_id':slip.journal_id.id,
402                     'period_id':period_id,
403                     'ref':slip.number
404                 }
405                 line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
406
407             rec = {
408                 'state':'done',
409                 'move_payment_ids':[(6, 0, line_ids)],
410                 'paid':True
411             }
412             self.write(cr, uid, [slip.id], rec, context=context)
413             for exp_id in exp_ids:
414                 self.write(cr, uid, [slip.id], {'move_line_ids':[(4, exp_id)]}, context=context)
415
416         return True
417
418     def account_check_sheet(self, cr, uid, ids, context=None):
419         self.write(cr, uid, ids, {'state':'accont_check'}, context=context)
420         return True
421
422     def hr_check_sheet(self, cr, uid, ids, context=None):
423         self.write(cr, uid, ids, {'state':'hr_check'}, context=context)
424         return True
425
426     def verify_sheet(self, cr, uid, ids, context=None):
427         move_pool = self.pool.get('account.move')
428         movel_pool = self.pool.get('account.move.line')
429         exp_pool = self.pool.get('hr.expense.expense')
430         fiscalyear_pool = self.pool.get('account.fiscalyear')
431         period_pool = self.pool.get('account.period')
432         property_pool = self.pool.get('ir.property')
433         payslip_pool = self.pool.get('hr.payslip.line')
434
435         for slip in self.browse(cr, uid, ids, context=context):
436             for line in slip.line_ids:
437                 if line.category_id.name == 'Basic':
438                     basic_amt = line.total
439             if not slip.journal_id:
440                 # Call super method to verify sheet if journal_id is not specified.
441                 super(hr_payslip, self).verify_sheet(cr, uid, [slip.id], context=context)
442                 continue
443             total_deduct = 0.0
444
445             line_ids = []
446             partner = False
447             partner_id = False
448
449             if not slip.employee_id.bank_account_id:
450                 raise osv.except_osv(_('Integrity Error !'), _('Please define bank account for %s !') % (slip.employee_id.name))
451
452             if not slip.employee_id.bank_account_id.partner_id:
453                 raise osv.except_osv(_('Integrity Error !'), _('Please define partner in bank account for %s !') % (slip.employee_id.name))
454
455             partner = slip.employee_id.bank_account_id.partner_id
456             partner_id = slip.employee_id.bank_account_id.partner_id.id
457
458             period_id = False
459
460             if slip.period_id:
461                 period_id = slip.period_id.id
462             else:
463                 fiscal_year_ids = fiscalyear_pool.search(cr, uid, [], context=context)
464                 if not fiscal_year_ids:
465                     raise osv.except_osv(_('Warning !'), _('Please define fiscal year for perticular contract'))
466                 fiscal_year_objs = fiscalyear_pool.read(cr, uid, fiscal_year_ids, ['date_start','date_stop'], context=context)
467                 year_exist = False
468                 for fiscal_year in fiscal_year_objs:
469                     if ((fiscal_year['date_start'] <= slip.date_from) and (fiscal_year['date_stop'] >= slip.date_to)):
470                         year_exist = True
471                 if not year_exist:
472                     raise osv.except_osv(_('Warning !'), _('Fiscal Year is not defined for slip date %s') % slip.date)
473                 search_periods = period_pool.search(cr,uid,[('date_start','=',slip.date_from),('date_stop','=',slip.date_to)], context=context)
474                 if not search_periods:
475                     raise osv.except_osv(_('Warning !'), _('Period is not defined for slip date %s') % slip.date)
476                 period_id = search_periods[0]
477
478             move = {
479                 'journal_id': slip.journal_id.id,
480                 'period_id': period_id,
481                 'date': slip.date_from,
482                 'ref':slip.number,
483                 'narration': slip.name
484             }
485             move_id = move_pool.create(cr, uid, move, context=context)
486             self.create_voucher(cr, uid, [slip.id], slip.name, move_id)
487
488             if not slip.employee_id.salary_account.id:
489                 raise osv.except_osv(_('Warning !'), _('Please define Salary Account for %s.') % slip.employee_id.name)
490
491             line = {
492                 'move_id':move_id,
493                 'name': "By Basic Salary / " + slip.employee_id.name,
494                 'date': slip.date_from,
495                 'account_id': slip.employee_id.salary_account.id,
496                 'debit': basic_amt,
497                 'credit': 0.0,
498 #                'quantity':slip.working_days,
499                 'journal_id': slip.journal_id.id,
500                 'period_id': period_id,
501                 'analytic_account_id': False,
502                 'ref':slip.number
503             }
504
505             #Setting Analysis Account for Basic Salary
506             if slip.employee_id.analytic_account:
507                 line['analytic_account_id'] = slip.employee_id.analytic_account.id
508
509             move_line_id = movel_pool.create(cr, uid, line, context=context)
510             line_ids += [move_line_id]
511
512             if not slip.employee_id.employee_account.id:
513                 raise osv.except_osv(_('Warning !'), _('Please define Employee Payable Account for %s.') % slip.employee_id.name)
514
515             line = {
516                 'move_id':move_id,
517                 'name': "To Basic Payble Salary / " + slip.employee_id.name,
518                 'partner_id': partner_id,
519                 'date': slip.date_from,
520                 'account_id': slip.employee_id.employee_account.id,
521                 'debit': 0.0,
522 #                'quantity':slip.working_days,
523                 'credit': basic_amt,
524                 'journal_id': slip.journal_id.id,
525                 'period_id': period_id,
526                 'ref':slip.number
527             }
528             line_ids += [movel_pool.create(cr, uid, line, context=context)]
529
530             for line in slip.line_ids:
531                 name = "[%s] - %s / %s" % (line.code, line.name, slip.employee_id.name)
532                 amount = line.total
533
534 #                if line.type == 'leaves':
535 #                    continue
536
537                 rec = {
538                     'move_id': move_id,
539                     'name': name,
540                     'date': slip.date_from,
541                     'account_id': line.account_id.id,
542                     'debit': 0.0,
543                     'credit': 0.0,
544                     'journal_id': slip.journal_id.id,
545                     'period_id': period_id,
546                     'analytic_account_id': False,
547                     'ref': slip.number,
548                     'quantity': 1
549                 }
550
551                 #Setting Analysis Account for Salary Slip Lines
552                 if line.analytic_account_id:
553                     rec['analytic_account_id'] = line.analytic_account_id.id
554 #                else:
555 #                    rec['analytic_account_id'] = slip.deg_id.account_id.id
556
557 #                if line.type == 'allowance' or line.type == 'otherpay':
558                 if line.category_id.name == 'Allowance' :
559                     rec['debit'] = amount
560                     if not partner.property_account_payable:
561                         raise osv.except_osv(_('Integrity Error !'), _('Please Configure Partners Payable Account!!'))
562                     ded_rec = {
563                         'move_id': move_id,
564                         'name': name,
565                         'partner_id': partner_id,
566                         'date': slip.date_from,
567                         'account_id': partner.property_account_payable.id,
568                         'debit': 0.0,
569                         'quantity': 1,
570                         'credit': amount,
571                         'journal_id': slip.journal_id.id,
572                         'period_id': period_id,
573                         'ref': slip.number
574                     }
575                     line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
576                 elif line.category_id.name  == 'Deduction':
577                     if not partner.property_account_receivable:
578                         raise osv.except_osv(_('Integrity Error !'), _('Please Configure Partners Receivable Account!!'))
579                     rec['credit'] = amount
580                     total_deduct += amount
581                     ded_rec = {
582                         'move_id': move_id,
583                         'name': name,
584                         'partner_id': partner_id,
585                         'date': slip.date_from,
586                         'quantity': 1,
587                         'account_id': partner.property_account_receivable.id,
588                         'debit': amount,
589                         'credit': 0.0,
590                         'journal_id': slip.journal_id.id,
591                         'period_id': period_id,
592                         'ref': slip.number
593                     }
594                     line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
595                 line_ids += [movel_pool.create(cr, uid, rec, context=context)]
596                 # if self._debug:
597                 #    for contrib in line.category_id.contribute_ids:
598                 #       _log.debug("%s %s %s %s %s",  contrib.name, contrub.code, contrub.amount_type, contrib.contribute_per, line.total)
599
600             adj_move_id = False
601             if total_deduct > 0:
602                 move = {
603                     'journal_id': slip.journal_id.id,
604                     'period_id': period_id,
605                     'date': slip.date_from,
606                     'ref':slip.number,
607                     'narration': 'Adjustment: %s' % (slip.name)
608                 }
609                 adj_move_id = move_pool.create(cr, uid, move, context=context)
610                 name = "Adjustment Entry - %s" % (slip.employee_id.name)
611                 self.create_voucher(cr, uid, [slip.id], name, adj_move_id)
612
613                 ded_rec = {
614                     'move_id': adj_move_id,
615                     'name': name,
616                     'partner_id': partner_id,
617                     'date': slip.date_from,
618                     'account_id': partner.property_account_receivable.id,
619                     'debit': 0.0,
620                     'quantity': 1,
621                     'credit': total_deduct,
622                     'journal_id': slip.journal_id.id,
623                     'period_id': period_id,
624                     'ref': slip.number
625                 }
626                 line_ids += [movel_pool.create(cr, uid, ded_rec, context=context)]
627                 cre_rec = {
628                     'move_id': adj_move_id,
629                     'name': name,
630                     'partner_id': partner_id,
631                     'date': slip.date_from,
632                     'account_id': partner.property_account_payable.id,
633                     'debit': total_deduct,
634                     'quantity': 1,
635                     'credit': 0.0,
636                     'journal_id': slip.journal_id.id,
637                     'period_id': period_id,
638                     'ref': slip.number
639                 }
640                 line_ids += [movel_pool.create(cr, uid, cre_rec, context=context)]
641
642             rec = {
643                 'state':'confirm',
644                 'move_line_ids':[(6, 0,line_ids)],
645             }
646             if not slip.period_id:
647                 rec['period_id'] = period_id
648
649 #            dates = prev_bounds(slip.date)
650             exp_ids = exp_pool.search(cr, uid, [('date_valid','>=',slip.date_from), ('date_valid','<=',slip.date_to), ('state','=','invoiced')], context=context)
651             if exp_ids:
652                 acc = property_pool.get(cr, uid, 'property_account_expense_categ', 'product.category')
653                 for exp in exp_pool.browse(cr, uid, exp_ids, context=context):
654                     exp_res = {
655                         'name':exp.name,
656                         'amount_type':'fix',
657                         'type':'otherpay',
658                         'category_id':exp.category_id.id,
659                         'amount':exp.amount,
660                         'slip_id':slip.id,
661                         'expanse_id':exp.id,
662                         'account_id':acc
663                     }
664                     payslip_pool.create(cr, uid, exp_res, context=context)
665
666             self.write(cr, uid, [slip.id], rec, context=context)
667         return True
668
669 hr_payslip()
670
671 class hr_payslip_line(osv.osv):
672     _inherit = 'hr.payslip.line'
673     _columns = {
674         'account_id': fields.many2one('account.account', 'General Account'),
675         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
676     }
677 hr_payslip_line()
678
679 class hr_salary_rule(osv.osv):
680     _inherit = 'hr.salary.rule'
681     _columns = {
682 #        'account_id': fields.many2one('account.account', 'General Account'),
683         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
684         'account_tax_id':fields.many2one('account.tax.code', 'Tax Code'),
685         'account_debit': fields.many2one('account.account', 'Debit Account'),
686         'account_credit': fields.many2one('account.account', 'Credit Account'),
687     }
688 hr_salary_rule()
689
690 class account_move_link_slip(osv.osv):
691     '''
692     Account Move Link to Pay Slip
693     '''
694     _name = 'hr.payslip.account.move'
695     _description = 'Account Move Link to Pay Slip'
696     _columns = {
697         'name':fields.char('Name', size=256, required=True, readonly=False),
698         'move_id':fields.many2one('account.move', 'Expense Entries', required=False, readonly=True),
699         'slip_id':fields.many2one('hr.payslip', 'Pay Slip', required=False),
700         'sequence': fields.integer('Sequence'),
701     }
702 account_move_link_slip()
703
704 class hr_contract(osv.osv):
705   
706     _inherit = 'hr.contract'
707     _description = 'Employee Contract'
708     _columns = {
709         'analytic_account_id':fields.many2one('account.analytic.account', 'Analytic Account'),
710         'journal_id': fields.many2one('account.journal', 'Journal'),
711     }
712 hr_contract()
713
714
715 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: