mirror of
https://github.com/bringout/oca-technical.git
synced 2026-04-18 06:32:00 +02:00
120 lines
4.2 KiB
Python
120 lines
4.2 KiB
Python
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import fields
|
|
|
|
|
|
class BaseBrowsableObject(object):
|
|
def __init__(self, vals_dict):
|
|
self.__dict__["base_fields"] = ["base_fields", "dict"]
|
|
self.dict = vals_dict
|
|
|
|
def __getattr__(self, attr):
|
|
return attr in self.dict and self.dict.__getitem__(attr) or 0.0
|
|
|
|
def __setattr__(self, attr, value):
|
|
_fields = self.__dict__["base_fields"]
|
|
if attr in _fields:
|
|
return super().__setattr__(attr, value)
|
|
self.__dict__["dict"][attr] = value
|
|
|
|
def __str__(self):
|
|
return str(self.__dict__)
|
|
|
|
|
|
# These classes are used in the _get_payslip_lines() method
|
|
class BrowsableObject(BaseBrowsableObject):
|
|
def __init__(self, employee_id, vals_dict, env):
|
|
super().__init__(vals_dict)
|
|
self.base_fields += ["employee_id", "env"]
|
|
self.employee_id = employee_id
|
|
self.env = env
|
|
|
|
|
|
class InputLine(BrowsableObject):
|
|
"""a class that will be used into the python code, mainly for
|
|
usability purposes"""
|
|
|
|
def sum(self, code, from_date, to_date=None):
|
|
if to_date is None:
|
|
to_date = fields.Date.today()
|
|
self.env.cr.execute(
|
|
"""
|
|
SELECT sum(amount) as sum
|
|
FROM hr_payslip as hp, hr_payslip_input as pi
|
|
WHERE hp.employee_id = %s AND hp.state = 'done'
|
|
AND hp.date_from >= %s AND hp.date_to <= %s
|
|
AND hp.id = pi.payslip_id AND pi.code = %s""",
|
|
(self.employee_id, from_date, to_date, code),
|
|
)
|
|
return self.env.cr.fetchone()[0] or 0.0
|
|
|
|
|
|
class WorkedDays(BrowsableObject):
|
|
"""a class that will be used into the python code, mainly for
|
|
usability purposes"""
|
|
|
|
def _sum(self, code, from_date, to_date=None):
|
|
if to_date is None:
|
|
to_date = fields.Date.today()
|
|
self.env.cr.execute(
|
|
"""
|
|
SELECT sum(number_of_days) as number_of_days,
|
|
sum(number_of_hours) as number_of_hours
|
|
FROM hr_payslip as hp, hr_payslip_worked_days as pi
|
|
WHERE hp.employee_id = %s AND hp.state = 'done'
|
|
AND hp.date_from >= %s AND hp.date_to <= %s
|
|
AND hp.id = pi.payslip_id AND pi.code = %s""",
|
|
(self.employee_id, from_date, to_date, code),
|
|
)
|
|
return self.env.cr.fetchone()
|
|
|
|
def sum(self, code, from_date, to_date=None):
|
|
res = self._sum(code, from_date, to_date)
|
|
return res and res[0] or 0.0
|
|
|
|
def sum_hours(self, code, from_date, to_date=None):
|
|
res = self._sum(code, from_date, to_date)
|
|
return res and res[1] or 0.0
|
|
|
|
|
|
class Payslips(BrowsableObject):
|
|
"""a class that will be used into the python code, mainly for
|
|
usability purposes"""
|
|
|
|
def sum(self, code, from_date, to_date=None):
|
|
if to_date is None:
|
|
to_date = fields.Date.today()
|
|
self.env.cr.execute(
|
|
"""SELECT sum(case when hp.credit_note = False then
|
|
(pl.total) else (-pl.total) end)
|
|
FROM hr_payslip as hp, hr_payslip_line as pl
|
|
WHERE hp.employee_id = %s AND hp.state = 'done'
|
|
AND hp.date_from >= %s AND hp.date_to <= %s AND
|
|
hp.id = pl.slip_id AND pl.code = %s""",
|
|
(self.employee_id, from_date, to_date, code),
|
|
)
|
|
res = self.env.cr.fetchone()
|
|
return res and res[0] or 0.0
|
|
|
|
def rule_parameter(self, code):
|
|
return self.env["hr.rule.parameter"]._get_parameter_from_code(
|
|
code, self.dict.date_to
|
|
)
|
|
def koef_min_rad(self):
|
|
|
|
# https://stackoverflow.com/questions/765797/convert-timedelta-to-years
|
|
year_payslip = self.dict.date_to.year
|
|
year_from = self.dict.employee_id.first_contract_date.year
|
|
|
|
month_payslip = self.dict.date_to.month
|
|
month_from = self.dict.employee_id.first_contract_date.month
|
|
|
|
# 20.09.2006 - 31.03.2023 = 2023-2006 = 17
|
|
diff = year_payslip - year_from
|
|
|
|
# no increment until full year
|
|
# month 09 contract start, month 03 current payroll
|
|
if (diff > 0) and (month_payslip < month_payslip):
|
|
diff = diff - 1
|
|
|
|
return diff * 0.6
|