Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: savepoint policy assignment submission, log errors & inform the user about failures #459

Merged
merged 4 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from frappe.utils import (
add_months,
cint,
comma_and,
date_diff,
flt,
formatdate,
Expand Down Expand Up @@ -259,14 +260,15 @@ def is_earned_leave_applicable_for_current_month(date_of_joining, allocate_on_da

@frappe.whitelist()
def create_assignment_for_multiple_employees(employees, data):

if isinstance(employees, str):
employees = json.loads(employees)

if isinstance(data, str):
data = frappe._dict(json.loads(data))

docs_name = []
failed = []

for employee in employees:
assignment = frappe.new_doc("Leave Policy Assignment")
assignment.employee = employee
Expand All @@ -277,18 +279,45 @@ def create_assignment_for_multiple_employees(employees, data):
assignment.leave_period = data.leave_period or None
assignment.carry_forward = data.carry_forward
assignment.save()

savepoint = "before_assignment_submission"
try:
frappe.db.savepoint(savepoint)
assignment.submit()
except frappe.exceptions.ValidationError:
continue

frappe.db.commit()
except Exception as e:
frappe.db.rollback(save_point=savepoint)
assignment.log_error("Leave Policy Assignment submission failed")
failed.append(assignment.name)

docs_name.append(assignment.name)

if failed:
show_assignment_submission_status(failed)

return docs_name


def show_assignment_submission_status(failed):
frappe.clear_messages()
assignment_list = [get_link_to_form("Leave Policy Assignment", entry) for entry in failed]

msg = _("Failed to submit some leave policy assignments:")
msg += " " + comma_and(assignment_list, False)
msg += "<br><hr><br>"
ruchamahabal marked this conversation as resolved.
Show resolved Hide resolved
msg += (
_("Check {0} for more details")
.format("<a href='/app/List/Error Log?reference_doctype=Leave Policy Assignment'>{0}</a>")
.format(_("Error Log"))
)

frappe.msgprint(
msg,
indicator="red",
title=_("Submission Failed"),
is_minimizable=True,
)


def get_leave_type_details():
leave_type_details = frappe._dict()
leave_types = frappe.get_all(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ frappe.listview_settings['Leave Policy Assignment'] = {
},
get_query() {
let filters = {"is_active": 1};
if (cur_dialog.fields_dict.company.value)
if (cur_dialog?.fields_dict?.company?.value)
filters["company"] = cur_dialog.fields_dict.company.value;

return {
Expand Down