Fix P0-P2 bugs: sale.order M2M, display_name, DefaultGet

P0: Fix sale.order creation (was completely broken)
- Corrected M2M junction table name from sale_order_line_account_tax_rel
  to account_tax_sale_order_line_rel (ORM sorts alphabetically)
- Added fallback in BeforeCreate if sequence generation fails

P1: Add display_name as magic field on ALL models
- Added to addMagicFields() in pkg/orm/model.go (like Python BaseModel)
- Computed on-the-fly in Read() from recName field, no DB column
- Removed explicit display_name from res.partner (now auto-inherited)

P2: Add DefaultGet hooks for sale.order and purchase.order
- Sets company_id, currency_id, date_order/date_planned from environment
- Follows same pattern as account.move's DefaultGet

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc
2026-04-01 02:28:49 +02:00
parent 1aa9351054
commit 70649c4b4e
6 changed files with 107 additions and 3 deletions

View File

@@ -107,6 +107,32 @@ func initPurchaseOrder() {
orm.Char("origin", orm.FieldOpts{String: "Source Document"}),
)
// -- DefaultGet: Provide dynamic defaults for new records --
// Mirrors: odoo/addons/purchase/models/purchase_order.py PurchaseOrder.default_get()
// Supplies company_id, currency_id, date_order when creating a new PO.
m.DefaultGet = func(env *orm.Environment, fields []string) orm.Values {
vals := make(orm.Values)
// Default company from the current user's session
companyID := env.CompanyID()
if companyID > 0 {
vals["company_id"] = companyID
}
// Default currency from the company
var currencyID int64
err := env.Tx().QueryRow(env.Ctx(),
`SELECT currency_id FROM res_company WHERE id = $1`, companyID).Scan(&currencyID)
if err == nil && currencyID > 0 {
vals["currency_id"] = currencyID
}
// Default date_order = now
vals["date_order"] = time.Now().Format("2006-01-02 15:04:05")
return vals
}
// button_confirm: draft → purchase
m.RegisterMethod("button_confirm", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env()