Deep dive: Account, Stock, Sale, Purchase — +800 LOC business logic
Account: - Multi-currency: company_currency_id, amount_total_signed - Lock dates on res.company (period, fiscal year, tax) + enforcement in action_post - Recurring entries: account.move.recurring with action_generate (copy+advance) - Tax groups: amount_type='group' computes child taxes with include_base_amount - ComputeTaxes batch function, findTaxAccount helper Stock: - Lot/Serial tracking: enhanced stock.lot with expiration dates + qty compute - Routes: stock.route model with product/category/warehouse selectable flags - Rules: stock.rule model with pull/push/buy/manufacture actions + procure methods - Returns: action_return on picking (swap locations, copy moves) - Product tracking extension (none/lot/serial) + route_ids M2M Sale: - Pricelist: get_product_price with fixed/percentage/formula computation - Margin: purchase_price, margin, margin_percent on line + order totals - Down payments: action_create_down_payment (deposit invoice at X%) Purchase: - 3-way matching: action_create_bill now updates qty_invoiced on PO lines - Purchase agreements: purchase.requisition + line with state machine Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -109,6 +109,7 @@ func initAccountTax() {
|
||||
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"}),
|
||||
|
||||
27
addons/account/models/account_company.go
Normal file
27
addons/account/models/account_company.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
|
||||
// initAccountCompanyExtension extends res.company with accounting lock date fields.
|
||||
// Mirrors: odoo/addons/account/models/company.py ResCompany (extends res.company)
|
||||
//
|
||||
// In Python Odoo:
|
||||
//
|
||||
// class ResCompany(models.Model):
|
||||
// _inherit = 'res.company'
|
||||
// period_lock_date = fields.Date(...)
|
||||
// fiscalyear_lock_date = fields.Date(...)
|
||||
// tax_lock_date = fields.Date(...)
|
||||
//
|
||||
// Lock dates prevent posting journal entries before a certain date:
|
||||
// - period_lock_date: blocks non-adviser users
|
||||
// - fiscalyear_lock_date: blocks all users
|
||||
// - tax_lock_date: blocks tax-related entries
|
||||
func initAccountCompanyExtension() {
|
||||
c := orm.ExtendModel("res.company")
|
||||
c.AddFields(
|
||||
orm.Date("period_lock_date", orm.FieldOpts{String: "Lock Date for Non-Advisers"}),
|
||||
orm.Date("fiscalyear_lock_date", orm.FieldOpts{String: "Lock Date for All Users"}),
|
||||
orm.Date("tax_lock_date", orm.FieldOpts{String: "Tax Lock Date"}),
|
||||
)
|
||||
}
|
||||
@@ -126,6 +126,13 @@ func initAccountMove() {
|
||||
}),
|
||||
)
|
||||
|
||||
// -- Multi-Currency --
|
||||
m.AddFields(
|
||||
orm.Many2one("company_currency_id", "res.currency", orm.FieldOpts{
|
||||
String: "Company Currency", Related: "company_id.currency_id",
|
||||
}),
|
||||
)
|
||||
|
||||
// -- Amounts (Computed) --
|
||||
m.AddFields(
|
||||
orm.Monetary("amount_untaxed", orm.FieldOpts{String: "Untaxed Amount", Compute: "_compute_amount", Store: true, CurrencyField: "currency_id"}),
|
||||
@@ -133,6 +140,7 @@ func initAccountMove() {
|
||||
orm.Monetary("amount_total", orm.FieldOpts{String: "Total", Compute: "_compute_amount", Store: true, CurrencyField: "currency_id"}),
|
||||
orm.Monetary("amount_residual", orm.FieldOpts{String: "Amount Due", Compute: "_compute_amount", Store: true, CurrencyField: "currency_id"}),
|
||||
orm.Monetary("amount_total_in_currency_signed", orm.FieldOpts{String: "Total in Currency Signed", Compute: "_compute_amount", Store: true}),
|
||||
orm.Monetary("amount_total_signed", orm.FieldOpts{String: "Total Signed", Compute: "_compute_amount", Store: true, CurrencyField: "company_currency_id"}),
|
||||
)
|
||||
|
||||
// -- Invoice specific --
|
||||
@@ -176,11 +184,24 @@ func initAccountMove() {
|
||||
|
||||
total := untaxed + tax
|
||||
|
||||
// amount_total_signed: total in company currency (sign depends on move type)
|
||||
// For customer invoices/receipts the sign is positive, for credit notes negative.
|
||||
var moveType string
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(move_type, 'entry') FROM account_move WHERE id = $1`, moveID,
|
||||
).Scan(&moveType)
|
||||
|
||||
sign := 1.0
|
||||
if moveType == "out_refund" || moveType == "in_refund" {
|
||||
sign = -1.0
|
||||
}
|
||||
|
||||
return orm.Values{
|
||||
"amount_untaxed": untaxed,
|
||||
"amount_tax": tax,
|
||||
"amount_total": total,
|
||||
"amount_residual": total, // Simplified: residual = total until payments
|
||||
"amount_untaxed": untaxed,
|
||||
"amount_tax": tax,
|
||||
"amount_total": total,
|
||||
"amount_residual": total, // Simplified: residual = total until payments
|
||||
"amount_total_signed": total * sign,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -188,6 +209,7 @@ func initAccountMove() {
|
||||
m.RegisterCompute("amount_tax", computeAmount)
|
||||
m.RegisterCompute("amount_total", computeAmount)
|
||||
m.RegisterCompute("amount_residual", computeAmount)
|
||||
m.RegisterCompute("amount_total_signed", computeAmount)
|
||||
|
||||
// -- Business Methods: State Transitions --
|
||||
// Mirrors: odoo/addons/account/models/account_move.py action_post(), button_cancel()
|
||||
@@ -206,6 +228,24 @@ func initAccountMove() {
|
||||
return nil, fmt.Errorf("account: can only post draft entries (current: %s)", state)
|
||||
}
|
||||
|
||||
// Check lock dates
|
||||
// Mirrors: odoo/addons/account/models/account_move.py _check_fiscalyear_lock_date()
|
||||
var moveDate, periodLock, fiscalLock, taxLock *string
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT m.date::text, c.period_lock_date::text, c.fiscalyear_lock_date::text, c.tax_lock_date::text
|
||||
FROM account_move m JOIN res_company c ON c.id = m.company_id WHERE m.id = $1`, id,
|
||||
).Scan(&moveDate, &periodLock, &fiscalLock, &taxLock)
|
||||
|
||||
if fiscalLock != nil && moveDate != nil && *moveDate <= *fiscalLock {
|
||||
return nil, fmt.Errorf("account: cannot post entry dated %s, fiscal year is locked until %s", *moveDate, *fiscalLock)
|
||||
}
|
||||
if periodLock != nil && moveDate != nil && *moveDate <= *periodLock {
|
||||
return nil, fmt.Errorf("account: cannot post entry dated %s, period is locked until %s", *moveDate, *periodLock)
|
||||
}
|
||||
if taxLock != nil && moveDate != nil && *moveDate <= *taxLock {
|
||||
return nil, fmt.Errorf("account: cannot post entry dated %s, tax return is locked until %s", *moveDate, *taxLock)
|
||||
}
|
||||
|
||||
// Check partner is set for invoice types
|
||||
var moveType string
|
||||
env.Tx().QueryRow(env.Ctx(), `SELECT move_type FROM account_move WHERE id = $1`, id).Scan(&moveType)
|
||||
|
||||
116
addons/account/models/account_recurring.go
Normal file
116
addons/account/models/account_recurring.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
|
||||
// initAccountRecurring registers account.move.recurring — recurring entry templates.
|
||||
// Mirrors: odoo/addons/account/models/account_move.py (recurring entries feature)
|
||||
//
|
||||
// Allows defining templates that automatically generate journal entries
|
||||
// on a schedule (daily, weekly, monthly, quarterly, yearly).
|
||||
func initAccountRecurring() {
|
||||
m := orm.NewModel("account.move.recurring", orm.ModelOpts{
|
||||
Description: "Recurring Entry",
|
||||
Order: "name",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Char("name", orm.FieldOpts{String: "Name", Required: true}),
|
||||
orm.Many2one("journal_id", "account.journal", orm.FieldOpts{String: "Journal", Required: true}),
|
||||
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
|
||||
orm.Selection("period", []orm.SelectionItem{
|
||||
{Value: "daily", Label: "Daily"},
|
||||
{Value: "weekly", Label: "Weekly"},
|
||||
{Value: "monthly", Label: "Monthly"},
|
||||
{Value: "quarterly", Label: "Quarterly"},
|
||||
{Value: "yearly", Label: "Yearly"},
|
||||
}, orm.FieldOpts{String: "Period", Required: true, Default: "monthly"}),
|
||||
orm.Date("date_next", orm.FieldOpts{String: "Next Date", Required: true}),
|
||||
orm.Date("date_end", orm.FieldOpts{String: "End Date"}),
|
||||
orm.Many2one("template_move_id", "account.move", orm.FieldOpts{String: "Template Entry"}),
|
||||
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
|
||||
orm.Selection("state", []orm.SelectionItem{
|
||||
{Value: "draft", Label: "Draft"},
|
||||
{Value: "running", Label: "Running"},
|
||||
{Value: "done", Label: "Done"},
|
||||
}, orm.FieldOpts{String: "Status", Default: "draft"}),
|
||||
)
|
||||
|
||||
// action_start: draft -> running
|
||||
m.RegisterMethod("action_start", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE account_move_recurring SET state = 'running' WHERE id = $1 AND state = 'draft'`, id)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_done: running -> done
|
||||
m.RegisterMethod("action_done", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE account_move_recurring SET state = 'done' WHERE id = $1`, id)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_generate: create journal entries from the template and advance next date.
|
||||
// Mirrors: odoo/addons/account/models/account_move.py _cron_recurring_entries()
|
||||
m.RegisterMethod("action_generate", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, recID := range rs.IDs() {
|
||||
var templateID *int64
|
||||
var dateNext, period string
|
||||
var dateEnd *string
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT template_move_id, date_next::text, period, date_end::text
|
||||
FROM account_move_recurring WHERE id = $1 AND state = 'running'`, recID,
|
||||
).Scan(&templateID, &dateNext, &period, &dateEnd)
|
||||
if err != nil {
|
||||
continue // not running or not found
|
||||
}
|
||||
|
||||
if templateID == nil || *templateID == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if past end date
|
||||
if dateEnd != nil && dateNext > *dateEnd {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE account_move_recurring SET state = 'done' WHERE id = $1`, recID)
|
||||
continue
|
||||
}
|
||||
|
||||
// Copy the template move with the next date
|
||||
templateRS := env.Model("account.move").Browse(*templateID)
|
||||
newMove, err := templateRS.Copy(orm.Values{"date": dateNext})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
_ = newMove
|
||||
|
||||
// Advance next date based on period
|
||||
var interval string
|
||||
switch period {
|
||||
case "daily":
|
||||
interval = "1 day"
|
||||
case "weekly":
|
||||
interval = "7 days"
|
||||
case "monthly":
|
||||
interval = "1 month"
|
||||
case "quarterly":
|
||||
interval = "3 months"
|
||||
case "yearly":
|
||||
interval = "1 year"
|
||||
default:
|
||||
interval = "1 month"
|
||||
}
|
||||
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE account_move_recurring SET date_next = date_next + $1::interval WHERE id = $2`,
|
||||
interval, recID)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// TaxResult holds the result of computing a tax on an amount.
|
||||
type TaxResult struct {
|
||||
@@ -13,6 +17,9 @@ type TaxResult struct {
|
||||
|
||||
// ComputeTax calculates tax for a given base amount.
|
||||
// Mirrors: odoo/addons/account/models/account_tax.py AccountTax._compute_amount()
|
||||
//
|
||||
// Supports amount_type: percent, fixed, division, group.
|
||||
// For group taxes, iterates children in sequence order and sums their results.
|
||||
func ComputeTax(env *orm.Environment, taxID int64, baseAmount float64) (*TaxResult, error) {
|
||||
var name string
|
||||
var amount float64
|
||||
@@ -27,6 +34,12 @@ func ComputeTax(env *orm.Environment, taxID int64, baseAmount float64) (*TaxResu
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Handle group taxes: iterate child taxes and sum their amounts.
|
||||
// Mirrors: odoo/addons/account/models/account_tax.py AccountTax._compute_amount() group branch
|
||||
if amountType == "group" {
|
||||
return computeGroupTax(env, taxID, name, baseAmount)
|
||||
}
|
||||
|
||||
var taxAmount float64
|
||||
switch amountType {
|
||||
case "percent":
|
||||
@@ -45,7 +58,99 @@ func ComputeTax(env *orm.Environment, taxID int64, baseAmount float64) (*TaxResu
|
||||
}
|
||||
}
|
||||
|
||||
// Find the tax account (from repartition lines)
|
||||
accountID := findTaxAccount(env, taxID)
|
||||
|
||||
return &TaxResult{
|
||||
TaxID: taxID,
|
||||
TaxName: name,
|
||||
Amount: taxAmount,
|
||||
Base: baseAmount,
|
||||
AccountID: accountID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ComputeTaxes calculates multiple taxes on a base amount, returning all results.
|
||||
// Mirrors: odoo/addons/account/models/account_tax.py AccountTax.compute_all()
|
||||
func ComputeTaxes(env *orm.Environment, taxIDs []int64, baseAmount float64) ([]*TaxResult, error) {
|
||||
var results []*TaxResult
|
||||
for _, taxID := range taxIDs {
|
||||
result, err := ComputeTax(env, taxID, baseAmount)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("account: compute tax %d: %w", taxID, err)
|
||||
}
|
||||
results = append(results, result)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// computeGroupTax handles amount_type='group': reads child taxes and sums their amounts.
|
||||
// Mirrors: odoo/addons/account/models/account_tax.py AccountTax._compute_amount() for groups
|
||||
func computeGroupTax(env *orm.Environment, parentID int64, parentName string, baseAmount float64) (*TaxResult, error) {
|
||||
// Read child taxes via the many2many relation table
|
||||
// The ORM creates: account_tax_children_tax_ids_rel (or similar)
|
||||
// We query the children_tax_ids M2M relationship
|
||||
childRows, err := env.Tx().Query(env.Ctx(),
|
||||
`SELECT ct.id FROM account_tax ct
|
||||
JOIN account_tax_account_tax_children_tax_ids_rel rel ON rel.account_tax_id2 = ct.id
|
||||
WHERE rel.account_tax_id1 = $1
|
||||
ORDER BY ct.sequence, ct.id`, parentID)
|
||||
if err != nil {
|
||||
// Fallback: try with parent_tax_id if the M2M table doesn't exist
|
||||
childRows, err = env.Tx().Query(env.Ctx(),
|
||||
`SELECT id FROM account_tax WHERE parent_tax_id = $1 ORDER BY sequence, id`, parentID)
|
||||
if err != nil {
|
||||
return &TaxResult{
|
||||
TaxID: parentID, TaxName: parentName, Amount: 0, Base: baseAmount,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
defer childRows.Close()
|
||||
|
||||
var totalTax float64
|
||||
var lastAccountID int64
|
||||
currentBase := baseAmount
|
||||
|
||||
for childRows.Next() {
|
||||
var childID int64
|
||||
if err := childRows.Scan(&childID); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
childResult, err := ComputeTax(env, childID, currentBase)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
totalTax += childResult.Amount
|
||||
|
||||
if childResult.AccountID > 0 {
|
||||
lastAccountID = childResult.AccountID
|
||||
}
|
||||
|
||||
// Check if this child tax affects the base of subsequent taxes
|
||||
var includeBase bool
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(include_base_amount, false) FROM account_tax WHERE id = $1`, childID,
|
||||
).Scan(&includeBase)
|
||||
if includeBase {
|
||||
currentBase += childResult.Amount
|
||||
}
|
||||
}
|
||||
|
||||
if lastAccountID == 0 {
|
||||
lastAccountID = findTaxAccount(env, parentID)
|
||||
}
|
||||
|
||||
return &TaxResult{
|
||||
TaxID: parentID,
|
||||
TaxName: parentName,
|
||||
Amount: totalTax,
|
||||
Base: baseAmount,
|
||||
AccountID: lastAccountID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// findTaxAccount looks up the account for a tax from its repartition lines.
|
||||
func findTaxAccount(env *orm.Environment, taxID int64) int64 {
|
||||
var accountID int64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(account_id, 0) FROM account_tax_repartition_line
|
||||
@@ -60,11 +165,5 @@ func ComputeTax(env *orm.Environment, taxID int64, baseAmount float64) (*TaxResu
|
||||
).Scan(&accountID)
|
||||
}
|
||||
|
||||
return &TaxResult{
|
||||
TaxID: taxID,
|
||||
TaxName: name,
|
||||
Amount: taxAmount,
|
||||
Base: baseAmount,
|
||||
AccountID: accountID,
|
||||
}, nil
|
||||
return accountID
|
||||
}
|
||||
|
||||
@@ -15,4 +15,6 @@ func Init() {
|
||||
initAccountTaxReport()
|
||||
initAccountReportLine()
|
||||
initAccountAnalytic()
|
||||
initAccountRecurring()
|
||||
initAccountCompanyExtension()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user