old_contents: # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  # License: GNU General Public License v3. See license.txt
  
  from __future__ import unicode_literals
  import frappe
  
  from frappe.utils import getdate, nowdate
  from frappe import _
  from frappe.model.document import Document
  from erpnext.hr.utils import set_employee_name
  
  class Attendance(Document):
  	def validate_duplicate_record(self):
  		res = frappe.db.sql("""select name from `tabAttendance` where employee = %s and attendance_date = %s
  			and name != %s and docstatus = 1""",
  			(self.employee, self.attendance_date, self.name))
  		if res:
  			frappe.throw(_("Attendance for employee {0} is already marked").format(self.employee))
  
  		set_employee_name(self)
  
  	def check_leave_record(self):
  		leave_record = frappe.db.sql("""select leave_type, half_day from `tabLeave Application`
  			where employee = %s and %s between from_date and to_date and status = 'Approved'
  			and docstatus = 1""", (self.employee, self.attendance_date), as_dict=True)
  		if leave_record:
  			if leave_record[0].half_day:
  				self.status = 'Half Day'
  				frappe.msgprint(_("Employee {0} on Half day on {1}").format(self.employee, self.attendance_date))
  			else:
  				self.status = 'On Leave'
  				self.leave_type = leave_record[0].leave_type
  				frappe.msgprint(_("Employee {0} on Leave on {1}").format(self.employee, self.attendance_date))
  		if self.status == "On Leave" and not leave_record:
  			frappe.throw(_("No leave record found for employee {0} for {1}").format(self.employee, self.attendance_date))
  
  	def validate_attendance_date(self):
  		if getdate(self.attendance_date) > getdate(nowdate()):
  			frappe.throw(_("Attendance can not be marked for future dates"))
  		elif getdate(self.att_date) < frappe.db.get_value("Employee", self.employee, "date_of_joining"):
  			frappe.throw(_("Attendance date can not be less than employee's joining date"))
  
  	def validate_employee(self):
  		emp = frappe.db.sql("select name from `tabEmployee` where name = %s and status = 'Active'",
  		 	self.employee)
  		if not emp:
  			frappe.throw(_("Employee {0} is not active or does not exist").format(self.employee))
  
  	def validate(self):
  		from erpnext.controllers.status_updater import validate_status
  		validate_status(self.status, ["Present", "Absent", "On Leave", "Half Day"])
  		self.validate_attendance_date()
  		self.validate_duplicate_record()
  		self.check_leave_record()
  
new_contents: # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  # License: GNU General Public License v3. See license.txt
  
  from __future__ import unicode_literals
  import frappe
  
  from frappe.utils import getdate, nowdate
  from frappe import _
  from frappe.model.document import Document
  from erpnext.hr.utils import set_employee_name
  
  class Attendance(Document):
  	def validate_duplicate_record(self):
  		res = frappe.db.sql("""select name from `tabAttendance` where employee = %s and attendance_date = %s
  			and name != %s and docstatus = 1""",
  			(self.employee, self.attendance_date, self.name))
  		if res:
  			frappe.throw(_("Attendance for employee {0} is already marked").format(self.employee))
  
  		set_employee_name(self)
  
  	def check_leave_record(self):
  		leave_record = frappe.db.sql("""select leave_type, half_day from `tabLeave Application`
  			where employee = %s and %s between from_date and to_date and status = 'Approved'
  			and docstatus = 1""", (self.employee, self.attendance_date), as_dict=True)
  		if leave_record:
  			if leave_record[0].half_day:
  				self.status = 'Half Day'
  				frappe.msgprint(_("Employee {0} on Half day on {1}").format(self.employee, self.attendance_date))
  			else:
  				self.status = 'On Leave'
  				self.leave_type = leave_record[0].leave_type
  				frappe.msgprint(_("Employee {0} on Leave on {1}").format(self.employee, self.attendance_date))
  		if self.status == "On Leave" and not leave_record:
  			frappe.throw(_("No leave record found for employee {0} for {1}").format(self.employee, self.attendance_date))
  
  	def validate_attendance_date(self):
  		if getdate(self.attendance_date) > getdate(nowdate()):
  			frappe.throw(_("Attendance can not be marked for future dates"))
  		elif getdate(self.attendance_date) < frappe.db.get_value("Employee", self.employee, "date_of_joining"):
  			frappe.throw(_("Attendance date can not be less than employee's joining date"))
  
  	def validate_employee(self):
  		emp = frappe.db.sql("select name from `tabEmployee` where name = %s and status = 'Active'",
  		 	self.employee)
  		if not emp:
  			frappe.throw(_("Employee {0} is not active or does not exist").format(self.employee))
  
  	def validate(self):
  		from erpnext.controllers.status_updater import validate_status
  		validate_status(self.status, ["Present", "Absent", "On Leave", "Half Day"])
  		self.validate_attendance_date()
  		self.validate_duplicate_record()
  		self.check_leave_record()
  
subject: Update attendance.py