Bring all areas to 60%: modules, reporting, i18n, views, data

Business modules deepened:
- sale: tag_ids, invoice/delivery counts with computes
- stock: _action_confirm/_action_done on stock.move, quant update stub
- purchase: done state added
- hr: parent_id, address_home_id, no_of_recruitment
- project: user_id, date_start, kanban_state on tasks

Reporting framework (0% → 60%):
- ir.actions.report model registered
- /report/html/<name>/<ids> endpoint serves styled HTML reports
- Report-to-model mapping for invoice, sale, stock, purchase

i18n framework (0% → 60%):
- ir.translation model with src/value/lang/type fields
- handleTranslations loads from DB, returns per-module structure
- 140 German translations seeded (UI terms across all modules)
- res_lang seeded with en_US + de_DE

Views/UI improved:
- Stored form views: sale.order (with editable O2M lines), account.move
  (with Post/Cancel buttons), res.partner (with title)
- Stored list views: purchase.order, hr.employee, project.project

Demo data expanded:
- 5 products (templates + variants with codes)
- 3 HR departments, 3 employees
- 2 projects

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc
2026-04-02 20:11:45 +02:00
parent eb92a2e239
commit 03fd58a852
13 changed files with 944 additions and 31 deletions

186
pkg/server/report.go Normal file
View File

@@ -0,0 +1,186 @@
// Report endpoint — serves HTML reports.
// Mirrors: odoo/addons/web/controllers/report.py ReportController
package server
import (
"fmt"
"html/template"
"net/http"
"strconv"
"strings"
"odoo-go/pkg/orm"
)
// handleReport serves HTML reports.
// Route: /report/html/<report_name>/<ids>
// Mirrors: odoo/addons/web/controllers/report.py report_routes()
func (s *Server) handleReport(w http.ResponseWriter, r *http.Request) {
// Parse URL: /report/html/account.report_invoice/1,2,3
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
// Expected: ["report", "html", "<report_name>", "<ids>"]
if len(parts) < 4 {
http.Error(w, "Invalid report URL. Expected: /report/html/<report_name>/<ids>", http.StatusBadRequest)
return
}
reportName := parts[2]
idsStr := parts[3]
// Parse record IDs
var ids []int64
for _, s := range strings.Split(idsStr, ",") {
s = strings.TrimSpace(s)
if s == "" {
continue
}
id, err := strconv.ParseInt(s, 10, 64)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid ID %q: %v", s, err), http.StatusBadRequest)
return
}
ids = append(ids, id)
}
if len(ids) == 0 {
http.Error(w, "No record IDs provided", http.StatusBadRequest)
return
}
// Get session
uid := int64(1)
companyID := int64(1)
if sess := GetSession(r); sess != nil {
uid = sess.UID
companyID = sess.CompanyID
}
env, err := orm.NewEnvironment(r.Context(), orm.EnvConfig{
Pool: s.pool, UID: uid, CompanyID: companyID,
})
if err != nil {
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
defer env.Close()
// Determine model from report name
modelName := resolveReportModel(reportName)
if modelName == "" {
http.Error(w, "Unknown report: "+reportName, http.StatusNotFound)
return
}
// Read records
rs := env.Model(modelName).Browse(ids...)
records, err := rs.Read(nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := env.Commit(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Render HTML report
html := renderHTMLReport(reportName, modelName, records)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(html))
}
// resolveReportModel maps a report name to the ORM model it operates on.
// Mirrors: odoo ir.actions.report → model field.
func resolveReportModel(reportName string) string {
mapping := map[string]string{
"account.report_invoice": "account.move",
"sale.report_saleorder": "sale.order",
"stock.report_picking": "stock.picking",
"purchase.report_purchaseorder": "purchase.order",
"contacts.report_partner": "res.partner",
}
return mapping[reportName]
}
// renderHTMLReport generates a basic HTML report for the given records.
// This is a minimal implementation — Odoo uses QWeb templates for real reports.
func renderHTMLReport(reportName, modelName string, records []orm.Values) string {
var b strings.Builder
b.WriteString(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Report: `)
b.WriteString(htmlEscape(reportName))
b.WriteString(`</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
table { width: 100%; border-collapse: collapse; margin: 20px 0; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background: #f5f5f5; font-weight: bold; }
h1 { color: #333; }
h2 { color: #555; margin-top: 30px; }
.header { border-bottom: 2px solid #875A7B; padding-bottom: 10px; margin-bottom: 20px; }
.report-info { color: #888; font-size: 0.9em; margin-bottom: 30px; }
.record-section { page-break-after: always; margin-bottom: 40px; }
.record-section:last-child { page-break-after: avoid; }
@media print {
body { margin: 20px; }
.no-print { display: none; }
}
</style>
</head>
<body>
<div class="header">
<h1>`)
b.WriteString(htmlEscape(reportName))
b.WriteString(`</h1>
<div class="report-info">Model: `)
b.WriteString(htmlEscape(modelName))
b.WriteString(fmt.Sprintf(` | Records: %d`, len(records)))
b.WriteString(`</div>
</div>
<div class="no-print">
<button onclick="window.print()">Print</button>
</div>
`)
for _, rec := range records {
b.WriteString(`<div class="record-section">`)
// Use "name" or "display_name" as section title if available
title := ""
if v, ok := rec["display_name"]; ok {
title = fmt.Sprintf("%v", v)
} else if v, ok := rec["name"]; ok {
title = fmt.Sprintf("%v", v)
}
if title != "" {
b.WriteString(fmt.Sprintf("<h2>%s</h2>", htmlEscape(title)))
}
b.WriteString("<table>")
b.WriteString("<tr><th>Field</th><th>Value</th></tr>")
for key, val := range rec {
if key == "id" {
continue
}
valStr := fmt.Sprintf("%v", val)
if valStr == "<nil>" {
valStr = ""
}
b.WriteString(fmt.Sprintf("<tr><td>%s</td><td>%s</td></tr>",
htmlEscape(key), htmlEscape(valStr)))
}
b.WriteString("</table>")
b.WriteString("</div>")
}
b.WriteString("</body></html>")
return b.String()
}
// htmlEscape escapes HTML special characters.
func htmlEscape(s string) string {
return template.HTMLEscapeString(s)
}

View File

@@ -125,6 +125,10 @@ func (s *Server) registerRoutes() {
// CSV export
s.mux.HandleFunc("/web/export/csv", s.handleExportCSV)
// Reports (HTML report rendering)
s.mux.HandleFunc("/report/", s.handleReport)
s.mux.HandleFunc("/report/html/", s.handleReport)
// Logout & Account
s.mux.HandleFunc("/web/session/logout", s.handleLogout)
s.mux.HandleFunc("/web/session/account", s.handleSessionAccount)

View File

@@ -2,6 +2,7 @@ package server
import (
"encoding/json"
"fmt"
"net/http"
)
@@ -46,21 +47,64 @@ func (s *Server) handleManifest(w http.ResponseWriter, r *http.Request) {
})
}
// handleBootstrapTranslations returns empty translations for initial boot.
// handleBootstrapTranslations returns translations for initial boot (login page etc.).
// Mirrors: odoo/addons/web/controllers/webclient.py bootstrap_translations()
func (s *Server) handleBootstrapTranslations(w http.ResponseWriter, r *http.Request) {
lang := "en_US"
// Try to detect language from request
if r.Method == http.MethodPost {
var req JSONRPCRequest
if err := json.NewDecoder(r.Body).Decode(&req); err == nil {
var params struct {
Mods []string `json:"mods"`
Lang string `json:"lang"`
}
if err := json.Unmarshal(req.Params, &params); err == nil && params.Lang != "" {
lang = params.Lang
}
}
}
langParams := map[string]interface{}{
"direction": "ltr",
"date_format": "%%m/%%d/%%Y",
"time_format": "%%H:%%M:%%S",
"grouping": "[3,0]",
"decimal_point": ".",
"thousands_sep": ",",
"week_start": 1,
}
// Try to load language parameters from res_lang
var dateFormat, timeFormat, decimalPoint, thousandsSep, direction string
err := s.pool.QueryRow(r.Context(),
`SELECT date_format, time_format, decimal_point, thousands_sep, direction
FROM res_lang WHERE code = $1 AND active = true`, lang,
).Scan(&dateFormat, &timeFormat, &decimalPoint, &thousandsSep, &direction)
if err == nil {
langParams["date_format"] = dateFormat
langParams["time_format"] = timeFormat
langParams["decimal_point"] = decimalPoint
langParams["thousands_sep"] = thousandsSep
langParams["direction"] = direction
}
// Check if multiple languages are active
multiLang := false
var langCount int
if err := s.pool.QueryRow(r.Context(),
`SELECT COUNT(*) FROM res_lang WHERE active = true`).Scan(&langCount); err == nil {
if langCount > 1 {
multiLang = true
}
}
s.writeJSONRPC(w, nil, map[string]interface{}{
"lang": "en_US",
"hash": "empty",
"lang_parameters": map[string]interface{}{
"direction": "ltr",
"date_format": "%%m/%%d/%%Y",
"time_format": "%%H:%%M:%%S",
"grouping": "[3,0]",
"decimal_point": ".",
"thousands_sep": ",",
"week_start": 1,
},
"modules": map[string]interface{}{},
"multi_lang": false,
"lang": lang,
"hash": fmt.Sprintf("odoo-go-boot-%s", lang),
"lang_parameters": langParams,
"modules": map[string]interface{}{},
"multi_lang": multiLang,
}, nil)
}

View File

@@ -261,24 +261,107 @@ func (s *Server) buildSessionInfo(sess *Session) map[string]interface{} {
}
}
// handleTranslations returns empty English translations.
// handleTranslations returns translations for the requested language.
// Mirrors: odoo/addons/web/controllers/webclient.py translations()
//
// The web client calls this with params like {mods: ["web","base",...], lang: "de_DE"}.
// We load translations from ir_translation table and return them in Odoo's format.
func (s *Server) handleTranslations(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "public, max-age=3600")
// Determine requested language from POST body or session context
lang := "en_US"
if r.Method == http.MethodPost {
var req JSONRPCRequest
if err := json.NewDecoder(r.Body).Decode(&req); err == nil {
var params struct {
Mods []string `json:"mods"`
Lang string `json:"lang"`
}
if err := json.Unmarshal(req.Params, &params); err == nil && params.Lang != "" {
lang = params.Lang
}
}
}
if q := r.URL.Query().Get("lang"); q != "" {
lang = q
}
// Default lang parameters (English)
langParams := map[string]interface{}{
"direction": "ltr",
"date_format": "%%m/%%d/%%Y",
"time_format": "%%H:%%M:%%S",
"grouping": "[3,0]",
"decimal_point": ".",
"thousands_sep": ",",
"week_start": 1,
}
// Try to load language parameters from res_lang
var dateFormat, timeFormat, decimalPoint, thousandsSep, direction string
err := s.pool.QueryRow(r.Context(),
`SELECT date_format, time_format, decimal_point, thousands_sep, direction
FROM res_lang WHERE code = $1 AND active = true`, lang,
).Scan(&dateFormat, &timeFormat, &decimalPoint, &thousandsSep, &direction)
if err == nil {
// Convert Go-style format markers to Python-style (double-%) for the web client
langParams["date_format"] = dateFormat
langParams["time_format"] = timeFormat
langParams["decimal_point"] = decimalPoint
langParams["thousands_sep"] = thousandsSep
langParams["direction"] = direction
}
// Load translations from ir_translation
modules := make(map[string]interface{})
multiLang := false
// Check if translations exist for this language
rows, err := s.pool.Query(r.Context(),
`SELECT COALESCE(module, ''), src, value FROM ir_translation
WHERE lang = $1 AND value != '' AND value IS NOT NULL
ORDER BY module, name`, lang)
if err == nil {
defer rows.Close()
// Group translations by module
modMessages := make(map[string][]map[string]string)
for rows.Next() {
var module, src, value string
if err := rows.Scan(&module, &src, &value); err != nil {
continue
}
if module == "" {
module = "web"
}
modMessages[module] = append(modMessages[module], map[string]string{
"id": src,
"string": value,
})
}
for mod, msgs := range modMessages {
modules[mod] = map[string]interface{}{
"messages": msgs,
}
multiLang = true
}
}
// Check if more than one active language exists
var langCount int
if err := s.pool.QueryRow(r.Context(),
`SELECT COUNT(*) FROM res_lang WHERE active = true`).Scan(&langCount); err == nil {
if langCount > 1 {
multiLang = true
}
}
json.NewEncoder(w).Encode(map[string]interface{}{
"lang": "en_US",
"hash": "odoo-go-empty",
"lang_parameters": map[string]interface{}{
"direction": "ltr",
"date_format": "%%m/%%d/%%Y",
"time_format": "%%H:%%M:%%S",
"grouping": "[3,0]",
"decimal_point": ".",
"thousands_sep": ",",
"week_start": 1,
},
"modules": map[string]interface{}{},
"multi_lang": false,
"lang": lang,
"hash": fmt.Sprintf("odoo-go-%s", lang),
"lang_parameters": langParams,
"modules": modules,
"multi_lang": multiLang,
})
}