105 lines
5.0 KiB
Python
105 lines
5.0 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
##############################################################################
|
||
|
#
|
||
|
# TicTac allows several HR functionalities. This program bases on Odoo v. 8. Copyright
|
||
|
# (C) 2018 ITIS www.itis.de commissioned by Wikimedia Deutschland e.V.
|
||
|
#
|
||
|
# This program is free software: you can redistribute it and/or modify it under the
|
||
|
# terms of the GNU Affero General Public License as published by 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 GNU Affero General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU Affero General Public License along with
|
||
|
# this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
#
|
||
|
##############################################################################
|
||
|
|
||
|
from openerp import models, api, fields, _
|
||
|
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
|
||
|
from datetime import datetime, timedelta
|
||
|
|
||
|
|
||
|
class HRPayslip(models.Model):
|
||
|
|
||
|
_inherit = 'hr.payslip'
|
||
|
|
||
|
def get_worked_day_lines(self, cr, uid, contract_ids, date_from, date_to, context=None):
|
||
|
"""
|
||
|
@param contract_ids: list of contract id
|
||
|
@return: returns a list of dict containing the input that should be applied for the given contract between date_from and date_to
|
||
|
"""
|
||
|
|
||
|
def was_on_leave(employee_id, datetime_day, context=None):
|
||
|
res = False
|
||
|
day = datetime_day.strftime("%Y-%m-%d")
|
||
|
holiday_ids = self.pool.get('hr.holidays').search(cr, uid, [('state','=','validate'),('employee_id','=',employee_id),('type','=','remove'),('date_from','<=',day),('date_to','>=',day)])
|
||
|
if holiday_ids:
|
||
|
res = self.pool.get('hr.holidays').browse(cr, uid, holiday_ids, context=context)[0].holiday_status_id.name
|
||
|
return res
|
||
|
|
||
|
res = []
|
||
|
for contract in self.pool.get('hr.contract').browse(cr, uid, contract_ids, context=context):
|
||
|
if not contract.working_hours:
|
||
|
#fill only if the contract as a working schedule linked
|
||
|
continue
|
||
|
attendances = {
|
||
|
'name': _("Normal Working Days paid at 100%"),
|
||
|
'sequence': 1,
|
||
|
'code': 'WORK100',
|
||
|
'number_of_days': 0.0,
|
||
|
'number_of_hours': 0.0,
|
||
|
'contract_id': contract.id,
|
||
|
}
|
||
|
leaves = {}
|
||
|
day_from = datetime.strptime(date_from,"%Y-%m-%d")
|
||
|
day_to = datetime.strptime(date_to,"%Y-%m-%d")
|
||
|
contract_day_start = datetime.strptime(contract.date_start,"%Y-%m-%d")
|
||
|
|
||
|
if day_from < contract_day_start:
|
||
|
day_from = contract_day_start
|
||
|
if contract.date_end:
|
||
|
contract_day_end = datetime.strptime(contract.date_end,"%Y-%m-%d")
|
||
|
if day_to > contract_day_end:
|
||
|
day_to = contract_day_end
|
||
|
nb_of_days = (day_to - day_from).days + 1
|
||
|
for day in range(0, nb_of_days):
|
||
|
working_hours_on_day = self.pool.get('resource.calendar').working_hours_on_day(cr, uid, contract.working_hours, day_from + timedelta(days=day), context)
|
||
|
is_holiday = self.is_holiday(cr,uid,day_from + timedelta(days=day), context)
|
||
|
if is_holiday:
|
||
|
continue
|
||
|
if working_hours_on_day:
|
||
|
#the employee had to work
|
||
|
leave_type = was_on_leave(contract.employee_id.id, day_from + timedelta(days=day), context=context)
|
||
|
if leave_type:
|
||
|
#if he was on leave, fill the leaves dict
|
||
|
if leave_type in leaves:
|
||
|
leaves[leave_type]['number_of_days'] += 1.0
|
||
|
leaves[leave_type]['number_of_hours'] += working_hours_on_day
|
||
|
else:
|
||
|
leaves[leave_type] = {
|
||
|
'name': leave_type,
|
||
|
'sequence': 5,
|
||
|
'code': leave_type,
|
||
|
'number_of_days': 1.0,
|
||
|
'number_of_hours': working_hours_on_day,
|
||
|
'contract_id': contract.id,
|
||
|
}
|
||
|
else:
|
||
|
#add the input vals to tmp (increment if existing)
|
||
|
attendances['number_of_days'] += 1.0
|
||
|
attendances['number_of_hours'] += working_hours_on_day
|
||
|
leaves = [value for key,value in leaves.items()]
|
||
|
res += [attendances] + leaves
|
||
|
return res
|
||
|
|
||
|
def is_holiday(self,cr,uid,day,context):
|
||
|
search_id = self.pool.get('itis.holiday').search(cr,uid,[('date','=',day)])
|
||
|
if search_id:
|
||
|
return True
|
||
|
else:
|
||
|
return False
|