Skip to content

Commit

Permalink
fix: clear company field in single docs, delete other linked docs
Browse files Browse the repository at this point in the history
(cherry picked from commit 6cd653b)
  • Loading branch information
AyshaHakeem authored and mergify[bot] committed Jan 24, 2025
1 parent 466e487 commit d026ca5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion hrms/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
"hrms.overrides.company.make_company_fixtures",
"hrms.overrides.company.set_default_hr_accounts",
],
"on_trash": "hrms.overrides.company.clear_company_field_in_linked_docs",
"on_trash": "hrms.overrides.company.handle_linked_docs",
},
"Holiday List": {
"on_update": "hrms.utils.holiday_list.invalidate_cache",
Expand Down
61 changes: 47 additions & 14 deletions hrms/overrides/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,53 @@ def validate_default_accounts(doc, method=None):
)


def clear_company_field_in_linked_docs(doc, method=None):
def handle_linked_docs(doc, method=None):
delete_docs_with_company_field(doc)
clear_company_field_for_single_doctypes(doc)


def delete_docs_with_company_field(doc, method=None):
"""
Deletes records from linked doctypes where the 'company' field matches the company's name
"""
company_data_to_be_ignored = frappe.get_hooks("company_data_to_be_ignored") or []
for doctype in company_data_to_be_ignored:
# get field in the doctype linked to Company
company_field = frappe.db.get_value(
"DocField",
{"parent": doctype, "fieldtype": "Link", "options": "Company"},
"fieldname",
records_to_delete = frappe.get_all(doctype, filters={"company": doc.name}, pluck="name")
if records_to_delete:
frappe.db.delete(doctype, {"name": ["in", records_to_delete]})


def clear_company_field_for_single_doctypes(doc):
"""
Clears the 'company' value in Single doctypes where applicable
"""
single_docs = get_single_doctypes_with_company_field()
singles = frappe.qb.DocType("Singles")
(
frappe.qb.update(singles)
.set(singles.value, "")
.where(singles.doctype.isin(single_docs))
.where(singles.field == "company")
.where(singles.value == doc.name)
).run()


def get_single_doctypes_with_company_field():
DocType = frappe.qb.DocType("DocType")
DocField = frappe.qb.DocType("DocField")

return (
frappe.qb.from_(DocField)
.select(DocField.parent)
.where(
(DocField.fieldtype == "Link")
& (DocField.options == "Company")
& (
DocField.parent.isin(
frappe.qb.from_(DocType)
.select(DocType.name)
.where((DocType.issingle == 1) & (DocType.module.isin(["HR", "Payroll"])))
)
)
)

if company_field:
doctype_table = frappe.qb.DocType(doctype)
(
frappe.qb.update(doctype_table)
.set(doctype_table[company_field], "")
.where(doctype_table[company_field] == doc.name)
).run()
).run(pluck=True)

0 comments on commit d026ca5

Please sign in to comment.