138 lines
4.3 KiB
JavaScript
138 lines
4.3 KiB
JavaScript
// models/odoo/attendance.js
|
|
import axios from "axios";
|
|
import { authenticate, JSON_RPC_URL, odooConfig } from "./index.js";
|
|
import { toOdooDatetimeFormat } from "../../utils/date.js";
|
|
|
|
export const toggleEmployeeAttendance = async (employee_id) => {
|
|
try {
|
|
const uid = await authenticate();
|
|
|
|
if (!uid) throw new Error("Authentication failed.");
|
|
|
|
// Step 1: Check if employee exists
|
|
const empCheck = {
|
|
jsonrpc: "2.0",
|
|
method: "call",
|
|
params: {
|
|
service: "object",
|
|
method: "execute_kw",
|
|
args: [
|
|
odooConfig.db,
|
|
uid,
|
|
odooConfig.apiKey,
|
|
"hr.employee",
|
|
"search",
|
|
[[["id", "=", employee_id]]],
|
|
],
|
|
},
|
|
id: 0,
|
|
};
|
|
|
|
const empRes = await axios.post(JSON_RPC_URL, empCheck);
|
|
if (!empRes.data.result || empRes.data.result.length === 0) {
|
|
throw new Error(`Employee ${employee_id} not found.`);
|
|
}
|
|
|
|
// Step 2: Look for open attendance
|
|
const searchPayload = {
|
|
jsonrpc: "2.0",
|
|
method: "call",
|
|
params: {
|
|
service: "object",
|
|
method: "execute_kw",
|
|
args: [
|
|
odooConfig.db,
|
|
uid,
|
|
odooConfig.apiKey,
|
|
"hr.attendance",
|
|
"search_read",
|
|
[
|
|
[
|
|
["employee_id", "=", employee_id],
|
|
["check_out", "=", false],
|
|
],
|
|
],
|
|
{ fields: ["id", "check_in"], limit: 1 },
|
|
],
|
|
},
|
|
id: 3,
|
|
};
|
|
|
|
const searchRes = await axios.post(JSON_RPC_URL, searchPayload);
|
|
const openRecords = searchRes.data.result;
|
|
|
|
if (openRecords.length > 0) {
|
|
// Step 3: Write check_out
|
|
const attendanceId = openRecords[0].id;
|
|
const now = toOdooDatetimeFormat(new Date());
|
|
|
|
const writePayload = {
|
|
jsonrpc: "2.0",
|
|
method: "call",
|
|
params: {
|
|
service: "object",
|
|
method: "execute_kw",
|
|
args: [
|
|
odooConfig.db,
|
|
uid,
|
|
odooConfig.apiKey,
|
|
"hr.attendance",
|
|
"write",
|
|
[[attendanceId], { check_out: now }],
|
|
],
|
|
},
|
|
id: 4,
|
|
};
|
|
|
|
const writeRes = await axios.post(JSON_RPC_URL, writePayload);
|
|
|
|
if (!writeRes.data.result) throw new Error("Check-out failed.");
|
|
|
|
return {
|
|
status: "Checked out",
|
|
attendance_id: attendanceId,
|
|
check_out: now,
|
|
odoo_response: writeRes.data,
|
|
};
|
|
} else {
|
|
// Step 4: Create new check_in
|
|
const now = toOdooDatetimeFormat(new Date());
|
|
|
|
const createPayload = {
|
|
jsonrpc: "2.0",
|
|
method: "call",
|
|
params: {
|
|
service: "object",
|
|
method: "execute_kw",
|
|
args: [
|
|
odooConfig.db,
|
|
uid,
|
|
odooConfig.apiKey,
|
|
"hr.attendance",
|
|
"create",
|
|
[{ employee_id, check_in: now }],
|
|
],
|
|
},
|
|
id: 5,
|
|
};
|
|
|
|
const createRes = await axios.post(JSON_RPC_URL, createPayload);
|
|
|
|
if (createRes.data.error) {
|
|
console.error("Odoo error:", createRes.data.error);
|
|
throw new Error(createRes.data.error.message);
|
|
}
|
|
|
|
return {
|
|
status: "Checked in",
|
|
attendance_id: createRes.data.result,
|
|
check_in: now,
|
|
odoo_response: createRes.data,
|
|
};
|
|
}
|
|
} catch (error) {
|
|
console.error("toggleEmployeeAttendance error:", error);
|
|
throw error;
|
|
}
|
|
};
|