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

export icalendar file on_change hook #51

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion hr_addon/hooks.py
Expand Up @@ -187,4 +187,10 @@
# For example: Role, Gender, etc.
# translated_search_doctypes = []

required_apps = ["hrms"]
required_apps = ["hrms"]

doc_events = {
"Leave Application": {
"on_change": "hr_addon.hr_addon.api.export_calendar.export_calendar",
}
}
41 changes: 41 additions & 0 deletions hr_addon/hr_addon/api/export_calendar.py
@@ -0,0 +1,41 @@
import io
import frappe
from icalendar import Event, Calendar
from datetime import datetime
from frappe.utils.file_manager import save_file

def generate_leave_ical_file(leave_applications):
cal = Calendar()

for leave_application in leave_applications:
event = Event()

# Extract data from the Leave Application document
start_date = leave_application.get('from_date')
end_date = leave_application.get('to_date')
employee_name = leave_application.get('employee')
leave_type = leave_application.get('leave_type')
description = leave_application.get('description')

event.add('dtstart', start_date)
event.add('dtend', end_date)
event.add('summary', f'{employee_name} - {leave_type}')
event.add('description', description)

cal.add_component(event)

# Generate the iCalendar data
ical_data = cal.to_ical()

return ical_data

def export_calendar(doc, method=None):
if doc.status == "Approved":
leave_applications = frappe.db.get_list("Leave Application",
filters={"status": "Approved"},
fields=["from_date", "to_date", "employee", "leave_type", "description"])
ical_data = generate_leave_ical_file(leave_applications)

# Save the iCalendar data as a File document
file_name = "Urlaubskalender.ics" # Set the desired filename here
save_file(file_name, ical_data, dt="Leave Application", dn=doc.name, is_private=1)
1 change: 1 addition & 0 deletions requirements.txt
@@ -1,3 +1,4 @@
# frappe -- https://github.com/frappe/frappe is installed via 'bench init'
#paytmchecksum~=1.7.0
#hrms
icalendar