Bring odoo-go to ~70%: read_group, record rules, admin, sessions
Phase 1: read_group/web_read_group with SQL GROUP BY, aggregates (sum/avg/min/max/count/array_agg/sum_currency), date granularity, M2O groupby resolution to [id, display_name]. Phase 2: Record rules with domain_force parsing (Python literal parser), global AND + group OR merging. Domain operators: child_of, parent_of, any, not any compiled to SQL hierarchy/EXISTS queries. Phase 3: Button dispatch via /web/dataset/call_button, method return values interpreted as actions. Payment register wizard (account.payment.register) for sale→invoice→pay flow. Phase 4: ir.filters, ir.default, product fields expanded, SO line product_id onchange, ir_model+ir_model_fields DB seeding. Phase 5: CSV export (/web/export/csv), attachment upload/download via ir.attachment, fields_get with aggregator hints. Admin/System: Session persistence (PostgreSQL-backed), ir.config_parameter with get_param/set_param, ir.cron, ir.logging, res.lang, res.config.settings with company-related fields, Settings form view. Technical menu with Views/Actions/Parameters/Security/Logging sub-menus. User change_password, preferences. Password never exposed in UI/API. Bugfixes: false→nil for varchar/int fields, int32 in toInt64, call_button route with trailing slash, create_invoices returns action, search view always included, get_formview_action, name_create, ir.http stub. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -727,6 +727,200 @@ func initAccountPayment() {
|
||||
orm.Char("payment_reference", orm.FieldOpts{String: "Payment Reference"}),
|
||||
orm.Char("payment_method_code", orm.FieldOpts{String: "Payment Method Code"}),
|
||||
)
|
||||
|
||||
// action_post: confirm and post the payment.
|
||||
// Mirrors: odoo/addons/account/models/account_payment.py action_post()
|
||||
m.RegisterMethod("action_post", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE account_payment SET state = 'paid' WHERE id = $1 AND state = 'draft'`, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_cancel: cancel the payment.
|
||||
m.RegisterMethod("action_cancel", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE account_payment SET state = 'canceled' WHERE id = $1`, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
}
|
||||
|
||||
// initAccountPaymentRegister registers the payment register wizard.
|
||||
// Mirrors: odoo/addons/account/wizard/account_payment_register.py
|
||||
// This is a TransientModel wizard opened via "Register Payment" button on invoices.
|
||||
func initAccountPaymentRegister() {
|
||||
m := orm.NewModel("account.payment.register", orm.ModelOpts{
|
||||
Description: "Register Payment",
|
||||
Type: orm.ModelTransient,
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Date("payment_date", orm.FieldOpts{String: "Payment Date", Required: true}),
|
||||
orm.Monetary("amount", orm.FieldOpts{String: "Amount", CurrencyField: "currency_id"}),
|
||||
orm.Many2one("currency_id", "res.currency", orm.FieldOpts{String: "Currency", Required: true}),
|
||||
orm.Many2one("journal_id", "account.journal", orm.FieldOpts{String: "Journal", Required: true}),
|
||||
orm.Many2one("partner_id", "res.partner", orm.FieldOpts{String: "Customer/Vendor"}),
|
||||
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
|
||||
orm.Selection("payment_type", []orm.SelectionItem{
|
||||
{Value: "outbound", Label: "Send"},
|
||||
{Value: "inbound", Label: "Receive"},
|
||||
}, orm.FieldOpts{String: "Payment Type", Default: "inbound"}),
|
||||
orm.Selection("partner_type", []orm.SelectionItem{
|
||||
{Value: "customer", Label: "Customer"},
|
||||
{Value: "supplier", Label: "Vendor"},
|
||||
}, orm.FieldOpts{String: "Partner Type", Default: "customer"}),
|
||||
orm.Char("communication", orm.FieldOpts{String: "Memo"}),
|
||||
// Context-only: which invoice(s) are being paid
|
||||
orm.Many2many("line_ids", "account.move.line", orm.FieldOpts{
|
||||
String: "Journal items",
|
||||
Relation: "payment_register_move_line_rel",
|
||||
Column1: "wizard_id",
|
||||
Column2: "line_id",
|
||||
}),
|
||||
)
|
||||
|
||||
// action_create_payments: create account.payment from the wizard and mark invoice as paid.
|
||||
// Mirrors: odoo/addons/account/wizard/account_payment_register.py action_create_payments()
|
||||
m.RegisterMethod("action_create_payments", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
wizardData, err := rs.Read([]string{
|
||||
"payment_date", "amount", "currency_id", "journal_id",
|
||||
"partner_id", "company_id", "payment_type", "partner_type", "communication",
|
||||
})
|
||||
if err != nil || len(wizardData) == 0 {
|
||||
return nil, fmt.Errorf("account: cannot read payment register wizard")
|
||||
}
|
||||
wiz := wizardData[0]
|
||||
|
||||
paymentRS := env.Model("account.payment")
|
||||
paymentVals := orm.Values{
|
||||
"payment_type": wiz["payment_type"],
|
||||
"partner_type": wiz["partner_type"],
|
||||
"amount": wiz["amount"],
|
||||
"date": wiz["payment_date"],
|
||||
"currency_id": wiz["currency_id"],
|
||||
"journal_id": wiz["journal_id"],
|
||||
"partner_id": wiz["partner_id"],
|
||||
"company_id": wiz["company_id"],
|
||||
"payment_reference": wiz["communication"],
|
||||
"state": "draft",
|
||||
}
|
||||
|
||||
payment, err := paymentRS.Create(paymentVals)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("account: create payment: %w", err)
|
||||
}
|
||||
|
||||
// Auto-post the payment
|
||||
paymentModel := orm.Registry.Get("account.payment")
|
||||
if paymentModel != nil {
|
||||
if postMethod, ok := paymentModel.Methods["action_post"]; ok {
|
||||
if _, err := postMethod(payment); err != nil {
|
||||
return nil, fmt.Errorf("account: post payment: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mark related invoices as paid (simplified: update payment_state on active invoices in context)
|
||||
// In Python Odoo this happens through reconciliation; we simplify for 70% target.
|
||||
if ctx := env.Context(); ctx != nil {
|
||||
if activeIDs, ok := ctx["active_ids"].([]interface{}); ok {
|
||||
for _, rawID := range activeIDs {
|
||||
if moveID, ok := toInt64Arg(rawID); ok && moveID > 0 {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE account_move SET payment_state = 'paid' WHERE id = $1 AND state = 'posted'`, moveID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return action to close wizard (standard Odoo pattern)
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window_close",
|
||||
}, nil
|
||||
})
|
||||
|
||||
// DefaultGet: pre-fill wizard from active invoice context.
|
||||
// Mirrors: odoo/addons/account/wizard/account_payment_register.py default_get()
|
||||
m.DefaultGet = func(env *orm.Environment, fields []string) orm.Values {
|
||||
vals := orm.Values{
|
||||
"payment_date": time.Now().Format("2006-01-02"),
|
||||
}
|
||||
|
||||
ctx := env.Context()
|
||||
if ctx == nil {
|
||||
return vals
|
||||
}
|
||||
|
||||
// Get active invoice IDs from context
|
||||
var moveIDs []int64
|
||||
if ids, ok := ctx["active_ids"].([]interface{}); ok {
|
||||
for _, rawID := range ids {
|
||||
if id, ok := toInt64Arg(rawID); ok && id > 0 {
|
||||
moveIDs = append(moveIDs, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(moveIDs) == 0 {
|
||||
return vals
|
||||
}
|
||||
|
||||
// Read first invoice to pre-fill defaults
|
||||
moveRS := env.Model("account.move").Browse(moveIDs[0])
|
||||
moveData, err := moveRS.Read([]string{
|
||||
"partner_id", "company_id", "currency_id", "amount_residual", "move_type",
|
||||
})
|
||||
if err != nil || len(moveData) == 0 {
|
||||
return vals
|
||||
}
|
||||
mv := moveData[0]
|
||||
|
||||
if pid, ok := toInt64Arg(mv["partner_id"]); ok && pid > 0 {
|
||||
vals["partner_id"] = pid
|
||||
}
|
||||
if cid, ok := toInt64Arg(mv["company_id"]); ok && cid > 0 {
|
||||
vals["company_id"] = cid
|
||||
}
|
||||
if curID, ok := toInt64Arg(mv["currency_id"]); ok && curID > 0 {
|
||||
vals["currency_id"] = curID
|
||||
}
|
||||
if amt, ok := mv["amount_residual"].(float64); ok {
|
||||
vals["amount"] = amt
|
||||
}
|
||||
|
||||
// Determine payment type from move type
|
||||
moveType, _ := mv["move_type"].(string)
|
||||
switch moveType {
|
||||
case "out_invoice", "out_receipt":
|
||||
vals["payment_type"] = "inbound"
|
||||
vals["partner_type"] = "customer"
|
||||
case "in_invoice", "in_receipt":
|
||||
vals["payment_type"] = "outbound"
|
||||
vals["partner_type"] = "supplier"
|
||||
}
|
||||
|
||||
// Default bank journal
|
||||
var journalID int64
|
||||
companyID := env.CompanyID()
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT id FROM account_journal
|
||||
WHERE type = 'bank' AND active = true AND company_id = $1
|
||||
ORDER BY sequence, id LIMIT 1`, companyID).Scan(&journalID)
|
||||
if journalID > 0 {
|
||||
vals["journal_id"] = journalID
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
}
|
||||
|
||||
// initAccountPaymentTerm registers payment terms.
|
||||
|
||||
@@ -7,6 +7,7 @@ func Init() {
|
||||
initAccountMove()
|
||||
initAccountMoveLine()
|
||||
initAccountPayment()
|
||||
initAccountPaymentRegister()
|
||||
initAccountPaymentTerm()
|
||||
initAccountReconcile()
|
||||
initAccountBankStatement()
|
||||
|
||||
@@ -13,4 +13,11 @@ func Init() {
|
||||
initIrModelAccess()
|
||||
initIrRule()
|
||||
initIrModelData()
|
||||
initIrFilter()
|
||||
initIrDefault()
|
||||
initIrConfigParameter() // ir.config_parameter (System Parameters)
|
||||
initIrLogging() // ir.logging (Server log entries)
|
||||
initIrCron() // ir.cron (Scheduled Actions)
|
||||
initResLang() // res.lang (Languages)
|
||||
initResConfigSettings() // res.config.settings (TransientModel)
|
||||
}
|
||||
|
||||
60
addons/base/models/ir_config_parameter.go
Normal file
60
addons/base/models/ir_config_parameter.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
|
||||
// initIrConfigParameter registers ir.config_parameter — System parameters.
|
||||
// Mirrors: odoo/addons/base/models/ir_config_parameter.py class IrConfigParameter
|
||||
//
|
||||
// Key/value store for system-wide configuration.
|
||||
// Examples: web.base.url, database.uuid, mail.catchall.domain
|
||||
func initIrConfigParameter() {
|
||||
m := orm.NewModel("ir.config_parameter", orm.ModelOpts{
|
||||
Description: "System Parameter",
|
||||
Order: "key",
|
||||
RecName: "key",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Char("key", orm.FieldOpts{String: "Key", Required: true, Index: true}),
|
||||
orm.Text("value", orm.FieldOpts{String: "Value", Required: true}),
|
||||
)
|
||||
|
||||
// get_param: returns the value for a key, or default.
|
||||
// Mirrors: odoo/addons/base/models/ir_config_parameter.py IrConfigParameter.get_param()
|
||||
m.RegisterMethod("get_param", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
if len(args) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
key, _ := args[0].(string)
|
||||
defaultVal := ""
|
||||
if len(args) > 1 {
|
||||
defaultVal, _ = args[1].(string)
|
||||
}
|
||||
env := rs.Env()
|
||||
var value string
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT value FROM ir_config_parameter WHERE key = $1`, key).Scan(&value)
|
||||
if err != nil {
|
||||
return defaultVal, nil
|
||||
}
|
||||
return value, nil
|
||||
})
|
||||
|
||||
// set_param: sets the value for a key (upsert).
|
||||
// Mirrors: odoo/addons/base/models/ir_config_parameter.py IrConfigParameter.set_param()
|
||||
m.RegisterMethod("set_param", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
if len(args) < 2 {
|
||||
return false, nil
|
||||
}
|
||||
key, _ := args[0].(string)
|
||||
value, _ := args[1].(string)
|
||||
env := rs.Env()
|
||||
_, err := env.Tx().Exec(env.Ctx(),
|
||||
`INSERT INTO ir_config_parameter (key, value) VALUES ($1, $2)
|
||||
ON CONFLICT (key) DO UPDATE SET value = $2`, key, value)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
}
|
||||
34
addons/base/models/ir_cron.go
Normal file
34
addons/base/models/ir_cron.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
|
||||
// initIrCron registers ir.cron — Scheduled actions.
|
||||
// Mirrors: odoo/addons/base/models/ir_cron.py class IrCron
|
||||
//
|
||||
// Defines recurring tasks executed by the scheduler.
|
||||
func initIrCron() {
|
||||
m := orm.NewModel("ir.cron", orm.ModelOpts{
|
||||
Description: "Scheduled Actions",
|
||||
Order: "name",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Char("name", orm.FieldOpts{String: "Name", Required: true}),
|
||||
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
|
||||
orm.Many2one("user_id", "res.users", orm.FieldOpts{String: "User", Required: true}),
|
||||
orm.Integer("interval_number", orm.FieldOpts{String: "Interval Number", Default: 1}),
|
||||
orm.Selection("interval_type", []orm.SelectionItem{
|
||||
{Value: "minutes", Label: "Minutes"},
|
||||
{Value: "hours", Label: "Hours"},
|
||||
{Value: "days", Label: "Days"},
|
||||
{Value: "weeks", Label: "Weeks"},
|
||||
{Value: "months", Label: "Months"},
|
||||
}, orm.FieldOpts{String: "Interval Type", Default: "months"}),
|
||||
orm.Integer("numbercall", orm.FieldOpts{String: "Number of Calls", Default: -1}),
|
||||
orm.Datetime("nextcall", orm.FieldOpts{String: "Next Execution Date", Required: true}),
|
||||
orm.Datetime("lastcall", orm.FieldOpts{String: "Last Execution Date"}),
|
||||
orm.Integer("priority", orm.FieldOpts{String: "Priority", Default: 5}),
|
||||
orm.Char("code", orm.FieldOpts{String: "Python Code"}),
|
||||
orm.Many2one("model_id", "ir.model", orm.FieldOpts{String: "Model"}),
|
||||
)
|
||||
}
|
||||
35
addons/base/models/ir_logging.go
Normal file
35
addons/base/models/ir_logging.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
|
||||
// initIrLogging registers ir.logging — Server-side log entries.
|
||||
// Mirrors: odoo/addons/base/models/ir_logging.py class IrLogging
|
||||
//
|
||||
// Stores structured log entries written by the server,
|
||||
// accessible via Settings > Technical > Logging.
|
||||
func initIrLogging() {
|
||||
m := orm.NewModel("ir.logging", orm.ModelOpts{
|
||||
Description: "Logging",
|
||||
Order: "id desc",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Char("name", orm.FieldOpts{String: "Name", Required: true}),
|
||||
orm.Selection("type", []orm.SelectionItem{
|
||||
{Value: "client", Label: "Client"},
|
||||
{Value: "server", Label: "Server"},
|
||||
}, orm.FieldOpts{String: "Type", Required: true}),
|
||||
orm.Char("dbname", orm.FieldOpts{String: "Database Name"}),
|
||||
orm.Selection("level", []orm.SelectionItem{
|
||||
{Value: "DEBUG", Label: "Debug"},
|
||||
{Value: "INFO", Label: "Info"},
|
||||
{Value: "WARNING", Label: "Warning"},
|
||||
{Value: "ERROR", Label: "Error"},
|
||||
{Value: "CRITICAL", Label: "Critical"},
|
||||
}, orm.FieldOpts{String: "Level"}),
|
||||
orm.Text("message", orm.FieldOpts{String: "Message", Required: true}),
|
||||
orm.Char("path", orm.FieldOpts{String: "Path"}),
|
||||
orm.Char("func", orm.FieldOpts{String: "Function"}),
|
||||
orm.Char("line", orm.FieldOpts{String: "Line"}),
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// initIrModel registers ir.model and ir.model.fields — Odoo's model metadata.
|
||||
// Mirrors: odoo/addons/base/models/ir_model.py
|
||||
@@ -173,3 +177,139 @@ func initIrModelData() {
|
||||
orm.Boolean("noupdate", orm.FieldOpts{String: "Non Updatable", Default: false}),
|
||||
)
|
||||
}
|
||||
|
||||
// initIrFilter registers ir.filters — Saved search filters (Favorites).
|
||||
// Mirrors: odoo/addons/base/models/ir_filters.py class IrFilters
|
||||
//
|
||||
// Filters are saved by the web client when a user bookmarks a search.
|
||||
// user_id = NULL means the filter is shared with everyone.
|
||||
func initIrFilter() {
|
||||
m := orm.NewModel("ir.filters", orm.ModelOpts{
|
||||
Description: "Filters",
|
||||
Order: "model_id, name, id desc",
|
||||
RecName: "name",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Char("name", orm.FieldOpts{String: "Filter Name", Required: true, Translate: true}),
|
||||
orm.Many2one("user_id", "res.users", orm.FieldOpts{
|
||||
String: "User",
|
||||
OnDelete: orm.OnDeleteCascade,
|
||||
Help: "The user this filter is private to. When left empty the filter is shared.",
|
||||
}),
|
||||
orm.Text("domain", orm.FieldOpts{String: "Domain", Required: true, Default: "[]"}),
|
||||
orm.Text("context", orm.FieldOpts{String: "Context", Required: true, Default: "{}"}),
|
||||
orm.Text("sort", orm.FieldOpts{String: "Sort", Required: true, Default: "[]"}),
|
||||
orm.Char("model_id", orm.FieldOpts{
|
||||
String: "Model", Required: true,
|
||||
Help: "Model name of the filtered view, e.g. 'res.partner'.",
|
||||
}),
|
||||
orm.Boolean("is_default", orm.FieldOpts{String: "Default Filter"}),
|
||||
orm.Many2one("action_id", "ir.act.window", orm.FieldOpts{
|
||||
String: "Action",
|
||||
OnDelete: orm.OnDeleteCascade,
|
||||
Help: "The menu action this filter applies to.",
|
||||
}),
|
||||
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
|
||||
)
|
||||
|
||||
// create_or_replace: Creates or updates a filter by (name, model_id, user_id, action_id).
|
||||
// Mirrors: odoo/addons/base/models/ir_filters.py IrFilters.create_or_replace()
|
||||
// Called by the web client when saving a favorite.
|
||||
m.RegisterMethod("create_or_replace", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
if len(args) == 0 {
|
||||
return nil, fmt.Errorf("ir.filters: create_or_replace requires a filter dict")
|
||||
}
|
||||
vals, ok := args[0].(orm.Values)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("ir.filters: create_or_replace expects Values argument")
|
||||
}
|
||||
|
||||
env := rs.Env()
|
||||
name, _ := vals["name"].(string)
|
||||
modelID, _ := vals["model_id"].(string)
|
||||
|
||||
// Build lookup query
|
||||
query := `SELECT id FROM ir_filters WHERE name = $1 AND model_id = $2`
|
||||
qArgs := []interface{}{name, modelID}
|
||||
idx := 3
|
||||
|
||||
// user_id
|
||||
if uid, ok := vals["user_id"]; ok && uid != nil {
|
||||
query += fmt.Sprintf(` AND user_id = $%d`, idx)
|
||||
qArgs = append(qArgs, uid)
|
||||
idx++
|
||||
} else {
|
||||
query += ` AND user_id IS NULL`
|
||||
}
|
||||
|
||||
// action_id
|
||||
if aid, ok := vals["action_id"]; ok && aid != nil {
|
||||
query += fmt.Sprintf(` AND action_id = $%d`, idx)
|
||||
qArgs = append(qArgs, aid)
|
||||
} else {
|
||||
query += ` AND action_id IS NULL`
|
||||
}
|
||||
|
||||
query += ` LIMIT 1`
|
||||
|
||||
var existingID int64
|
||||
err := env.Tx().QueryRow(env.Ctx(), query, qArgs...).Scan(&existingID)
|
||||
if err == nil && existingID > 0 {
|
||||
// Update existing
|
||||
existing := rs.Browse(existingID)
|
||||
if err := existing.Write(vals); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return existingID, nil
|
||||
}
|
||||
|
||||
// Create new
|
||||
created, err := env.Model("ir.filters").Create(vals)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return created.ID(), nil
|
||||
})
|
||||
}
|
||||
|
||||
// initIrDefault registers ir.default — User-defined field defaults.
|
||||
// Mirrors: odoo/addons/base/models/ir_default.py class IrDefault
|
||||
//
|
||||
// Stores default values for specific fields, optionally scoped to a user or company.
|
||||
// Example: default value for sale.order.payment_term_id for company 1.
|
||||
func initIrDefault() {
|
||||
m := orm.NewModel("ir.default", orm.ModelOpts{
|
||||
Description: "Default Values",
|
||||
Order: "id",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
// In Python Odoo this is Many2one to ir.model.fields.
|
||||
// We use Char for now since ir.model.fields may not be fully populated.
|
||||
orm.Char("field_id", orm.FieldOpts{
|
||||
String: "Field",
|
||||
Required: true,
|
||||
Index: true,
|
||||
Help: "Reference to the field, format: 'model_name.field_name'.",
|
||||
}),
|
||||
orm.Text("json_value", orm.FieldOpts{
|
||||
String: "Default Value (JSON)",
|
||||
Help: "JSON-encoded default value for the field.",
|
||||
}),
|
||||
orm.Many2one("user_id", "res.users", orm.FieldOpts{
|
||||
String: "User",
|
||||
OnDelete: orm.OnDeleteCascade,
|
||||
Help: "If set, this default only applies to this user.",
|
||||
}),
|
||||
orm.Many2one("company_id", "res.company", orm.FieldOpts{
|
||||
String: "Company",
|
||||
OnDelete: orm.OnDeleteCascade,
|
||||
Help: "If set, this default only applies in this company.",
|
||||
}),
|
||||
orm.Char("condition", orm.FieldOpts{
|
||||
String: "Condition",
|
||||
Help: "Optional condition for applying this default.",
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
76
addons/base/models/res_config_settings.go
Normal file
76
addons/base/models/res_config_settings.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
|
||||
// initResConfigSettings registers the res.config.settings transient model.
|
||||
// Mirrors: odoo/addons/base/models/res_config.py class ResConfigSettings(TransientModel)
|
||||
//
|
||||
// This wizard provides the Settings form. Each "save" creates a new transient
|
||||
// record, applies the values, then the record is eventually cleaned up.
|
||||
func initResConfigSettings() {
|
||||
m := orm.NewModel("res.config.settings", orm.ModelOpts{
|
||||
Description: "Config Settings",
|
||||
Type: orm.ModelTransient,
|
||||
})
|
||||
|
||||
// -- General settings --
|
||||
m.AddFields(
|
||||
orm.Many2one("company_id", "res.company", orm.FieldOpts{
|
||||
String: "Company", Index: true,
|
||||
}),
|
||||
orm.Boolean("user_default_rights", orm.FieldOpts{String: "Default Access Rights"}),
|
||||
orm.Boolean("external_email_server_default", orm.FieldOpts{String: "External Email Servers"}),
|
||||
orm.Boolean("module_base_import", orm.FieldOpts{String: "Allow Import"}),
|
||||
orm.Boolean("module_google_calendar", orm.FieldOpts{String: "Google Calendar"}),
|
||||
orm.Boolean("group_multi_company", orm.FieldOpts{String: "Multi Companies"}),
|
||||
orm.Boolean("show_effect", orm.FieldOpts{String: "Show Effect", Default: true}),
|
||||
)
|
||||
|
||||
// -- Company info fields (mirrors res.company, for display in settings) --
|
||||
// Mirrors: odoo/addons/base/models/res_config_settings.py company-related fields
|
||||
m.AddFields(
|
||||
orm.Char("company_name", orm.FieldOpts{String: "Company Name", Related: "company_id.name"}),
|
||||
orm.Many2one("company_currency_id", "res.currency", orm.FieldOpts{
|
||||
String: "Currency", Related: "company_id.currency_id",
|
||||
}),
|
||||
orm.Many2one("company_country_id", "res.country", orm.FieldOpts{
|
||||
String: "Country", Related: "company_id.country_id",
|
||||
}),
|
||||
orm.Char("company_street", orm.FieldOpts{String: "Street", Related: "company_id.street"}),
|
||||
orm.Char("company_street2", orm.FieldOpts{String: "Street2", Related: "company_id.street2"}),
|
||||
orm.Char("company_zip", orm.FieldOpts{String: "Zip", Related: "company_id.zip"}),
|
||||
orm.Char("company_city", orm.FieldOpts{String: "City", Related: "company_id.city"}),
|
||||
orm.Char("company_phone", orm.FieldOpts{String: "Phone", Related: "company_id.phone"}),
|
||||
orm.Char("company_email", orm.FieldOpts{String: "Email", Related: "company_id.email"}),
|
||||
orm.Char("company_website", orm.FieldOpts{String: "Website", Related: "company_id.website"}),
|
||||
orm.Char("company_vat", orm.FieldOpts{String: "Tax ID", Related: "company_id.vat"}),
|
||||
orm.Char("company_registry", orm.FieldOpts{String: "Company Registry", Related: "company_id.company_registry"}),
|
||||
)
|
||||
|
||||
// -- Accounting settings --
|
||||
m.AddFields(
|
||||
orm.Char("chart_template", orm.FieldOpts{String: "Chart of Accounts"}),
|
||||
orm.Selection("tax_calculation_rounding_method", []orm.SelectionItem{
|
||||
{Value: "round_per_line", Label: "Round per Line"},
|
||||
{Value: "round_globally", Label: "Round Globally"},
|
||||
}, orm.FieldOpts{String: "Tax Calculation Rounding Method", Default: "round_per_line"}),
|
||||
)
|
||||
|
||||
// execute: called by the Settings form to apply configuration.
|
||||
// Mirrors: odoo/addons/base/models/res_config.py ResConfigSettings.execute()
|
||||
m.RegisterMethod("execute", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
// In Python Odoo this writes Related fields back to res.company.
|
||||
// For now we just return true; the Related fields are read-only display.
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// DefaultGet: pre-fill from current company.
|
||||
// Mirrors: odoo/addons/base/models/res_config.py ResConfigSettings.default_get()
|
||||
m.DefaultGet = func(env *orm.Environment, fields []string) orm.Values {
|
||||
vals := orm.Values{
|
||||
"company_id": env.CompanyID(),
|
||||
"show_effect": true,
|
||||
}
|
||||
return vals
|
||||
}
|
||||
}
|
||||
27
addons/base/models/res_lang.go
Normal file
27
addons/base/models/res_lang.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
|
||||
// initResLang registers res.lang — Languages.
|
||||
// Mirrors: odoo/addons/base/models/res_lang.py
|
||||
func initResLang() {
|
||||
m := orm.NewModel("res.lang", orm.ModelOpts{
|
||||
Description: "Languages",
|
||||
RecName: "name",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Char("name", orm.FieldOpts{String: "Name", Required: true}),
|
||||
orm.Char("code", orm.FieldOpts{String: "Locale Code", Required: true}),
|
||||
orm.Char("iso_code", orm.FieldOpts{String: "ISO Code"}),
|
||||
orm.Char("url_code", orm.FieldOpts{String: "URL Code"}),
|
||||
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
|
||||
orm.Char("direction", orm.FieldOpts{String: "Direction", Default: "ltr"}),
|
||||
orm.Char("date_format", orm.FieldOpts{String: "Date Format", Default: "%m/%d/%Y"}),
|
||||
orm.Char("time_format", orm.FieldOpts{String: "Time Format", Default: "%H:%M:%S"}),
|
||||
orm.Char("week_start", orm.FieldOpts{String: "First Day of Week", Default: "7"}),
|
||||
orm.Char("decimal_point", orm.FieldOpts{String: "Decimal Separator", Default: "."}),
|
||||
orm.Char("thousands_sep", orm.FieldOpts{String: "Thousands Separator", Default: ","}),
|
||||
orm.Char("grouping", orm.FieldOpts{String: "Separator Format", Default: "[]"}),
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
"odoo-go/pkg/tools"
|
||||
)
|
||||
|
||||
// initResUsers registers the res.users model.
|
||||
// Mirrors: odoo/addons/base/models/res_users.py class Users
|
||||
@@ -78,6 +83,55 @@ func initResUsers() {
|
||||
"context": map[string]interface{}{},
|
||||
}, nil
|
||||
})
|
||||
|
||||
// change_password: verifies old password and sets a new one.
|
||||
// Mirrors: odoo/addons/base/models/res_users.py Users.change_password()
|
||||
m.RegisterMethod("change_password", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
if len(args) < 2 {
|
||||
return false, fmt.Errorf("change_password requires old_password and new_password")
|
||||
}
|
||||
oldPw, _ := args[0].(string)
|
||||
newPw, _ := args[1].(string)
|
||||
|
||||
env := rs.Env()
|
||||
uid := env.UID()
|
||||
|
||||
// Verify old password
|
||||
var hashedPw string
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT password FROM res_users WHERE id = $1`, uid).Scan(&hashedPw)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("user not found")
|
||||
}
|
||||
if !tools.CheckPassword(hashedPw, oldPw) {
|
||||
return false, fmt.Errorf("incorrect old password")
|
||||
}
|
||||
|
||||
// Hash and set new password
|
||||
newHash, err := tools.HashPassword(newPw)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
_, err = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE res_users SET password = $1 WHERE id = $2`, newHash, uid)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// preference_save: called when saving user preferences.
|
||||
// Mirrors: odoo/addons/base/models/res_users.py Users.preference_save()
|
||||
m.RegisterMethod("preference_save", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
// Preferences are saved via normal write; just return true.
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// preference_change_password: alias for change_password from preferences dialog.
|
||||
// Mirrors: odoo/addons/base/models/res_users.py Users.preference_change_password()
|
||||
m.RegisterMethod("preference_change_password", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
return rs.Env().Model("res.users").Browse(rs.Env().UID()).Read([]string{"id"})
|
||||
})
|
||||
}
|
||||
|
||||
// initResGroups registers the res.groups model.
|
||||
|
||||
@@ -7,14 +7,20 @@ import "odoo-go/pkg/orm"
|
||||
func initProductCategory() {
|
||||
m := orm.NewModel("product.category", orm.ModelOpts{
|
||||
Description: "Product Category",
|
||||
Order: "name",
|
||||
Order: "complete_name",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}),
|
||||
orm.Char("complete_name", orm.FieldOpts{
|
||||
String: "Complete Name", Compute: "_compute_complete_name", Store: true,
|
||||
}),
|
||||
orm.Many2one("parent_id", "product.category", orm.FieldOpts{
|
||||
String: "Parent Category", Index: true, OnDelete: orm.OnDeleteCascade,
|
||||
}),
|
||||
orm.One2many("child_id", "product.category", "parent_id", orm.FieldOpts{
|
||||
String: "Child Categories",
|
||||
}),
|
||||
orm.Many2one("property_account_income_categ_id", "account.account", orm.FieldOpts{
|
||||
String: "Income Account",
|
||||
Help: "This account will be used when validating a customer invoice.",
|
||||
@@ -83,6 +89,8 @@ func initProductTemplate() {
|
||||
}, orm.FieldOpts{String: "Product Type", Required: true, Default: "consu"}),
|
||||
orm.Float("list_price", orm.FieldOpts{String: "Sales Price", Default: 1.0}),
|
||||
orm.Float("standard_price", orm.FieldOpts{String: "Cost"}),
|
||||
orm.Char("default_code", orm.FieldOpts{String: "Internal Reference", Index: true}),
|
||||
orm.Char("barcode", orm.FieldOpts{String: "Barcode", Index: true}),
|
||||
orm.Many2one("categ_id", "product.category", orm.FieldOpts{
|
||||
String: "Product Category", Required: true,
|
||||
}),
|
||||
@@ -93,6 +101,15 @@ func initProductTemplate() {
|
||||
orm.Boolean("purchase_ok", orm.FieldOpts{String: "Can be Purchased", Default: true}),
|
||||
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
|
||||
orm.Text("description", orm.FieldOpts{String: "Description", Translate: true}),
|
||||
orm.Text("description_sale", orm.FieldOpts{
|
||||
String: "Sales Description", Translate: true,
|
||||
Help: "Description used in sales orders, quotations, and invoices.",
|
||||
}),
|
||||
orm.Text("description_purchase", orm.FieldOpts{
|
||||
String: "Purchase Description", Translate: true,
|
||||
Help: "Description used in purchase orders.",
|
||||
}),
|
||||
orm.Binary("image_1920", orm.FieldOpts{String: "Image"}),
|
||||
orm.Many2many("taxes_id", "account.tax", orm.FieldOpts{
|
||||
String: "Customer Taxes",
|
||||
Help: "Default taxes used when selling the product.",
|
||||
@@ -106,6 +123,10 @@ func initProductTemplate() {
|
||||
|
||||
// initProductProduct registers product.product — a concrete product variant.
|
||||
// Mirrors: odoo/addons/product/models/product_product.py
|
||||
//
|
||||
// In Odoo, product.product delegates to product.template via _inherits.
|
||||
// Here we define the variant-specific fields plus Related fields that proxy
|
||||
// key template fields for convenience (the web client reads these directly).
|
||||
func initProductProduct() {
|
||||
m := orm.NewModel("product.product", orm.ModelOpts{
|
||||
Description: "Product",
|
||||
@@ -116,11 +137,48 @@ func initProductProduct() {
|
||||
orm.Many2one("product_tmpl_id", "product.template", orm.FieldOpts{
|
||||
String: "Product Template", Required: true, OnDelete: orm.OnDeleteCascade, Index: true,
|
||||
}),
|
||||
|
||||
// Variant-specific fields
|
||||
orm.Char("default_code", orm.FieldOpts{String: "Internal Reference", Index: true}),
|
||||
orm.Char("barcode", orm.FieldOpts{String: "Barcode", Index: true}),
|
||||
orm.Float("volume", orm.FieldOpts{String: "Volume", Help: "The volume in m3."}),
|
||||
orm.Float("weight", orm.FieldOpts{String: "Weight", Help: "The weight in kg."}),
|
||||
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
|
||||
|
||||
// Related fields from product.template — proxied for direct access.
|
||||
// Mirrors: product_product.py fields with related='product_tmpl_id.xxx'
|
||||
orm.Char("name", orm.FieldOpts{
|
||||
String: "Name", Related: "product_tmpl_id.name",
|
||||
}),
|
||||
orm.Float("lst_price", orm.FieldOpts{
|
||||
String: "Public Price", Related: "product_tmpl_id.list_price",
|
||||
}),
|
||||
orm.Float("standard_price", orm.FieldOpts{
|
||||
String: "Cost", Related: "product_tmpl_id.standard_price",
|
||||
}),
|
||||
orm.Many2one("categ_id", "product.category", orm.FieldOpts{
|
||||
String: "Product Category", Related: "product_tmpl_id.categ_id",
|
||||
}),
|
||||
orm.Many2one("uom_id", "uom.uom", orm.FieldOpts{
|
||||
String: "Unit of Measure", Related: "product_tmpl_id.uom_id",
|
||||
}),
|
||||
orm.Selection("type", []orm.SelectionItem{
|
||||
{Value: "consu", Label: "Consumable"},
|
||||
{Value: "service", Label: "Service"},
|
||||
{Value: "storable", Label: "Storable Product"},
|
||||
}, orm.FieldOpts{String: "Product Type", Related: "product_tmpl_id.type"}),
|
||||
orm.Boolean("sale_ok", orm.FieldOpts{
|
||||
String: "Can be Sold", Related: "product_tmpl_id.sale_ok",
|
||||
}),
|
||||
orm.Boolean("purchase_ok", orm.FieldOpts{
|
||||
String: "Can be Purchased", Related: "product_tmpl_id.purchase_ok",
|
||||
}),
|
||||
orm.Text("description_sale", orm.FieldOpts{
|
||||
String: "Sales Description", Related: "product_tmpl_id.description_sale",
|
||||
}),
|
||||
orm.Binary("image_1920", orm.FieldOpts{
|
||||
String: "Image", Related: "product_tmpl_id.image_1920",
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -322,7 +322,34 @@ func initSaleOrder() {
|
||||
`UPDATE sale_order SET invoice_status = 'invoiced' WHERE id = $1`, soID)
|
||||
}
|
||||
|
||||
return invoiceIDs, nil
|
||||
if len(invoiceIDs) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
// Return action to open the created invoice(s)
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py action_view_invoice()
|
||||
if len(invoiceIDs) == 1 {
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"res_model": "account.move",
|
||||
"res_id": invoiceIDs[0],
|
||||
"view_mode": "form",
|
||||
"views": [][]interface{}{{nil, "form"}},
|
||||
"target": "current",
|
||||
}, nil
|
||||
}
|
||||
// Multiple invoices → list view
|
||||
ids := make([]interface{}, len(invoiceIDs))
|
||||
for i, id := range invoiceIDs {
|
||||
ids[i] = id
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"res_model": "account.move",
|
||||
"view_mode": "list,form",
|
||||
"views": [][]interface{}{{nil, "list"}, {nil, "form"}},
|
||||
"domain": []interface{}{[]interface{}{"id", "in", ids}},
|
||||
"target": "current",
|
||||
}, nil
|
||||
})
|
||||
|
||||
// action_create_delivery: Generate a stock picking (delivery) from a confirmed sale order.
|
||||
@@ -437,6 +464,54 @@ func initSaleOrderLine() {
|
||||
Order: "order_id, sequence, id",
|
||||
})
|
||||
|
||||
// -- Onchange: product_id → name + price_unit --
|
||||
// Mirrors: odoo/addons/sale/models/sale_order_line.py _compute_name(), _compute_price_unit()
|
||||
// When the user selects a product on a SO line, automatically fill in the
|
||||
// description from the product name and the unit price from the product list price.
|
||||
m.RegisterOnchange("product_id", func(env *orm.Environment, vals orm.Values) orm.Values {
|
||||
result := make(orm.Values)
|
||||
|
||||
// Extract product_id — may arrive as int64, int32, float64, or map with "id" key
|
||||
var productID int64
|
||||
switch v := vals["product_id"].(type) {
|
||||
case int64:
|
||||
productID = v
|
||||
case int32:
|
||||
productID = int64(v)
|
||||
case float64:
|
||||
productID = int64(v)
|
||||
case map[string]interface{}:
|
||||
if id, ok := v["id"]; ok {
|
||||
switch n := id.(type) {
|
||||
case float64:
|
||||
productID = int64(n)
|
||||
case int64:
|
||||
productID = n
|
||||
}
|
||||
}
|
||||
}
|
||||
if productID <= 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
// Read product name and list price
|
||||
var name string
|
||||
var listPrice float64
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(pt.name, ''), COALESCE(pt.list_price, 0)
|
||||
FROM product_product pp
|
||||
JOIN product_template pt ON pt.id = pp.product_tmpl_id
|
||||
WHERE pp.id = $1`, productID,
|
||||
).Scan(&name, &listPrice)
|
||||
if err != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
result["name"] = name
|
||||
result["price_unit"] = listPrice
|
||||
return result
|
||||
})
|
||||
|
||||
// -- Parent --
|
||||
m.AddFields(
|
||||
orm.Many2one("order_id", "sale.order", orm.FieldOpts{
|
||||
|
||||
Reference in New Issue
Block a user