Implement core business logic depth: reconciliation, quants, invoicing

Account Reconciliation Engine:
- reconcile() method on account.move.line matches debit↔credit lines
- Creates account.partial.reconcile records for each match
- Detects full reconciliation (all residuals=0) → account.full.reconcile
- updatePaymentState() tracks paid/partial/not_paid on invoices
- Payment register wizard now creates journal entries + reconciles

Stock Quant Reservation:
- assignMove() reserves products from source location quants
- getAvailableQty() queries unreserved on-hand stock
- _action_confirm → confirmed + auto-assigns if stock available
- _action_assign creates stock.move.line reservations
- _action_done updates quants (decrease source, increase dest)
- button_validate on picking delegates to move._action_done
- Clears reserved_quantity on completion

Sale Invoice Creation (rewritten):
- Proper debit/credit/balance on invoice lines
- Tax computation from SO line M2M tax_ids (percent/fixed/division)
- Revenue account lookup (SKR03 8300 with fallbacks)
- amount_residual set on receivable line (enables reconciliation)
- qty_invoiced tracking on SO lines
- Line amount computes now include tax in price_total

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc
2026-04-02 23:25:32 +02:00
parent 9ad633fc3c
commit 5d48737c9d
3 changed files with 921 additions and 171 deletions

View File

