# -*- coding: utf-8 -*-
# Copyright (c) 2017, VHRS and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import json
import frappe
from frappe.model.document import Document
from frappe.model.naming import make_autoname
import time
from frappe.utils.data import today, get_timestamp


@frappe.whitelist(allow_guest=True)
def attendance():
    # restrict request from list of IP addresses
    userid = frappe.form_dict.get("userid")
    employee = frappe.db.get_value("Employee", {
        "employee_no": userid})
    if employee:
        date = time.strftime("%Y-%m-%d", time.gmtime(
            int(frappe.form_dict.get("att_time"))))
        is_leave = check_leave_record(employee, date)
        if is_leave == 'Half Day' or is_leave == 'On Leave':
            pass
        else:
            name, company = frappe.db.get_value(
                "Employee", employee, ["employee_name", "company"])
            attendance_id = frappe.db.get_value("Attendance", {
                "employee": employee, "attendance_date": date})
            if attendance_id:
                attendance = frappe.get_doc("Attendance", attendance_id)
                out_time = time.strftime("%H:%M:%S", time.gmtime(
                    int(frappe.form_dict.get("att_time"))))
                times = [out_time, attendance.in_time]
                attendance.out_time = max(times)
                attendance.in_time = min(times)
                attendance.db_update()
            else:
                attendance = frappe.new_doc("Attendance")
                in_time = time.strftime("%H:%M:%S", time.gmtime(
                    int(frappe.form_dict.get("att_time"))))
                attendance.update({
                    "employee": employee,
                    "employee_name": name,
                    "attendance_date": date,
                    "status": "Present",
                    "in_time": in_time,
                    "company": company
                })
            attendance.save(ignore_permissions=True)
            attendance.submit()
            frappe.db.commit()
        frappe.response.type = "text"
        return "ok"


def check_leave_record(employee, date):
    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""", (employee, date), as_dict=True)
    if leave_record:
        if leave_record[0].half_day:
            status = 'Half Day'
        else:
            status = 'On Leave'
            leave_type = leave_record[0].leave_type

        return status

b90bc0eeed353d584af297ff175c4d1e7574318d
<mask token>

<mask token>


def check_leave_record(employee, date):
    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"""
        , (employee, date), as_dict=True)
    if leave_record:
        if leave_record[0].half_day:
            status = 'Half Day'
        else:
            status = 'On Leave'
            leave_type = leave_record[0].leave_type
        return status

<mask token>


@frappe.whitelist(allow_guest=True)
def attendance():
    userid = frappe.form_dict.get('userid')
    employee = frappe.db.get_value('Employee', {'employee_no': userid})
    if employee:
        date = time.strftime('%Y-%m-%d', time.gmtime(int(frappe.form_dict.
            get('att_time'))))
        is_leave = check_leave_record(employee, date)
        if is_leave == 'Half Day' or is_leave == 'On Leave':
            pass
        else:
            name, company = frappe.db.get_value('Employee', employee, [
                'employee_name', 'company'])
            attendance_id = frappe.db.get_value('Attendance', {'employee':
                employee, 'attendance_date': date})
            if attendance_id:
                attendance = frappe.get_doc('Attendance', attendance_id)
                out_time = time.strftime('%H:%M:%S', time.gmtime(int(frappe
                    .form_dict.get('att_time'))))
                times = [out_time, attendance.in_time]
                attendance.out_time = max(times)
                attendance.in_time = min(times)
                attendance.db_update()
            else:
                attendance = frappe.new_doc('Attendance')
                in_time = time.strftime('%H:%M:%S', time.gmtime(int(frappe.
                    form_dict.get('att_time'))))
                attendance.update({'employee': employee, 'employee_name':
                    name, 'attendance_date': date, 'status': 'Present',
                    'in_time': in_time, 'company': company})
            attendance.save(ignore_permissions=True)
            attendance.submit()
            frappe.db.commit()
        frappe.response.type = 'text'
        return 'ok'


