You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ERPNext v15 与大华考勤机Employee CheckIn集成报错求助:无法找到对应员工

ERPNext v15 与大华考勤机Employee CheckIn集成报错求助:无法找到对应员工

各位大佬好,我正在做大华考勤机和ERPNext v15的集成开发,需求是:员工通过RFID卡打卡(签到/签退)后,用Python脚本拉取考勤机的打卡数据(卡号、时间、状态),再推送到ERPNext的「HR/Employee Checkin」模块中。

但现在测试推送代码时一直报错找不到对应员工,以下是相关代码和报错信息,麻烦帮忙排查一下问题所在!


1. 测试用的推送脚本(test.py)

import local_config as config
import datetime
import requests
import json

def _safe_get_error_str(response):
    """
    Extracts a safe error string from the response object.
    """
    try:
        error_data = response.json()
        return error_data.get('message', str(response.content))
    except Exception:
        return str(response.content)

def send_to_erpnext(employee_field_value, timestamp, device_id=None, log_type=None):
    """
    Example: send_to_erpnext('12349', datetime.datetime.now(), 'HO1', 'IN')
    """
    endpoint_app = "hrms" if config.ERPNEXT_VERSION > 13 else "erpnext"
    url = f"{config.ERPNEXT_URL}/api/method/{endpoint_app}.hr.doctype.employee_checkin.employee_checkin.add_log_based_on_employee_field"
    
    headers = {
        'Authorization': "token " + config.ERPNEXT_API_KEY + ":" + config.ERPNEXT_API_SECRET,
        'Accept': 'application/json'
    }
    data = {
        'employee_field': 'attendance_device_id',  # Dodato polje
        'employee_field_value': employee_field_value,
        'timestamp': timestamp.isoformat(),
        'device_id': device_id,
        'log_type': log_type
    }
    
    response = requests.post(url, headers=headers, json=data)
    
    if response.status_code == 200:
        print("Successfully logged employee check-in.")
        return 200, response.json()
    else:
        error_str = _safe_get_error_str(response)
        print(f"Error during ERPNext API Call: {error_str}")
        return response.status_code, error_str


# Test funkcija
send_to_erpnext('HR-EMP-00004', datetime.datetime.now(), 'HO1', 'IN')

2. 配置文件(local_config.py)

(注:API密钥和URL已隐去,实际运行时是填写完整的)

# ERPNext related configs
ERPNEXT_API_KEY = ''
ERPNEXT_API_SECRET = ''
ERPNEXT_URL = ''
ERPNEXT_VERSION = 15



# operational configs
PULL_FREQUENCY = 60 # in minutes
LOGS_DIRECTORY = 'logs' # logs of this script is stored in this directory
IMPORT_START_DATE = None # format: '20190501'

# Biometric device configs (all keys mandatory)
    #- device_id - must be unique, strictly alphanumerical chars only. no space allowed.
    #- ip - device IP Address
    #- punch_direction - 'IN'/'OUT'/'AUTO'/None
    #- clear_from_device_on_fetch: if set to true then attendance is deleted after fetch is successful.
                                    #(Caution: this feature can lead to data loss if used carelessly.)
# Biometric device configs (Dahua device)
devices = [
    {'device_id': 'mangi', 'ip': 'http://192.168.', 'punch_direction': 'AUTO', 'clear_from_device_on_fetch': False}
]


# Configs updating sync timestamp in the Shift Type DocType 
shift_type_device_mapping = [
    {'shift_type_name': ['Shift1'], 'related_device_id': ['test_1','test_2']}
]


# Ignore following exceptions thrown by ERPNext and continue importing punch logs.
# Note: All other exceptions will halt the punch log import to erpnext.
#       1. No Employee found for the given employee User ID in the Biometric device.
#       2. Employee is inactive for the given employee User ID in the Biometric device.
#       3. Duplicate Employee Checkin found. (This exception can happen if you have cleared the logs/status.json of this script)
# Use the corresponding number to ignore the above exceptions. (Default: Ignores all the listed exceptions)
allowed_exceptions = [1,2,3]

3. 报错信息

/bin/python /home/mangi/frappe-bench/biometric-attendance-sync-tool/test/test.py ✖ ✹ ✭master
Error during ERPNext API Call: b'{"exception":"frappe.exceptions.ValidationError: No Employee found for the given employee field value. 'attendance_device_id': HR-EMP-00004","exc_type":"ValidationError","_exc_source":"hrms (app)","exc":"["Traceback (most recent call last):\n File \"apps/frappe/frappe/app.py\", line 114, in application\n response = frappe.api.handle(request)\n File \"apps/frappe/frappe/api/init.py\", line 49, in handle\n data = endpoint(**arguments)\n File \"apps/frappe/frappe/api/v1.py\", line 36, in handle_rpc_call\n return frappe.handler.handle()\n File \"apps/frappe/frappe/handler.py\", line 49, in handle\n data = execute_cmd(cmd)\n File \"apps/frappe/frappe/handler.py\", line 85, in execute_cmd\n return frappe.call(method, **frappe.form_dict)\n File \"apps/frappe/frappe/init.py\", line 1768, in call\n return fn(*args, **newargs)\n File \"apps/frappe/frappe/utils/typing_validations.py\", line 31, in wrapper\n return func(*args, **kwargs)\n File \"apps/hrms/hrms/hr/doctype/employee_checkin/employee_checkin.py\", line 118, in add_log_based_on_employee_field\n frappe.throw(\n File \"apps/frappe/frappe/init.py\", line 645, in throw\n msgprint(\n File \"apps/frappe/frappe/init.py\", line 610, in msgprint\n _raise_exception()\n File \"apps/frappe/frappe/init.py\", line 561, in _raise_exception\n raise exc\nfrappe.exceptions.ValidationError: No Employee found for the given employee field value. 'attendance_device_id': HR-EMP-00004\n"]","_server_messages":"[{\"message\": \"No Employee found for the given employee field value. 'attendance_device_id': HR-EMP-00004\", \"title\": \"Message\", \"indicator\": \"red\", \"raise_exception\": 1, \"__frappe_exc_id\": \"c50c9b943a17a6e5425a0057cfa9c01d3cbd13bca6d84a8796fdf93b\"}]"}'


我已经确认ERPNext后台里,对应员工的「attendance_device_id」字段确实填写了「HR-EMP-00004」,而且员工状态是活跃的,但还是报这个错。有没有可能是API参数、端点路径或者其他配置的问题?麻烦各位帮忙看看!

备注:内容来源于stack exchange,提问作者Mangi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 17:59:38