@@ -397,18 +397,19 @@ func initAccountMove() {
// Step 3: Create the receivable line (debit = total of all credits) // Step 3: Create the receivable line (debit = total of all credits)
receivableVals := orm.Values{ receivableVals := orm.Values{
"move_id": moveID, "move_id": moveID,
"name": "/", "name": "/",
"quantity": 1.0, "quantity": 1.0,
"account_id": receivableAccountID, "account_id": receivableAccountID,
"company_id": companyID, "company_id": companyID,
"journal_id": journalID, "journal_id": journalID,
"currency_id": currencyID, "currency_id": currencyID,
"partner_id": partnerID, "partner_id": partnerID,
"display_type": "payment_term", "display_type": "payment_term",
"debit": totalDebit, "debit": totalDebit,
"credit": 0.0, "credit": 0.0,
"balance": totalDebit, "balance": totalDebit,
"amount_residual": totalDebit,
} }
if _, err := lineRS.Create(receivableVals); err != nil { if _, err := lineRS.Create(receivableVals); err != nil {
return nil, fmt.Errorf("account: create receivable line: %w", err) return nil, fmt.Errorf("account: create receivable line: %w", err)
@@ -567,11 +568,115 @@ func initAccountMove() {
return nil, fmt.Errorf("account: create payment for invoice %d: %w", moveID, err) return nil, fmt.Errorf("account: create payment for invoice %d: %w", moveID, err)
} }
// Update invoice payment state // Create journal entry lines on the payment move for reconciliation:
_, err = env.Tx().Exec(env.Ctx(), // - Debit line on bank account (asset_cash)
`UPDATE account_move SET payment_state = 'paid' WHERE id = $1`, moveID) // - Credit line on receivable/payable account (mirrors invoice's payment_term line)
if err != nil {
return nil, fmt.Errorf("account: update payment state for invoice %d: %w", moveID, err) // Find bank account for the journal
var bankAccountID int64
env.Tx().QueryRow(env.Ctx(),
`SELECT COALESCE(default_account_id, 0) FROM account_journal WHERE id = $1`,
bankJournalID).Scan(&bankAccountID)
if bankAccountID == 0 {
// Fallback: find any cash account
env.Tx().QueryRow(env.Ctx(),
`SELECT id FROM account_account WHERE account_type = 'asset_cash' AND company_id = $1 ORDER BY code LIMIT 1`,
companyID).Scan(&bankAccountID)
}
// Find receivable/payable account from the invoice's payment_term line
var invoiceReceivableAccountID int64
env.Tx().QueryRow(env.Ctx(),
`SELECT account_id FROM account_move_line
WHERE move_id = $1 AND display_type = 'payment_term'
LIMIT 1`, moveID).Scan(&invoiceReceivableAccountID)
if invoiceReceivableAccountID == 0 {
accountType := "asset_receivable"
if moveType == "in_invoice" || moveType == "in_refund" {
accountType = "liability_payable"
}
env.Tx().QueryRow(env.Ctx(),
`SELECT id FROM account_account WHERE account_type = $1 AND company_id = $2 ORDER BY code LIMIT 1`,
accountType, companyID).Scan(&invoiceReceivableAccountID)
}
if bankAccountID > 0 && invoiceReceivableAccountID > 0 {
// Bank line (debit for inbound, credit for outbound)
var bankDebit, bankCredit float64
if paymentType == "inbound" {
bankDebit = amountTotal
} else {
bankCredit = amountTotal
}
_, err = env.Tx().Exec(env.Ctx(),
`INSERT INTO account_move_line
(move_id, name, account_id, partner_id, company_id, journal_id, currency_id,
debit, credit, balance, amount_residual, display_type)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, 0, 'product')`,
payMoveID, fmt.Sprintf("PAY/%d", moveID), bankAccountID, partnerID,
companyID, bankJournalID, currencyID,
bankDebit, bankCredit, bankDebit-bankCredit)
if err != nil {
return nil, fmt.Errorf("account: create bank line for payment %d: %w", moveID, err)
}
// Counterpart line on receivable/payable (opposite of bank line)
var cpDebit, cpCredit float64
var cpResidual float64
if paymentType == "inbound" {
cpCredit = amountTotal
cpResidual = -amountTotal // Negative residual for credit line
} else {
cpDebit = amountTotal
cpResidual = amountTotal
}
var paymentLineID int64
err = env.Tx().QueryRow(env.Ctx(),
`INSERT INTO account_move_line
(move_id, name, account_id, partner_id, company_id, journal_id, currency_id,
debit, credit, balance, amount_residual, display_type)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, 'payment_term')
RETURNING id`,
payMoveID, fmt.Sprintf("PAY/%d", moveID), invoiceReceivableAccountID, partnerID,
companyID, bankJournalID, currencyID,
cpDebit, cpCredit, cpDebit-cpCredit, cpResidual).Scan(&paymentLineID)
if err != nil {
return nil, fmt.Errorf("account: create counterpart line for payment %d: %w", moveID, err)
}
// Find the invoice's receivable/payable line and reconcile
var invoiceLineID int64
env.Tx().QueryRow(env.Ctx(),
`SELECT id FROM account_move_line
WHERE move_id = $1 AND display_type = 'payment_term'
ORDER BY id LIMIT 1`, moveID).Scan(&invoiceLineID)
if invoiceLineID > 0 && paymentLineID > 0 {
lineModel := orm.Registry.Get("account.move.line")
if lineModel != nil {
if reconcileMethod, ok := lineModel.Methods["reconcile"]; ok {
lineRS := env.Model("account.move.line").Browse(invoiceLineID, paymentLineID)
if _, err := reconcileMethod(lineRS); err != nil {
// Non-fatal: fall back to direct update
env.Tx().Exec(env.Ctx(),
`UPDATE account_move SET payment_state = 'paid' WHERE id = $1`, moveID)
}
} else {
env.Tx().Exec(env.Ctx(),
`UPDATE account_move SET payment_state = 'paid' WHERE id = $1`, moveID)
}
} else {
env.Tx().Exec(env.Ctx(),
`UPDATE account_move SET payment_state = 'paid' WHERE id = $1`, moveID)
}
} else {
env.Tx().Exec(env.Ctx(),
`UPDATE account_move SET payment_state = 'paid' WHERE id = $1`, moveID)
}
} else {
// Fallback: direct payment state update (no reconciliation possible)
env.Tx().Exec(env.Ctx(),
`UPDATE account_move SET payment_state = 'paid' WHERE id = $1`, moveID)
} }
} }
return true, nil return true, nil
@@ -684,6 +789,142 @@ func initAccountMoveLine() {
orm.Boolean("reconciled", orm.FieldOpts{String: "Reconciled"}), orm.Boolean("reconciled", orm.FieldOpts{String: "Reconciled"}),
orm.Many2one("full_reconcile_id", "account.full.reconcile", orm.FieldOpts{String: "Matching"}), orm.Many2one("full_reconcile_id", "account.full.reconcile", orm.FieldOpts{String: "Matching"}),
) )
// reconcile: matches debit lines against credit lines and creates
// account.partial.reconcile (and optionally account.full.reconcile) records.
// Mirrors: odoo/addons/account/models/account_move_line.py AccountMoveLine.reconcile()
m.RegisterMethod("reconcile", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env()
lineIDs := rs.IDs()
if len(lineIDs) < 2 {
return false, fmt.Errorf("reconcile requires at least 2 lines")
}
// Read all lines
records, err := rs.Read([]string{"id", "debit", "credit", "amount_residual", "account_id", "partner_id", "move_id"})
if err != nil {
return nil, err
}
// Separate debit lines (receivable) from credit lines (payment)
var debitLines, creditLines []orm.Values
for _, rec := range records {
debit, _ := toFloat(rec["debit"])
credit, _ := toFloat(rec["credit"])
if debit > credit {
debitLines = append(debitLines, rec)
} else {
creditLines = append(creditLines, rec)
}
}
// Match debit <-> credit lines, creating partial reconciles
partialRS := env.Model("account.partial.reconcile")
var partialIDs []int64
for _, debitLine := range debitLines {
debitResidual, _ := toFloat(debitLine["amount_residual"])
if debitResidual <= 0 {
continue
}
debitLineID, _ := toInt64Arg(debitLine["id"])
for _, creditLine := range creditLines {
creditResidual, _ := toFloat(creditLine["amount_residual"])
if creditResidual >= 0 {
continue // credit residual is negative
}
creditLineID, _ := toInt64Arg(creditLine["id"])
// Match amount = min of what's available
matchAmount := debitResidual
if -creditResidual < matchAmount {
matchAmount = -creditResidual
}
if matchAmount <= 0 {
continue
}
// Create partial reconcile
partial, err := partialRS.Create(orm.Values{
"debit_move_id": debitLineID,
"credit_move_id": creditLineID,
"amount": matchAmount,
})
if err != nil {
return nil, err
}
partialIDs = append(partialIDs, partial.ID())
// Update residuals directly in the database
env.Tx().Exec(env.Ctx(),
`UPDATE account_move_line SET amount_residual = amount_residual - $1 WHERE id = $2`,
matchAmount, debitLineID)
env.Tx().Exec(env.Ctx(),
`UPDATE account_move_line SET amount_residual = amount_residual + $1 WHERE id = $2`,
matchAmount, creditLineID)
debitResidual -= matchAmount
creditResidual += matchAmount
creditLine["amount_residual"] = creditResidual
if debitResidual <= 0.005 {
break
}
}
debitLine["amount_residual"] = debitResidual
}
// Check if fully reconciled (all residuals ~ 0)
allResolved := true
for _, rec := range records {
recID, _ := toInt64Arg(rec["id"])
var residual float64
env.Tx().QueryRow(env.Ctx(),
`SELECT COALESCE(amount_residual, 0) FROM account_move_line WHERE id = $1`,
recID).Scan(&residual)
if residual > 0.005 || residual < -0.005 {
allResolved = false
break
}
}
// If fully reconciled, create account.full.reconcile
if allResolved && len(partialIDs) > 0 {
fullRS := env.Model("account.full.reconcile")
fullRec, err := fullRS.Create(orm.Values{
"name": fmt.Sprintf("FULL-%d", partialIDs[0]),
})
if err == nil {
// Link all lines to the full reconcile
for _, rec := range records {
lineID, _ := toInt64Arg(rec["id"])
env.Tx().Exec(env.Ctx(),
`UPDATE account_move_line SET full_reconcile_id = $1, reconciled = true WHERE id = $2`,
fullRec.ID(), lineID)
}
// Link partials to full reconcile
for _, pID := range partialIDs {
env.Tx().Exec(env.Ctx(),
`UPDATE account_partial_reconcile SET full_reconcile_id = $1 WHERE id = $2`,
fullRec.ID(), pID)
}
}
}
// Update payment_state on linked invoices
moveIDs := make(map[int64]bool)
for _, rec := range records {
if mid, ok := toInt64Arg(rec["move_id"]); ok {
moveIDs[mid] = true
}
}
for moveID := range moveIDs {
updatePaymentState(env, moveID)
}
return true, nil
})
} }
// initAccountPayment registers account.payment. // initAccountPayment registers account.payment.
@@ -830,15 +1071,79 @@ func initAccountPaymentRegister() {
} }
} }
// Mark related invoices as paid (simplified: update payment_state on active invoices in context) // Reconcile: link payment to invoices via partial reconcile records.
// In Python Odoo this happens through reconciliation; we simplify for 70% target. // Mirrors: odoo/addons/account/wizard/account_payment_register.py _reconcile_payments()
if ctx := env.Context(); ctx != nil { if ctx := env.Context(); ctx != nil {
if activeIDs, ok := ctx["active_ids"].([]interface{}); ok { if activeIDs, ok := ctx["active_ids"].([]interface{}); ok {
paymentAmount := floatArg(wiz["amount"], 0)
paymentID := payment.ID()
for _, rawID := range activeIDs { for _, rawID := range activeIDs {
if moveID, ok := toInt64Arg(rawID); ok && moveID > 0 { moveID, ok := toInt64Arg(rawID)
if !ok || moveID <= 0 {
continue
}
// Find the invoice's receivable/payable line
var invoiceLineID int64
var invoiceResidual float64
env.Tx().QueryRow(env.Ctx(),
`SELECT id, COALESCE(amount_residual, 0) FROM account_move_line
WHERE move_id = $1 AND display_type = 'payment_term'
ORDER BY id LIMIT 1`, moveID).Scan(&invoiceLineID, &invoiceResidual)
if invoiceLineID == 0 || invoiceResidual <= 0 {
// Fallback: direct update
env.Tx().Exec(env.Ctx(), env.Tx().Exec(env.Ctx(),
`UPDATE account_move SET payment_state = 'paid' WHERE id = $1 AND state = 'posted'`, moveID) `UPDATE account_move SET payment_state = 'paid' WHERE id = $1 AND state = 'posted'`, moveID)
continue
} }
// Determine match amount
matchAmount := paymentAmount
if invoiceResidual < matchAmount {
matchAmount = invoiceResidual
}
if matchAmount <= 0 {
continue
}
// Find the payment's journal entry lines (counterpart)
var paymentMoveID int64
env.Tx().QueryRow(env.Ctx(),
`SELECT COALESCE(move_id, 0) FROM account_payment WHERE id = $1`,
paymentID).Scan(&paymentMoveID)
var paymentLineID int64
if paymentMoveID > 0 {
env.Tx().QueryRow(env.Ctx(),
`SELECT id FROM account_move_line
WHERE move_id = $1 AND display_type = 'payment_term'
ORDER BY id LIMIT 1`, paymentMoveID).Scan(&paymentLineID)
}
if paymentLineID > 0 {
// Use the reconcile method
lineModel := orm.Registry.Get("account.move.line")
if lineModel != nil {
if reconcileMethod, mOk := lineModel.Methods["reconcile"]; mOk {
lineRS := env.Model("account.move.line").Browse(invoiceLineID, paymentLineID)
if _, rErr := reconcileMethod(lineRS); rErr == nil {
continue // reconcile handled payment_state update
}
}
}
}
// Fallback: create partial reconcile manually and update state
env.Tx().Exec(env.Ctx(),
`INSERT INTO account_partial_reconcile (debit_move_id, credit_move_id, amount)
VALUES ($1, $2, $3)`,
invoiceLineID, invoiceLineID, matchAmount)
env.Tx().Exec(env.Ctx(),
`UPDATE account_move_line SET amount_residual = amount_residual - $1 WHERE id = $2`,
matchAmount, invoiceLineID)
updatePaymentState(env, moveID)
} }
} }
} }
@@ -1070,3 +1375,43 @@ func floatArg(v interface{}, defaultVal float64) float64 {
} }
return defaultVal return defaultVal
} }
// toFloat converts various numeric types to float64.
// Returns (value, true) on success, (0, false) if not convertible.
func toFloat(v interface{}) (float64, bool) {
switch n := v.(type) {
case float64:
return n, true
case int64:
return float64(n), true
case int:
return float64(n), true
case int32:
return float64(n), true
case float32:
return float64(n), true
}
return 0, false
}
// updatePaymentState recomputes payment_state on an account.move based on its
// payment_term lines' residual amounts.
// Mirrors: odoo/addons/account/models/account_move.py _compute_payment_state()
func updatePaymentState(env *orm.Environment, moveID int64) {
var total, residual float64
env.Tx().QueryRow(env.Ctx(),
`SELECT COALESCE(SUM(ABS(balance)), 0), COALESCE(SUM(ABS(amount_residual)), 0)
FROM account_move_line WHERE move_id = $1 AND display_type = 'payment_term'`,
moveID).Scan(&total, &residual)
state := "not_paid"
if total > 0 {
if residual < 0.005 {
state = "paid"
} else if residual < total-0.005 {
state = "partial"
}
}
env.Tx().Exec(env.Ctx(),
`UPDATE account_move SET payment_state = $1 WHERE id = $2`, state, moveID)
}