def check_leave_record(employee, date):
    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"""
        , (employee, date), as_dict=True)
    if leave_record:
        if leave_record[0].half_day:
            status = 'Half Day'
        else:
            status = 'On Leave'
            leave_type = leave_record[0].leave_type
        return status

from __future__ import unicode_literals
import json
import frappe
from frappe.model.document import Document
from frappe.model.naming import make_autoname
import time
from frappe.utils.data import today, get_timestamp


@frappe.whitelist(allow_guest=True)
def attendance():
    userid = frappe.form_dict.get('userid')
    employee = frappe.db.get_value('Employee', {'employee_no': userid})
    if employee:
        date = time.strftime('%Y-%m-%d', time.gmtime(int(frappe.form_dict.
            get('att_time'))))
        is_leave = check_leave_record(employee, date)
        if is_leave == 'Half Day' or is_leave == 'On Leave':
            pass
        else:
            name, company = frappe.db.get_value('Employee', employee, [
                'employee_name', 'company'])
            attendance_id = frappe.db.get_value('Attendance', {'employee':
                employee, 'attendance_date': date})
            if attendance_id:
                attendance = frappe.get_doc('Attendance', attendance_id)
                out_time = time.strftime('%H:%M:%S', time.gmtime(int(frappe
                    .form_dict.get('att_time'))))
                times = [out_time, attendance.in_time]
                attendance.out_time = max(times)
                attendance.in_time = min(times)
                attendance.db_update()
            else:
                attendance = frappe.new_doc('Attendance')
                in_time = time.strftime('%H:%M:%S', time.gmtime(int(frappe.
                    form_dict.get('att_time'))))
                attendance.update({'employee': employee, 'employee_name':
                    name, 'attendance_date': date, 'status': 'Present',
                    'in_time': in_time, 'company': company})
            attendance.save(ignore_permissions=True)
            attendance.submit()
            frappe.db.commit()
        frappe.response.type = 'text'
        return 'ok'


def check_leave_record(employee, date):
    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"""
        , (employee, date), as_dict=True)
    if leave_record:
        if leave_record[0].half_day:
            status = 'Half Day'
        else:
            status = 'On Leave'
            leave_type = leave_record[0].leave_type
        return status

# -*- coding: utf-8 -*-
# Copyright (c) 2017, VHRS and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import json
import frappe
from frappe.model.document import Document
from frappe.model.naming import make_autoname
import time
from frappe.utils.data import today, get_timestamp


@frappe.whitelist(allow_guest=True)
def attendance():
    # restrict request from list of IP addresses
    userid = frappe.form_dict.get("userid")
    employee = frappe.db.get_value("Employee", {
        "employee_no": userid})
    if employee:
        date = time.strftime("%Y-%m-%d", time.gmtime(
            int(frappe.form_dict.get("att_time"))))
        is_leave = check_leave_record(employee, date)
        if is_leave == 'Half Day' or is_leave == 'On Leave':
            pass
        else:
            name, company = frappe.db.get_value(
                "Employee", employee, ["employee_name", "company"])
            attendance_id = frappe.db.get_value("Attendance", {
                "employee": employee, "attendance_date": date})
            if attendance_id:
                attendance = frappe.get_doc("Attendance", attendance_id)
                out_time = time.strftime("%H:%M:%S", time.gmtime(
                    int(frappe.form_dict.get("att_time"))))
                times = [out_time, attendance.in_time]
                attendance.out_time = max(times)
                attendance.in_time = min(times)
                attendance.db_update()
            else:
                attendance = frappe.new_doc("Attendance")
                in_time = time.strftime("%H:%M:%S", time.gmtime(
                    int(frappe.form_dict.get("att_time"))))
                attendance.update({
                    "employee": employee,
                    "employee_name": name,
                    "attendance_date": date,
                    "status": "Present",
                    "in_time": in_time,
                    "company": company
                })
            attendance.save(ignore_permissions=True)
            attendance.submit()
            frappe.db.commit()
        frappe.response.type = "text"
        return "ok"


def check_leave_record(employee, date):
    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""", (employee, date), as_dict=True)
    if leave_record:
        if leave_record[0].half_day:
            status = 'Half Day'
        else:
            status = 'On Leave'
            leave_type = leave_record[0].leave_type

        return status
