mirror of
https://github.com/bringout/oca-payroll.git
synced 2026-04-24 12:42:04 +02:00
Initial commit: OCA Payroll packages (5 packages)
This commit is contained in:
commit
d19274f581
407 changed files with 214057 additions and 0 deletions
|
|
@ -0,0 +1,10 @@
|
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from . import date_range_type
|
||||
from . import hr_contract
|
||||
from . import hr_fiscal_year
|
||||
from . import hr_payslip
|
||||
from . import hr_payslip_employees
|
||||
from . import hr_payslip_run
|
||||
from . import hr_period
|
||||
from . import hr_employee
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
# Copyright 2015 Savoir-faire Linux. All Rights Reserved.
|
||||
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class DateRangeType(models.Model):
|
||||
|
||||
_inherit = "date.range.type"
|
||||
|
||||
hr_period = fields.Boolean(string="Is HR period?")
|
||||
hr_fiscal_year = fields.Boolean(string="Is HR Fiscal Year?")
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
# Copyright 2015 Savoir-faire Linux. All Rights Reserved.
|
||||
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
from .hr_fiscal_year import get_schedules
|
||||
|
||||
|
||||
class HrContract(models.Model):
|
||||
_inherit = "hr.contract"
|
||||
|
||||
# Add semi-monthly to payroll schedules
|
||||
schedule_pay = fields.Selection(get_schedules, index=True)
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
# Copyright 2015 Savoir-faire Linux. All Rights Reserved.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class HrEmployee(models.AbstractModel):
|
||||
_inherit = "hr.employee.base"
|
||||
|
||||
contract_id = fields.Many2one(comodel_name="hr.contract", search="_search_contract")
|
||||
|
||||
@api.model
|
||||
def _search_contract(self, operator, value):
|
||||
res = []
|
||||
contract_ids = self.env["hr.contract"].search(
|
||||
[("employee_id", operator, value)]
|
||||
)
|
||||
res.append(("id", "in", contract_ids.ids))
|
||||
return res
|
||||
|
|
@ -0,0 +1,354 @@
|
|||
# Copyright 2015 Savoir-faire Linux. All Rights Reserved.
|
||||
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from dateutil.rrule import DAILY, MONTHLY, WEEKLY, YEARLY
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT as DF
|
||||
|
||||
strptime = datetime.strptime
|
||||
strftime = datetime.strftime
|
||||
|
||||
INTERVALS = {
|
||||
"annually": (relativedelta(months=12), 1),
|
||||
"semi-annually": (relativedelta(months=6), 2),
|
||||
"quarterly": (relativedelta(months=3), 4),
|
||||
"bi-monthly": (relativedelta(months=2), 6),
|
||||
"semi-monthly": (relativedelta(weeks=2), 24),
|
||||
"monthly": (relativedelta(months=1), 12),
|
||||
"bi-weekly": (relativedelta(weeks=2), 26),
|
||||
"weekly": (relativedelta(weeks=1), 52),
|
||||
"daily": (relativedelta(days=1), 365),
|
||||
}
|
||||
|
||||
|
||||
@api.model
|
||||
def get_schedules(self):
|
||||
return [
|
||||
("annually", _("Annually (1)")),
|
||||
("semi-annually", _("Semi-annually (2)")),
|
||||
("quarterly", _("Quarterly (4)")),
|
||||
("bi-monthly", _("Bi-monthly (6)")),
|
||||
("monthly", _("Monthly (12)")),
|
||||
("semi-monthly", _("Semi-monthly (24)")),
|
||||
("bi-weekly", _("Bi-weekly (26)")),
|
||||
("weekly", _("Weekly (52)")),
|
||||
("daily", _("Daily (365)")),
|
||||
]
|
||||
|
||||
|
||||
@api.model
|
||||
def get_payment_days(self):
|
||||
expr = _("%s day of the next period")
|
||||
expr_2 = _("%s day of the current period")
|
||||
return [
|
||||
("1", expr % _("First")),
|
||||
("2", expr % _("Second")),
|
||||
("3", expr % _("Third")),
|
||||
("4", expr % _("Fourth")),
|
||||
("5", expr % _("Fifth")),
|
||||
("0", expr_2 % _("Last")),
|
||||
]
|
||||
|
||||
|
||||
class HrFiscalYear(models.Model):
|
||||
_name = "hr.fiscalyear"
|
||||
_inherit = "date.range"
|
||||
_description = "HR Fiscal Year"
|
||||
|
||||
@api.model
|
||||
def _default_date_start(self):
|
||||
today = datetime.now()
|
||||
return datetime(today.year, 1, 1).strftime(DF)
|
||||
|
||||
@api.model
|
||||
def _default_date_end(self):
|
||||
today = datetime.now()
|
||||
return datetime(today.year, 12, 31).strftime(DF)
|
||||
|
||||
@api.model
|
||||
def _default_type(self, company_id=False):
|
||||
if not company_id:
|
||||
company_id = self.env.company
|
||||
period_type = self.env["date.range.type"].search(
|
||||
[("hr_fiscal_year", "=", True), ("company_id", "=", company_id.id)], limit=1
|
||||
)
|
||||
return period_type
|
||||
|
||||
period_ids = fields.One2many(
|
||||
"hr.period", "fiscalyear_id", "Periods", states={"draft": [("readonly", False)]}
|
||||
)
|
||||
state = fields.Selection(
|
||||
[
|
||||
("draft", "Draft"),
|
||||
("open", "Open"),
|
||||
("done", "Closed"),
|
||||
],
|
||||
"Status",
|
||||
default="draft",
|
||||
)
|
||||
schedule_pay = fields.Selection(
|
||||
get_schedules,
|
||||
required=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
default="monthly",
|
||||
)
|
||||
type_id = fields.Many2one(
|
||||
domain=[("hr_fiscal_year", "=", True)], default=_default_type
|
||||
)
|
||||
payment_weekday = fields.Selection(
|
||||
[
|
||||
("0", "Sunday"),
|
||||
("1", "Monday"),
|
||||
("2", "Tuesday"),
|
||||
("3", "Wednesday"),
|
||||
("4", "Thursday"),
|
||||
("5", "Friday"),
|
||||
("6", "Saturday"),
|
||||
],
|
||||
"Weekday of Payment",
|
||||
states={"draft": [("readonly", False)]},
|
||||
)
|
||||
payment_week = fields.Selection(
|
||||
[
|
||||
("0", "Same Week"),
|
||||
("1", "Following Week"),
|
||||
("2", "Second Following Week"),
|
||||
],
|
||||
"Week of Payment",
|
||||
states={"draft": [("readonly", False)]},
|
||||
)
|
||||
payment_day = fields.Selection(
|
||||
get_payment_days, "Day of Payment", states={"draft": [("readonly", False)]}
|
||||
)
|
||||
|
||||
def _count_range_no(self):
|
||||
days_range = (
|
||||
abs(
|
||||
(
|
||||
strptime(str(self.date_end), DF)
|
||||
- strptime(str(self.date_start), DF)
|
||||
).days
|
||||
)
|
||||
+ 1
|
||||
)
|
||||
return INTERVALS[self.schedule_pay][1] * days_range / 365
|
||||
|
||||
@api.onchange("schedule_pay", "date_start")
|
||||
def onchange_schedule(self):
|
||||
if self.schedule_pay and self.date_start:
|
||||
year = strptime(str(self.date_start), DF).year
|
||||
schedule_name = next(
|
||||
(s[1] for s in get_schedules(self) if s[0] == self.schedule_pay), False
|
||||
)
|
||||
self.name = "%(year)s - %(schedule)s" % {
|
||||
"year": year,
|
||||
"schedule": schedule_name,
|
||||
}
|
||||
|
||||
def get_generator_vals(self):
|
||||
self.ensure_one()
|
||||
no_interval = 1
|
||||
if self.schedule_pay == "daily":
|
||||
unit_of_time = DAILY
|
||||
elif self.schedule_pay == "weekly":
|
||||
unit_of_time = WEEKLY
|
||||
elif self.schedule_pay == "bi-weekly":
|
||||
unit_of_time = WEEKLY
|
||||
no_interval = 2
|
||||
elif self.schedule_pay == "monthly":
|
||||
unit_of_time = MONTHLY
|
||||
elif self.schedule_pay == "bi-monthly":
|
||||
unit_of_time = MONTHLY
|
||||
no_interval = 2
|
||||
elif self.schedule_pay == "quarterly":
|
||||
unit_of_time = MONTHLY
|
||||
no_interval = 4
|
||||
elif self.schedule_pay == "semi-annually":
|
||||
unit_of_time = MONTHLY
|
||||
no_interval = 6
|
||||
else:
|
||||
unit_of_time = YEARLY
|
||||
return {
|
||||
"name_prefix": self.name,
|
||||
"date_start": self.date_start,
|
||||
"type_id": self.type_id.id,
|
||||
"company_id": self.company_id.id,
|
||||
"unit_of_time": str(unit_of_time),
|
||||
"duration_count": no_interval,
|
||||
"count": self._count_range_no(),
|
||||
}
|
||||
|
||||
def get_ranges(self):
|
||||
self.ensure_one()
|
||||
vals = self.get_generator_vals()
|
||||
range_generator = self.env["date.range.generator"].create(vals)
|
||||
date_ranges = range_generator._generate_date_ranges()
|
||||
return date_ranges
|
||||
|
||||
def create_periods(self):
|
||||
"""
|
||||
Create every periods a payroll fiscal year
|
||||
"""
|
||||
self.ensure_one()
|
||||
for fy in self:
|
||||
for period in fy.period_ids:
|
||||
period.unlink()
|
||||
fy.invalidate_recordset()
|
||||
if self.date_start > self.date_end:
|
||||
raise UserError(
|
||||
_(
|
||||
"""Date stop cannot be sooner than the date start
|
||||
"""
|
||||
)
|
||||
)
|
||||
if self.schedule_pay == "semi-monthly":
|
||||
period_start = strptime(str(self.date_start), DF)
|
||||
next_year_start = strptime(str(self.date_end), DF) + relativedelta(days=1)
|
||||
# Case for semi-monthly schedules
|
||||
delta_1 = relativedelta(days=14)
|
||||
delta_2 = relativedelta(months=1)
|
||||
i = 1
|
||||
while not period_start + delta_2 > next_year_start:
|
||||
# create periods for one month
|
||||
half_month = period_start + delta_1
|
||||
self._create_single_period(period_start, half_month, i)
|
||||
self._create_single_period(
|
||||
half_month + relativedelta(days=1),
|
||||
period_start + delta_2 - relativedelta(days=1),
|
||||
i + 1,
|
||||
)
|
||||
# setup for next month
|
||||
period_start += delta_2
|
||||
i += 2
|
||||
else:
|
||||
i = 0
|
||||
for period in self.get_ranges():
|
||||
i += 1
|
||||
period_start = strptime(str(period.get("date_start", False)), DF)
|
||||
period_end = strptime(str(period.get("date_end", False)), DF)
|
||||
self._create_single_period(period_start, period_end, i)
|
||||
return True
|
||||
|
||||
def _create_single_period(self, date_start, date_end, number):
|
||||
"""Create a single payroll period
|
||||
:param date_start: the first day of the actual period
|
||||
:param date_end: the first day of the following period
|
||||
"""
|
||||
self.ensure_one()
|
||||
period_type = self.env["hr.period"]._default_type(self.company_id.id)
|
||||
self.write(
|
||||
{
|
||||
"period_ids": [
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"date_start": date_start,
|
||||
"date_end": date_end,
|
||||
"date_payment": self._get_day_of_payment(date_end),
|
||||
"company_id": self.company_id.id,
|
||||
"name": _("%(name)s Period #%(number)s")
|
||||
% {"name": self.name, "number": number},
|
||||
"number": number,
|
||||
"state": "draft",
|
||||
"type_id": period_type.id,
|
||||
"schedule_pay": self.schedule_pay,
|
||||
},
|
||||
)
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
def _get_day_of_payment(self, date_end):
|
||||
"""
|
||||
Get the date of payment for a period to create
|
||||
:param date_end: the last day of the current period
|
||||
"""
|
||||
self.ensure_one()
|
||||
|
||||
date_payment = date_end
|
||||
if self.schedule_pay in ["weekly", "bi-weekly"]:
|
||||
date_payment += relativedelta(weeks=int(self.payment_week))
|
||||
while date_payment.strftime("%w") != self.payment_weekday:
|
||||
date_payment -= relativedelta(days=1)
|
||||
else:
|
||||
date_payment += relativedelta(days=int(self.payment_day))
|
||||
return date_payment
|
||||
|
||||
def button_confirm(self):
|
||||
for fy in self:
|
||||
if not fy.period_ids:
|
||||
raise UserError(
|
||||
_("You must create periods before confirming " "the fiscal year.")
|
||||
)
|
||||
self.state = "open"
|
||||
for fy in self:
|
||||
first_period = fy.period_ids.sorted(key=lambda p: p.number)[0]
|
||||
first_period.button_open()
|
||||
|
||||
def button_set_to_draft(self):
|
||||
# Set all periods to draft
|
||||
periods = self.mapped("period_ids")
|
||||
periods.button_set_to_draft()
|
||||
self.state = "draft"
|
||||
|
||||
def search_period(self, number):
|
||||
return next(
|
||||
(p for p in self.period_ids if p.number == number), self.env["hr.period"]
|
||||
)
|
||||
|
||||
@api.model
|
||||
def cron_create_next_fiscal_year(self):
|
||||
current_year = datetime.now().year
|
||||
next_year = current_year + 1
|
||||
# Get the latest fiscal year that has not ended yet
|
||||
latest_fiscal_year = self.search(
|
||||
[("date_end", "<", datetime(next_year, 1, 1).strftime(DF))],
|
||||
order="date_end desc",
|
||||
limit=1,
|
||||
)
|
||||
if not latest_fiscal_year:
|
||||
return self
|
||||
latest_period_end = max(latest_fiscal_year.period_ids.mapped("date_end"))
|
||||
fiscal_year_start = latest_period_end + relativedelta(days=1)
|
||||
fiscal_year_end = datetime(next_year, 12, 31).strftime(DF)
|
||||
# Check if a fiscal year with the same start and end dates already exists
|
||||
existing_fiscal_year = self.search(
|
||||
[
|
||||
("date_start", "=", fiscal_year_start),
|
||||
("date_end", "=", fiscal_year_end),
|
||||
],
|
||||
limit=1,
|
||||
)
|
||||
if existing_fiscal_year:
|
||||
return existing_fiscal_year
|
||||
|
||||
schedule_pay = latest_fiscal_year.schedule_pay
|
||||
payment_weekday = latest_fiscal_year.payment_weekday
|
||||
payment_week = latest_fiscal_year.payment_week
|
||||
schedule_name = next(
|
||||
(s[1] for s in get_schedules(self) if s[0] == schedule_pay), False
|
||||
)
|
||||
|
||||
fiscal_year = self.create(
|
||||
{
|
||||
"name": "%(year)s - %(schedule)s"
|
||||
% {
|
||||
"year": next_year,
|
||||
"schedule": schedule_name,
|
||||
},
|
||||
"date_start": fiscal_year_start,
|
||||
"date_end": fiscal_year_end,
|
||||
"schedule_pay": schedule_pay,
|
||||
"payment_weekday": payment_weekday,
|
||||
"payment_week": payment_week,
|
||||
}
|
||||
)
|
||||
fiscal_year.create_periods()
|
||||
return fiscal_year
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
# Copyright 2015 Savoir-faire Linux. All Rights Reserved.
|
||||
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class HrPayslip(models.Model):
|
||||
_inherit = "hr.payslip"
|
||||
|
||||
hr_period_id = fields.Many2one(
|
||||
"hr.period",
|
||||
string="Period",
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
)
|
||||
date_payment = fields.Date(
|
||||
"Date of Payment", readonly=True, states={"draft": [("readonly", False)]}
|
||||
)
|
||||
|
||||
@api.constrains("hr_period_id", "company_id")
|
||||
def _check_period_company(self):
|
||||
for slip in self:
|
||||
if slip.hr_period_id and slip.hr_period_id.company_id != slip.company_id:
|
||||
if slip.hr_period_id.company_id != slip.company_id:
|
||||
raise UserError(
|
||||
_(
|
||||
"The company on the selected period must be the same "
|
||||
"as the company on the payslip."
|
||||
)
|
||||
)
|
||||
|
||||
@api.onchange("company_id", "contract_id")
|
||||
def onchange_company_id(self):
|
||||
if self.company_id:
|
||||
if self.contract_id:
|
||||
contract = self.contract_id
|
||||
period = self.env["hr.period"].get_next_period(
|
||||
self.company_id.id, contract.schedule_pay
|
||||
)
|
||||
else:
|
||||
schedule_pay = self.env["hr.payslip.run"].get_default_schedule(
|
||||
self.company_id.id
|
||||
)
|
||||
if self.company_id and schedule_pay:
|
||||
period = self.env["hr.period"].get_next_period(
|
||||
self.company_id.id, schedule_pay
|
||||
)
|
||||
self.hr_period_id = period.id if period else False
|
||||
|
||||
@api.onchange("contract_id")
|
||||
def onchange_contract_period(self):
|
||||
if self.contract_id.employee_id and self.contract_id:
|
||||
employee = self.contract_id.employee_id
|
||||
contract = self.contract_id
|
||||
period = self.env["hr.period"].get_next_period(
|
||||
employee.company_id.id, contract.schedule_pay
|
||||
)
|
||||
if period:
|
||||
self.hr_period_id = period.id if period else False
|
||||
|
||||
@api.onchange("hr_period_id")
|
||||
def onchange_hr_period_id(self):
|
||||
if self.hr_period_id:
|
||||
# dates must be updated together to prevent constraint
|
||||
self.date_from = self.hr_period_id.date_start
|
||||
self.date_to = self.hr_period_id.date_end
|
||||
self.date_payment = self.hr_period_id.date_payment
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
if vals.get("payslip_run_id"):
|
||||
payslip_run = self.env["hr.payslip.run"].browse(vals["payslip_run_id"])
|
||||
self.env["hr.employee"].browse(vals["employee_id"])
|
||||
period = payslip_run.hr_period_id
|
||||
vals["date_payment"] = payslip_run.date_payment
|
||||
vals["hr_period_id"] = period.id
|
||||
elif vals.get("date_to") and not vals.get("date_payment"):
|
||||
vals["date_payment"] = vals["date_to"]
|
||||
return super(HrPayslip, self).create(vals)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
# Copyright 2015 Savoir-faire Linux. All Rights Reserved.
|
||||
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
from .hr_fiscal_year import get_schedules
|
||||
|
||||
|
||||
class HrPayslipEmployees(models.TransientModel):
|
||||
|
||||
_inherit = "hr.payslip.employees"
|
||||
|
||||
company_id = fields.Many2one("res.company", "Company", readonly=True)
|
||||
schedule_pay = fields.Selection(get_schedules, readonly=True)
|
||||
|
|
@ -0,0 +1,154 @@
|
|||
# Copyright 2015 Savoir-faire Linux. All Rights Reserved.
|
||||
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
from .hr_fiscal_year import get_schedules
|
||||
|
||||
|
||||
class HrPayslipRun(models.Model):
|
||||
_inherit = "hr.payslip.run"
|
||||
|
||||
name = fields.Char(
|
||||
required=True,
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
default=lambda obj: obj.env["ir.sequence"].next_by_code("hr.payslip.run"),
|
||||
)
|
||||
company_id = fields.Many2one(
|
||||
"res.company",
|
||||
"Company",
|
||||
states={"close": [("readonly", True)]},
|
||||
default=lambda obj: obj.env.company,
|
||||
)
|
||||
hr_period_id = fields.Many2one(
|
||||
"hr.period", string="Period", states={"close": [("readonly", True)]}
|
||||
)
|
||||
date_payment = fields.Date(
|
||||
"Date of Payment", states={"close": [("readonly", True)]}
|
||||
)
|
||||
schedule_pay = fields.Selection(
|
||||
get_schedules, states={"close": [("readonly", True)]}
|
||||
)
|
||||
|
||||
@api.constrains("hr_period_id", "company_id")
|
||||
def _check_period_company(self):
|
||||
for run in self:
|
||||
if run.hr_period_id and run.hr_period_id.company_id != run.company_id:
|
||||
if run.hr_period_id.company_id != run.company_id:
|
||||
raise UserError(
|
||||
_(
|
||||
"The company on the selected period must be the same "
|
||||
"as the company on the payslip batch."
|
||||
)
|
||||
)
|
||||
|
||||
@api.constrains("hr_period_id", "schedule_pay")
|
||||
def _check_period_schedule(self):
|
||||
for run in self:
|
||||
if run.hr_period_id and run.hr_period_id.schedule_pay != run.schedule_pay:
|
||||
if run.hr_period_id.schedule_pay != run.schedule_pay:
|
||||
raise UserError(
|
||||
_(
|
||||
"""The schedule on the selected period
|
||||
must be the same as the schedule on the
|
||||
payslip batch."""
|
||||
)
|
||||
)
|
||||
|
||||
@api.model
|
||||
def get_default_schedule(self, company_id):
|
||||
company = self.env["res.company"].browse(company_id)
|
||||
fys = self.env["hr.fiscalyear"].search(
|
||||
[("state", "=", "open"), ("company_id", "=", company.id)]
|
||||
)
|
||||
return fys[0].schedule_pay if fys else "monthly"
|
||||
|
||||
@api.onchange("company_id", "schedule_pay")
|
||||
def onchange_company_id(self):
|
||||
self.ensure_one()
|
||||
schedule_pay = self.schedule_pay or self.get_default_schedule(
|
||||
self.company_id.id
|
||||
)
|
||||
if self.company_id and schedule_pay:
|
||||
period = self.env["hr.period"].get_next_period(
|
||||
self.company_id.id,
|
||||
schedule_pay,
|
||||
)
|
||||
self.hr_period_id = (period.id if period else False,)
|
||||
|
||||
@api.onchange("hr_period_id")
|
||||
def onchange_period_id(self):
|
||||
period = self.hr_period_id
|
||||
if period:
|
||||
self.date_start = period.date_start
|
||||
self.date_end = period.date_end
|
||||
self.date_payment = period.date_payment
|
||||
self.schedule_pay = period.schedule_pay
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
"""
|
||||
Keep compatibility between modules
|
||||
"""
|
||||
if vals.get("date_end") and not vals.get("date_payment"):
|
||||
vals.update({"date_payment": vals["date_end"]})
|
||||
return super(HrPayslipRun, self).create(vals)
|
||||
|
||||
def get_payslip_employees_wizard(self):
|
||||
"""Replace the static action used to call the wizard"""
|
||||
self.ensure_one()
|
||||
view = self.env.ref("payroll.view_hr_payslip_by_employees")
|
||||
company = self.company_id
|
||||
employee_ids = (
|
||||
self.env["hr.employee"]
|
||||
.search(
|
||||
[
|
||||
("company_id", "=", company.id),
|
||||
("contract_id.schedule_pay", "=", self.schedule_pay),
|
||||
]
|
||||
)
|
||||
.ids
|
||||
)
|
||||
return {
|
||||
"type": "ir.actions.act_window",
|
||||
"name": _("Generate Payslips"),
|
||||
"res_model": "hr.payslip.employees",
|
||||
"view_mode": "form",
|
||||
"view_id": view.id,
|
||||
"target": "new",
|
||||
"context": {
|
||||
"default_company_id": company.id,
|
||||
"default_schedule_pay": self.schedule_pay,
|
||||
"default_employee_ids": [(6, 0, employee_ids)],
|
||||
},
|
||||
}
|
||||
|
||||
def close_payslip_run(self):
|
||||
for run in self:
|
||||
if next((p for p in run.slip_ids if p.state == "draft"), False):
|
||||
raise UserError(
|
||||
_("The payslip batch %s still has unconfirmed " "payslips.")
|
||||
% run.name
|
||||
)
|
||||
self.update_periods()
|
||||
return super(HrPayslipRun, self).close_payslip_run()
|
||||
|
||||
def draft_payslip_run(self):
|
||||
for run in self:
|
||||
run.hr_period_id.button_re_open()
|
||||
return super(HrPayslipRun, self).draft_payslip_run()
|
||||
|
||||
def update_periods(self):
|
||||
self.ensure_one()
|
||||
period = self.hr_period_id
|
||||
if period:
|
||||
# Close the current period
|
||||
period.button_close()
|
||||
# Open the next period of the fiscal year
|
||||
fiscal_year = period.fiscalyear_id
|
||||
next_period = fiscal_year.search_period(number=period.number + 1)
|
||||
if next_period:
|
||||
next_period.button_open()
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
# Copyright 2015 Savoir-faire Linux. All Rights Reserved.
|
||||
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
#
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
from .hr_fiscal_year import get_schedules
|
||||
|
||||
|
||||
class HrPeriod(models.Model):
|
||||
_name = "hr.period"
|
||||
_inherit = "date.range"
|
||||
_description = "HR Payroll Period"
|
||||
_order = "date_start"
|
||||
|
||||
@api.model
|
||||
def _default_type(self, company_id=False):
|
||||
if not company_id:
|
||||
company_id = self.env.company
|
||||
if not isinstance(company_id, int):
|
||||
company_id = company_id.id
|
||||
period_type = self.env["date.range.type"].search(
|
||||
[("hr_period", "=", True), ("company_id", "=", company_id)], limit=1
|
||||
)
|
||||
return period_type
|
||||
|
||||
name = fields.Char(required=True, states={"draft": [("readonly", False)]})
|
||||
number = fields.Integer(required=True, states={"draft": [("readonly", False)]})
|
||||
date_payment = fields.Date(
|
||||
"Date of Payment", required=True, states={"draft": [("readonly", False)]}
|
||||
)
|
||||
fiscalyear_id = fields.Many2one(
|
||||
"hr.fiscalyear",
|
||||
"Fiscal Year",
|
||||
required=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
ondelete="cascade",
|
||||
)
|
||||
state = fields.Selection(
|
||||
[("draft", "Draft"), ("open", "Open"), ("done", "Closed")],
|
||||
"Status",
|
||||
required=True,
|
||||
default="draft",
|
||||
)
|
||||
company_id = fields.Many2one(
|
||||
"res.company",
|
||||
string="Company",
|
||||
store=True,
|
||||
related="fiscalyear_id.company_id",
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
)
|
||||
schedule_pay = fields.Selection(
|
||||
get_schedules,
|
||||
required=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
default="monthly",
|
||||
)
|
||||
payslip_ids = fields.One2many(
|
||||
"hr.payslip", "hr_period_id", "Payslips", readonly=True
|
||||
)
|
||||
|
||||
type_id = fields.Many2one(domain=[("hr_period", "=", True)], default=_default_type)
|
||||
|
||||
@api.model
|
||||
def get_next_period(self, company_id, schedule_pay):
|
||||
"""
|
||||
Get the next payroll period to process
|
||||
:rtype: hr.period browse record
|
||||
"""
|
||||
period = self.search(
|
||||
[
|
||||
("company_id", "=", company_id),
|
||||
("schedule_pay", "=", schedule_pay),
|
||||
("state", "=", "open"),
|
||||
],
|
||||
order="date_start",
|
||||
limit=1,
|
||||
)
|
||||
return period if period else False
|
||||
|
||||
def button_set_to_draft(self):
|
||||
for period in self:
|
||||
if period.payslip_ids:
|
||||
raise UserError(
|
||||
_(
|
||||
"You can not set to draft a period that already "
|
||||
"has payslips computed"
|
||||
)
|
||||
)
|
||||
|
||||
self.write({"state": "draft"})
|
||||
|
||||
def button_open(self):
|
||||
self.write({"state": "open"})
|
||||
|
||||
def button_close(self):
|
||||
self.write({"state": "done"})
|
||||
for period in self:
|
||||
fy = period.fiscalyear_id
|
||||
|
||||
# If all periods are closed, close the fiscal year
|
||||
if all(p.state == "done" for p in fy.period_ids):
|
||||
fy.write({"state": "done"})
|
||||
|
||||
def button_re_open(self):
|
||||
self.write({"state": "open"})
|
||||
for period in self:
|
||||
fy = period.fiscalyear_id
|
||||
if fy.state != "open":
|
||||
fy.write({"state": "open"})
|
||||
Loading…
Add table
Add a link
Reference in a new issue