Files
goodie/addons/account/models/account_account.go
Marc 66383adf06 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>
2026-04-12 18:41:57 +02:00

302 lines
13 KiB
Go

package models
import (
"fmt"
"odoo-go/pkg/orm"
)
// initAccountAccount registers the chart of accounts.
// Mirrors: odoo/addons/account/models/account_account.py
func initAccountAccount() {
// account.account — Chart of Accounts
m := orm.NewModel("account.account", orm.ModelOpts{
Description: "Account",
Order: "code, company_id",
RecName: "name",
})
m.AddFields(
orm.Char("name", orm.FieldOpts{String: "Account Name", Required: true, Index: true, Translate: true}),
orm.Char("code", orm.FieldOpts{String: "Code", Required: true, Size: 64, Index: true}),
orm.Selection("account_type", []orm.SelectionItem{
// Balance Sheet
{Value: "asset_receivable", Label: "Receivable"},
{Value: "asset_cash", Label: "Bank and Cash"},
{Value: "asset_current", Label: "Current Assets"},
{Value: "asset_non_current", Label: "Non-current Assets"},
{Value: "asset_prepayments", Label: "Prepayments"},
{Value: "asset_fixed", Label: "Fixed Assets"},
{Value: "liability_payable", Label: "Payable"},
{Value: "liability_credit_card", Label: "Credit Card"},
{Value: "liability_current", Label: "Current Liabilities"},
{Value: "liability_non_current", Label: "Non-current Liabilities"},
{Value: "equity", Label: "Equity"},
{Value: "equity_unaffected", Label: "Current Year Earnings"},
// P&L
{Value: "income", Label: "Income"},
{Value: "income_other", Label: "Other Income"},
{Value: "expense", Label: "Expenses"},
{Value: "expense_depreciation", Label: "Depreciation"},
{Value: "expense_direct_cost", Label: "Cost of Revenue"},
// Special
{Value: "off_balance", Label: "Off-Balance Sheet"},
}, orm.FieldOpts{String: "Type", Required: true}),
orm.Boolean("reconcile", orm.FieldOpts{String: "Allow Reconciliation"}),
orm.Boolean("non_trade", orm.FieldOpts{String: "Non-Trade Receivable/Payable"}),
orm.Many2one("company_id", "res.company", orm.FieldOpts{
String: "Company", Required: true, Index: true,
}),
orm.Many2one("currency_id", "res.currency", orm.FieldOpts{String: "Account Currency"}),
orm.Many2many("tax_ids", "account.tax", orm.FieldOpts{String: "Default Taxes"}),
orm.Many2many("tag_ids", "account.account.tag", orm.FieldOpts{String: "Tags"}),
orm.Text("note", orm.FieldOpts{String: "Internal Notes"}),
orm.Boolean("deprecated", orm.FieldOpts{String: "Deprecated"}),
orm.Boolean("include_initial_balance", orm.FieldOpts{String: "Include Initial Balance"}),
)
// account.account.tag — Account tags for reporting
orm.NewModel("account.account.tag", orm.ModelOpts{
Description: "Account Tag",
Order: "name",
}).AddFields(
orm.Char("name", orm.FieldOpts{String: "Tag Name", Required: true, Translate: true}),
orm.Selection("applicability", []orm.SelectionItem{
{Value: "accounts", Label: "Accounts"},
{Value: "taxes", Label: "Taxes"},
{Value: "products", Label: "Products"},
}, orm.FieldOpts{String: "Applicability", Default: "accounts", Required: true}),
orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
orm.Char("color", orm.FieldOpts{String: "Color Index"}),
)
// account.group — Account groups for hierarchy
orm.NewModel("account.group", orm.ModelOpts{
Description: "Account Group",
Order: "code_prefix_start",
}).AddFields(
orm.Char("name", orm.FieldOpts{String: "Account Group", Required: true, Translate: true}),
orm.Char("code_prefix_start", orm.FieldOpts{String: "Code Prefix Start"}),
orm.Char("code_prefix_end", orm.FieldOpts{String: "Code Prefix End"}),
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company", Required: true}),
)
}
// initAccountTax registers tax models.
// Mirrors: odoo/addons/account/models/account_tax.py
func initAccountTax() {
m := orm.NewModel("account.tax", orm.ModelOpts{
Description: "Tax",
Order: "sequence, id",
})
m.AddFields(
orm.Char("name", orm.FieldOpts{String: "Tax Name", Required: true, Translate: true}),
orm.Selection("type_tax_use", []orm.SelectionItem{
{Value: "sale", Label: "Sales"},
{Value: "purchase", Label: "Purchases"},
{Value: "none", Label: "None"},
}, orm.FieldOpts{String: "Tax Type", Required: true, Default: "sale"}),
orm.Selection("amount_type", []orm.SelectionItem{
{Value: "group", Label: "Group of Taxes"},
{Value: "fixed", Label: "Fixed"},
{Value: "percent", Label: "Percentage of Price"},
{Value: "division", Label: "Percentage of Price Tax Included"},
}, orm.FieldOpts{String: "Tax Computation", Required: true, Default: "percent"}),
orm.Float("amount", orm.FieldOpts{String: "Amount", Required: true}),
orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 1}),
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company", Required: true}),
orm.Boolean("price_include", orm.FieldOpts{String: "Included in Price"}),
orm.Boolean("include_base_amount", orm.FieldOpts{String: "Affect Base of Subsequent Taxes"}),
orm.Boolean("is_base_affected", orm.FieldOpts{String: "Base Affected by Previous Taxes", Default: true}),
orm.Many2many("children_tax_ids", "account.tax", orm.FieldOpts{String: "Children Taxes"}),
orm.Many2one("parent_tax_id", "account.tax", orm.FieldOpts{String: "Parent Tax Group"}),
orm.Char("description", orm.FieldOpts{String: "Label on Invoices"}),
orm.Many2one("tax_group_id", "account.tax.group", orm.FieldOpts{String: "Tax Group"}),
orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
// Repartition lines define how tax amounts are distributed to accounts
orm.One2many("invoice_repartition_line_ids", "account.tax.repartition.line", "tax_id", orm.FieldOpts{String: "Invoice Repartition"}),
orm.One2many("refund_repartition_line_ids", "account.tax.repartition.line", "tax_id", orm.FieldOpts{String: "Refund Repartition"}),
)
// account.tax.group — Tax group for reports
orm.NewModel("account.tax.group", orm.ModelOpts{
Description: "Tax Group",
Order: "sequence, name",
}).AddFields(
orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}),
orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 10}),
orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
)
// account.tax.repartition.line — How tax is split across accounts
// Mirrors: odoo/addons/account/models/account_tax.py AccountTaxRepartitionLine
orm.NewModel("account.tax.repartition.line", orm.ModelOpts{
Description: "Tax Repartition Line",
Order: "sequence, id",
}).AddFields(
orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 1}),
orm.Float("factor_percent", orm.FieldOpts{String: "Percentage", Default: 100, Required: true}),
orm.Selection("repartition_type", []orm.SelectionItem{
{Value: "base", Label: "Base"},
{Value: "tax", Label: "Tax"},
}, orm.FieldOpts{String: "Based On", Default: "tax", Required: true}),
orm.Many2one("account_id", "account.account", orm.FieldOpts{String: "Account"}),
orm.Many2one("tax_id", "account.tax", orm.FieldOpts{String: "Tax", OnDelete: orm.OnDeleteCascade}),
orm.Many2many("tag_ids", "account.account.tag", orm.FieldOpts{String: "Tax Grids"}),
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
orm.Selection("document_type", []orm.SelectionItem{
{Value: "invoice", Label: "Invoice"},
{Value: "refund", Label: "Refund"},
}, orm.FieldOpts{String: "Document Type"}),
orm.Boolean("use_in_tax_closing", orm.FieldOpts{String: "Tax Closing Entry", Default: true}),
)
// account.fiscal.position — Tax mapping per country/partner
// Mirrors: odoo/addons/account/models/account_fiscal_position.py
initAccountFiscalPosition()
}
func initAccountFiscalPosition() {
m := orm.NewModel("account.fiscal.position", orm.ModelOpts{
Description: "Fiscal Position",
Order: "sequence",
})
m.AddFields(
orm.Char("name", orm.FieldOpts{String: "Fiscal Position", Required: true, Translate: true}),
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company", Required: true}),
orm.One2many("tax_ids", "account.fiscal.position.tax", "position_id", orm.FieldOpts{String: "Tax Mapping"}),
orm.One2many("account_ids", "account.fiscal.position.account", "position_id", orm.FieldOpts{String: "Account Mapping"}),
orm.Integer("sequence", orm.FieldOpts{String: "Sequence"}),
orm.Boolean("auto_apply", orm.FieldOpts{String: "Detect Automatically"}),
orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
orm.Many2one("country_group_id", "res.country.group", orm.FieldOpts{String: "Country Group"}),
orm.Boolean("vat_required", orm.FieldOpts{String: "VAT required"}),
orm.Text("note", orm.FieldOpts{String: "Notes", Translate: true}),
)
// Fiscal position tax mapping
orm.NewModel("account.fiscal.position.tax", orm.ModelOpts{
Description: "Tax Mapping",
}).AddFields(
orm.Many2one("position_id", "account.fiscal.position", orm.FieldOpts{String: "Fiscal Position", Required: true, OnDelete: orm.OnDeleteCascade}),
orm.Many2one("tax_src_id", "account.tax", orm.FieldOpts{String: "Tax on Product", Required: true}),
orm.Many2one("tax_dest_id", "account.tax", orm.FieldOpts{String: "Tax to Apply"}),
)
// Fiscal position account mapping
orm.NewModel("account.fiscal.position.account", orm.ModelOpts{
Description: "Account Mapping",
}).AddFields(
orm.Many2one("position_id", "account.fiscal.position", orm.FieldOpts{String: "Fiscal Position", Required: true, OnDelete: orm.OnDeleteCascade}),
orm.Many2one("account_src_id", "account.account", orm.FieldOpts{String: "Account on Product", Required: true}),
orm.Many2one("account_dest_id", "account.account", orm.FieldOpts{String: "Account to Use", Required: true}),
)
// res.country.group — for fiscal position country grouping
orm.NewModel("res.country.group", orm.ModelOpts{
Description: "Country Group",
Order: "name",
}).AddFields(
orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}),
orm.Many2many("country_ids", "res.country", orm.FieldOpts{String: "Countries"}),
)
}
// initAccountTaxComputes adds computed fields to account.tax for the tax computation engine.
// Mirrors: odoo/addons/account/models/account_tax.py
//
// - is_base_affected: whether this tax's base is affected by previous taxes in the sequence
// - repartition_line_ids: combined view of invoice + refund repartition lines
func initAccountTaxComputes() {
ext := orm.ExtendModel("account.tax")
ext.AddFields(
orm.Boolean("computed_is_base_affected", orm.FieldOpts{
String: "Base Affected (Computed)",
Compute: "_compute_is_base_affected",
Help: "Computed: true when include_base_amount is set on a preceding tax in the same group",
}),
orm.Char("repartition_line_ids_json", orm.FieldOpts{
String: "Repartition Lines (All)",
Compute: "_compute_repartition_line_ids",
Help: "JSON list of all repartition line IDs (invoice + refund) for the tax engine",
}),
)
// _compute_is_base_affected: determines if this tax's base amount should be
// influenced by preceding taxes in the same tax group.
// Mirrors: odoo/addons/account/models/account_tax.py _compute_is_base_affected()
//
// A tax is base-affected when:
// 1. It belongs to a group tax (has parent_tax_id), AND
// 2. Any sibling tax with a lower sequence has include_base_amount=true
// Otherwise it falls back to the manual is_base_affected field value.
ext.RegisterCompute("computed_is_base_affected", func(rs *orm.Recordset) (orm.Values, error) {
env := rs.Env()
taxID := rs.IDs()[0]
var parentID *int64
var seq int64
var manualFlag bool
env.Tx().QueryRow(env.Ctx(),
`SELECT parent_tax_id, COALESCE(sequence, 0), COALESCE(is_base_affected, true)
FROM account_tax WHERE id = $1`, taxID,
).Scan(&parentID, &seq, &manualFlag)
// If no parent group, use the manual field value
if parentID == nil || *parentID == 0 {
return orm.Values{"computed_is_base_affected": manualFlag}, nil
}
// Check if any preceding sibling in the group has include_base_amount=true
var count int
env.Tx().QueryRow(env.Ctx(),
`SELECT COUNT(*) FROM account_tax
WHERE parent_tax_id = $1 AND sequence < $2
AND include_base_amount = true AND id != $3`,
*parentID, seq, taxID,
).Scan(&count)
return orm.Values{"computed_is_base_affected": count > 0 || manualFlag}, nil
})
// _compute_repartition_line_ids: collects all repartition line IDs (invoice + refund)
// into a JSON array string for the tax computation engine.
// Mirrors: odoo/addons/account/models/account_tax.py _compute_repartition_line_ids()
ext.RegisterCompute("repartition_line_ids_json", func(rs *orm.Recordset) (orm.Values, error) {
env := rs.Env()
taxID := rs.IDs()[0]
rows, err := env.Tx().Query(env.Ctx(),
`SELECT id FROM account_tax_repartition_line
WHERE tax_id = $1 ORDER BY sequence, id`, taxID)
if err != nil {
return orm.Values{"repartition_line_ids_json": "[]"}, nil
}
defer rows.Close()
result := "["
first := true
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
continue
}
if !first {
result += ","
}
result += fmt.Sprintf("%d", id)
first = false
}
result += "]"
return orm.Values{"repartition_line_ids_json": result}, nil
})
}