118 lines
3.5 KiB
Python
118 lines
3.5 KiB
Python
from odoo import api, SUPERUSER_ID
|
|
|
|
|
|
def post_init_hook(env_or_cr, registry=None):
|
|
if registry is not None:
|
|
env = api.Environment(env_or_cr, SUPERUSER_ID, {})
|
|
else:
|
|
env = env_or_cr
|
|
SimplePayrollSetup(env).run()
|
|
|
|
|
|
class SimplePayrollSetup:
|
|
def __init__(self, env):
|
|
self.env = env
|
|
self.Category = env["hr.salary.rule.category"]
|
|
self.Rule = env["hr.salary.rule"]
|
|
self.Structure = env["hr.payroll.structure"]
|
|
self.Contract = env["hr.contract"]
|
|
self.Company = env["res.company"]
|
|
|
|
def run(self):
|
|
categories = self._ensure_categories()
|
|
for company in self.Company.search([]):
|
|
structure = self._ensure_structure(company, categories)
|
|
self._assign_open_contracts(company, structure)
|
|
|
|
def _ensure_categories(self):
|
|
specs = [
|
|
("BASIC", "Basic"),
|
|
("GROSS", "Gross"),
|
|
("NET", "Net"),
|
|
]
|
|
result = {}
|
|
for code, name in specs:
|
|
category = self.Category.search([("code", "=", code)], limit=1)
|
|
if not category:
|
|
category = self.Category.create({"code": code, "name": name})
|
|
result[code] = category
|
|
return result
|
|
|
|
def _ensure_rule(self, company, code, name, sequence, category, python_code):
|
|
rule = self.Rule.search(
|
|
[
|
|
("code", "=", code),
|
|
("company_id", "=", company.id),
|
|
("name", "=", name),
|
|
],
|
|
limit=1,
|
|
)
|
|
values = {
|
|
"name": name,
|
|
"code": code,
|
|
"sequence": sequence,
|
|
"category_id": category.id,
|
|
"company_id": company.id,
|
|
"condition_select": "none",
|
|
"amount_select": "code",
|
|
"amount_python_compute": python_code.strip(),
|
|
"appears_on_payslip": True,
|
|
"active": True,
|
|
}
|
|
if rule:
|
|
rule.write(values)
|
|
else:
|
|
rule = self.Rule.create(values)
|
|
return rule
|
|
|
|
def _ensure_structure(self, company, categories):
|
|
basic = self._ensure_rule(
|
|
company,
|
|
"MCS_BASIC",
|
|
"Basic Salary",
|
|
10,
|
|
categories["BASIC"],
|
|
"result = contract.wage",
|
|
)
|
|
gross = self._ensure_rule(
|
|
company,
|
|
"MCS_GROSS",
|
|
"Gross Salary",
|
|
100,
|
|
categories["GROSS"],
|
|
"result = categories.BASIC",
|
|
)
|
|
net = self._ensure_rule(
|
|
company,
|
|
"MCS_NET",
|
|
"Net Salary",
|
|
200,
|
|
categories["NET"],
|
|
"result = categories.GROSS",
|
|
)
|
|
structure = self.Structure.search(
|
|
[("code", "=", "MCS_SIMPLE_MONTHLY"), ("company_id", "=", company.id)],
|
|
limit=1,
|
|
)
|
|
values = {
|
|
"name": "MCS Simple Monthly Payroll",
|
|
"code": "MCS_SIMPLE_MONTHLY",
|
|
"company_id": company.id,
|
|
"rule_ids": [(6, 0, [basic.id, gross.id, net.id])],
|
|
}
|
|
if structure:
|
|
structure.write(values)
|
|
else:
|
|
structure = self.Structure.create(values)
|
|
return structure
|
|
|
|
def _assign_open_contracts(self, company, structure):
|
|
contracts = self.Contract.search(
|
|
[
|
|
("company_id", "=", company.id),
|
|
("state", "=", "open"),
|
|
("employee_id.active", "=", True),
|
|
]
|
|
)
|
|
contracts.write({"struct_id": structure.id, "schedule_pay": "monthly"})
|