Files
goodie/addons/product/models/product.go
Marc eb92a2e239 Complete ORM core: _inherits, Related write-back, Copy, constraints
Batch 1 (quick-fixes):
- Field.Copy attribute + IsCopyable() method for copy control
- Constraints now run in Write() (was only Create — bug fix)
- Readonly fields silently skipped in Write()
- active_test: auto-filter archived records in Search()

Batch 2 (Related field write-back):
- preprocessRelatedWrites() follows FK chain and writes to target model
- Enables Settings page to edit company name/address/etc.
- Loop protection via _write_related_depth context counter

Batch 3 (_inherits delegation):
- SetupAllInherits() generates Related fields from parent models
- preprocessInheritsCreate() auto-creates parent records on Create
- Declared on res.users, res.company, product.product
- Called in LoadModules before compute setup

Batch 4 (Copy method):
- Recordset.Copy(defaults) with blacklist, IsCopyable check
- M2M re-linking, rec_name "(copy)" suffix
- Replaces simplified copy case in server dispatch

Batch 5 (Onchange compute):
- RunOnchangeComputes() triggers dependent computes on field change
- Virtual record (ID=-1) with client values in cache
- Integrated into onchange RPC handler

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 19:57:04 +02:00

231 lines
10 KiB
Go

package models
import "odoo-go/pkg/orm"
// initProductCategory registers product.category — product classification tree.
// Mirrors: odoo/addons/product/models/product_category.py
func initProductCategory() {
m := orm.NewModel("product.category", orm.ModelOpts{
Description: "Product Category",
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.",
}),
orm.Many2one("property_account_expense_categ_id", "account.account", orm.FieldOpts{
String: "Expense Account",
Help: "This account will be used when validating a vendor bill.",
}),
)
}
// initUoM registers uom.category and uom.uom — units of measure.
// Mirrors: odoo/addons/product/models/product_uom.py
func initUoM() {
// uom.category — groups compatible units (e.g., Weight, Volume)
orm.NewModel("uom.category", orm.ModelOpts{
Description: "Product UoM Categories",
Order: "name",
}).AddFields(
orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}),
)
// uom.uom — individual units of measure
m := orm.NewModel("uom.uom", orm.ModelOpts{
Description: "Product Unit of Measure",
Order: "name",
})
m.AddFields(
orm.Char("name", orm.FieldOpts{String: "Unit of Measure", Required: true, Translate: true}),
orm.Many2one("category_id", "uom.category", orm.FieldOpts{
String: "Category", Required: true, OnDelete: orm.OnDeleteCascade,
Help: "Conversion between Units of Measure can only occur if they belong to the same category.",
}),
orm.Float("factor", orm.FieldOpts{
String: "Ratio", Required: true, Default: 1.0,
Help: "How much bigger or smaller this unit is compared to the reference unit of measure for this category.",
}),
orm.Selection("uom_type", []orm.SelectionItem{
{Value: "bigger", Label: "Bigger than the reference Unit of Measure"},
{Value: "reference", Label: "Reference Unit of Measure for this category"},
{Value: "smaller", Label: "Smaller than the reference Unit of Measure"},
}, orm.FieldOpts{String: "Type", Required: true, Default: "reference"}),
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
orm.Float("rounding", orm.FieldOpts{
String: "Rounding Precision", Required: true, Default: 0.01,
Help: "The computed quantity will be a multiple of this value. Use 1.0 for a Unit of Measure that cannot be further split.",
}),
)
}
// initProductTemplate registers product.template — the base product definition.
// Mirrors: odoo/addons/product/models/product_template.py
func initProductTemplate() {
m := orm.NewModel("product.template", orm.ModelOpts{
Description: "Product Template",
Order: "name",
})
m.AddFields(
orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Index: true, Translate: true}),
orm.Selection("type", []orm.SelectionItem{
{Value: "consu", Label: "Consumable"},
{Value: "service", Label: "Service"},
{Value: "storable", Label: "Storable Product"},
}, 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,
}),
orm.Many2one("uom_id", "uom.uom", orm.FieldOpts{
String: "Unit of Measure", Required: true,
}),
orm.Boolean("sale_ok", orm.FieldOpts{String: "Can be Sold", Default: true}),
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.",
}),
orm.Many2many("supplier_taxes_id", "account.tax", orm.FieldOpts{
String: "Vendor Taxes",
Help: "Default taxes used when buying the product.",
}),
)
}
// 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",
Order: "default_code, name, id",
Inherits: map[string]string{"product.template": "product_tmpl_id"},
})
m.AddFields(
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",
}),
)
}
// initProductPricelist registers product.pricelist — price lists for customers.
// Mirrors: odoo/addons/product/models/product_pricelist.py
func initProductPricelist() {
m := orm.NewModel("product.pricelist", orm.ModelOpts{
Description: "Pricelist",
Order: "sequence, id",
})
m.AddFields(
orm.Char("name", orm.FieldOpts{String: "Pricelist Name", Required: true, Translate: true}),
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
orm.Many2one("currency_id", "res.currency", orm.FieldOpts{String: "Currency", Required: true}),
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 16}),
orm.One2many("item_ids", "product.pricelist.item", "pricelist_id", orm.FieldOpts{String: "Pricelist Rules"}),
)
// product.pricelist.item — Price rules
orm.NewModel("product.pricelist.item", orm.ModelOpts{
Description: "Pricelist Rule",
Order: "applied_on, min_quantity desc, categ_id desc, id desc",
}).AddFields(
orm.Many2one("pricelist_id", "product.pricelist", orm.FieldOpts{
String: "Pricelist", Required: true, OnDelete: orm.OnDeleteCascade, Index: true,
}),
orm.Many2one("product_tmpl_id", "product.template", orm.FieldOpts{String: "Product"}),
orm.Many2one("categ_id", "product.category", orm.FieldOpts{String: "Product Category"}),
orm.Float("min_quantity", orm.FieldOpts{String: "Min. Quantity"}),
orm.Selection("applied_on", []orm.SelectionItem{
{Value: "3_global", Label: "All Products"},
{Value: "2_product_category", Label: "Product Category"},
{Value: "1_product", Label: "Product"},
{Value: "0_product_variant", Label: "Product Variant"},
}, orm.FieldOpts{String: "Apply On", Default: "3_global", Required: true}),
orm.Selection("compute_price", []orm.SelectionItem{
{Value: "fixed", Label: "Fixed Price"},
{Value: "percentage", Label: "Discount"},
{Value: "formula", Label: "Formula"},
}, orm.FieldOpts{String: "Computation", Default: "fixed", Required: true}),
orm.Float("fixed_price", orm.FieldOpts{String: "Fixed Price"}),
orm.Float("percent_price", orm.FieldOpts{String: "Percentage Price"}),
orm.Date("date_start", orm.FieldOpts{String: "Start Date"}),
orm.Date("date_end", orm.FieldOpts{String: "End Date"}),
)
}