Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][FW] [14.0] [IMP] payroll: Add new "payslips" object methods #111

Merged
merged 9 commits into from
Dec 22, 2022
268 changes: 267 additions & 1 deletion payroll/models/base_browsable.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ 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):
def sum_rule(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()
self.env.cr.execute(
Expand All @@ -99,3 +99,269 @@ def sum(self, code, from_date, to_date=None):
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def sum(self, code, from_date, to_date=None):
_logger.warning(
"Payslips Object: sum() method is DEPRECATED. Use sum_rule() instead."
)
return self.sum_rule(code, from_date, to_date)

def average_rule(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()
self.env.cr.execute(
"""SELECT avg(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 average_rule_monthly(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()
self.env.cr.execute(
"""SELECT avg(total) FROM (SELECT max(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) AS monthly_sum""",
(self.employee_id, from_date, to_date, code),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def max_rule(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()
self.env.cr.execute(
"""SELECT max(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 max_rule_monthly(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()
self.env.cr.execute(
"""SELECT max(total) FROM (SELECT max(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) AS monthly_sum""",
(self.employee_id, from_date, to_date, code),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def min_rule(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()
self.env.cr.execute(
"""SELECT min(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 min_rule_monthly(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()
self.env.cr.execute(
"""SELECT min(total) FROM (SELECT max(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) AS monthly_sum""",
(self.employee_id, from_date, to_date, code),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def sum_category(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()

hierarchy_codes = (
self.env["hr.salary.rule.category"]
.search([("code", "=", code)])
.children_ids.mapped("code")
)
hierarchy_codes.append(code)

self.env.cr.execute(
"""SELECT sum(case when hp.credit_note is not True then
(pl.total) else (-pl.total) end)
FROM hr_payslip as hp, hr_payslip_line as pl, hr_salary_rule_category as rc
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 rc.id = pl.category_id AND rc.code in %s""",
(self.employee_id, from_date, to_date, tuple(hierarchy_codes)),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def average_category(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()

hierarchy_codes = (
self.env["hr.salary.rule.category"]
.search([("code", "=", code)])
.children_ids.mapped("code")
)
hierarchy_codes.append(code)

self.env.cr.execute(
"""SELECT avg(case when hp.credit_note is not True then
(pl.total) else (-pl.total) end)
FROM hr_payslip as hp, hr_payslip_line as pl, hr_salary_rule_category as rc
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 rc.id = pl.category_id AND rc.code in %s""",
(self.employee_id, from_date, to_date, tuple(hierarchy_codes)),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def average_category_monthly(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()

hierarchy_codes = (
self.env["hr.salary.rule.category"]
.search([("code", "=", code)])
.children_ids.mapped("code")
)
hierarchy_codes.append(code)

self.env.cr.execute(
"""SELECT avg(total) FROM (
SELECT DATE_TRUNC('month',hp.date_from) AS date_month,
sum(case when hp.credit_note is not True then
(pl.total) else (-pl.total) end) AS total
FROM hr_payslip as hp, hr_payslip_line as pl, hr_salary_rule_category as rc
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 rc.id = pl.category_id AND rc.code in %s
GROUP BY date_month) AS monthly_sum""",
(self.employee_id, from_date, to_date, tuple(hierarchy_codes)),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def max_category(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()

hierarchy_codes = (
self.env["hr.salary.rule.category"]
.search([("code", "=", code)])
.children_ids.mapped("code")
)
hierarchy_codes.append(code)

self.env.cr.execute(
"""SELECT max(case when hp.credit_note is not True then
(pl.total) else (-pl.total) end)
FROM hr_payslip as hp, hr_payslip_line as pl, hr_salary_rule_category as rc
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 rc.id = pl.category_id AND rc.code in %s""",
(self.employee_id, from_date, to_date, tuple(hierarchy_codes)),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def max_category_monthly(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()

hierarchy_codes = (
self.env["hr.salary.rule.category"]
.search([("code", "=", code)])
.children_ids.mapped("code")
)
hierarchy_codes.append(code)

self.env.cr.execute(
"""SELECT max(total) FROM (
SELECT DATE_TRUNC('month',hp.date_from) AS date_month,
sum(case when hp.credit_note is not True then
(pl.total) else (-pl.total) end) AS total
FROM hr_payslip as hp, hr_payslip_line as pl, hr_salary_rule_category as rc
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 rc.id = pl.category_id AND rc.code in %s
GROUP BY date_month) AS monthly_sum""",
(self.employee_id, from_date, to_date, tuple(hierarchy_codes)),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def min_category(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()

hierarchy_codes = (
self.env["hr.salary.rule.category"]
.search([("code", "=", code)])
.children_ids.mapped("code")
)
hierarchy_codes.append(code)

self.env.cr.execute(
"""SELECT min(case when hp.credit_note is not True then
(pl.total) else (-pl.total) end)
FROM hr_payslip as hp, hr_payslip_line as pl, hr_salary_rule_category as rc
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 rc.id = pl.category_id AND rc.code in %s""",
(self.employee_id, from_date, to_date, tuple(hierarchy_codes)),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0

def min_category_monthly(self, code, from_date, to_date=None):
if to_date is None:
to_date = fields.Date.today()

hierarchy_codes = (
self.env["hr.salary.rule.category"]
.search([("code", "=", code)])
.children_ids.mapped("code")
)
hierarchy_codes.append(code)

self.env.cr.execute(
"""SELECT min(total) FROM (
SELECT DATE_TRUNC('month',hp.date_from) AS date_month,
sum(case when hp.credit_note is not True then
(pl.total) else (-pl.total) end) AS total
FROM hr_payslip as hp, hr_payslip_line as pl, hr_salary_rule_category as rc
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 rc.id = pl.category_id AND rc.code in %s
GROUP BY date_month) AS monthly_sum""",
(self.employee_id, from_date, to_date, tuple(hierarchy_codes)),
)
res = self.env.cr.fetchone()
return res and res[0] or 0.0
5 changes: 4 additions & 1 deletion payroll/models/hr_payslip.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,10 @@ def get_current_contract_dict(self, contract, contracts):
def _get_tools_dict(self):
# _get_tools_dict() is intended to be inherited by other private modules
# to add tools or python libraries available in localdict
return {"math": math} # "math" object is useful for doing calculations
return {
"math": math,
"datetime": datetime,
} # "math" object is useful for doing calculations

def _get_baselocaldict(self, contracts):
self.ensure_one()
Expand Down
20 changes: 11 additions & 9 deletions payroll/views/hr_payslip_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,17 @@
</field>
</page>
<page name="computation" string="Salary Computation">
<group>
<field
name="hide_child_lines"
widget="boolean_toggle"
/>
<field
name="hide_invisible_lines"
widget="boolean_toggle"
/>
<group name="controls">
<group>
<field
name="hide_child_lines"
widget="boolean_toggle"
/>
<field
name="hide_invisible_lines"
widget="boolean_toggle"
/>
</group>
</group>
<field
name="dynamic_filtered_payslip_lines"
Expand Down