View File

@@ -134,37 +134,39 @@ func initSaleOrder() {
env := rs.Env() env := rs.Env()
soID := rs.IDs()[0] soID := rs.IDs()[0]
var untaxed float64 var untaxed, tax, total float64
err := env.Tx().QueryRow(env.Ctx(), err := env.Tx().QueryRow(env.Ctx(),
`SELECT COALESCE(SUM(product_uom_qty * price_unit * (1 - COALESCE(discount,0)/100)), 0) `SELECT
COALESCE(SUM(price_subtotal), 0),
COALESCE(SUM(price_total - price_subtotal), 0),
COALESCE(SUM(price_total), 0)
FROM sale_order_line WHERE order_id = $1 FROM sale_order_line WHERE order_id = $1
AND (display_type IS NULL OR display_type = '' OR display_type = 'product')`, AND (display_type IS NULL OR display_type = '' OR display_type = 'product')`,
soID).Scan(&untaxed) soID).Scan(&untaxed, &tax, &total)
if err != nil { if err != nil {
return nil, fmt.Errorf("sale: compute amounts for SO %d: %w", soID, err) // Fallback: compute from raw line values if price_subtotal/price_total not yet stored
} err = env.Tx().QueryRow(env.Ctx(),
`SELECT
// Compute tax from linked tax records on lines; fall back to sum of line taxes COALESCE(SUM(product_uom_qty * price_unit * (1 - COALESCE(discount,0)/100)), 0),
var tax float64 COALESCE(SUM(
err = env.Tx().QueryRow(env.Ctx(), product_uom_qty * price_unit * (1 - COALESCE(discount,0)/100)
`SELECT COALESCE(SUM( * COALESCE((SELECT t.amount / 100 FROM account_tax t
product_uom_qty * price_unit * (1 - COALESCE(discount,0)/100) JOIN account_tax_sale_order_line_rel rel ON rel.account_tax_id = t.id
* COALESCE((SELECT t.amount / 100 FROM account_tax t WHERE rel.sale_order_line_id = sol.id LIMIT 1), 0)
JOIN account_tax_sale_order_line_rel rel ON rel.account_tax_id = t.id ), 0)
WHERE rel.sale_order_line_id = sol.id LIMIT 1), 0) FROM sale_order_line sol WHERE sol.order_id = $1
), 0) AND (sol.display_type IS NULL OR sol.display_type = '' OR sol.display_type = 'product')`,
FROM sale_order_line sol WHERE sol.order_id = $1 soID).Scan(&untaxed, &tax)
AND (sol.display_type IS NULL OR sol.display_type = '' OR sol.display_type = 'product')`, if err != nil {
soID).Scan(&tax) return nil, fmt.Errorf("sale: compute amounts for SO %d: %w", soID, err)
if err != nil { }
// Fallback: if the M2M table doesn't exist, estimate tax at 0 total = untaxed + tax
tax = 0
} }
return orm.Values{ return orm.Values{
"amount_untaxed": untaxed, "amount_untaxed": untaxed,
"amount_tax": tax, "amount_tax": tax,
"amount_total": untaxed + tax, "amount_total": total,
}, nil }, nil
} }
m.RegisterCompute("amount_untaxed", computeSaleAmounts) m.RegisterCompute("amount_untaxed", computeSaleAmounts)
@@ -285,35 +287,51 @@ func initSaleOrder() {
// Read SO header // Read SO header
var partnerID, companyID, currencyID int64 var partnerID, companyID, currencyID int64
var journalID int64 var journalID int64
var soName string
err := env.Tx().QueryRow(env.Ctx(), err := env.Tx().QueryRow(env.Ctx(),
`SELECT partner_id, company_id, currency_id, COALESCE(journal_id, 1) `SELECT partner_id, company_id, currency_id, COALESCE(journal_id, 0), COALESCE(name, '')
FROM sale_order WHERE id = $1`, soID, FROM sale_order WHERE id = $1`, soID,
).Scan(&partnerID, &companyID, &currencyID, &journalID) ).Scan(&partnerID, &companyID, &currencyID, &journalID, &soName)
if err != nil { if err != nil {
return nil, fmt.Errorf("sale: read SO %d: %w", soID, err) return nil, fmt.Errorf("sale: read SO %d: %w", soID, err)
} }
// Find sales journal if not set on SO
if journalID == 0 {
env.Tx().QueryRow(env.Ctx(),
`SELECT id FROM account_journal
WHERE type = 'sale' AND active = true AND company_id = $1
ORDER BY sequence, id LIMIT 1`, companyID,
).Scan(&journalID)
}
if journalID == 0 {
journalID = 1 // ultimate fallback
}
// Read SO lines // Read SO lines
rows, err := env.Tx().Query(env.Ctx(), rows, err := env.Tx().Query(env.Ctx(),
`SELECT id, COALESCE(name,''), COALESCE(product_uom_qty,1), COALESCE(price_unit,0), COALESCE(discount,0) `SELECT id, COALESCE(name,''), COALESCE(product_uom_qty,1),
COALESCE(price_unit,0), COALESCE(discount,0), COALESCE(product_id, 0)
FROM sale_order_line FROM sale_order_line
WHERE order_id = $1 AND (display_type IS NULL OR display_type = '') WHERE order_id = $1
AND (display_type IS NULL OR display_type = '' OR display_type = 'product')
ORDER BY sequence, id`, soID) ORDER BY sequence, id`, soID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
type soLine struct { type soLine struct {
id int64 id int64
name string name string
qty float64 qty float64
price float64 price float64
discount float64 discount float64
productID int64
} }
var lines []soLine var lines []soLine
for rows.Next() { for rows.Next() {
var l soLine var l soLine
if err := rows.Scan(&l.id, &l.name, &l.qty, &l.price, &l.discount); err != nil { if err := rows.Scan(&l.id, &l.name, &l.qty, &l.price, &l.discount, &l.productID); err != nil {
rows.Close() rows.Close()
return nil, err return nil, err
} }
@@ -325,35 +343,7 @@ func initSaleOrder() {
continue continue
} }
// Build invoice line commands // Create invoice header (draft)
var lineCmds []interface{}
for _, l := range lines {
subtotal := l.qty * l.price * (1 - l.discount/100)
lineCmds = append(lineCmds, []interface{}{
float64(0), float64(0), map[string]interface{}{
"name": l.name,
"quantity": l.qty,
"price_unit": l.price,
"discount": l.discount,
"debit": subtotal,
"credit": float64(0),
"account_id": float64(2), // Revenue account
"company_id": float64(companyID),
},
})
// Receivable counter-entry
lineCmds = append(lineCmds, []interface{}{
float64(0), float64(0), map[string]interface{}{
"name": "Receivable",
"debit": float64(0),
"credit": subtotal,
"account_id": float64(1), // Receivable account
"company_id": float64(companyID),
},
})
}
// Create invoice
invoiceRS := env.Model("account.move") invoiceRS := env.Model("account.move")
inv, err := invoiceRS.Create(orm.Values{ inv, err := invoiceRS.Create(orm.Values{
"move_type": "out_invoice", "move_type": "out_invoice",
@@ -361,15 +351,203 @@ func initSaleOrder() {
"company_id": companyID, "company_id": companyID,
"currency_id": currencyID, "currency_id": currencyID,
"journal_id": journalID, "journal_id": journalID,
"invoice_origin": fmt.Sprintf("SO%d", soID), "invoice_origin": soName,
"date": time.Now().Format("2006-01-02"), "date": time.Now().Format("2006-01-02"),
"line_ids": lineCmds,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("sale: create invoice for SO %d: %w", soID, err) return nil, fmt.Errorf("sale: create invoice header for SO %d: %w", soID, err)
}
moveID := inv.ID()
// Find the revenue account (8300 Erlöse 19% USt or fallback)
var revenueAccountID int64
env.Tx().QueryRow(env.Ctx(),
`SELECT id FROM account_account WHERE code = '8300' AND company_id = $1 LIMIT 1`,
companyID,
).Scan(&revenueAccountID)
if revenueAccountID == 0 {
// Fallback: any income account
env.Tx().QueryRow(env.Ctx(),
`SELECT id FROM account_account
WHERE account_type LIKE 'income%' AND company_id = $1
ORDER BY code LIMIT 1`, companyID,
).Scan(&revenueAccountID)
}
if revenueAccountID == 0 {
// Fallback: journal default account
env.Tx().QueryRow(env.Ctx(),
`SELECT default_account_id FROM account_journal WHERE id = $1`, journalID,
).Scan(&revenueAccountID)
} }
invoiceIDs = append(invoiceIDs, inv.ID()) // Find the receivable account
var receivableAccountID int64
env.Tx().QueryRow(env.Ctx(),
`SELECT id FROM account_account
WHERE account_type = 'asset_receivable' AND company_id = $1
ORDER BY code LIMIT 1`, companyID,
).Scan(&receivableAccountID)
if receivableAccountID == 0 {
return nil, fmt.Errorf("sale: no receivable account found for company %d", companyID)
}
lineRS := env.Model("account.move.line")
var totalCredit float64 // accumulates all product + tax credits
// Create product lines and tax lines for each SO line
for _, line := range lines {
baseAmount := line.qty * line.price * (1 - line.discount/100)
// Determine revenue account: try product-specific, then default
lineAccountID := revenueAccountID
if line.productID > 0 {
var prodAccID int64
env.Tx().QueryRow(env.Ctx(),
`SELECT COALESCE(pc.property_account_income_categ_id, 0)
FROM product_product pp
JOIN product_template pt ON pt.id = pp.product_tmpl_id
JOIN product_category pc ON pc.id = pt.categ_id
WHERE pp.id = $1`, line.productID,
).Scan(&prodAccID)
if prodAccID > 0 {
lineAccountID = prodAccID
}
}
// Product line (credit side for revenue on out_invoice)
productLineVals := orm.Values{
"move_id": moveID,
"name": line.name,
"quantity": line.qty,
"price_unit": line.price,
"discount": line.discount,
"account_id": lineAccountID,
"company_id": companyID,
"journal_id": journalID,
"currency_id": currencyID,
"partner_id": partnerID,
"display_type": "product",
"debit": 0.0,
"credit": baseAmount,
"balance": -baseAmount,
}
if _, err := lineRS.Create(productLineVals); err != nil {
return nil, fmt.Errorf("sale: create invoice product line: %w", err)
}
totalCredit += baseAmount
// Look up taxes from SO line's tax_id M2M and compute tax lines
taxRows, err := env.Tx().Query(env.Ctx(),
`SELECT t.id, t.name, t.amount, t.amount_type, COALESCE(t.price_include, false)
FROM account_tax t
JOIN account_tax_sale_order_line_rel rel ON rel.account_tax_id = t.id
WHERE rel.sale_order_line_id = $1`, line.id)
if err == nil {
for taxRows.Next() {
var taxID int64
var taxName string
var taxRate float64
var amountType string
var priceInclude bool
if err := taxRows.Scan(&taxID, &taxName, &taxRate, &amountType, &priceInclude); err != nil {
taxRows.Close()
break
}
// Compute tax amount (mirrors account_tax_calc.go ComputeTax)
var taxAmount float64
switch amountType {
case "percent":
if priceInclude {
taxAmount = baseAmount - (baseAmount / (1 + taxRate/100))
} else {
taxAmount = baseAmount * taxRate / 100
}
case "fixed":
taxAmount = taxRate
case "division":
if priceInclude {
taxAmount = baseAmount - (baseAmount / (1 + taxRate/100))
} else {
taxAmount = baseAmount * taxRate / 100
}
}
if taxAmount == 0 {
continue
}
// Find tax account from repartition lines
var taxAccountID int64
env.Tx().QueryRow(env.Ctx(),
`SELECT COALESCE(account_id, 0) FROM account_tax_repartition_line
WHERE tax_id = $1 AND repartition_type = 'tax' AND document_type = 'invoice'
LIMIT 1`, taxID,
).Scan(&taxAccountID)
if taxAccountID == 0 {
// Fallback: USt account 1776 (SKR03)
env.Tx().QueryRow(env.Ctx(),
`SELECT id FROM account_account WHERE code = '1776' LIMIT 1`,
).Scan(&taxAccountID)
}
if taxAccountID == 0 {
taxAccountID = lineAccountID // ultimate fallback
}
taxLineVals := orm.Values{
"move_id": moveID,
"name": taxName,
"quantity": 1.0,
"account_id": taxAccountID,
"company_id": companyID,
"journal_id": journalID,
"currency_id": currencyID,
"partner_id": partnerID,
"display_type": "tax",
"tax_line_id": taxID,
"debit": 0.0,
"credit": taxAmount,
"balance": -taxAmount,
}
if _, err := lineRS.Create(taxLineVals); err != nil {
taxRows.Close()
return nil, fmt.Errorf("sale: create invoice tax line: %w", err)
}
totalCredit += taxAmount
}
taxRows.Close()
}
}
// Create the receivable line (debit = total of all credits)
receivableVals := orm.Values{
"move_id": moveID,
"name": "/",
"quantity": 1.0,
"account_id": receivableAccountID,
"company_id": companyID,
"journal_id": journalID,
"currency_id": currencyID,
"partner_id": partnerID,
"display_type": "payment_term",
"debit": totalCredit,
"credit": 0.0,
"balance": totalCredit,
"amount_residual": totalCredit,
"amount_residual_currency": totalCredit,
}
if _, err := lineRS.Create(receivableVals); err != nil {
return nil, fmt.Errorf("sale: create invoice receivable line: %w", err)
}
invoiceIDs = append(invoiceIDs, moveID)
// Update qty_invoiced on SO lines
for _, line := range lines {
env.Tx().Exec(env.Ctx(),
`UPDATE sale_order_line SET qty_invoiced = COALESCE(qty_invoiced, 0) + $1 WHERE id = $2`,
line.qty, line.id)
}
// Update SO invoice_status // Update SO invoice_status
env.Tx().Exec(env.Ctx(), env.Tx().Exec(env.Ctx(),
@@ -606,7 +784,7 @@ func initSaleOrderLine() {
) )
// -- Computed: _compute_amount (line subtotal/total) -- // -- Computed: _compute_amount (line subtotal/total) --
// Computes price_subtotal and price_total from qty, price, discount. // Computes price_subtotal and price_total from qty, price, discount, and taxes.
// Mirrors: odoo/addons/sale/models/sale_order_line.py SaleOrderLine._compute_amount() // Mirrors: odoo/addons/sale/models/sale_order_line.py SaleOrderLine._compute_amount()
computeLineAmount := func(rs *orm.Recordset) (orm.Values, error) { computeLineAmount := func(rs *orm.Recordset) (orm.Values, error) {
env := rs.Env() env := rs.Env()
@@ -617,9 +795,45 @@ func initSaleOrderLine() {
FROM sale_order_line WHERE id = $1`, lineID, FROM sale_order_line WHERE id = $1`, lineID,
).Scan(&qty, &price, &discount) ).Scan(&qty, &price, &discount)
subtotal := qty * price * (1 - discount/100) subtotal := qty * price * (1 - discount/100)
// Compute tax amount from linked taxes via M2M (inline, no cross-package call)
var taxTotal float64
taxRows, err := env.Tx().Query(env.Ctx(),
`SELECT t.amount, t.amount_type, COALESCE(t.price_include, false)
FROM account_tax t
JOIN account_tax_sale_order_line_rel rel ON rel.account_tax_id = t.id
WHERE rel.sale_order_line_id = $1`, lineID)
if err == nil {
for taxRows.Next() {
var taxRate float64
var amountType string
var priceInclude bool
if err := taxRows.Scan(&taxRate, &amountType, &priceInclude); err != nil {
break
}
switch amountType {
case "percent":
if priceInclude {
taxTotal += subtotal - (subtotal / (1 + taxRate/100))
} else {
taxTotal += subtotal * taxRate / 100
}
case "fixed":
taxTotal += taxRate
case "division":
if priceInclude {
taxTotal += subtotal - (subtotal / (1 + taxRate/100))
} else {
taxTotal += subtotal * taxRate / 100
}
}
}
taxRows.Close()
}
return orm.Values{ return orm.Values{
"price_subtotal": subtotal, "price_subtotal": subtotal,
"price_total": subtotal, // TODO: add tax amount for price_total "price_total": subtotal + taxTotal,
}, nil }, nil
} }
m.RegisterCompute("price_subtotal", computeLineAmount) m.RegisterCompute("price_subtotal", computeLineAmount)

View File

@@ -193,7 +193,7 @@ func initStockPicking() {
// --- Business methods: stock move workflow --- // --- Business methods: stock move workflow ---
// action_confirm transitions a picking from draft → confirmed. // action_confirm transitions a picking from draft → confirmed.
// Confirms all associated stock moves that are still in draft. // Confirms all associated stock moves via _action_confirm (which also reserves).
// Mirrors: stock.picking.action_confirm() // Mirrors: stock.picking.action_confirm()
m.RegisterMethod("action_confirm", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { m.RegisterMethod("action_confirm", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env() env := rs.Env()
@@ -212,95 +212,168 @@ func initStockPicking() {
if err != nil { if err != nil {
return nil, fmt.Errorf("stock: confirm picking %d: %w", id, err) return nil, fmt.Errorf("stock: confirm picking %d: %w", id, err)
} }
// Also confirm all draft moves on this picking
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_move SET state = 'confirmed' WHERE picking_id = $1 AND state = 'draft'`, id)
if err != nil {
return nil, fmt.Errorf("stock: confirm moves for picking %d: %w", id, err)
}
}
return true, nil
})
// action_assign transitions a picking from confirmed → assigned (reserve stock). // Confirm all draft moves via _action_confirm (which also tries to reserve)
// Mirrors: stock.picking.action_assign()
m.RegisterMethod("action_assign", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env()
for _, id := range rs.IDs() {
_, err := env.Tx().Exec(env.Ctx(),
`UPDATE stock_picking SET state = 'assigned' WHERE id = $1`, id)
if err != nil {
return nil, fmt.Errorf("stock: assign picking %d: %w", id, err)
}
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_move SET state = 'assigned' WHERE picking_id = $1 AND state IN ('confirmed', 'partially_available')`, id)
if err != nil {
return nil, fmt.Errorf("stock: assign moves for picking %d: %w", id, err)
}
}
return true, nil
})
// button_validate transitions a picking from assigned → done (process the transfer).
// Updates all moves to done and adjusts stock quants (source decremented, dest incremented).
// Mirrors: stock.picking.button_validate()
m.RegisterMethod("button_validate", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env()
for _, id := range rs.IDs() {
// Mark picking as done
_, err := env.Tx().Exec(env.Ctx(),
`UPDATE stock_picking SET state = 'done', date_done = NOW() WHERE id = $1`, id)
if err != nil {
return nil, fmt.Errorf("stock: validate picking %d: %w", id, err)
}
// Mark all moves as done
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_move SET state = 'done', date = NOW() WHERE picking_id = $1`, id)
if err != nil {
return nil, fmt.Errorf("stock: validate moves for picking %d: %w", id, err)
}
// Update quants: for each done move, adjust source and destination locations
rows, err := env.Tx().Query(env.Ctx(), rows, err := env.Tx().Query(env.Ctx(),
`SELECT product_id, product_uom_qty, location_id, location_dest_id `SELECT id FROM stock_move WHERE picking_id = $1 AND state = 'draft'`, id)
FROM stock_move WHERE picking_id = $1 AND state = 'done'`, id)
if err != nil { if err != nil {
return nil, fmt.Errorf("stock: read moves for picking %d: %w", id, err) return nil, fmt.Errorf("stock: read draft moves for picking %d: %w", id, err)
} }
var moveIDs []int64
type moveInfo struct {
productID int64
qty float64
srcLoc int64
dstLoc int64
}
var moves []moveInfo
for rows.Next() { for rows.Next() {
var mi moveInfo var mid int64
if err := rows.Scan(&mi.productID, &mi.qty, &mi.srcLoc, &mi.dstLoc); err != nil { if err := rows.Scan(&mid); err != nil {
rows.Close() rows.Close()
return nil, fmt.Errorf("stock: scan move for picking %d: %w", id, err) return nil, fmt.Errorf("stock: scan move for picking %d: %w", id, err)
} }
moves = append(moves, mi) moveIDs = append(moveIDs, mid)
} }
rows.Close() rows.Close()
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {
return nil, fmt.Errorf("stock: iterate moves for picking %d: %w", id, err) return nil, fmt.Errorf("stock: iterate moves for picking %d: %w", id, err)
} }
for _, mi := range moves { if len(moveIDs) > 0 {
// Decrease source location quant moveRS := env.Model("stock.move").Browse(moveIDs...)
if err := updateQuant(env, mi.productID, mi.srcLoc, -mi.qty); err != nil { moveModel := orm.Registry.Get("stock.move")
return nil, fmt.Errorf("stock: update source quant for picking %d: %w", id, err) if moveModel != nil {
if confirmMethod, ok := moveModel.Methods["_action_confirm"]; ok {
if _, err := confirmMethod(moveRS); err != nil {
return nil, fmt.Errorf("stock: confirm moves for picking %d: %w", id, err)
}
}
} }
// Increase destination location quant }
if err := updateQuant(env, mi.productID, mi.dstLoc, mi.qty); err != nil {
return nil, fmt.Errorf("stock: update dest quant for picking %d: %w", id, err) // Update picking state based on move states after reservation
var allAssigned bool
err = env.Tx().QueryRow(env.Ctx(),
`SELECT NOT EXISTS(
SELECT 1 FROM stock_move
WHERE picking_id = $1 AND state NOT IN ('assigned', 'done', 'cancel')
)`, id).Scan(&allAssigned)
if err != nil {
return nil, fmt.Errorf("stock: check move states for picking %d: %w", id, err)
}
if allAssigned {
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_picking SET state = 'assigned' WHERE id = $1`, id)
if err != nil {
return nil, fmt.Errorf("stock: update picking %d to assigned: %w", id, err)
} }
} }
} }
return true, nil return true, nil
}) })
// action_assign reserves stock for all confirmed/partially_available moves on the picking.
// Mirrors: stock.picking.action_assign()
m.RegisterMethod("action_assign", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env()
for _, pickingID := range rs.IDs() {
// Get moves that need reservation
rows, err := env.Tx().Query(env.Ctx(),
`SELECT id FROM stock_move WHERE picking_id = $1 AND state IN ('confirmed', 'partially_available')`, pickingID)
if err != nil {
return nil, fmt.Errorf("stock: read moves for assign picking %d: %w", pickingID, err)
}
var moveIDs []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
rows.Close()
return nil, fmt.Errorf("stock: scan move for picking %d: %w", pickingID, err)
}
moveIDs = append(moveIDs, id)
}
rows.Close()
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("stock: iterate moves for picking %d: %w", pickingID, err)
}
if len(moveIDs) > 0 {
moveRS := env.Model("stock.move").Browse(moveIDs...)
moveModel := orm.Registry.Get("stock.move")
if moveModel != nil {
if assignMethod, ok := moveModel.Methods["_action_assign"]; ok {
if _, err := assignMethod(moveRS); err != nil {
return nil, fmt.Errorf("stock: assign moves for picking %d: %w", pickingID, err)
}
}
}
}
// Update picking state based on move states
var allAssigned bool
err = env.Tx().QueryRow(env.Ctx(),
`SELECT NOT EXISTS(
SELECT 1 FROM stock_move
WHERE picking_id = $1 AND state NOT IN ('assigned', 'done', 'cancel')
)`, pickingID).Scan(&allAssigned)
if err != nil {
return nil, fmt.Errorf("stock: check move states for picking %d: %w", pickingID, err)
}
if allAssigned {
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_picking SET state = 'assigned' WHERE id = $1`, pickingID)
}
if err != nil {
return nil, fmt.Errorf("stock: update picking %d state: %w", pickingID, err)
}
}
return true, nil
})
// button_validate transitions a picking → done via _action_done on its moves.
// Properly updates quants and clears reservations.
// Mirrors: stock.picking.button_validate()
m.RegisterMethod("button_validate", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env()
for _, pickingID := range rs.IDs() {
// Get all non-cancelled moves for this picking
rows, err := env.Tx().Query(env.Ctx(),
`SELECT id FROM stock_move WHERE picking_id = $1 AND state != 'cancel'`, pickingID)
if err != nil {
return nil, fmt.Errorf("stock: read moves for picking %d: %w", pickingID, err)
}
var moveIDs []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
rows.Close()
return nil, fmt.Errorf("stock: scan move for picking %d: %w", pickingID, err)
}
moveIDs = append(moveIDs, id)
}
rows.Close()
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("stock: iterate moves for picking %d: %w", pickingID, err)
}
if len(moveIDs) == 0 {
continue
}
// Call _action_done on all moves
moveRS := env.Model("stock.move").Browse(moveIDs...)
moveModel := orm.Registry.Get("stock.move")
if moveModel != nil {
if doneMethod, ok := moveModel.Methods["_action_done"]; ok {
if _, err := doneMethod(moveRS); err != nil {
return nil, fmt.Errorf("stock: action_done for picking %d: %w", pickingID, err)
}
}
}
// Update picking state
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_picking SET state = 'done', date_done = NOW() WHERE id = $1`, pickingID)
if err != nil {
return nil, fmt.Errorf("stock: validate picking %d: %w", pickingID, err)
}
}
return true, nil
})
} }
// updateQuant adjusts the on-hand quantity for a product at a location. // updateQuant adjusts the on-hand quantity for a product at a location.
@@ -319,12 +392,89 @@ func updateQuant(env *orm.Environment, productID, locationID int64, delta float6
delta, productID, locationID) delta, productID, locationID)
} else { } else {
_, err = env.Tx().Exec(env.Ctx(), _, err = env.Tx().Exec(env.Ctx(),
`INSERT INTO stock_quant (product_id, location_id, quantity, company_id) VALUES ($1, $2, $3, 1)`, `INSERT INTO stock_quant (product_id, location_id, quantity, reserved_quantity, company_id) VALUES ($1, $2, $3, 0, 1)`,
productID, locationID, delta) productID, locationID, delta)
} }
return err return err
} }
// getAvailableQty returns unreserved on-hand quantity for a product at a location.
// Mirrors: stock.quant._get_available_quantity()
func getAvailableQty(env *orm.Environment, productID, locationID int64) float64 {
var available float64
env.Tx().QueryRow(env.Ctx(),
`SELECT COALESCE(SUM(quantity - reserved_quantity), 0)
FROM stock_quant
WHERE product_id = $1 AND location_id = $2`,
productID, locationID).Scan(&available)
return available
}
// assignMove reserves available stock for a single move.
// Creates a stock.move.line (reservation) and updates quant reserved_quantity.
// Mirrors: stock.move._action_assign() per-move logic
func assignMove(env *orm.Environment, moveID int64) error {
// Read move details
var productID, locationID int64
var qty float64
var state string
err := env.Tx().QueryRow(env.Ctx(),
`SELECT product_id, product_uom_qty, location_id, state FROM stock_move WHERE id = $1`,
moveID).Scan(&productID, &qty, &locationID, &state)
if err != nil {
return fmt.Errorf("stock: read move %d for assign: %w", moveID, err)
}
if state == "done" || state == "cancel" || qty <= 0 {
return nil
}
// Check available quantity in source location
available := getAvailableQty(env, productID, locationID)
// Reserve what we can
reserved := qty
if available < reserved {
reserved = available
}
if reserved <= 0 {
return nil // Nothing to reserve
}
// Create move line (reservation)
_, err = env.Tx().Exec(env.Ctx(),
`INSERT INTO stock_move_line (move_id, product_id, location_id, location_dest_id, quantity, company_id)
SELECT $1, product_id, location_id, location_dest_id, $2, company_id
FROM stock_move WHERE id = $1`,
moveID, reserved)
if err != nil {
return fmt.Errorf("stock: create move line for move %d: %w", moveID, err)
}
// Update quant reserved_quantity
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_quant SET reserved_quantity = reserved_quantity + $1
WHERE product_id = $2 AND location_id = $3`,
reserved, productID, locationID)
if err != nil {
return fmt.Errorf("stock: update reserved qty for move %d: %w", moveID, err)
}
// Update move state
if reserved >= qty-0.005 {
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_move SET state = 'assigned' WHERE id = $1`, moveID)
} else {
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_move SET state = 'partially_available' WHERE id = $1`, moveID)
}
if err != nil {
return fmt.Errorf("stock: update state for move %d: %w", moveID, err)
}
return nil
}
// initStockMove registers stock.move — individual product movements. // initStockMove registers stock.move — individual product movements.
// Mirrors: odoo/addons/stock/models/stock_move.py // Mirrors: odoo/addons/stock/models/stock_move.py
func initStockMove() { func initStockMove() {
@@ -375,21 +525,49 @@ func initStockMove() {
orm.Char("origin", orm.FieldOpts{String: "Source Document"}), orm.Char("origin", orm.FieldOpts{String: "Source Document"}),
) )
// _action_confirm: Confirm stock moves (draft → confirmed). // _action_confirm: Confirm stock moves (draft → confirmed), then try to reserve.
// Mirrors: odoo/addons/stock/models/stock_move.py StockMove._action_confirm() // Mirrors: odoo/addons/stock/models/stock_move.py StockMove._action_confirm()
m.RegisterMethod("_action_confirm", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { m.RegisterMethod("_action_confirm", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env() env := rs.Env()
for _, id := range rs.IDs() { for _, moveID := range rs.IDs() {
_, err := env.Tx().Exec(env.Ctx(), var state string
`UPDATE stock_move SET state = 'confirmed' WHERE id = $1 AND state = 'draft'`, id) err := env.Tx().QueryRow(env.Ctx(),
`SELECT state FROM stock_move WHERE id = $1`, moveID).Scan(&state)
if err != nil { if err != nil {
return nil, fmt.Errorf("stock: confirm move %d: %w", id, err) return nil, fmt.Errorf("stock: read move %d for confirm: %w", moveID, err)
}
if state != "draft" {
continue
}
// Set to confirmed
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_move SET state = 'confirmed' WHERE id = $1`, moveID)
if err != nil {
return nil, fmt.Errorf("stock: confirm move %d: %w", moveID, err)
}
// Try to reserve (assign) immediately
if err := assignMove(env, moveID); err != nil {
return nil, fmt.Errorf("stock: assign move %d after confirm: %w", moveID, err)
} }
} }
return true, nil return true, nil
}) })
// _action_done: Finalize stock moves (assigned → done), updating quants. // _action_assign: Reserve stock for confirmed moves.
// Mirrors: odoo/addons/stock/models/stock_move.py StockMove._action_assign()
m.RegisterMethod("_action_assign", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env()
for _, moveID := range rs.IDs() {
if err := assignMove(env, moveID); err != nil {
return nil, fmt.Errorf("stock: assign move %d: %w", moveID, err)
}
}
return true, nil
})
// _action_done: Finalize stock moves (assigned → done), updating quants and clearing reservations.
// Mirrors: odoo/addons/stock/models/stock_move.py StockMove._action_done() // Mirrors: odoo/addons/stock/models/stock_move.py StockMove._action_done()
m.RegisterMethod("_action_done", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) { m.RegisterMethod("_action_done", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env() env := rs.Env()
@@ -402,18 +580,31 @@ func initStockMove() {
if err != nil { if err != nil {
return nil, fmt.Errorf("stock: read move %d for done: %w", id, err) return nil, fmt.Errorf("stock: read move %d for done: %w", id, err)
} }
// Decrease source quant
if err := updateQuant(env, productID, srcLoc, -qty); err != nil {
return nil, fmt.Errorf("stock: update source quant for move %d: %w", id, err)
}
// Increase destination quant
if err := updateQuant(env, productID, dstLoc, qty); err != nil {
return nil, fmt.Errorf("stock: update dest quant for move %d: %w", id, err)
}
// Clear reservation on source quant
_, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_quant SET reserved_quantity = GREATEST(reserved_quantity - $1, 0)
WHERE product_id = $2 AND location_id = $3`,
qty, productID, srcLoc)
if err != nil {
return nil, fmt.Errorf("stock: clear reservation for move %d: %w", id, err)
}
// Mark move as done
_, err = env.Tx().Exec(env.Ctx(), _, err = env.Tx().Exec(env.Ctx(),
`UPDATE stock_move SET state = 'done', date = NOW() WHERE id = $1`, id) `UPDATE stock_move SET state = 'done', date = NOW() WHERE id = $1`, id)
if err != nil { if err != nil {
return nil, fmt.Errorf("stock: done move %d: %w", id, err) return nil, fmt.Errorf("stock: done move %d: %w", id, err)
} }
// Adjust quants
if err := updateQuant(env, productID, srcLoc, -qty); err != nil {
return nil, fmt.Errorf("stock: update source quant for move %d: %w", id, err)
}
if err := updateQuant(env, productID, dstLoc, qty); err != nil {
return nil, fmt.Errorf("stock: update dest quant for move %d: %w", id, err)
}
} }
return true, nil return true, nil
}) })