2010-06-18 20:30:58 +00:00
#-*- coding:utf-8 -*-
##############################################################################
#
2010-07-27 07:11:45 +00:00
# OpenERP, Open Source Management Solution
2010-06-18 20:30:58 +00:00
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# d$
#
# This program is free software: you can redistribute it and/or modify
2010-10-28 06:54:18 +00:00
# it under the terms of the GNU Affero General Public License as published by
2010-06-18 20:30:58 +00:00
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2010-10-28 06:54:18 +00:00
# GNU Affero General Public License for more details.
2010-06-18 20:30:58 +00:00
#
2010-10-28 06:54:18 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-06-18 20:30:58 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import time
2012-12-06 14:56:32 +00:00
from openerp import netsvc
2010-07-27 07:11:45 +00:00
from datetime import date , datetime , timedelta
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv
from openerp . tools import config
from openerp . tools . translate import _
2010-06-18 20:30:58 +00:00
class hr_payslip ( osv . osv ) :
'''
Pay Slip
'''
_inherit = ' hr.payslip '
_description = ' Pay Slip '
2010-07-27 07:11:45 +00:00
2010-06-18 20:30:58 +00:00
_columns = {
2011-04-21 07:22:56 +00:00
' 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. " ) ,
2012-10-03 12:49:11 +00:00
' journal_id ' : fields . many2one ( ' account.journal ' , ' Salary Journal ' , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , readonly = True , required = True ) ,
2011-05-12 11:01:21 +00:00
' move_id ' : fields . many2one ( ' account.move ' , ' Accounting Entry ' , readonly = True ) ,
2010-06-18 20:30:58 +00:00
}
2011-11-17 12:48:11 +00:00
2011-11-30 14:01:07 +00:00
def _get_default_journal ( self , cr , uid , context = None ) :
model_data = self . pool . get ( ' ir.model.data ' )
res = model_data . search ( cr , uid , [ ( ' name ' , ' = ' , ' expenses_journal ' ) ] )
if res :
return model_data . browse ( cr , uid , res [ 0 ] ) . res_id
return False
2011-11-22 12:41:29 +00:00
_defaults = {
2011-11-30 14:01:07 +00:00
' journal_id ' : _get_default_journal ,
2011-11-22 12:41:29 +00:00
}
2011-11-17 12:48:11 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
if default is None :
default = { }
2011-11-17 19:14:55 +00:00
default [ ' move_id ' ] = False
2011-11-17 12:48:11 +00:00
return super ( hr_payslip , self ) . copy ( cr , uid , id , default , context = context )
2011-09-15 05:45:39 +00:00
def create ( self , cr , uid , vals , context = None ) :
if context is None :
context = { }
if ' journal_id ' in context :
vals . update ( { ' journal_id ' : context . get ( ' journal_id ' ) } )
return super ( hr_payslip , self ) . create ( cr , uid , vals , context = context )
2011-04-21 07:22:56 +00:00
2011-04-20 13:25:08 +00:00
def onchange_contract_id ( self , cr , uid , ids , date_from , date_to , employee_id = False , contract_id = False , context = None ) :
contract_obj = self . pool . get ( ' hr.contract ' )
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 )
2011-05-12 11:47:49 +00:00
journal_id = contract_id and contract_obj . browse ( cr , uid , contract_id , context = context ) . journal_id . id or False
2011-04-20 13:25:08 +00:00
res [ ' value ' ] . update ( { ' journal_id ' : journal_id } )
return res
2011-04-20 11:22:22 +00:00
2010-07-27 07:11:45 +00:00
def cancel_sheet ( self , cr , uid , ids , context = None ) :
2010-06-18 20:30:58 +00:00
move_pool = self . pool . get ( ' account.move ' )
2010-07-02 10:37:15 +00:00
move_ids = [ ]
2011-05-12 11:01:21 +00:00
move_to_cancel = [ ]
2010-07-27 07:11:45 +00:00
for slip in self . browse ( cr , uid , ids , context = context ) :
2011-10-07 13:49:26 +00:00
if slip . move_id :
move_ids . append ( slip . move_id . id )
if slip . move_id . state == ' posted ' :
move_to_cancel . append ( slip . move_id . id )
2011-05-12 11:01:21 +00:00
move_pool . button_cancel ( cr , uid , move_to_cancel , context = context )
move_pool . unlink ( cr , uid , move_ids , context = context )
return super ( hr_payslip , self ) . cancel_sheet ( cr , uid , ids , context = context )
2010-07-27 07:11:45 +00:00
def process_sheet ( self , cr , uid , ids , context = None ) :
2010-06-18 20:30:58 +00:00
move_pool = self . pool . get ( ' account.move ' )
2010-07-27 07:11:45 +00:00
period_pool = self . pool . get ( ' account.period ' )
2011-05-12 11:47:49 +00:00
timenow = time . strftime ( ' % Y- % m- %d ' )
2010-07-27 07:11:45 +00:00
for slip in self . browse ( cr , uid , ids , context = context ) :
2010-06-18 20:30:58 +00:00
line_ids = [ ]
2011-05-26 13:10:45 +00:00
debit_sum = 0.0
credit_sum = 0.0
2011-05-12 11:01:21 +00:00
if not slip . period_id :
2011-07-11 22:40:10 +00:00
search_periods = period_pool . find ( cr , uid , slip . date_to , context = context )
2010-07-27 07:11:45 +00:00
period_id = search_periods [ 0 ]
2011-05-12 11:01:21 +00:00
else :
period_id = slip . period_id . id
2010-07-27 07:11:45 +00:00
2012-06-20 13:04:49 +00:00
default_partner_id = slip . employee_id . address_home_id . id
2011-05-12 11:01:21 +00:00
name = _ ( ' Payslip of %s ' ) % ( slip . employee_id . name )
2010-06-18 20:30:58 +00:00
move = {
2011-05-31 07:02:28 +00:00
' narration ' : name ,
' date ' : timenow ,
' ref ' : slip . number ,
2010-06-18 20:30:58 +00:00
' journal_id ' : slip . journal_id . id ,
2010-07-27 07:11:45 +00:00
' period_id ' : period_id ,
2010-06-18 20:30:58 +00:00
}
2012-06-21 06:38:00 +00:00
for line in slip . details_by_salary_rule_category :
2011-05-16 10:32:31 +00:00
amt = slip . credit_note and - line . total or line . total
2012-06-20 13:04:49 +00:00
partner_id = line . salary_rule_id . register_id . partner_id and line . salary_rule_id . register_id . partner_id . id or default_partner_id
2011-05-16 10:32:31 +00:00
debit_account_id = line . salary_rule_id . account_debit . id
credit_account_id = line . salary_rule_id . account_credit . id
2011-11-17 12:48:11 +00:00
2011-11-04 08:27:55 +00:00
if debit_account_id :
2011-11-17 12:48:11 +00:00
2011-11-04 08:27:55 +00:00
debit_line = ( 0 , 0 , {
2011-05-16 10:32:31 +00:00
' name ' : line . name ,
' date ' : timenow ,
2012-06-20 13:04:49 +00:00
' 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 ,
2011-05-31 07:02:28 +00:00
' account_id ' : debit_account_id ,
2011-05-16 10:32:31 +00:00
' journal_id ' : slip . journal_id . id ,
' period_id ' : period_id ,
2011-05-31 07:02:28 +00:00
' debit ' : amt > 0.0 and amt or 0.0 ,
' credit ' : amt < 0.0 and - amt or 0.0 ,
2011-07-11 22:40:10 +00:00
' analytic_account_id ' : line . salary_rule_id . analytic_account_id and line . salary_rule_id . analytic_account_id . id or False ,
' tax_code_id ' : line . salary_rule_id . account_tax_id and line . salary_rule_id . account_tax_id . id or False ,
' tax_amount ' : line . salary_rule_id . account_tax_id and amt or 0.0 ,
2011-05-16 10:32:31 +00:00
} )
2011-11-04 08:27:55 +00:00
line_ids . append ( debit_line )
debit_sum + = debit_line [ 2 ] [ ' debit ' ] - debit_line [ 2 ] [ ' credit ' ]
2011-11-17 12:48:11 +00:00
2011-11-04 08:27:55 +00:00
if credit_account_id :
2011-11-17 12:48:11 +00:00
2011-11-04 08:27:55 +00:00
credit_line = ( 0 , 0 , {
2011-05-31 07:02:28 +00:00
' name ' : line . name ,
2011-05-16 10:32:31 +00:00
' date ' : timenow ,
2012-06-20 13:04:49 +00:00
' 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 ,
2011-05-16 10:32:31 +00:00
' account_id ' : credit_account_id ,
2011-05-31 07:02:28 +00:00
' journal_id ' : slip . journal_id . id ,
' period_id ' : period_id ,
2011-05-16 10:32:31 +00:00
' debit ' : amt < 0.0 and - amt or 0.0 ,
' credit ' : amt > 0.0 and amt or 0.0 ,
2011-07-11 22:40:10 +00:00
' analytic_account_id ' : line . salary_rule_id . analytic_account_id and line . salary_rule_id . analytic_account_id . id or False ,
' tax_code_id ' : line . salary_rule_id . account_tax_id and line . salary_rule_id . account_tax_id . id or False ,
' tax_amount ' : line . salary_rule_id . account_tax_id and amt or 0.0 ,
2011-05-16 10:32:31 +00:00
} )
line_ids . append ( credit_line )
2011-08-31 13:27:31 +00:00
credit_sum + = credit_line [ 2 ] [ ' credit ' ] - credit_line [ 2 ] [ ' debit ' ]
2011-05-31 07:02:28 +00:00
2011-05-26 13:10:45 +00:00
if debit_sum > credit_sum :
2011-11-04 08:27:55 +00:00
acc_id = slip . journal_id . default_credit_account_id . id
if not acc_id :
raise osv . except_osv ( _ ( ' Configuration Error! ' ) , _ ( ' The Expense Journal " %s " has not properly configured the Credit Account! ' ) % ( slip . journal_id . name ) )
2011-05-31 07:02:28 +00:00
adjust_credit = ( 0 , 0 , {
2011-05-30 10:06:45 +00:00
' name ' : _ ( ' Adjustment Entry ' ) ,
2011-05-31 07:02:28 +00:00
' date ' : timenow ,
2012-06-20 13:04:49 +00:00
' partner_id ' : False ,
2011-11-04 08:27:55 +00:00
' account_id ' : acc_id ,
2011-05-31 07:02:28 +00:00
' journal_id ' : slip . journal_id . id ,
' period_id ' : period_id ,
2011-05-26 13:10:45 +00:00
' debit ' : 0.0 ,
' credit ' : debit_sum - credit_sum ,
} )
2011-05-27 05:16:11 +00:00
line_ids . append ( adjust_credit )
2011-11-17 12:48:11 +00:00
2011-05-30 10:06:45 +00:00
elif debit_sum < credit_sum :
2011-11-04 08:27:55 +00:00
acc_id = slip . journal_id . default_debit_account_id . id
if not acc_id :
raise osv . except_osv ( _ ( ' Configuration Error! ' ) , _ ( ' The Expense Journal " %s " has not properly configured the Debit Account! ' ) % ( slip . journal_id . name ) )
2011-05-31 07:02:28 +00:00
adjust_debit = ( 0 , 0 , {
2011-05-30 10:06:45 +00:00
' name ' : _ ( ' Adjustment Entry ' ) ,
2011-05-31 07:02:28 +00:00
' date ' : timenow ,
2012-06-20 13:04:49 +00:00
' partner_id ' : False ,
2011-11-04 08:27:55 +00:00
' account_id ' : acc_id ,
2011-05-31 07:02:28 +00:00
' journal_id ' : slip . journal_id . id ,
' period_id ' : period_id ,
2011-05-27 05:29:21 +00:00
' debit ' : credit_sum - debit_sum ,
2011-07-11 22:40:10 +00:00
' credit ' : 0.0 ,
2011-05-26 13:10:45 +00:00
} )
2011-05-27 05:16:11 +00:00
line_ids . append ( adjust_debit )
2011-05-12 11:47:49 +00:00
move . update ( { ' line_id ' : line_ids } )
2011-05-12 11:01:21 +00:00
move_id = move_pool . create ( cr , uid , move , context = context )
2011-11-22 12:17:04 +00:00
self . write ( cr , uid , [ slip . id ] , { ' move_id ' : move_id , ' period_id ' : period_id } , context = context )
2011-05-31 07:02:28 +00:00
if slip . journal_id . entry_posted :
move_pool . post ( cr , uid , [ move_id ] , context = context )
2011-05-12 11:01:21 +00:00
return super ( hr_payslip , self ) . process_sheet ( cr , uid , [ slip . id ] , context = context )
2011-05-12 11:47:49 +00:00
hr_payslip ( )
2010-06-18 20:30:58 +00:00
2011-04-13 13:16:28 +00:00
class hr_salary_rule ( osv . osv ) :
_inherit = ' hr.salary.rule '
_columns = {
' analytic_account_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' ) ,
' account_tax_id ' : fields . many2one ( ' account.tax.code ' , ' Tax Code ' ) ,
' account_debit ' : fields . many2one ( ' account.account ' , ' Debit Account ' ) ,
' account_credit ' : fields . many2one ( ' account.account ' , ' Credit Account ' ) ,
}
hr_salary_rule ( )
class hr_contract ( osv . osv ) :
2011-04-20 11:22:22 +00:00
2011-04-13 13:16:28 +00:00
_inherit = ' hr.contract '
_description = ' Employee Contract '
_columns = {
' analytic_account_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' ) ,
2011-05-12 08:01:47 +00:00
' journal_id ' : fields . many2one ( ' account.journal ' , ' Salary Journal ' ) ,
2011-04-13 13:16:28 +00:00
}
hr_contract ( )
2011-09-15 05:16:36 +00:00
class hr_payslip_run ( osv . osv ) :
_inherit = ' hr.payslip.run '
_description = ' Payslip Run '
_columns = {
2012-10-03 12:49:11 +00:00
' journal_id ' : fields . many2one ( ' account.journal ' , ' Salary Journal ' , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , readonly = True , required = True ) ,
2011-09-15 05:16:36 +00:00
}
2012-08-16 12:44:07 +00:00
def _get_default_journal ( self , cr , uid , context = None ) :
model_data = self . pool . get ( ' ir.model.data ' )
res = model_data . search ( cr , uid , [ ( ' name ' , ' = ' , ' expenses_journal ' ) ] )
if res :
return model_data . browse ( cr , uid , res [ 0 ] ) . res_id
return False
_defaults = {
' journal_id ' : _get_default_journal ,
}
2011-09-15 05:16:36 +00:00
hr_payslip_run ( )
2011-04-13 13:16:28 +00:00
2010-11-18 16:21:31 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: