2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2011 OpenERP SA (<http://openerp.com>). All Rights Reserved
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 from datetime import datetime
24 from dateutil.relativedelta import relativedelta
25 from calendar import isleap
27 from openerp.tools.translate import _
28 from openerp.osv import fields, osv
29 import openerp.addons.decimal_precision as dp
31 DATETIME_FORMAT = "%Y-%m-%d"
33 class hr_contract(osv.osv):
35 Employee contract allows to add different values in fields.
36 Fields are used in salary rule computation.
39 _inherit = 'hr.contract'
40 _description = 'HR Contract'
43 'tds': fields.float('TDS', digits_compute=dp.get_precision('Payroll'), help="Amount for Tax Deduction at Source"),
44 'driver_salay': fields.boolean('Driver Salary', help="Check this box if you provide allowance for driver"),
45 'medical_insurance': fields.float('Medical Insurance', digits_compute=dp.get_precision('Payroll'), help="Deduction towards company provided medical insurance"),
46 'voluntary_provident_fund': fields.float('Voluntary Provident Fund (%)', digits_compute=dp.get_precision('Payroll'), help="VPF is a safe option wherein you can contribute more than the PF ceiling of 12% that has been mandated by the government and VPF computed as percentage(%)"),
47 'house_rent_allowance_metro_nonmetro': fields.float('House Rent Allowance (%)', digits_compute=dp.get_precision('Payroll'), help="HRA is an allowance given by the employer to the employee for taking care of his rental or accommodation expenses for metro city it is 50% and for non metro 40%. \nHRA computed as percentage(%)"),
48 'supplementary_allowance': fields.float('Supplementary Allowance', digits_compute=dp.get_precision('Payroll')),
52 class payroll_advice(osv.osv):
56 _name = 'hr.payroll.advice'
57 _description = 'Bank Advice'
59 'name':fields.char('Name', readonly=True, required=True, states={'draft': [('readonly', False)]},),
60 'note': fields.text('Description'),
61 'date': fields.date('Date', readonly=True, required=True, states={'draft': [('readonly', False)]}, help="Advice Date is used to search Payslips"),
62 'state':fields.selection([
64 ('confirm', 'Confirmed'),
65 ('cancel', 'Cancelled'),
66 ], 'Status', select=True, readonly=True),
67 'number': fields.char('Reference', readonly=True),
68 'line_ids': fields.one2many('hr.payroll.advice.line', 'advice_id', 'Employee Salary', states={'draft': [('readonly', False)]}, readonly=True, copy=True),
69 'chaque_nos': fields.char('Cheque Numbers'),
70 'neft': fields.boolean('NEFT Transaction', help="Check this box if your company use online transfer for salary"),
71 'company_id':fields.many2one('res.company', 'Company', required=True, readonly=True, states={'draft': [('readonly', False)]}),
72 'bank_id':fields.many2one('res.bank', 'Bank', readonly=True, states={'draft': [('readonly', False)]}, help="Select the Bank from which the salary is going to be paid"),
73 'batch_id': fields.many2one('hr.payslip.run', 'Batch', readonly=True)
77 'date': lambda * a: time.strftime('%Y-%m-%d'),
78 'state': lambda * a: 'draft',
79 'company_id': lambda self, cr, uid, context: \
80 self.pool.get('res.users').browse(cr, uid, uid,
81 context=context).company_id.id,
82 'note': "Please make the payroll transfer from above account number to the below mentioned account numbers towards employee salaries:"
85 def compute_advice(self, cr, uid, ids, context=None):
87 Advice - Create Advice lines in Payment Advice and
89 @param cr: the current row, from the database cursor,
90 @param uid: the current user’s ID for security checks,
91 @param ids: List of Advice’s IDs
93 @param context: A standard dictionary for contextual values
95 payslip_pool = self.pool.get('hr.payslip')
96 advice_line_pool = self.pool.get('hr.payroll.advice.line')
97 payslip_line_pool = self.pool.get('hr.payslip.line')
99 for advice in self.browse(cr, uid, ids, context=context):
100 old_line_ids = advice_line_pool.search(cr, uid, [('advice_id', '=', advice.id)], context=context)
102 advice_line_pool.unlink(cr, uid, old_line_ids, context=context)
103 slip_ids = payslip_pool.search(cr, uid, [('date_from', '<=', advice.date), ('date_to', '>=', advice.date), ('state', '=', 'done')], context=context)
104 for slip in payslip_pool.browse(cr, uid, slip_ids, context=context):
105 if not slip.employee_id.bank_account_id and not slip.employee_id.bank_account_id.acc_number:
106 raise osv.except_osv(_('Error!'), _('Please define bank account for the %s employee') % (slip.employee_id.name))
107 line_ids = payslip_line_pool.search(cr, uid, [ ('slip_id', '=', slip.id), ('code', '=', 'NET')], context=context)
109 line = payslip_line_pool.browse(cr, uid, line_ids, context=context)[0]
111 'advice_id': advice.id,
112 'name': slip.employee_id.bank_account_id.acc_number,
113 'employee_id': slip.employee_id.id,
116 advice_line_pool.create(cr, uid, advice_line, context=context)
117 payslip_pool.write(cr, uid, slip_ids, {'advice_id': advice.id}, context=context)
120 def confirm_sheet(self, cr, uid, ids, context=None):
122 confirm Advice - confirmed Advice after computing Advice Lines..
123 @param cr: the current row, from the database cursor,
124 @param uid: the current user’s ID for security checks,
125 @param ids: List of confirm Advice’s IDs
126 @return: confirmed Advice lines and set sequence of Advice.
127 @param context: A standard dictionary for contextual values
129 seq_obj = self.pool.get('ir.sequence')
130 for advice in self.browse(cr, uid, ids, context=context):
131 if not advice.line_ids:
132 raise osv.except_osv(_('Error!'), _('You can not confirm Payment advice without advice lines.'))
133 advice_date = datetime.strptime(advice.date, DATETIME_FORMAT)
134 advice_year = advice_date.strftime('%m') + '-' + advice_date.strftime('%Y')
135 number = seq_obj.get(cr, uid, 'payment.advice')
136 sequence_num = 'PAY' + '/' + advice_year + '/' + number
137 self.write(cr, uid, [advice.id], {'number': sequence_num, 'state': 'confirm'}, context=context)
140 def set_to_draft(self, cr, uid, ids, context=None):
141 """Resets Advice as draft.
143 return self.write(cr, uid, ids, {'state':'draft'}, context=context)
145 def cancel_sheet(self, cr, uid, ids, context=None):
146 """Marks Advice as cancelled.
148 return self.write(cr, uid, ids, {'state':'cancel'}, context=context)
150 def onchange_company_id(self, cr, uid, ids, company_id=False, context=None):
153 company = self.pool.get('res.company').browse(cr, uid, [company_id], context=context)[0]
154 if company.partner_id.bank_ids:
155 res.update({'bank_id': company.partner_id.bank_ids[0].bank.id})
160 class hr_payslip_run(osv.osv):
162 _inherit = 'hr.payslip.run'
163 _description = 'Payslip Batches'
165 'available_advice': fields.boolean('Made Payment Advice?',
166 help="If this box is checked which means that Payment Advice exists for current batch",
167 readonly=False, copy=False),
170 def draft_payslip_run(self, cr, uid, ids, context=None):
171 res = super(hr_payslip_run, self).draft_payslip_run(cr, uid, ids, context=context)
172 self.write(cr, uid, ids, {'available_advice': False}, context=context)
175 def create_advice(self, cr, uid, ids, context=None):
176 payslip_pool = self.pool.get('hr.payslip')
177 payslip_line_pool = self.pool.get('hr.payslip.line')
178 advice_pool = self.pool.get('hr.payroll.advice')
179 advice_line_pool = self.pool.get('hr.payroll.advice.line')
180 users = self.pool.get('res.users').browse(cr, uid, [uid], context=context)
181 for run in self.browse(cr, uid, ids, context=context):
182 if run.available_advice:
183 raise osv.except_osv(_('Error!'), _("Payment advice already exists for %s, 'Set to Draft' to create a new advice.") %(run.name))
186 'company_id': users[0].company_id.id,
188 'date': run.date_end,
189 'bank_id': users[0].company_id.bank_ids and users[0].company_id.bank_ids[0].id or False
191 advice_id = advice_pool.create(cr, uid, advice_data, context=context)
193 for slip_id in run.slip_ids:
194 # TODO is it necessary to interleave the calls ?
195 payslip_pool.signal_workflow(cr, uid, [slip_id.id], 'hr_verify_sheet')
196 payslip_pool.signal_workflow(cr, uid, [slip_id.id], 'process_sheet')
197 slip_ids.append(slip_id.id)
199 for slip in payslip_pool.browse(cr, uid, slip_ids, context=context):
200 if not slip.employee_id.bank_account_id or not slip.employee_id.bank_account_id.acc_number:
201 raise osv.except_osv(_('Error!'), _('Please define bank account for the %s employee') % (slip.employee_id.name))
202 line_ids = payslip_line_pool.search(cr, uid, [('slip_id', '=', slip.id), ('code', '=', 'NET')], context=context)
204 line = payslip_line_pool.browse(cr, uid, line_ids, context=context)[0]
206 'advice_id': advice_id,
207 'name': slip.employee_id.bank_account_id.acc_number,
208 'employee_id': slip.employee_id.id,
211 advice_line_pool.create(cr, uid, advice_line, context=context)
212 return self.write(cr, uid, ids, {'available_advice' : True})
215 class payroll_advice_line(osv.osv):
219 def onchange_employee_id(self, cr, uid, ids, employee_id=False, context=None):
221 hr_obj = self.pool.get('hr.employee')
223 return {'value': res}
224 employee = hr_obj.browse(cr, uid, [employee_id], context=context)[0]
225 res.update({'name': employee.bank_account_id.acc_number , 'ifsc_code': employee.bank_account_id.bank_bic or ''})
226 return {'value': res}
228 _name = 'hr.payroll.advice.line'
229 _description = 'Bank Advice Lines'
231 'advice_id': fields.many2one('hr.payroll.advice', 'Bank Advice'),
232 'name': fields.char('Bank Account No.', size=25, required=True),
233 'ifsc_code': fields.char('IFSC Code', size=16),
234 'employee_id': fields.many2one('hr.employee', 'Employee', required=True),
235 'bysal': fields.float('By Salary', digits_compute=dp.get_precision('Payroll')),
236 'debit_credit': fields.char('C/D', size=3, required=False),
237 'company_id': fields.related('advice_id', 'company_id', type='many2one', required=False, relation='res.company', string='Company', store=True),
238 'ifsc': fields.related('advice_id', 'neft', type='boolean', string='IFSC'),
245 class hr_payslip(osv.osv):
249 _inherit = 'hr.payslip'
250 _description = 'Pay Slips'
252 'advice_id': fields.many2one('hr.payroll.advice', 'Bank Advice', copy=False)
255 class res_company(osv.osv):
257 _inherit = 'res.company'
259 'dearness_allowance': fields.boolean('Dearness Allowance', help="Check this box if your company provide Dearness Allowance to employee")
262 'dearness_allowance': True,
266 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: