add Leave Type export button and default setup from JSON

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Ali 2026-03-26 16:04:06 +04:00
parent dc273b3f2b
commit 4c2eb7e400
4 changed files with 69 additions and 1 deletions

View File

@ -0,0 +1,20 @@
import frappe
SKIP_FIELDS = {"name", "creation", "modified", "modified_by", "owner", "docstatus", "idx"}
@frappe.whitelist()
def export_leave_types():
leave_types = frappe.get_all("Leave Type", fields=["name"])
result = []
for lt in leave_types:
doc = frappe.get_doc("Leave Type", lt.name)
data = doc.as_dict()
for field in SKIP_FIELDS:
data.pop(field, None)
result.append(data)
return result

View File

@ -33,6 +33,7 @@ doctype_list_js = {
"Currency Exchange": "public/js/currency_exchange_list.js",
"Salary Component": "public/js/salary_component_list.js",
"Account": "public/js/account_list.js",
"Leave Type": "public/js/leave_type_list.js",
}
extend_doctype_class = {
@ -82,7 +83,10 @@ doc_events = {
# Asset categories are now created automatically via Company.on_update event
# setup_wizard_complete = "jey_erp.setup.setup_wizard_handler.setup_wizard_complete_handler"
setup_wizard_complete = "jey_erp.setup.setup_wizard_handler.create_default_salary_components"
setup_wizard_complete = [
"jey_erp.setup.setup_wizard_handler.create_default_salary_components",
"jey_erp.setup.setup_wizard_handler.create_default_leave_types",
]
after_migrate = "jey_erp.hooks.after_migrate_combined"

View File

@ -0,0 +1,20 @@
frappe.listview_settings["Leave Type"] = {
onload: function (listview) {
listview.page.add_inner_button(__("Export Defaults"), function () {
frappe.call({
method: "jey_erp.custom.leave_type_export.export_leave_types",
callback: function (r) {
if (!r.message) return;
const json = JSON.stringify(r.message, null, 2);
const blob = new Blob([json], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "leave_types.json";
a.click();
URL.revokeObjectURL(url);
},
});
});
},
};

View File

@ -9,6 +9,12 @@ SALARY_COMPONENTS_JSON = os.path.join(
"salary_components.json",
)
LEAVE_TYPES_JSON = os.path.join(
os.path.dirname(os.path.dirname(__file__)),
"default_data",
"leave_types.json",
)
HRMS_DEFAULT_COMPONENTS = ["Basic", "Arrear", "Leave Encashment", "Income Tax"]
@ -39,6 +45,24 @@ def create_default_salary_components(args):
frappe.db.commit()
def create_default_leave_types(args):
if not os.path.exists(LEAVE_TYPES_JSON):
return
with open(LEAVE_TYPES_JSON, encoding="utf-8") as f:
leave_types = json.load(f)
for data in leave_types:
name = data.get("leave_type_name")
if not name or frappe.db.exists("Leave Type", name):
continue
data["doctype"] = "Leave Type"
frappe.get_doc(data).insert(ignore_permissions=True)
frappe.db.commit()
def setup_wizard_complete_handler(args):
"""
Вызывается после завершения ERPNext setup wizard.