def calculate_total_hours(week_start, week_end, month_start, month_end): from datetime import datetime, timedelta import datetime emp = frappe.db.get_values("Employee", {"user_id":frappe.session.user}, ["name"], as_dict= True) if emp: month_start = month_start + " " + "00:00:00" month_end = month_end + " " + "00:00:00" week_end = week_end.replace("00:00:00", "23:59:59") today = datetime.date.today() week_start_day = today - datetime.timedelta(days=7) week_start_day = week_start_day - datetime.timedelta(days=today.weekday()) week_end_day = week_start_day + datetime.timedelta(days=6) week_start_day = cstr(week_start_day) + " " + "00:00:00" week_end_day = cstr(week_end_day) + " " + "23:59:59" last_week_hours = frappe.db.sql(""" select ifnull(sum(tsd.hours),0) from `tabTimesheet`ts, `tabTimesheet Detail` tsd where ts.name = tsd.parent and ts.employee = '%s' and ts.docstatus != 2 and tsd.from_time between '%s' and '%s' """%(emp[0]['name'], week_start_day, week_end_day), as_list=1) last_week_hours = "%.3f" % last_week_hours[0][0] monthly_hours = frappe.db.sql(""" select ifnull(sum(tsd.hours),0) from `tabTimesheet`ts, `tabTimesheet Detail` tsd where ts.name = tsd.parent and ts.employee = '%s' and ts.docstatus != 2 and tsd.from_time between '%s' and '%s' """%(emp[0]['name'], month_start, month_end), as_list=1) monthly_hours = "%.3f" % monthly_hours[0][0] return last_week_hours, monthly_hours else: frappe.throw(_("Logged In user has not an Employee to create Timesheet. Please create Employee at first."))
def get_from_to_date(self): today = now_datetime().date() # decide from date based on email digest frequency if self.frequency == "Daily": # from date, to_date is yesterday from_date = to_date = today - timedelta(days=1) elif self.frequency == "Weekly": # from date is the previous week's monday from_date = today - timedelta(days=today.weekday(), weeks=1) # to date is sunday i.e. the previous day to_date = from_date + timedelta(days=6) else: # from date is the 1st day of the previous month from_date = today - relativedelta(days=today.day - 1, months=1) # to date is the last day of the previous month to_date = today - relativedelta(days=today.day) return from_date, to_date