<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from datetime import datetime as dt,timedelta

class TimesheetReport(Document):
	def validate(self):
		self.get_timesheet_report()

	def get_timesheet_report(self):
		# validate from date to date
		# If send notification to manager if marked hours are more than 8 hrs
		# fetch the records and save the data in HTML table as string
		# save record in html_code fields
		# return the html_code string to render the table on html field

		timesheet_report = ""

		if self.is_valid_from_to_dates():
			# get the attendance records between given date range
			query = """SELECT DISTINCT
					    ats.task,
					    att.att_date,
					    att.status,
					    att.working_hours as day_working_hours,
					    SEC_TO_TIME(SUM(TIME_TO_SEC(ats.working_hours))) as task_working_hours,
					    MIN(ats.in_time)  AS in_time,
					    MAX(ats.out_time) AS out_time
					FROM
					    `tabAttendance` AS att,
					    `tabAttendance Time Sheet` ats
					WHERE
					    att.docstatus=1
					AND att.employee='{emp}'
					AND (
					        att.att_date BETWEEN '{from_date}' AND '{to_date}')
					AND ats.parent=att.name
					GROUP BY
					    ats.task,
					    att.att_date
					ORDER BY
					    att.att_date DESC, ats.idx ASC""".format(emp=self.employee, from_date=self.from_date, to_date=self.to_date)

			records = frappe.db.sql(query,as_dict=True)
			records = self.get_formatted_records(records)
			timesheet_html_report = self.get_html_code(records.get("timesheet_record"), records.get("timing_totals"))

	def is_valid_from_to_dates(self):
		if self.from_date and self.to_date:
			from_date = dt.strptime(self.from_date,"%Y-%m-%d")
			to_date = dt.strptime(self.to_date,"%Y-%m-%d")
			date_diff = to_date - from_date

			if (from_date.strftime("%a") == "Fri") and (to_date.strftime("%a") == "Thu") and (date_diff.days == 6):
				return True
			else:
				frappe.throw("Invalid From Date and To Date")
		else:
			frappe.throw("From Date and To Date fields are mandatory")

	def get_formatted_records(self,records):
		day_to_index={'Fri':1,'Sat':2,'Sun':3,'Mon':4,'Tue':5,'Wed':6,'Thu':7}

		days = ['Day','Fri','Sat','Sun','Mon','Tue','Wed','Thu']
		dates = ["Date",'','','','','','','']
		# setting up dates
		from_date = dt.strptime(self.from_date,"%Y-%m-%d")
		for i in range(1,8):
			date = from_date + timedelta(i-1)<commit_msg>raised mandatory extraption instade of frappe.throw<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from datetime import datetime as dt,timedelta

class TimesheetReport(Document):
	def validate(self):
		self.get_timesheet_report()

	def get_timesheet_report(self):
		# validate from date to date
		# If send notification to manager if marked hours are more than 8 hrs
		# fetch the records and save the data in HTML table as string
		# save record in html_code fields
		# return the html_code string to render the table on html field

		timesheet_report = ""

		if self.is_valid_from_to_dates():
			# get the attendance records between given date range
			query = """SELECT DISTINCT
					    ats.task,
					    att.att_date,
					    att.status,
					    att.working_hours as day_working_hours,
					    SEC_TO_TIME(SUM(TIME_TO_SEC(ats.working_hours))) as task_working_hours,
					    MIN(ats.in_time)  AS in_time,
					    MAX(ats.out_time) AS out_time
					FROM
					    `tabAttendance` AS att,
					    `tabAttendance Time Sheet` ats
					WHERE
					    att.docstatus=1
					AND att.employee='{emp}'
					AND (
					        att.att_date BETWEEN '{from_date}' AND '{to_date}')
					AND ats.parent=att.name
					GROUP BY
					    ats.task,
					    att.att_date
					ORDER BY
					    att.att_date DESC, ats.idx ASC""".format(emp=self.employee, from_date=self.from_date, to_date=self.to_date)

			records = frappe.db.sql(query,as_dict=True)
			records = self.get_formatted_records(records)
			timesheet_html_report = self.get_html_code(records.get("timesheet_record"), records.get("timing_totals"))

	def is_valid_from_to_dates(self):
		if self.from_date and self.to_date:
			from_date = dt.strptime(self.from_date,"%Y-%m-%d")
			to_date = dt.strptime(self.to_date,"%Y-%m-%d")
			date_diff = to_date - from_date

			if (from_date.strftime("%a") == "Fri") and (to_date.strftime("%a") == "Thu") and (date_diff.days == 6):
				return True
			else:
				raise frappe.ValidationError("Invalid From Date and To Date")
		else:
			raise frappe.MandatoryError("From Date and To Date fields are mandatory")

	def get_formatted_records(self,records):
		day_to_index={'Fri':1,'Sat':2,'Sun':3,'Mon':4,'Tue':5,'Wed':6,'Thu':7}

		days = ['Day','Fri','Sat','Sun','Mon','Tue','Wed','Thu']
		dates = ["Date",'','','','','','','']
		# setting up dates
		from_date = dt.strptime(self.from_date,"%Y-%m-%d")
		for i in range(1,8):
			date = from_date + timedelta(i-1)
{'fraction_non_alphanumeric': 0.12696906433858418, 'fraction_numerical': 0.006832416018219776, 'mean_word_length': 3.66784765279008, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4437512', 'n_tokens_mistral': 2124, 'n_tokens_neox': 1758, 'n_words': 438}