Expand Sale/Purchase/Project + 102 ORM tests — +4633 LOC
Sale (1177→2321 LOC): - Quotation templates (apply to order, option lines) - Sales reports (by month, product, customer, salesperson, category) - Advance payment wizard (delivered/percentage/fixed modes) - SO cancel wizard, discount wizard - action_quotation_sent, action_lock/unlock, preview_quotation - Line computes: invoice_status, price_reduce, untaxed_amount - Partner extension: sale_order_total Purchase (478→1424 LOC): - Purchase reports (by month, category, bill status, receipt analysis) - Receipt creation from PO (action_create_picking) - 3-way matching: action_view_picking, action_view_invoice - button_approve, button_done, action_rfq_send - Line computes: price_subtotal/total with tax, product onchange - Partner extension: purchase_order_count/total Project (218→1161 LOC): - Project updates (status tracking: on_track/at_risk/off_track) - Milestones (deadline, reached tracking, task count) - Timesheet integration (account.analytic.line extension) - Timesheet reports (by project, employee, task, week) - Task recurrence model - Task: planned/effective/remaining hours, progress, subtask hours - Project: allocated/remaining hours, profitability actions ORM Tests (102 tests, 0→1257 LOC): - domain_test.go: 32 tests (compile, operators, AND/OR/NOT, null) - field_test.go: 15 tests (IsCopyable, SQLType, IsRelational, IsStored) - model_test.go: 21 tests (NewModel, AddFields, RegisterMethod, ExtendModel) - domain_parse_test.go: 21 tests (parse Python domain strings) - sanitize_test.go: 13 tests (false→nil, type conversions) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,16 @@ func Init() {
|
||||
initSaleOrderLine()
|
||||
initResPartnerSaleExtension()
|
||||
initSaleMargin()
|
||||
initSaleOrderTemplate()
|
||||
initSaleOrderTemplateLine()
|
||||
initSaleOrderTemplateOption()
|
||||
initSaleReport()
|
||||
initSaleOrderWarnMsg()
|
||||
initSaleAdvancePaymentWizard()
|
||||
initSaleOrderExtension()
|
||||
initSaleOrderLineExtension()
|
||||
initSaleOrderDiscount()
|
||||
initResPartnerSaleExtension2()
|
||||
}
|
||||
|
||||
// initResPartnerSaleExtension extends res.partner with sale-specific fields.
|
||||
|
||||
443
addons/sale/models/sale_order_extend.go
Normal file
443
addons/sale/models/sale_order_extend.go
Normal file
@@ -0,0 +1,443 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// initSaleOrderExtension extends sale.order with template support, additional workflow
|
||||
// methods, and computed fields.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py (additional fields)
|
||||
// odoo/addons/sale_management/models/sale_order.py (template fields)
|
||||
func initSaleOrderExtension() {
|
||||
so := orm.ExtendModel("sale.order")
|
||||
|
||||
// -- Template & Additional Fields --
|
||||
so.AddFields(
|
||||
orm.Many2one("sale_order_template_id", "sale.order.template", orm.FieldOpts{
|
||||
String: "Quotation Template",
|
||||
}),
|
||||
orm.Boolean("is_expired", orm.FieldOpts{
|
||||
String: "Expired", Compute: "_compute_is_expired",
|
||||
}),
|
||||
orm.Char("client_order_ref", orm.FieldOpts{String: "Customer Reference"}),
|
||||
orm.Char("origin", orm.FieldOpts{String: "Source Document"}),
|
||||
orm.Boolean("locked", orm.FieldOpts{String: "Locked", Readonly: true}),
|
||||
orm.Many2one("user_id", "res.users", orm.FieldOpts{String: "Salesperson", Index: true}),
|
||||
orm.Many2one("team_id", "crm.team", orm.FieldOpts{String: "Sales Team"}),
|
||||
orm.Char("reference", orm.FieldOpts{String: "Payment Reference"}),
|
||||
orm.Datetime("commitment_date", orm.FieldOpts{String: "Delivery Date"}),
|
||||
orm.Datetime("date_last_order_followup", orm.FieldOpts{String: "Last Follow-up Date"}),
|
||||
orm.Text("internal_note", orm.FieldOpts{String: "Internal Note"}),
|
||||
)
|
||||
|
||||
// -- Computed: is_expired --
|
||||
// An SO is expired when validity_date is in the past and state is still draft/sent.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py SaleOrder._compute_is_expired()
|
||||
so.RegisterCompute("is_expired", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
soID := rs.IDs()[0]
|
||||
var validityDate *time.Time
|
||||
var state string
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT validity_date, state FROM sale_order WHERE id = $1`, soID,
|
||||
).Scan(&validityDate, &state)
|
||||
|
||||
expired := false
|
||||
if validityDate != nil && (state == "draft" || state == "sent") {
|
||||
expired = validityDate.Before(time.Now().Truncate(24 * time.Hour))
|
||||
}
|
||||
return orm.Values{"is_expired": expired}, nil
|
||||
})
|
||||
|
||||
// -- Computed: _compute_invoice_status (extends the base) --
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py SaleOrder._compute_invoice_status()
|
||||
so.RegisterCompute("invoice_status", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
soID := rs.IDs()[0]
|
||||
|
||||
var state string
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(state, 'draft') FROM sale_order WHERE id = $1`, soID).Scan(&state)
|
||||
|
||||
// Only compute for confirmed/done orders
|
||||
if state != "sale" && state != "done" {
|
||||
return orm.Values{"invoice_status": "no"}, nil
|
||||
}
|
||||
|
||||
// Check line quantities
|
||||
var totalQty, totalInvoiced float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(SUM(product_uom_qty), 0), COALESCE(SUM(qty_invoiced), 0)
|
||||
FROM sale_order_line WHERE order_id = $1
|
||||
AND (display_type IS NULL OR display_type = '' OR display_type = 'product')`,
|
||||
soID).Scan(&totalQty, &totalInvoiced)
|
||||
|
||||
status := "no"
|
||||
if totalQty > 0 {
|
||||
if totalInvoiced >= totalQty {
|
||||
status = "invoiced"
|
||||
} else if totalInvoiced > 0 {
|
||||
status = "to invoice"
|
||||
} else {
|
||||
status = "to invoice"
|
||||
}
|
||||
}
|
||||
return orm.Values{"invoice_status": status}, nil
|
||||
})
|
||||
|
||||
// action_quotation_sent: Mark the SO as "sent" (quotation has been emailed).
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py SaleOrder.action_quotation_sent()
|
||||
so.RegisterMethod("action_quotation_sent", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, soID := range rs.IDs() {
|
||||
var state string
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT state FROM sale_order WHERE id = $1`, soID).Scan(&state)
|
||||
if state != "draft" {
|
||||
continue
|
||||
}
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE sale_order SET state = 'sent' WHERE id = $1`, soID)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_lock: Lock a confirmed sale order to prevent modifications.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py SaleOrder.action_lock()
|
||||
so.RegisterMethod("action_lock", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, soID := range rs.IDs() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE sale_order SET locked = true WHERE id = $1`, soID)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_unlock: Unlock a confirmed sale order.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py SaleOrder.action_unlock()
|
||||
so.RegisterMethod("action_unlock", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, soID := range rs.IDs() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE sale_order SET locked = false WHERE id = $1`, soID)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_view_delivery: Open delivery orders linked to this sale order.
|
||||
// Mirrors: odoo/addons/sale_stock/models/sale_order.py SaleOrder.action_view_delivery()
|
||||
so.RegisterMethod("action_view_delivery", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
soID := rs.IDs()[0]
|
||||
var soName string
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(name, '') FROM sale_order WHERE id = $1`, soID).Scan(&soName)
|
||||
|
||||
rows, err := env.Tx().Query(env.Ctx(),
|
||||
`SELECT id FROM stock_picking WHERE origin = $1`, soName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale: view delivery query: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var pickingIDs []interface{}
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
rows.Scan(&id)
|
||||
pickingIDs = append(pickingIDs, id)
|
||||
}
|
||||
|
||||
if len(pickingIDs) == 1 {
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window", "res_model": "stock.picking",
|
||||
"res_id": pickingIDs[0], "view_mode": "form",
|
||||
"views": [][]interface{}{{nil, "form"}}, "target": "current",
|
||||
}, nil
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window", "res_model": "stock.picking",
|
||||
"view_mode": "list,form", "views": [][]interface{}{{nil, "list"}, {nil, "form"}},
|
||||
"domain": []interface{}{[]interface{}{"id", "in", pickingIDs}}, "target": "current",
|
||||
}, nil
|
||||
})
|
||||
|
||||
// preview_quotation: Generate a preview URL for the quotation portal.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py SaleOrder.preview_sale_order()
|
||||
so.RegisterMethod("preview_quotation", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
soID := rs.IDs()[0]
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_url",
|
||||
"url": fmt.Sprintf("/my/orders/%d", soID),
|
||||
"target": "new",
|
||||
}, nil
|
||||
})
|
||||
|
||||
// action_open_discount_wizard: Open the discount wizard.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py SaleOrder.action_open_discount_wizard()
|
||||
so.RegisterMethod("action_open_discount_wizard", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"res_model": "sale.order.discount",
|
||||
"view_mode": "form",
|
||||
"target": "new",
|
||||
}, nil
|
||||
})
|
||||
|
||||
// _get_order_confirmation_date: Return the date when the SO was confirmed.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py SaleOrder._get_order_confirmation_date()
|
||||
so.RegisterMethod("_get_order_confirmation_date", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
soID := rs.IDs()[0]
|
||||
var dateOrder *time.Time
|
||||
var state string
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT date_order, state FROM sale_order WHERE id = $1`, soID).Scan(&dateOrder, &state)
|
||||
if state == "sale" || state == "done" {
|
||||
return dateOrder, nil
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
|
||||
// _compute_amount_to_invoice: Compute total amount still to invoice.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order.py SaleOrder._compute_amounts()
|
||||
so.RegisterMethod("_compute_amount_to_invoice", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
soID := rs.IDs()[0]
|
||||
var total, invoiced float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(SUM(price_subtotal::float8), 0),
|
||||
COALESCE(SUM(qty_invoiced * price_unit * (1 - COALESCE(discount,0)/100))::float8, 0)
|
||||
FROM sale_order_line WHERE order_id = $1
|
||||
AND (display_type IS NULL OR display_type = '' OR display_type = 'product')`,
|
||||
soID).Scan(&total, &invoiced)
|
||||
return total - invoiced, nil
|
||||
})
|
||||
}
|
||||
|
||||
// initSaleOrderLineExtension extends sale.order.line with additional fields and methods.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order_line.py (additional fields)
|
||||
func initSaleOrderLineExtension() {
|
||||
sol := orm.ExtendModel("sale.order.line")
|
||||
|
||||
sol.AddFields(
|
||||
orm.Many2one("salesman_id", "res.users", orm.FieldOpts{String: "Salesperson"}),
|
||||
orm.Selection("invoice_status", []orm.SelectionItem{
|
||||
{Value: "upselling", Label: "Upselling Opportunity"},
|
||||
{Value: "invoiced", Label: "Fully Invoiced"},
|
||||
{Value: "to invoice", Label: "To Invoice"},
|
||||
{Value: "no", Label: "Nothing to Invoice"},
|
||||
}, orm.FieldOpts{String: "Invoice Status", Compute: "_compute_invoice_status_line"}),
|
||||
orm.Boolean("is_downpayment", orm.FieldOpts{String: "Is a down payment"}),
|
||||
orm.Many2one("currency_id", "res.currency", orm.FieldOpts{String: "Currency"}),
|
||||
orm.Float("product_packaging_qty", orm.FieldOpts{String: "Packaging Quantity"}),
|
||||
orm.Char("product_type", orm.FieldOpts{String: "Product Type"}),
|
||||
orm.Boolean("product_updatable", orm.FieldOpts{String: "Can Edit Product", Default: true}),
|
||||
orm.Float("price_reduce", orm.FieldOpts{
|
||||
String: "Price Reduce", Compute: "_compute_price_reduce",
|
||||
}),
|
||||
orm.Float("price_reduce_taxinc", orm.FieldOpts{
|
||||
String: "Price Reduce Tax inc", Compute: "_compute_price_reduce_taxinc",
|
||||
}),
|
||||
orm.Float("price_reduce_taxexcl", orm.FieldOpts{
|
||||
String: "Price Reduce Tax excl", Compute: "_compute_price_reduce_taxexcl",
|
||||
}),
|
||||
orm.Monetary("untaxed_amount_to_invoice", orm.FieldOpts{
|
||||
String: "Untaxed Amount To Invoice", Compute: "_compute_untaxed_amount_to_invoice",
|
||||
CurrencyField: "currency_id",
|
||||
}),
|
||||
orm.Monetary("untaxed_amount_invoiced", orm.FieldOpts{
|
||||
String: "Untaxed Amount Invoiced", Compute: "_compute_untaxed_amount_invoiced",
|
||||
CurrencyField: "currency_id",
|
||||
}),
|
||||
)
|
||||
|
||||
// _compute_invoice_status_line: Per-line invoice status.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order_line.py SaleOrderLine._compute_invoice_status()
|
||||
sol.RegisterCompute("invoice_status", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
lineID := rs.IDs()[0]
|
||||
|
||||
var qty, qtyInvoiced float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(product_uom_qty, 0), COALESCE(qty_invoiced, 0)
|
||||
FROM sale_order_line WHERE id = $1`, lineID,
|
||||
).Scan(&qty, &qtyInvoiced)
|
||||
|
||||
status := "no"
|
||||
if qty > 0 {
|
||||
if qtyInvoiced >= qty {
|
||||
status = "invoiced"
|
||||
} else {
|
||||
status = "to invoice"
|
||||
}
|
||||
}
|
||||
return orm.Values{"invoice_status": status}, nil
|
||||
})
|
||||
|
||||
// _compute_price_reduce: Unit price after discount.
|
||||
sol.RegisterCompute("price_reduce", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
lineID := rs.IDs()[0]
|
||||
var price, discount float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(price_unit, 0), COALESCE(discount, 0) FROM sale_order_line WHERE id = $1`,
|
||||
lineID).Scan(&price, &discount)
|
||||
return orm.Values{"price_reduce": price * (1 - discount/100)}, nil
|
||||
})
|
||||
|
||||
// _compute_price_reduce_taxinc: Reduced price including taxes.
|
||||
sol.RegisterCompute("price_reduce_taxinc", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
lineID := rs.IDs()[0]
|
||||
var priceTotal, qty float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(price_total, 0), COALESCE(product_uom_qty, 1) FROM sale_order_line WHERE id = $1`,
|
||||
lineID).Scan(&priceTotal, &qty)
|
||||
val := float64(0)
|
||||
if qty > 0 {
|
||||
val = priceTotal / qty
|
||||
}
|
||||
return orm.Values{"price_reduce_taxinc": val}, nil
|
||||
})
|
||||
|
||||
// _compute_price_reduce_taxexcl: Reduced price excluding taxes.
|
||||
sol.RegisterCompute("price_reduce_taxexcl", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
lineID := rs.IDs()[0]
|
||||
var priceSubtotal, qty float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(price_subtotal, 0), COALESCE(product_uom_qty, 1) FROM sale_order_line WHERE id = $1`,
|
||||
lineID).Scan(&priceSubtotal, &qty)
|
||||
val := float64(0)
|
||||
if qty > 0 {
|
||||
val = priceSubtotal / qty
|
||||
}
|
||||
return orm.Values{"price_reduce_taxexcl": val}, nil
|
||||
})
|
||||
|
||||
// _compute_untaxed_amount_to_invoice
|
||||
sol.RegisterCompute("untaxed_amount_to_invoice", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
lineID := rs.IDs()[0]
|
||||
var qty, qtyInvoiced, price, discount float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(product_uom_qty, 0), COALESCE(qty_invoiced, 0),
|
||||
COALESCE(price_unit, 0), COALESCE(discount, 0)
|
||||
FROM sale_order_line WHERE id = $1`, lineID,
|
||||
).Scan(&qty, &qtyInvoiced, &price, &discount)
|
||||
remaining := qty - qtyInvoiced
|
||||
if remaining < 0 {
|
||||
remaining = 0
|
||||
}
|
||||
return orm.Values{"untaxed_amount_to_invoice": remaining * price * (1 - discount/100)}, nil
|
||||
})
|
||||
|
||||
// _compute_untaxed_amount_invoiced
|
||||
sol.RegisterCompute("untaxed_amount_invoiced", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
lineID := rs.IDs()[0]
|
||||
var qtyInvoiced, price, discount float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(qty_invoiced, 0), COALESCE(price_unit, 0), COALESCE(discount, 0)
|
||||
FROM sale_order_line WHERE id = $1`, lineID,
|
||||
).Scan(&qtyInvoiced, &price, &discount)
|
||||
return orm.Values{"untaxed_amount_invoiced": qtyInvoiced * price * (1 - discount/100)}, nil
|
||||
})
|
||||
|
||||
// _compute_qty_invoiced: Compute invoiced quantity from linked invoice lines.
|
||||
// Mirrors: odoo/addons/sale/models/sale_order_line.py SaleOrderLine._compute_qty_invoiced()
|
||||
sol.RegisterCompute("qty_invoiced", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
lineID := rs.IDs()[0]
|
||||
var qtyInvoiced float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(qty_invoiced, 0) FROM sale_order_line WHERE id = $1`, lineID,
|
||||
).Scan(&qtyInvoiced)
|
||||
return orm.Values{"qty_invoiced": qtyInvoiced}, nil
|
||||
})
|
||||
|
||||
// _compute_qty_to_invoice: Quantity to invoice = qty - qty_invoiced (if delivered policy: qty_delivered - qty_invoiced).
|
||||
// Mirrors: odoo/addons/sale/models/sale_order_line.py SaleOrderLine._compute_qty_to_invoice()
|
||||
sol.RegisterCompute("qty_to_invoice", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
lineID := rs.IDs()[0]
|
||||
var qty, qtyInvoiced float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(product_uom_qty, 0), COALESCE(qty_invoiced, 0)
|
||||
FROM sale_order_line WHERE id = $1`, lineID,
|
||||
).Scan(&qty, &qtyInvoiced)
|
||||
toInvoice := qty - qtyInvoiced
|
||||
if toInvoice < 0 {
|
||||
toInvoice = 0
|
||||
}
|
||||
return orm.Values{"qty_to_invoice": toInvoice}, nil
|
||||
})
|
||||
}
|
||||
|
||||
// initSaleOrderDiscount registers the sale.order.discount wizard.
|
||||
// Mirrors: odoo/addons/sale/wizard/sale_order_discount.py
|
||||
func initSaleOrderDiscount() {
|
||||
m := orm.NewModel("sale.order.discount", orm.ModelOpts{
|
||||
Description: "Sale Order Discount Wizard",
|
||||
Type: orm.ModelTransient,
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Float("discount", orm.FieldOpts{String: "Discount (%)", Required: true}),
|
||||
orm.Many2one("sale_order_id", "sale.order", orm.FieldOpts{String: "Sale Order"}),
|
||||
)
|
||||
|
||||
// action_apply_discount: Apply the discount to all lines of the SO.
|
||||
// Mirrors: odoo/addons/sale/wizard/sale_order_discount.py action_apply_discount()
|
||||
m.RegisterMethod("action_apply_discount", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
wizID := rs.IDs()[0]
|
||||
var discount float64
|
||||
var orderID int64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(discount, 0), COALESCE(sale_order_id, 0)
|
||||
FROM sale_order_discount WHERE id = $1`, wizID,
|
||||
).Scan(&discount, &orderID)
|
||||
|
||||
if orderID == 0 {
|
||||
return nil, fmt.Errorf("sale_discount: no sale order linked")
|
||||
}
|
||||
|
||||
_, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE sale_order_line SET discount = $1
|
||||
WHERE order_id = $2
|
||||
AND (display_type IS NULL OR display_type = '' OR display_type = 'product')`,
|
||||
discount, orderID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_discount: apply discount: %w", err)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
})
|
||||
}
|
||||
|
||||
// initResPartnerSaleExtension2 adds additional sale-specific computed fields to res.partner.
|
||||
// Mirrors: odoo/addons/sale/models/res_partner.py (additional fields)
|
||||
func initResPartnerSaleExtension2() {
|
||||
partner := orm.ExtendModel("res.partner")
|
||||
|
||||
partner.AddFields(
|
||||
orm.Monetary("sale_order_total", orm.FieldOpts{
|
||||
String: "Total Sales", Compute: "_compute_sale_order_total", CurrencyField: "currency_id",
|
||||
}),
|
||||
)
|
||||
|
||||
partner.RegisterCompute("sale_order_total", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
partnerID := rs.IDs()[0]
|
||||
var total float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(SUM(amount_total::float8), 0) FROM sale_order
|
||||
WHERE partner_id = $1 AND state IN ('sale', 'done')`, partnerID,
|
||||
).Scan(&total)
|
||||
return orm.Values{"sale_order_total": total}, nil
|
||||
})
|
||||
}
|
||||
421
addons/sale/models/sale_report.go
Normal file
421
addons/sale/models/sale_report.go
Normal file
@@ -0,0 +1,421 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// initSaleReport registers sale.report — a transient model for sales analysis.
|
||||
// Mirrors: odoo/addons/sale/report/sale_report.py
|
||||
//
|
||||
// class SaleReport(models.Model):
|
||||
// _name = 'sale.report'
|
||||
// _description = 'Sales Analysis Report'
|
||||
// _auto = False
|
||||
func initSaleReport() {
|
||||
m := orm.NewModel("sale.report", orm.ModelOpts{
|
||||
Description: "Sales Analysis Report",
|
||||
Type: orm.ModelTransient,
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Date("date_from", orm.FieldOpts{String: "Start Date"}),
|
||||
orm.Date("date_to", orm.FieldOpts{String: "End Date"}),
|
||||
orm.Many2one("partner_id", "res.partner", orm.FieldOpts{String: "Customer"}),
|
||||
orm.Many2one("product_id", "product.product", orm.FieldOpts{String: "Product"}),
|
||||
orm.Many2one("product_tmpl_id", "product.template", orm.FieldOpts{String: "Product Template"}),
|
||||
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
|
||||
orm.Many2one("user_id", "res.users", orm.FieldOpts{String: "Salesperson"}),
|
||||
orm.Many2one("team_id", "crm.team", orm.FieldOpts{String: "Sales Team"}),
|
||||
orm.Many2one("categ_id", "product.category", orm.FieldOpts{String: "Product Category"}),
|
||||
orm.Selection("state", []orm.SelectionItem{
|
||||
{Value: "draft", Label: "Quotation"},
|
||||
{Value: "sale", Label: "Sales Order"},
|
||||
{Value: "done", Label: "Done"},
|
||||
{Value: "cancel", Label: "Cancelled"},
|
||||
}, orm.FieldOpts{String: "Status"}),
|
||||
)
|
||||
|
||||
// get_sales_data: Retrieve aggregated sales data for dashboards and reports.
|
||||
// Mirrors: odoo/addons/sale/report/sale_report.py (the SQL view query logic)
|
||||
// Returns: { months, top_products, top_customers, summary }
|
||||
m.RegisterMethod("get_sales_data", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
// ── Revenue by month (last 12 months) ──
|
||||
monthRows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT date_trunc('month', so.date_order) AS month,
|
||||
COUNT(DISTINCT so.id) AS order_count,
|
||||
COALESCE(SUM(so.amount_total::float8), 0) AS revenue,
|
||||
COALESCE(AVG(so.amount_total::float8), 0) AS avg_order
|
||||
FROM sale_order so
|
||||
WHERE so.state IN ('sale', 'done')
|
||||
GROUP BY month
|
||||
ORDER BY month DESC
|
||||
LIMIT 12`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_report: monthly revenue query: %w", err)
|
||||
}
|
||||
defer monthRows.Close()
|
||||
|
||||
var months []map[string]interface{}
|
||||
for monthRows.Next() {
|
||||
var month time.Time
|
||||
var cnt int64
|
||||
var rev, avg float64
|
||||
if err := monthRows.Scan(&month, &cnt, &rev, &avg); err != nil {
|
||||
continue
|
||||
}
|
||||
months = append(months, map[string]interface{}{
|
||||
"month": month.Format("2006-01"),
|
||||
"orders": cnt,
|
||||
"revenue": rev,
|
||||
"avg_order": avg,
|
||||
})
|
||||
}
|
||||
|
||||
// ── Top 10 products by revenue ──
|
||||
prodRows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT pt.name,
|
||||
SUM(sol.product_uom_qty) AS qty,
|
||||
COALESCE(SUM(sol.price_subtotal::float8), 0) AS revenue,
|
||||
COUNT(DISTINCT sol.order_id) AS order_count
|
||||
FROM sale_order_line sol
|
||||
JOIN sale_order so ON so.id = sol.order_id AND so.state IN ('sale', 'done')
|
||||
JOIN product_product pp ON pp.id = sol.product_id
|
||||
JOIN product_template pt ON pt.id = pp.product_tmpl_id
|
||||
WHERE sol.product_id IS NOT NULL
|
||||
GROUP BY pt.name
|
||||
ORDER BY revenue DESC
|
||||
LIMIT 10`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_report: top products query: %w", err)
|
||||
}
|
||||
defer prodRows.Close()
|
||||
|
||||
var products []map[string]interface{}
|
||||
for prodRows.Next() {
|
||||
var name string
|
||||
var qty, rev float64
|
||||
var orderCnt int64
|
||||
if err := prodRows.Scan(&name, &qty, &rev, &orderCnt); err != nil {
|
||||
continue
|
||||
}
|
||||
products = append(products, map[string]interface{}{
|
||||
"product": name,
|
||||
"qty": qty,
|
||||
"revenue": rev,
|
||||
"orders": orderCnt,
|
||||
})
|
||||
}
|
||||
|
||||
// ── Top 10 customers by revenue ──
|
||||
custRows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT p.name,
|
||||
COUNT(DISTINCT so.id) AS orders,
|
||||
COALESCE(SUM(so.amount_total::float8), 0) AS revenue,
|
||||
COALESCE(AVG(so.amount_total::float8), 0) AS avg_order
|
||||
FROM sale_order so
|
||||
JOIN res_partner p ON p.id = so.partner_id
|
||||
WHERE so.state IN ('sale', 'done')
|
||||
GROUP BY p.id, p.name
|
||||
ORDER BY revenue DESC
|
||||
LIMIT 10`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_report: top customers query: %w", err)
|
||||
}
|
||||
defer custRows.Close()
|
||||
|
||||
var customers []map[string]interface{}
|
||||
for custRows.Next() {
|
||||
var name string
|
||||
var cnt int64
|
||||
var rev, avg float64
|
||||
if err := custRows.Scan(&name, &cnt, &rev, &avg); err != nil {
|
||||
continue
|
||||
}
|
||||
customers = append(customers, map[string]interface{}{
|
||||
"customer": name,
|
||||
"orders": cnt,
|
||||
"revenue": rev,
|
||||
"avg_order": avg,
|
||||
})
|
||||
}
|
||||
|
||||
// ── Summary totals ──
|
||||
var totalOrders int64
|
||||
var totalRevenue, avgOrderValue float64
|
||||
env.Tx().QueryRow(env.Ctx(), `
|
||||
SELECT COUNT(*), COALESCE(SUM(amount_total::float8), 0), COALESCE(AVG(amount_total::float8), 0)
|
||||
FROM sale_order WHERE state IN ('sale', 'done')
|
||||
`).Scan(&totalOrders, &totalRevenue, &avgOrderValue)
|
||||
|
||||
return map[string]interface{}{
|
||||
"months": months,
|
||||
"top_products": products,
|
||||
"top_customers": customers,
|
||||
"summary": map[string]interface{}{
|
||||
"total_orders": totalOrders,
|
||||
"total_revenue": totalRevenue,
|
||||
"avg_order_value": avgOrderValue,
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
|
||||
// get_sales_by_salesperson: Breakdown by salesperson.
|
||||
// Mirrors: odoo/addons/sale/report/sale_report.py (grouped by user_id)
|
||||
m.RegisterMethod("get_sales_by_salesperson", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
rows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT COALESCE(rp.name, 'Unassigned') AS salesperson,
|
||||
COUNT(DISTINCT so.id) AS orders,
|
||||
COALESCE(SUM(so.amount_total::float8), 0) AS revenue,
|
||||
COALESCE(AVG(so.amount_total::float8), 0) AS avg_order
|
||||
FROM sale_order so
|
||||
LEFT JOIN res_users ru ON ru.id = so.user_id
|
||||
LEFT JOIN res_partner rp ON rp.id = ru.partner_id
|
||||
WHERE so.state IN ('sale', 'done')
|
||||
GROUP BY rp.name
|
||||
ORDER BY revenue DESC`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_report: salesperson query: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []map[string]interface{}
|
||||
for rows.Next() {
|
||||
var name string
|
||||
var orders int64
|
||||
var rev, avg float64
|
||||
if err := rows.Scan(&name, &orders, &rev, &avg); err != nil {
|
||||
continue
|
||||
}
|
||||
results = append(results, map[string]interface{}{
|
||||
"salesperson": name,
|
||||
"orders": orders,
|
||||
"revenue": rev,
|
||||
"avg_order": avg,
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
})
|
||||
|
||||
// get_sales_by_category: Breakdown by product category.
|
||||
// Mirrors: odoo/addons/sale/report/sale_report.py (grouped by categ_id)
|
||||
m.RegisterMethod("get_sales_by_category", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
rows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT COALESCE(pc.name, 'Uncategorized') AS category,
|
||||
COUNT(DISTINCT sol.order_id) AS orders,
|
||||
SUM(sol.product_uom_qty) AS qty,
|
||||
COALESCE(SUM(sol.price_subtotal::float8), 0) AS revenue
|
||||
FROM sale_order_line sol
|
||||
JOIN sale_order so ON so.id = sol.order_id AND so.state IN ('sale', 'done')
|
||||
LEFT JOIN product_product pp ON pp.id = sol.product_id
|
||||
LEFT JOIN product_template pt ON pt.id = pp.product_tmpl_id
|
||||
LEFT JOIN product_category pc ON pc.id = pt.categ_id
|
||||
WHERE sol.product_id IS NOT NULL
|
||||
GROUP BY pc.name
|
||||
ORDER BY revenue DESC`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_report: category query: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []map[string]interface{}
|
||||
for rows.Next() {
|
||||
var name string
|
||||
var orders int64
|
||||
var qty, rev float64
|
||||
if err := rows.Scan(&name, &orders, &qty, &rev); err != nil {
|
||||
continue
|
||||
}
|
||||
results = append(results, map[string]interface{}{
|
||||
"category": name,
|
||||
"orders": orders,
|
||||
"qty": qty,
|
||||
"revenue": rev,
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
})
|
||||
|
||||
// get_invoice_analysis: Invoice status analysis.
|
||||
// Mirrors: odoo/addons/sale/report/sale_report.py (invoice_status grouping)
|
||||
m.RegisterMethod("get_invoice_analysis", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
rows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT COALESCE(invoice_status, 'no') AS status,
|
||||
COUNT(*) AS count,
|
||||
COALESCE(SUM(amount_total::float8), 0) AS revenue
|
||||
FROM sale_order
|
||||
WHERE state IN ('sale', 'done')
|
||||
GROUP BY invoice_status
|
||||
ORDER BY revenue DESC`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_report: invoice analysis query: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []map[string]interface{}
|
||||
for rows.Next() {
|
||||
var status string
|
||||
var count int64
|
||||
var rev float64
|
||||
if err := rows.Scan(&status, &count, &rev); err != nil {
|
||||
continue
|
||||
}
|
||||
results = append(results, map[string]interface{}{
|
||||
"status": status,
|
||||
"count": count,
|
||||
"revenue": rev,
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
})
|
||||
}
|
||||
|
||||
// initSaleOrderWarnMsg registers the sale.order.onchange.warning transient model.
|
||||
// Mirrors: odoo/addons/sale/wizard/sale_order_cancel.py (cancel warning dialog)
|
||||
func initSaleOrderWarnMsg() {
|
||||
m := orm.NewModel("sale.order.cancel", orm.ModelOpts{
|
||||
Description: "Sale Order Cancel",
|
||||
Type: orm.ModelTransient,
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Many2one("order_id", "sale.order", orm.FieldOpts{String: "Sale Order"}),
|
||||
orm.Text("display_name", orm.FieldOpts{String: "Warning"}),
|
||||
)
|
||||
|
||||
// action_cancel: Confirm the cancellation of the sale order.
|
||||
m.RegisterMethod("action_cancel", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
wizID := rs.IDs()[0]
|
||||
var orderID int64
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT order_id FROM sale_order_cancel WHERE id = $1`, wizID).Scan(&orderID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_cancel: read wizard %d: %w", wizID, err)
|
||||
}
|
||||
soRS := env.Model("sale.order").Browse(orderID)
|
||||
soModel := orm.Registry.Get("sale.order")
|
||||
if fn, ok := soModel.Methods["action_cancel"]; ok {
|
||||
return fn(soRS)
|
||||
}
|
||||
return nil, fmt.Errorf("sale_cancel: action_cancel method not found")
|
||||
})
|
||||
}
|
||||
|
||||
// initSaleAdvancePaymentWizard registers the sale.advance.payment.inv wizard.
|
||||
// Mirrors: odoo/addons/sale/wizard/sale_make_invoice_advance.py
|
||||
//
|
||||
// class SaleAdvancePaymentInv(models.TransientModel):
|
||||
// _name = 'sale.advance.payment.inv'
|
||||
// _description = 'Sales Advance Payment Invoice'
|
||||
func initSaleAdvancePaymentWizard() {
|
||||
m := orm.NewModel("sale.advance.payment.inv", orm.ModelOpts{
|
||||
Description: "Sales Advance Payment Invoice",
|
||||
Type: orm.ModelTransient,
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Selection("advance_payment_method", []orm.SelectionItem{
|
||||
{Value: "delivered", Label: "Regular invoice"},
|
||||
{Value: "percentage", Label: "Down payment (percentage)"},
|
||||
{Value: "fixed", Label: "Down payment (fixed amount)"},
|
||||
}, orm.FieldOpts{String: "Create Invoice", Default: "delivered", Required: true}),
|
||||
orm.Float("amount", orm.FieldOpts{String: "Down Payment Amount", Default: 0}),
|
||||
orm.Boolean("has_down_payments", orm.FieldOpts{String: "Has down payments"}),
|
||||
orm.Boolean("deduct_down_payments", orm.FieldOpts{String: "Deduct down payments", Default: true}),
|
||||
orm.Many2one("product_id", "product.product", orm.FieldOpts{String: "Down Payment Product"}),
|
||||
orm.Many2one("currency_id", "res.currency", orm.FieldOpts{String: "Currency"}),
|
||||
orm.Float("fixed_amount", orm.FieldOpts{String: "Fixed Amount"}),
|
||||
orm.Integer("count", orm.FieldOpts{String: "Order Count"}),
|
||||
orm.Many2many("sale_order_ids", "sale.order", orm.FieldOpts{String: "Sale Orders"}),
|
||||
)
|
||||
|
||||
// create_invoices: Generate invoices based on the wizard settings.
|
||||
// Mirrors: odoo/addons/sale/wizard/sale_make_invoice_advance.py create_invoices()
|
||||
m.RegisterMethod("create_invoices", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
wizID := rs.IDs()[0]
|
||||
|
||||
// Read wizard settings
|
||||
var method string
|
||||
var amount, fixedAmount float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(advance_payment_method, 'delivered'),
|
||||
COALESCE(amount, 0), COALESCE(fixed_amount, 0)
|
||||
FROM sale_advance_payment_inv WHERE id = $1`, wizID,
|
||||
).Scan(&method, &amount, &fixedAmount)
|
||||
|
||||
// Get linked sale order IDs from the M2M or from context
|
||||
soRows, err := env.Tx().Query(env.Ctx(),
|
||||
`SELECT sale_order_id FROM sale_order_sale_advance_payment_inv_rel
|
||||
WHERE sale_advance_payment_inv_id = $1`, wizID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_advance_wiz: read SO IDs: %w", err)
|
||||
}
|
||||
defer soRows.Close()
|
||||
|
||||
var soIDs []int64
|
||||
for soRows.Next() {
|
||||
var id int64
|
||||
soRows.Scan(&id)
|
||||
soIDs = append(soIDs, id)
|
||||
}
|
||||
|
||||
if len(soIDs) == 0 {
|
||||
return nil, fmt.Errorf("sale_advance_wiz: no sale orders linked")
|
||||
}
|
||||
|
||||
soModel := orm.Registry.Get("sale.order")
|
||||
|
||||
switch method {
|
||||
case "delivered":
|
||||
// Create regular invoices for all linked SOs
|
||||
soRS := env.Model("sale.order").Browse(soIDs...)
|
||||
if fn, ok := soModel.Methods["create_invoices"]; ok {
|
||||
return fn(soRS)
|
||||
}
|
||||
return nil, fmt.Errorf("sale_advance_wiz: create_invoices method not found")
|
||||
case "percentage":
|
||||
// Create down payment invoices
|
||||
for _, soID := range soIDs {
|
||||
soRS := env.Model("sale.order").Browse(soID)
|
||||
if fn, ok := soModel.Methods["action_create_down_payment"]; ok {
|
||||
if _, err := fn(soRS, amount); err != nil {
|
||||
return nil, fmt.Errorf("sale_advance_wiz: down payment for SO %d: %w", soID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
case "fixed":
|
||||
// Create fixed-amount down payment (treat as percentage by computing %)
|
||||
for _, soID := range soIDs {
|
||||
var total float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(amount_total::float8, 0) FROM sale_order WHERE id = $1`, soID).Scan(&total)
|
||||
pct := float64(0)
|
||||
if total > 0 {
|
||||
pct = fixedAmount / total * 100
|
||||
}
|
||||
soRS := env.Model("sale.order").Browse(soID)
|
||||
if fn, ok := soModel.Methods["action_create_down_payment"]; ok {
|
||||
if _, err := fn(soRS, pct); err != nil {
|
||||
return nil, fmt.Errorf("sale_advance_wiz: fixed down payment for SO %d: %w", soID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("sale_advance_wiz: unknown method %q", method)
|
||||
})
|
||||
}
|
||||
292
addons/sale/models/sale_template.go
Normal file
292
addons/sale/models/sale_template.go
Normal file
@@ -0,0 +1,292 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// initSaleOrderTemplate registers sale.order.template and sale.order.template.line.
|
||||
// Mirrors: odoo/addons/sale_management/models/sale_order_template.py
|
||||
//
|
||||
// class SaleOrderTemplate(models.Model):
|
||||
// _name = 'sale.order.template'
|
||||
// _description = 'Quotation Template'
|
||||
func initSaleOrderTemplate() {
|
||||
m := orm.NewModel("sale.order.template", orm.ModelOpts{
|
||||
Description: "Quotation Template",
|
||||
Order: "sequence, id",
|
||||
})
|
||||
|
||||
// -- Identity --
|
||||
m.AddFields(
|
||||
orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}),
|
||||
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
|
||||
orm.Integer("number_of_days", orm.FieldOpts{String: "Validity (Days)", Default: 30}),
|
||||
orm.Text("note", orm.FieldOpts{String: "Terms and Conditions", Translate: true}),
|
||||
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
|
||||
orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 10}),
|
||||
orm.Many2one("mail_template_id", "mail.template", orm.FieldOpts{String: "Confirmation Mail"}),
|
||||
)
|
||||
|
||||
// -- Lines --
|
||||
m.AddFields(
|
||||
orm.One2many("sale_order_template_line_ids", "sale.order.template.line", "sale_order_template_id", orm.FieldOpts{
|
||||
String: "Lines",
|
||||
}),
|
||||
orm.One2many("sale_order_template_option_ids", "sale.order.template.option", "sale_order_template_id", orm.FieldOpts{
|
||||
String: "Optional Products",
|
||||
}),
|
||||
)
|
||||
|
||||
// -- Computed: line_count --
|
||||
m.AddFields(
|
||||
orm.Integer("line_count", orm.FieldOpts{
|
||||
String: "Line Count", Compute: "_compute_line_count",
|
||||
}),
|
||||
)
|
||||
m.RegisterCompute("line_count", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
templateID := rs.IDs()[0]
|
||||
var count int
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COUNT(*) FROM sale_order_template_line WHERE sale_order_template_id = $1`,
|
||||
templateID).Scan(&count)
|
||||
if err != nil {
|
||||
count = 0
|
||||
}
|
||||
return orm.Values{"line_count": count}, nil
|
||||
})
|
||||
|
||||
// action_apply_to_order: Apply this template to a sale order.
|
||||
// Mirrors: odoo/addons/sale_management/models/sale_order.py SaleOrder._onchange_sale_order_template_id()
|
||||
// Copies template lines into the SO as order lines, and copies the template note.
|
||||
m.RegisterMethod("action_apply_to_order", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
if len(args) < 1 {
|
||||
return nil, fmt.Errorf("order_id required")
|
||||
}
|
||||
env := rs.Env()
|
||||
templateID := rs.IDs()[0]
|
||||
orderID, _ := args[0].(float64)
|
||||
|
||||
// Read template lines
|
||||
rows, err := env.Tx().Query(env.Ctx(),
|
||||
`SELECT name, product_id, product_uom_qty, price_unit, discount, sequence
|
||||
FROM sale_order_template_line
|
||||
WHERE sale_order_template_id = $1 ORDER BY sequence`, templateID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sale_template: read lines: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
lineRS := env.Model("sale.order.line")
|
||||
for rows.Next() {
|
||||
var name string
|
||||
var prodID *int64
|
||||
var qty, price, disc float64
|
||||
var seq int
|
||||
if err := rows.Scan(&name, &prodID, &qty, &price, &disc, &seq); err != nil {
|
||||
return nil, fmt.Errorf("sale_template: scan line: %w", err)
|
||||
}
|
||||
vals := orm.Values{
|
||||
"order_id": int64(orderID),
|
||||
"name": name,
|
||||
"product_uom_qty": qty,
|
||||
"price_unit": price,
|
||||
"discount": disc,
|
||||
"sequence": seq,
|
||||
}
|
||||
if prodID != nil {
|
||||
vals["product_id"] = *prodID
|
||||
}
|
||||
if _, err := lineRS.Create(vals); err != nil {
|
||||
return nil, fmt.Errorf("sale_template: create SO line: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Copy template note to the SO
|
||||
var note *string
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT note FROM sale_order_template WHERE id = $1`, templateID).Scan(¬e)
|
||||
if note != nil {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE sale_order SET note = $1 WHERE id = $2`, *note, int64(orderID))
|
||||
}
|
||||
|
||||
// Copy validity_date from number_of_days
|
||||
var numDays int
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(number_of_days, 0) FROM sale_order_template WHERE id = $1`,
|
||||
templateID).Scan(&numDays)
|
||||
if numDays > 0 {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE sale_order SET validity_date = CURRENT_DATE + $1 WHERE id = $2`,
|
||||
numDays, int64(orderID))
|
||||
}
|
||||
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_open_template: Return an action to open this template's form view.
|
||||
// Mirrors: odoo/addons/sale_management/models/sale_order_template.py
|
||||
m.RegisterMethod("action_open_template", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
templateID := rs.IDs()[0]
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"res_model": "sale.order.template",
|
||||
"res_id": templateID,
|
||||
"view_mode": "form",
|
||||
"views": [][]interface{}{{nil, "form"}},
|
||||
"target": "current",
|
||||
}, nil
|
||||
})
|
||||
}
|
||||
|
||||
// initSaleOrderTemplateLine registers sale.order.template.line.
|
||||
// Mirrors: odoo/addons/sale_management/models/sale_order_template.py SaleOrderTemplateLine
|
||||
func initSaleOrderTemplateLine() {
|
||||
m := orm.NewModel("sale.order.template.line", orm.ModelOpts{
|
||||
Description: "Quotation Template Line",
|
||||
Order: "sequence, id",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Many2one("sale_order_template_id", "sale.order.template", orm.FieldOpts{
|
||||
String: "Template", Required: true, OnDelete: orm.OnDeleteCascade, Index: true,
|
||||
}),
|
||||
orm.Many2one("product_id", "product.product", orm.FieldOpts{String: "Product"}),
|
||||
orm.Char("name", orm.FieldOpts{String: "Description", Required: true, Translate: true}),
|
||||
orm.Float("product_uom_qty", orm.FieldOpts{String: "Quantity", Default: 1}),
|
||||
orm.Many2one("product_uom_id", "uom.uom", orm.FieldOpts{String: "Unit of Measure"}),
|
||||
orm.Float("price_unit", orm.FieldOpts{String: "Unit Price"}),
|
||||
orm.Float("discount", orm.FieldOpts{String: "Discount (%)"}),
|
||||
orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 10}),
|
||||
orm.Selection("display_type", []orm.SelectionItem{
|
||||
{Value: "line_section", Label: "Section"},
|
||||
{Value: "line_note", Label: "Note"},
|
||||
}, orm.FieldOpts{String: "Display Type"}),
|
||||
orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
|
||||
)
|
||||
|
||||
// Onchange: product_id → name + price_unit
|
||||
// Mirrors: odoo/addons/sale_management/models/sale_order_template.py SaleOrderTemplateLine._compute_name()
|
||||
m.RegisterOnchange("product_id", func(env *orm.Environment, vals orm.Values) orm.Values {
|
||||
result := make(orm.Values)
|
||||
|
||||
var productID int64
|
||||
switch v := vals["product_id"].(type) {
|
||||
case int64:
|
||||
productID = v
|
||||
case float64:
|
||||
productID = int64(v)
|
||||
case map[string]interface{}:
|
||||
if id, ok := v["id"]; ok {
|
||||
switch n := id.(type) {
|
||||
case float64:
|
||||
productID = int64(n)
|
||||
case int64:
|
||||
productID = n
|
||||
}
|
||||
}
|
||||
}
|
||||
if productID <= 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
var name string
|
||||
var listPrice float64
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(pt.name, ''), COALESCE(pt.list_price, 0)
|
||||
FROM product_product pp
|
||||
JOIN product_template pt ON pt.id = pp.product_tmpl_id
|
||||
WHERE pp.id = $1`, productID,
|
||||
).Scan(&name, &listPrice)
|
||||
if err != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
result["name"] = name
|
||||
result["price_unit"] = listPrice
|
||||
return result
|
||||
})
|
||||
|
||||
// _compute_price_subtotal: qty * price * (1 - discount/100)
|
||||
m.AddFields(
|
||||
orm.Monetary("price_subtotal", orm.FieldOpts{
|
||||
String: "Subtotal", Compute: "_compute_price_subtotal", CurrencyField: "currency_id",
|
||||
}),
|
||||
)
|
||||
m.RegisterCompute("price_subtotal", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
lineID := rs.IDs()[0]
|
||||
var qty, price, discount float64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(product_uom_qty, 0), COALESCE(price_unit, 0), COALESCE(discount, 0)
|
||||
FROM sale_order_template_line WHERE id = $1`, lineID,
|
||||
).Scan(&qty, &price, &discount)
|
||||
return orm.Values{"price_subtotal": qty * price * (1 - discount/100)}, nil
|
||||
})
|
||||
}
|
||||
|
||||
// initSaleOrderTemplateOption registers sale.order.template.option.
|
||||
// Mirrors: odoo/addons/sale_management/models/sale_order_template.py SaleOrderTemplateOption
|
||||
func initSaleOrderTemplateOption() {
|
||||
m := orm.NewModel("sale.order.template.option", orm.ModelOpts{
|
||||
Description: "Quotation Template Option",
|
||||
Order: "sequence, id",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Many2one("sale_order_template_id", "sale.order.template", orm.FieldOpts{
|
||||
String: "Template", Required: true, OnDelete: orm.OnDeleteCascade, Index: true,
|
||||
}),
|
||||
orm.Many2one("product_id", "product.product", orm.FieldOpts{String: "Product", Required: true}),
|
||||
orm.Char("name", orm.FieldOpts{String: "Description", Required: true, Translate: true}),
|
||||
orm.Float("quantity", orm.FieldOpts{String: "Quantity", Default: 1}),
|
||||
orm.Many2one("uom_id", "uom.uom", orm.FieldOpts{String: "Unit of Measure"}),
|
||||
orm.Float("price_unit", orm.FieldOpts{String: "Unit Price"}),
|
||||
orm.Float("discount", orm.FieldOpts{String: "Discount (%)"}),
|
||||
orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 10}),
|
||||
)
|
||||
|
||||
// Onchange: product_id → name + price_unit
|
||||
m.RegisterOnchange("product_id", func(env *orm.Environment, vals orm.Values) orm.Values {
|
||||
result := make(orm.Values)
|
||||
|
||||
var productID int64
|
||||
switch v := vals["product_id"].(type) {
|
||||
case int64:
|
||||
productID = v
|
||||
case float64:
|
||||
productID = int64(v)
|
||||
case map[string]interface{}:
|
||||
if id, ok := v["id"]; ok {
|
||||
switch n := id.(type) {
|
||||
case float64:
|
||||
productID = int64(n)
|
||||
case int64:
|
||||
productID = n
|
||||
}
|
||||
}
|
||||
}
|
||||
if productID <= 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
var name string
|
||||
var listPrice float64
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(pt.name, ''), COALESCE(pt.list_price, 0)
|
||||
FROM product_product pp
|
||||
JOIN product_template pt ON pt.id = pp.product_tmpl_id
|
||||
WHERE pp.id = $1`, productID,
|
||||
).Scan(&name, &listPrice)
|
||||
if err != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
result["name"] = name
|
||||
result["price_unit"] = listPrice
|
||||
return result
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user