Files
default_thai_company/default_thai_company/payroll.py
T

63 lines
2.0 KiB
Python

import frappe
from frappe.utils import getdate
from default_thai_company.tax_withholding import thai_companies
# Thai personal income tax (P.N.D.1/91) is assessed per calendar year whatever
# the company's fiscal year, so the Payroll Period is January to December.
# Payroll Period is named by prompt (site-wide unique) but bound to one company,
# so each Thai company gets "<year> - <abbr>", like the Shipping Rule.
def hrms_installed():
return "hrms" in frappe.get_installed_apps()
def payroll_period_name(company, year):
abbr = frappe.get_cached_value("Company", company, "abbr")
return f"{year} - {abbr}"
def ensure_payroll_period(company, year):
"""Calendar-year Payroll Period for `company`. No-op when any period of the
company touches the year: an existing one covers it, or the company runs its
own scheme and a second period would only fail the overlap check."""
start, end = f"{year}-01-01", f"{year}-12-31"
if frappe.db.exists(
"Payroll Period", {"company": company, "start_date": ("<=", end), "end_date": (">=", start)}
):
return
frappe.get_doc(
{
"doctype": "Payroll Period",
"name": payroll_period_name(company, year),
"company": company,
"start_date": start,
"end_date": end,
}
).insert(ignore_permissions=True)
def setup_company(doc, method=None):
"""Company.on_update: this year's Payroll Period for a Thai company."""
if doc.country == "Thailand" and hrms_installed():
ensure_payroll_period(doc.name, getdate().year)
def setup_companies():
"""after_install, the daily scheduler, and the create_payroll_periods patch:
this year's Payroll Period for every Thai company. The scheduler rolls the
default over on 1 January."""
if not hrms_installed():
return
year = getdate().year
for company in thai_companies():
ensure_payroll_period(company, year)
def after_app_install(app_name):
"""Payroll Period only exists once hrms is installed; catch companies that
were created before it."""
if app_name == "hrms":
setup_companies()