我正在尝试使用以下代码推送员工出勤数据:
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')
在第二个文件中我有这个: Ofc 我只是为了这个问题删除了 api 密钥...
# 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
# please, read this thread to know why this is necessary https://discuss.erpnext.com/t/v-12-hr-auto-attendance-purpose-of-last-sync-of-checkin-in-shift-type/52997
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]
这是我得到的错误:
/bin/python /home/mangi/frappe-bench/biometric-attendance-sync-tool/test/test.py ✖ ✹ ✭master ERPNext API 调用期间出错:b'{"exception":"frappe.exceptions.ValidationError: 未找到给定员工字段值的员工。'attendance_device_id': HR-EMP-00004","exc_type":"ValidationError"," _exc_source":"hrms (app)","exc":"[\"回溯(最近一次调用):\n 文件\\“apps/frappe/frappe/app.py\\”,第114行,在应用程序中\n响应= frappe.api.handle(请求)\n文件\\“apps/frappe/frappe/api/init .py\\",第 49 行,在句柄\n 数据 = 端点(**参数)\n 文件中\\“apps/frappe/frappe/api/v1.py\\”,第36行,在handle_rpc_call中\n返回frappe.handler.handle()\n文件\\“apps/frappe/frappe/handler.py\\” ",第 49 行,在句柄\n data =execute_cmd(cmd)\n File \\"apps/frappe/frappe/handler.py\\",行85、在execute_cmd\n return frappe.call(method, **frappe.form_dict)\n File \\"apps/frappe/frappe/init.py\\",第1768行,在call\n return fn( *args, **newargs)\n 文件 \\"apps/frappe/frappe/utils/typing_validations.py\\",第 31 行,在包装器中\n return func(*args, **kwargs)\n File \\"apps/hrms/hrms/hr/doctype/employee_checkin/employee_checkin.py\\", 第 118 行,在 add_log_based_on_employee_field\n frappe.throw( \n 文件 \\"apps/frappe/frappe/init.py\\", 行645,在 throw\n msgprint(\n 文件 \\"apps/frappe/frappe/init.py\\",第 610 行,在 msgprint\n _raise_exception()\n 文件 \\"apps/frappe/frappe /init.py\\”,第 561 行,在 _raise_exception\n raise exc\nfrappe.exceptions.ValidationError: 未找到给定员工字段值的员工。 'attendance_device_id': HR-EMP-00004\n\"]","_server_messages":"[\"{\\"message\\": \\"未找到给定员工字段值的员工。'attendance_device_id': HR-EMP-00004\\", \\"标题\\": \\"消息\\", \\"指标\\": \\“红色\\”,\\“raise_exception \\”:1,\\“__frappe_exc_id \\”:\\“c50c9b943a17a6e5425a0057cfa9c01d3cbd13bca6d84a8796fdf93b\\”}\“]”}'
这个想法是将大华考勤设备与 ERPNext 集成,当员工签到或签出时,Python 脚本会提取哪个 rfid 购物车的数据以及人员何时签到以及进出的状态是什么以及之后前往 ERPNext HR/Employee Checkin 并推送数据。
send_to_erpnext('HR-EMP-00004', datetime.datetime.now(), 'HO1', 'IN') ^ 错误就在这里|
我将员工 ID 替换为考勤设备 ID。 它是在HR/员工/员工姓名/出勤和休假/
中创建的