package models import ( "fmt" "time" "odoo-go/pkg/orm" ) // initResourceCalendar registers resource.calendar — working schedules. // Mirrors: odoo/addons/resource/models/resource.py func initResourceCalendar() { m := orm.NewModel("resource.calendar", orm.ModelOpts{ Description: "Resource Working Time", Order: "name", }) m.AddFields( orm.Char("name", orm.FieldOpts{String: "Name", Required: true}), orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}), orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}), orm.Float("hours_per_week", orm.FieldOpts{String: "Hours per Week"}), orm.Char("tz", orm.FieldOpts{String: "Timezone", Default: "Europe/Berlin"}), orm.Boolean("flexible_hours", orm.FieldOpts{String: "Flexible Hours"}), orm.One2many("attendance_ids", "resource.calendar.attendance", "calendar_id", orm.FieldOpts{String: "Attendances"}), ) // resource.calendar.attendance — work time slots orm.NewModel("resource.calendar.attendance", orm.ModelOpts{ Description: "Work Detail", Order: "dayofweek, hour_from", }).AddFields( orm.Char("name", orm.FieldOpts{String: "Name", Required: true}), orm.Selection("dayofweek", []orm.SelectionItem{ {Value: "0", Label: "Monday"}, {Value: "1", Label: "Tuesday"}, {Value: "2", Label: "Wednesday"}, {Value: "3", Label: "Thursday"}, {Value: "4", Label: "Friday"}, {Value: "5", Label: "Saturday"}, {Value: "6", Label: "Sunday"}, }, orm.FieldOpts{String: "Day of Week", Required: true}), orm.Float("hour_from", orm.FieldOpts{String: "Work from", Required: true}), orm.Float("hour_to", orm.FieldOpts{String: "Work to", Required: true}), orm.Many2one("calendar_id", "resource.calendar", orm.FieldOpts{ String: "Calendar", Required: true, OnDelete: orm.OnDeleteCascade, }), ) } // initHREmployee registers the hr.employee model. // Mirrors: odoo/addons/hr/models/hr_employee.py func initHREmployee() { m := orm.NewModel("hr.employee", orm.ModelOpts{ Description: "Employee", Order: "name", }) m.AddFields( orm.Char("name", orm.FieldOpts{String: "Employee Name", Required: true, Index: true}), orm.Many2one("user_id", "res.users", orm.FieldOpts{String: "Related User"}), orm.Many2one("department_id", "hr.department", orm.FieldOpts{String: "Department", Index: true}), orm.Many2one("job_id", "hr.job", orm.FieldOpts{String: "Job Position"}), orm.Char("job_title", orm.FieldOpts{String: "Job Title"}), orm.Many2one("company_id", "res.company", orm.FieldOpts{ String: "Company", Required: true, Index: true, }), orm.Many2one("parent_id", "hr.employee", orm.FieldOpts{String: "Manager", Index: true}), orm.Many2one("address_id", "res.partner", orm.FieldOpts{String: "Work Address"}), orm.Many2one("address_home_id", "res.partner", orm.FieldOpts{ String: "Private Address", Groups: "hr.group_hr_user", }), orm.Char("identification_id", orm.FieldOpts{String: "Identification No", Groups: "hr.group_hr_user"}), orm.Char("work_email", orm.FieldOpts{String: "Work Email"}), orm.Char("work_phone", orm.FieldOpts{String: "Work Phone"}), orm.Char("mobile_phone", orm.FieldOpts{String: "Work Mobile"}), orm.Many2one("coach_id", "hr.employee", orm.FieldOpts{String: "Coach"}), orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}), orm.Many2one("resource_calendar_id", "resource.calendar", orm.FieldOpts{String: "Working Schedule"}), orm.Selection("gender", []orm.SelectionItem{ {Value: "male", Label: "Male"}, {Value: "female", Label: "Female"}, {Value: "other", Label: "Other"}, }, orm.FieldOpts{String: "Gender"}), orm.Date("birthday", orm.FieldOpts{String: "Date of Birth", Groups: "hr.group_hr_user"}), orm.Selection("marital", []orm.SelectionItem{ {Value: "single", Label: "Single"}, {Value: "married", Label: "Married"}, {Value: "cohabitant", Label: "Legal Cohabitant"}, {Value: "widower", Label: "Widower"}, {Value: "divorced", Label: "Divorced"}, }, orm.FieldOpts{String: "Marital Status", Default: "single"}), orm.Char("emergency_contact", orm.FieldOpts{String: "Emergency Contact"}), orm.Char("emergency_phone", orm.FieldOpts{String: "Emergency Phone"}), orm.Selection("certificate", []orm.SelectionItem{ {Value: "graduate", Label: "Graduate"}, {Value: "bachelor", Label: "Bachelor"}, {Value: "master", Label: "Master"}, {Value: "doctor", Label: "Doctor"}, {Value: "other", Label: "Other"}, }, orm.FieldOpts{String: "Certificate Level"}), orm.Char("study_field", orm.FieldOpts{String: "Field of Study"}), orm.Char("visa_no", orm.FieldOpts{String: "Visa No", Groups: "hr.group_hr_user"}), orm.Char("permit_no", orm.FieldOpts{String: "Work Permit No", Groups: "hr.group_hr_user"}), orm.Integer("km_home_work", orm.FieldOpts{String: "Home-Work Distance (km)"}), orm.Binary("image_1920", orm.FieldOpts{String: "Image"}), ) // DefaultGet: provide dynamic defaults for new employees. // Mirrors: odoo/addons/hr/models/hr_employee.py HrEmployee.default_get() m.DefaultGet = func(env *orm.Environment, fields []string) orm.Values { vals := make(orm.Values) // Default company from current user's session companyID := env.CompanyID() if companyID > 0 { vals["company_id"] = companyID } return vals } // toggle_active: archive/unarchive employee // Mirrors: odoo/addons/hr/models/hr_employee.py HrEmployee.toggle_active() m.RegisterMethod("toggle_active", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { env := rs.Env() for _, id := range rs.IDs() { _, err := env.Tx().Exec(env.Ctx(), `UPDATE hr_employee SET active = NOT active WHERE id = $1`, id) if err != nil { return nil, fmt.Errorf("hr.employee: toggle_active for %d: %w", id, err) } } return true, nil }) // action_archive: Archive employee (set active=false). // Mirrors: odoo/addons/hr/models/hr_employee.py HrEmployee.action_archive() m.RegisterMethod("action_archive", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { env := rs.Env() for _, id := range rs.IDs() { _, err := env.Tx().Exec(env.Ctx(), `UPDATE hr_employee SET active = false WHERE id = $1`, id) if err != nil { return nil, fmt.Errorf("hr.employee: action_archive for %d: %w", id, err) } } return true, nil }) // _compute_remaining_leaves: Compute remaining leave days for the employee. // Mirrors: odoo/addons/hr/models/hr_employee.py HrEmployee._compute_remaining_leaves() m.RegisterCompute("leaves_count", func(rs *orm.Recordset) (orm.Values, error) { env := rs.Env() empID := rs.IDs()[0] var allocated float64 if err := env.Tx().QueryRow(env.Ctx(), `SELECT COALESCE(SUM(number_of_days), 0) FROM hr_leave_allocation WHERE employee_id = $1 AND state = 'validate'`, empID, ).Scan(&allocated); err != nil { return orm.Values{"leaves_count": float64(0)}, nil } var used float64 if err := env.Tx().QueryRow(env.Ctx(), `SELECT COALESCE(SUM(number_of_days), 0) FROM hr_leave WHERE employee_id = $1 AND state = 'validate'`, empID, ).Scan(&used); err != nil { return orm.Values{"leaves_count": float64(0)}, nil } return orm.Values{"leaves_count": allocated - used}, nil }) m.RegisterCompute("attendance_state", func(rs *orm.Recordset) (orm.Values, error) { env := rs.Env() empID := rs.IDs()[0] var checkOut *string err := env.Tx().QueryRow(env.Ctx(), `SELECT check_out FROM hr_attendance WHERE employee_id = $1 ORDER BY check_in DESC LIMIT 1`, empID, ).Scan(&checkOut) if err != nil { // No attendance records or DB error → checked out return orm.Values{"attendance_state": "checked_out"}, nil } if checkOut == nil { return orm.Values{"attendance_state": "checked_in"}, nil } return orm.Values{"attendance_state": "checked_out"}, nil }) } // initHrEmployeeCategory registers the hr.employee.category model. // Mirrors: odoo/addons/hr/models/hr_employee_category.py func initHrEmployeeCategory() { orm.NewModel("hr.employee.category", orm.ModelOpts{ Description: "Employee Tag", Order: "name", }).AddFields( orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}), orm.Integer("color", orm.FieldOpts{String: "Color Index"}), ) } // initHrEmployeePublic registers the hr.employee.public model with limited fields. // Mirrors: odoo/addons/hr/models/hr_employee.py HrEmployeePublic func initHrEmployeePublic() { m := orm.NewModel("hr.employee.public", orm.ModelOpts{ Description: "Public Employee", Order: "name", }) m.AddFields( orm.Many2one("employee_id", "hr.employee", orm.FieldOpts{ String: "Employee", Required: true, Index: true, }), orm.Char("name", orm.FieldOpts{String: "Name", Readonly: true}), orm.Many2one("department_id", "hr.department", orm.FieldOpts{String: "Department", Readonly: true}), orm.Char("job_title", orm.FieldOpts{String: "Job Title", Readonly: true}), orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company", Readonly: true}), orm.Many2one("parent_id", "hr.employee.public", orm.FieldOpts{String: "Manager", Readonly: true}), orm.Char("work_email", orm.FieldOpts{String: "Work Email", Readonly: true}), orm.Char("work_phone", orm.FieldOpts{String: "Work Phone", Readonly: true}), orm.Binary("image_1920", orm.FieldOpts{String: "Image", Readonly: true}), ) // get_public_data: Reads limited public fields from hr.employee. // Mirrors: odoo/addons/hr/models/hr_employee.py HrEmployeePublic.read() m.RegisterMethod("get_public_data", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { env := rs.Env() // Accept employee_id from kwargs or use IDs var employeeIDs []int64 if len(args) > 0 { if kw, ok := args[0].(map[string]interface{}); ok { if v, ok := kw["employee_ids"]; ok { if ids, ok := v.([]int64); ok { employeeIDs = ids } } } } if len(employeeIDs) == 0 { employeeIDs = rs.IDs() } if len(employeeIDs) == 0 { return []map[string]interface{}{}, nil } rows, err := env.Tx().Query(env.Ctx(), `SELECT id, COALESCE(name, ''), COALESCE(department_id, 0), COALESCE(job_title, ''), COALESCE(company_id, 0), COALESCE(work_email, ''), COALESCE(work_phone, '') FROM hr_employee WHERE id = ANY($1) AND COALESCE(active, true) = true`, employeeIDs) if err != nil { return nil, fmt.Errorf("hr.employee.public: query: %w", err) } defer rows.Close() var result []map[string]interface{} for rows.Next() { var id, deptID, companyID int64 var name, jobTitle, email, phone string if err := rows.Scan(&id, &name, &deptID, &jobTitle, &companyID, &email, &phone); err != nil { continue } result = append(result, map[string]interface{}{ "id": id, "name": name, "department_id": deptID, "job_title": jobTitle, "company_id": companyID, "work_email": email, "work_phone": phone, }) } return result, nil }) } // initHrEmployeeExtensions adds skill, resume, attendance and leave fields // to hr.employee after the related models have been registered. func initHrEmployeeExtensions() { emp := orm.ExtendModel("hr.employee") emp.AddFields( orm.One2many("skill_ids", "hr.employee.skill", "employee_id", orm.FieldOpts{String: "Skills"}), orm.One2many("resume_line_ids", "hr.resume.line", "employee_id", orm.FieldOpts{String: "Resume"}), orm.One2many("attendance_ids", "hr.attendance", "employee_id", orm.FieldOpts{String: "Attendances"}), orm.Many2one("contract_id", "hr.contract", orm.FieldOpts{String: "Current Contract"}), orm.One2many("contract_ids", "hr.contract", "employee_id", orm.FieldOpts{String: "Contracts"}), orm.Float("leaves_count", orm.FieldOpts{String: "Time Off", Compute: "_compute_leaves"}), orm.Selection("attendance_state", []orm.SelectionItem{ {Value: "checked_out", Label: "Checked Out"}, {Value: "checked_in", Label: "Checked In"}, }, orm.FieldOpts{String: "Attendance", Compute: "_compute_attendance_state"}), orm.Float("seniority_years", orm.FieldOpts{ String: "Seniority (Years)", Compute: "_compute_seniority_years", }), orm.Date("first_contract_date", orm.FieldOpts{String: "First Contract Date"}), orm.Many2many("category_ids", "hr.employee.category", orm.FieldOpts{String: "Tags"}), orm.Integer("age", orm.FieldOpts{ String: "Age", Compute: "_compute_age", }), ) // _compute_seniority_years: Years since first contract start date. // Mirrors: odoo/addons/hr_contract/models/hr_employee.py _compute_first_contract_date emp.RegisterCompute("seniority_years", func(rs *orm.Recordset) (orm.Values, error) { env := rs.Env() empID := rs.IDs()[0] // Find earliest contract start date var firstDate *time.Time if err := env.Tx().QueryRow(env.Ctx(), `SELECT MIN(date_start) FROM hr_contract WHERE employee_id = $1 AND state NOT IN ('cancel')`, empID, ).Scan(&firstDate); err != nil { return orm.Values{"seniority_years": float64(0)}, nil } if firstDate == nil { return orm.Values{"seniority_years": float64(0)}, nil } years := time.Since(*firstDate).Hours() / (24 * 365.25) if years < 0 { years = 0 } return orm.Values{"seniority_years": years}, nil }) // get_attendance_by_date_range: Return attendance summary for an employee. // Mirrors: odoo/addons/hr_attendance/models/hr_employee.py emp.RegisterMethod("get_attendance_by_date_range", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { env := rs.Env() empID := rs.IDs()[0] // Parse date_from / date_to from kwargs dateFrom := time.Now().AddDate(0, -1, 0).Format("2006-01-02") dateTo := time.Now().Format("2006-01-02") if len(args) > 0 { if kw, ok := args[0].(map[string]interface{}); ok { if v, ok := kw["date_from"].(string); ok && v != "" { dateFrom = v } if v, ok := kw["date_to"].(string); ok && v != "" { dateTo = v } } } // Daily attendance summary rows, err := env.Tx().Query(env.Ctx(), `SELECT check_in::date AS day, COUNT(*) AS entries, COALESCE(SUM(EXTRACT(EPOCH FROM (COALESCE(check_out, NOW()) - check_in)) / 3600.0), 0) AS total_hours FROM hr_attendance WHERE employee_id = $1 AND check_in::date >= $2 AND check_in::date <= $3 GROUP BY check_in::date ORDER BY check_in::date`, empID, dateFrom, dateTo) if err != nil { return nil, fmt.Errorf("hr.employee: attendance report: %w", err) } defer rows.Close() var days []map[string]interface{} var totalHours float64 var totalDays int for rows.Next() { var day time.Time var entries int var hours float64 if err := rows.Scan(&day, &entries, &hours); err != nil { continue } days = append(days, map[string]interface{}{ "date": day.Format("2006-01-02"), "entries": entries, "hours": hours, }) totalHours += hours totalDays++ } return map[string]interface{}{ "employee_id": empID, "date_from": dateFrom, "date_to": dateTo, "days": days, "total_days": totalDays, "total_hours": totalHours, "avg_hours": func() float64 { if totalDays > 0 { return totalHours / float64(totalDays) }; return 0 }(), }, nil }) // _compute_age: Compute employee age from birthday. // Mirrors: odoo/addons/hr/models/hr_employee.py _compute_age() emp.RegisterCompute("age", func(rs *orm.Recordset) (orm.Values, error) { env := rs.Env() empID := rs.IDs()[0] var birthday *time.Time if err := env.Tx().QueryRow(env.Ctx(), `SELECT birthday FROM hr_employee WHERE id = $1`, empID, ).Scan(&birthday); err != nil || birthday == nil { return orm.Values{"age": int64(0)}, nil } now := time.Now() age := now.Year() - birthday.Year() // Adjust if birthday has not occurred yet this year if now.Month() < birthday.Month() || (now.Month() == birthday.Month() && now.Day() < birthday.Day()) { age-- } if age < 0 { age = 0 } return orm.Values{"age": int64(age)}, nil }) // action_check_in: Create a new attendance record with check_in = now. // Mirrors: odoo/addons/hr_attendance/models/hr_employee.py action_check_in() emp.RegisterMethod("action_check_in", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { env := rs.Env() now := time.Now().UTC().Format("2006-01-02 15:04:05") for _, empID := range rs.IDs() { // Verify employee is not already checked in var openCount int env.Tx().QueryRow(env.Ctx(), `SELECT COUNT(*) FROM hr_attendance WHERE employee_id = $1 AND check_out IS NULL`, empID, ).Scan(&openCount) if openCount > 0 { return nil, fmt.Errorf("hr.employee: employee %d is already checked in", empID) } // Get company_id from employee var companyID *int64 env.Tx().QueryRow(env.Ctx(), `SELECT company_id FROM hr_employee WHERE id = $1`, empID, ).Scan(&companyID) // Create attendance record attRS := env.Model("hr.attendance") vals := orm.Values{ "employee_id": empID, "check_in": now, } if companyID != nil { vals["company_id"] = *companyID } if _, err := attRS.Create(vals); err != nil { return nil, fmt.Errorf("hr.employee: check_in for %d: %w", empID, err) } } return true, nil }) // action_check_out: Set check_out on the latest open attendance record. // Mirrors: odoo/addons/hr_attendance/models/hr_employee.py action_check_out() emp.RegisterMethod("action_check_out", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { env := rs.Env() now := time.Now().UTC().Format("2006-01-02 15:04:05") for _, empID := range rs.IDs() { result, err := env.Tx().Exec(env.Ctx(), `UPDATE hr_attendance SET check_out = $1 WHERE employee_id = $2 AND check_out IS NULL`, now, empID) if err != nil { return nil, fmt.Errorf("hr.employee: check_out for %d: %w", empID, err) } if result.RowsAffected() == 0 { return nil, fmt.Errorf("hr.employee: employee %d is not checked in", empID) } } return true, nil }) // get_org_chart: Return hierarchical org chart data for the employee. // Mirrors: odoo/addons/hr_org_chart/models/hr_employee.py get_org_chart() emp.RegisterMethod("get_org_chart", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { env := rs.Env() empID := rs.IDs()[0] // Recursive function to build the tree var buildNode func(id int64, depth int) (map[string]interface{}, error) buildNode = func(id int64, depth int) (map[string]interface{}, error) { // Prevent infinite recursion if depth > 20 { return nil, nil } var name, jobTitle string var deptID, parentID *int64 err := env.Tx().QueryRow(env.Ctx(), `SELECT COALESCE(name, ''), COALESCE(job_title, ''), department_id, parent_id FROM hr_employee WHERE id = $1 AND COALESCE(active, true) = true`, id, ).Scan(&name, &jobTitle, &deptID, &parentID) if err != nil { return nil, nil // employee not found or inactive } node := map[string]interface{}{ "id": id, "name": name, "job_title": jobTitle, } if deptID != nil { node["department_id"] = *deptID } if parentID != nil { node["parent_id"] = *parentID } // Find subordinates subRows, err := env.Tx().Query(env.Ctx(), `SELECT id FROM hr_employee WHERE parent_id = $1 AND COALESCE(active, true) = true ORDER BY name`, id) if err != nil { node["subordinates"] = []map[string]interface{}{} return node, nil } var subIDs []int64 for subRows.Next() { var subID int64 if err := subRows.Scan(&subID); err != nil { continue } subIDs = append(subIDs, subID) } subRows.Close() var subordinates []map[string]interface{} for _, subID := range subIDs { subNode, err := buildNode(subID, depth+1) if err != nil || subNode == nil { continue } subordinates = append(subordinates, subNode) } if subordinates == nil { subordinates = []map[string]interface{}{} } node["subordinates"] = subordinates node["subordinate_count"] = len(subordinates) return node, nil } chart, err := buildNode(empID, 0) if err != nil { return nil, fmt.Errorf("hr.employee: get_org_chart: %w", err) } if chart == nil { return map[string]interface{}{}, nil } return chart, nil }) } // initHRDepartment registers the hr.department model. // Mirrors: odoo/addons/hr/models/hr_department.py func initHRDepartment() { m := orm.NewModel("hr.department", orm.ModelOpts{ Description: "HR Department", Order: "name", }) m.AddFields( orm.Char("name", orm.FieldOpts{String: "Department Name", Required: true, Translate: true}), orm.Char("complete_name", orm.FieldOpts{ String: "Complete Name", Compute: "_compute_complete_name", Store: true, }), orm.Many2one("parent_id", "hr.department", orm.FieldOpts{String: "Parent Department", Index: true}), orm.Many2one("manager_id", "hr.employee", orm.FieldOpts{String: "Manager"}), orm.Many2one("company_id", "res.company", orm.FieldOpts{ String: "Company", Required: true, Index: true, }), orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}), orm.One2many("child_ids", "hr.department", "parent_id", orm.FieldOpts{String: "Child Departments"}), orm.One2many("member_ids", "hr.employee", "department_id", orm.FieldOpts{String: "Members"}), ) m.DefaultGet = func(env *orm.Environment, fields []string) orm.Values { vals := make(orm.Values) if companyID := env.CompanyID(); companyID > 0 { vals["company_id"] = companyID } return vals } } // initHRJob registers the hr.job model. // Mirrors: odoo/addons/hr/models/hr_job.py func initHRJob() { m := orm.NewModel("hr.job", orm.ModelOpts{ Description: "Job Position", Order: "name", }) m.AddFields( orm.Char("name", orm.FieldOpts{String: "Job Position", Required: true, Index: true, Translate: true}), orm.Many2one("department_id", "hr.department", orm.FieldOpts{String: "Department"}), orm.Many2one("company_id", "res.company", orm.FieldOpts{ String: "Company", Required: true, Index: true, }), orm.Integer("expected_employees", orm.FieldOpts{String: "Expected New Employees", Default: 1}), orm.Integer("no_of_recruitment", orm.FieldOpts{String: "Expected in Recruitment"}), orm.Integer("no_of_hired_employee", orm.FieldOpts{String: "Hired Employees"}), orm.Selection("state", []orm.SelectionItem{ {Value: "recruit", Label: "Recruitment in Progress"}, {Value: "open", Label: "Not Recruiting"}, }, orm.FieldOpts{String: "Status", Required: true, Default: "recruit"}), orm.Text("description", orm.FieldOpts{String: "Job Description"}), ) m.DefaultGet = func(env *orm.Environment, fields []string) orm.Values { vals := make(orm.Values) if companyID := env.CompanyID(); companyID > 0 { vals["company_id"] = companyID } return vals } }