feat: Portal, Email Inbound, Discuss + module improvements
- Portal: /my/* routes, signup, password reset, portal user support - Email Inbound: IMAP polling (go-imap/v2), thread matching - Discuss: mail.channel, long-polling bus, DM, unread count - Cron: ir.cron runner (goroutine scheduler) - Bank Import, CSV/Excel Import - Automation (ir.actions.server) - Fetchmail service - HR Payroll model - Various fixes across account, sale, stock, purchase, crm, hr, project Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// initResourceCalendar registers resource.calendar — working schedules.
|
||||
// Mirrors: odoo/addons/resource/models/resource.py
|
||||
@@ -98,15 +103,181 @@ func initHREmployee() {
|
||||
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() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
_, 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
|
||||
@@ -117,12 +288,283 @@ func initHrEmployeeExtensions() {
|
||||
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.
|
||||
@@ -149,6 +591,14 @@ func initHRDepartment() {
|
||||
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.
|
||||
@@ -174,4 +624,12 @@ func initHRJob() {
|
||||
}, 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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user