@@ -1182,7 +1182,12 @@ def is_lwp(leave_type):
1182
1182
1183
1183
@frappe .whitelist ()
1184
1184
def get_events (start , end , filters = None ):
1185
- from frappe .desk .reportview import get_filters_cond
1185
+ import json
1186
+
1187
+ filters = json .loads (filters )
1188
+ for idx , filter in enumerate (filters ):
1189
+ # taking relevant fields from the list [doctype, fieldname, condition, value, hidden]
1190
+ filters [idx ] = filter [1 :- 1 ]
1186
1191
1187
1192
events = []
1188
1193
@@ -1196,91 +1201,67 @@ def get_events(start, end, filters=None):
1196
1201
employee = ""
1197
1202
company = frappe .db .get_value ("Global Defaults" , None , "default_company" )
1198
1203
1199
- conditions = get_filters_cond ("Leave Application" , filters , [])
1200
1204
# show department leaves for employee
1201
1205
if "Employee" in frappe .get_roles ():
1202
1206
add_department_leaves (events , start , end , employee , company )
1203
1207
1204
- add_leaves (events , start , end , conditions )
1208
+ add_leaves (events , start , end , filters )
1205
1209
add_block_dates (events , start , end , employee , company )
1206
1210
add_holidays (events , start , end , employee , company )
1207
1211
1208
1212
return events
1209
1213
1210
1214
1211
1215
def add_department_leaves (events , start , end , employee , company ):
1212
- department = frappe .db .get_value ("Employee" , employee , "department" )
1213
-
1214
- if not department :
1215
- return
1216
-
1217
- # department leaves
1218
- department_employees = frappe .db .sql_list (
1219
- """select name from tabEmployee where department=%s
1220
- and company=%s""" ,
1221
- (department , company ),
1216
+ if department := frappe .db .get_value ("Employee" , employee , "department" ):
1217
+ department_employees = frappe .get_list (
1218
+ "Employee" , filters = {"department" : department , "company" : company }, pluck = "name"
1219
+ )
1220
+ filters = [["employee" , "in" , department_employees ]]
1221
+ add_leaves (events , start , end , filters = filters )
1222
+
1223
+
1224
+ def add_leaves (events , start , end , filters = None ):
1225
+ if not filters :
1226
+ filters = []
1227
+ filters .extend (
1228
+ [
1229
+ ["from_date" , "<=" , getdate (end )],
1230
+ ["to_date" , ">=" , getdate (start )],
1231
+ ["status" , "in" , ["Approved" , "Open" ]],
1232
+ ["docstatus" , "<" , 2 ],
1233
+ ]
1222
1234
)
1223
1235
1224
- filter_conditions = ' and employee in ("%s")' % '", "' .join (department_employees )
1225
- add_leaves (events , start , end , filter_conditions = filter_conditions )
1226
-
1227
-
1228
- def add_leaves (events , start , end , filter_conditions = None ):
1229
- from frappe .desk .reportview import build_match_conditions
1230
-
1231
- conditions = []
1232
-
1233
- if not cint (
1234
- frappe .db .get_value ("HR Settings" , None , "show_leaves_of_all_department_members_in_calendar" )
1235
- ):
1236
- match_conditions = build_match_conditions ("Leave Application" )
1237
-
1238
- if match_conditions :
1239
- conditions .append (match_conditions )
1240
-
1241
- query = """SELECT
1242
- docstatus,
1243
- name,
1244
- employee,
1245
- employee_name,
1246
- leave_type,
1247
- from_date,
1248
- to_date,
1249
- half_day,
1250
- status,
1251
- color
1252
- FROM `tabLeave Application`
1253
- WHERE
1254
- from_date <= %(end)s AND to_date >= %(start)s <= to_date
1255
- AND docstatus < 2
1256
- AND status in ('Approved', 'Open')
1257
- """
1236
+ fields = [
1237
+ "name" ,
1238
+ "from_date" ,
1239
+ "to_date" ,
1240
+ "color" ,
1241
+ "docstatus" ,
1242
+ "employee_name" ,
1243
+ "leave_type" ,
1244
+ "(1) as allDay" ,
1245
+ "'Leave Application' as doctype" ,
1246
+ ]
1247
+
1248
+ show_leaves_of_all_members = frappe .db .get_single_value (
1249
+ "HR Settings" , "show_leaves_of_all_department_members_in_calendar"
1250
+ )
1251
+ if cint (show_leaves_of_all_members ):
1252
+ leave_applications = frappe .get_all ("Leave Application" , filters = filters , fields = fields )
1253
+ else :
1254
+ leave_applications = frappe .get_list ("Leave Application" , filters = filters , fields = fields )
1258
1255
1259
- if conditions :
1260
- query += " AND " + " AND " .join (conditions )
1261
-
1262
- if filter_conditions :
1263
- query += filter_conditions
1264
-
1265
- for d in frappe .db .sql (query , {"start" : start , "end" : end }, as_dict = True ):
1266
- e = {
1267
- "name" : d .name ,
1268
- "doctype" : "Leave Application" ,
1269
- "from_date" : d .from_date ,
1270
- "to_date" : d .to_date ,
1271
- "docstatus" : d .docstatus ,
1272
- "color" : d .color ,
1273
- "all_day" : int (not d .half_day ),
1274
- "title" : cstr (d .employee_name )
1275
- + f" ({ cstr (d .leave_type )} )"
1276
- + (" " + _ ("(Half Day)" ) if d .half_day else "" ),
1277
- }
1278
- if e not in events :
1279
- events .append (e )
1256
+ for d in leave_applications :
1257
+ d ["title" ] = f"{ d ['employee_name' ]} ({ d ['leave_type' ]} )"
1258
+ del d ["employee_name" ]
1259
+ del d ["leave_type" ]
1260
+ if d not in events :
1261
+ events .append (d )
1280
1262
1281
1263
1282
1264
def add_block_dates (events , start , end , employee , company ):
1283
- # block days
1284
1265
cnt = 0
1285
1266
block_dates = get_applicable_block_dates (start , end , employee , company , all_lists = True )
1286
1267
@@ -1292,6 +1273,7 @@ def add_block_dates(events, start, end, employee, company):
1292
1273
"to_date" : block_date .block_date ,
1293
1274
"title" : _ ("Leave Blocked" ) + ": " + block_date .reason ,
1294
1275
"name" : "_" + str (cnt ),
1276
+ "allDay" : 1 ,
1295
1277
}
1296
1278
)
1297
1279
cnt += 1
@@ -1315,6 +1297,7 @@ def add_holidays(events, start, end, employee, company):
1315
1297
"to_date" : holiday .holiday_date ,
1316
1298
"title" : _ ("Holiday" ) + ": " + cstr (holiday .description ),
1317
1299
"name" : holiday .name ,
1300
+ "allDay" : 1 ,
1318
1301
}
1319
1302
)
1320
1303
0 commit comments