feat: Portal, Email Inbound, Discuss + module improvements
- Portal: /my/* routes, signup, password reset, portal user support - Email Inbound: IMAP polling (go-imap/v2), thread matching - Discuss: mail.channel, long-polling bus, DM, unread count - Cron: ir.cron runner (goroutine scheduler) - Bank Import, CSV/Excel Import - Automation (ir.actions.server) - Fetchmail service - HR Payroll model - Various fixes across account, sale, stock, purchase, crm, hr, project Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package models
|
||||
|
||||
import "odoo-go/pkg/orm"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// initCRMLead registers the crm.lead model.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py
|
||||
@@ -67,73 +72,210 @@ func initCRMLead() {
|
||||
orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
|
||||
)
|
||||
|
||||
// DefaultGet: set company_id from the session so that DB NOT NULL constraint is satisfied
|
||||
// Onchange: stage_id -> auto-update probability from stage.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _onchange_stage_id
|
||||
m.RegisterOnchange("stage_id", func(env *orm.Environment, vals orm.Values) orm.Values {
|
||||
result := make(orm.Values)
|
||||
stageID, ok := vals["stage_id"]
|
||||
if !ok || stageID == nil {
|
||||
return result
|
||||
}
|
||||
var sid float64
|
||||
switch v := stageID.(type) {
|
||||
case float64:
|
||||
sid = v
|
||||
case int64:
|
||||
sid = float64(v)
|
||||
case int:
|
||||
sid = float64(v)
|
||||
default:
|
||||
return result
|
||||
}
|
||||
if sid == 0 {
|
||||
return result
|
||||
}
|
||||
var probability float64
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(probability, 10) FROM crm_stage WHERE id = $1`, int64(sid),
|
||||
).Scan(&probability); err != nil {
|
||||
return result
|
||||
}
|
||||
result["probability"] = probability
|
||||
result["date_last_stage_update"] = time.Now().Format("2006-01-02 15:04:05")
|
||||
return result
|
||||
})
|
||||
|
||||
// DefaultGet: set company_id, user_id, team_id, type from session/defaults.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py default_get
|
||||
m.DefaultGet = func(env *orm.Environment, fields []string) orm.Values {
|
||||
vals := make(orm.Values)
|
||||
if env.CompanyID() > 0 {
|
||||
vals["company_id"] = env.CompanyID()
|
||||
}
|
||||
if env.UID() > 0 {
|
||||
vals["user_id"] = env.UID()
|
||||
}
|
||||
vals["type"] = "lead"
|
||||
// Try to find a default sales team for the user
|
||||
var teamID int64
|
||||
if env.UID() > 0 {
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT ct.id FROM crm_team ct
|
||||
JOIN crm_team_member ctm ON ctm.crm_team_id = ct.id
|
||||
WHERE ctm.user_id = $1 AND ct.active = true
|
||||
ORDER BY ct.sequence LIMIT 1`, env.UID()).Scan(&teamID); err != nil {
|
||||
// No team found for user — not an error, just no default
|
||||
teamID = 0
|
||||
}
|
||||
}
|
||||
if teamID > 0 {
|
||||
vals["team_id"] = teamID
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
// action_set_won: mark lead as won
|
||||
// action_set_won: mark lead as won, set date_closed, find won stage.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py action_set_won
|
||||
m.RegisterMethod("action_set_won", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
var wonStageID int64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT id FROM crm_stage WHERE is_won = true ORDER BY sequence LIMIT 1`).Scan(&wonStageID)
|
||||
|
||||
for _, id := range rs.IDs() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET state = 'won', probability = 100 WHERE id = $1`, id)
|
||||
var err error
|
||||
if wonStageID > 0 {
|
||||
_, err = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET state = 'won', probability = 100, automated_probability = 100,
|
||||
date_closed = NOW(), active = true, stage_id = $2
|
||||
WHERE id = $1`, id, wonStageID)
|
||||
} else {
|
||||
_, err = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET state = 'won', probability = 100, automated_probability = 100,
|
||||
date_closed = NOW(), active = true
|
||||
WHERE id = $1`, id)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("crm.lead: set_won %d: %w", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_set_lost: mark lead as lost
|
||||
// action_set_lost: mark lead as lost, accept lost_reason_id from kwargs.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py action_set_lost
|
||||
m.RegisterMethod("action_set_lost", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
// Extract lost_reason_id from kwargs if provided
|
||||
var lostReasonID int64
|
||||
if len(args) > 0 {
|
||||
if kwargs, ok := args[0].(map[string]interface{}); ok {
|
||||
if rid, ok := kwargs["lost_reason_id"]; ok {
|
||||
switch v := rid.(type) {
|
||||
case float64:
|
||||
lostReasonID = int64(v)
|
||||
case int64:
|
||||
lostReasonID = v
|
||||
case int:
|
||||
lostReasonID = int64(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, id := range rs.IDs() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET state = 'lost', probability = 0, active = false WHERE id = $1`, id)
|
||||
var err error
|
||||
if lostReasonID > 0 {
|
||||
_, err = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET state = 'lost', probability = 0, automated_probability = 0,
|
||||
active = false, date_closed = NOW(), lost_reason_id = $2
|
||||
WHERE id = $1`, id, lostReasonID)
|
||||
} else {
|
||||
_, err = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET state = 'lost', probability = 0, automated_probability = 0,
|
||||
active = false, date_closed = NOW()
|
||||
WHERE id = $1`, id)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("crm.lead: set_lost %d: %w", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// convert_to_opportunity: lead → opportunity
|
||||
// convert_to_opportunity: lead -> opportunity, set date_conversion.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _convert_opportunity_data
|
||||
m.RegisterMethod("convert_to_opportunity", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET type = 'opportunity' WHERE id = $1 AND type = 'lead'`, id)
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET type = 'opportunity', date_conversion = NOW(),
|
||||
date_open = COALESCE(date_open, NOW())
|
||||
WHERE id = $1 AND type = 'lead'`, id); err != nil {
|
||||
return nil, fmt.Errorf("crm.lead: convert_to_opportunity %d: %w", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// convert_opportunity: alias for convert_to_opportunity
|
||||
// convert_opportunity: convert lead to opportunity with optional partner/team assignment.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py convert_opportunity
|
||||
m.RegisterMethod("convert_opportunity", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
// Optional partner_id from args
|
||||
var partnerID int64
|
||||
if len(args) > 0 {
|
||||
if pid, ok := args[0].(float64); ok {
|
||||
partnerID = int64(pid)
|
||||
}
|
||||
}
|
||||
|
||||
for _, id := range rs.IDs() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET type = 'opportunity' WHERE id = $1`, id)
|
||||
if partnerID > 0 {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET type = 'opportunity', date_conversion = NOW(),
|
||||
date_open = COALESCE(date_open, NOW()), partner_id = $2
|
||||
WHERE id = $1`, id, partnerID)
|
||||
} else {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET type = 'opportunity', date_conversion = NOW(),
|
||||
date_open = COALESCE(date_open, NOW())
|
||||
WHERE id = $1`, id)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_set_won_rainbowman: set won stage + rainbow effect
|
||||
// action_set_won_rainbowman: set won + rainbow effect.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py action_set_won_rainbowman
|
||||
m.RegisterMethod("action_set_won_rainbowman", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
// Find Won stage
|
||||
// Find the first won stage
|
||||
var wonStageID int64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT id FROM crm_stage WHERE is_won = true LIMIT 1`).Scan(&wonStageID)
|
||||
if wonStageID == 0 {
|
||||
wonStageID = 4 // fallback
|
||||
}
|
||||
`SELECT id FROM crm_stage WHERE is_won = true ORDER BY sequence LIMIT 1`).Scan(&wonStageID)
|
||||
|
||||
for _, id := range rs.IDs() {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET stage_id = $1, probability = 100 WHERE id = $2`, wonStageID, id)
|
||||
if wonStageID > 0 {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET state = 'won', probability = 100, automated_probability = 100,
|
||||
date_closed = NOW(), active = true, stage_id = $2
|
||||
WHERE id = $1`, id, wonStageID)
|
||||
} else {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET state = 'won', probability = 100, automated_probability = 100,
|
||||
date_closed = NOW(), active = true
|
||||
WHERE id = $1`, id)
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"effect": map[string]interface{}{
|
||||
"type": "rainbow_man",
|
||||
"message": "Congrats, you won this opportunity!",
|
||||
"fadeout": "slow",
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
@@ -152,6 +294,11 @@ func initCRMStage() {
|
||||
orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 1}),
|
||||
orm.Boolean("fold", orm.FieldOpts{String: "Folded in Pipeline"}),
|
||||
orm.Boolean("is_won", orm.FieldOpts{String: "Is Won Stage"}),
|
||||
orm.Float("probability", orm.FieldOpts{
|
||||
String: "Probability (%)",
|
||||
Help: "Default probability when a lead enters this stage.",
|
||||
Default: float64(10),
|
||||
}),
|
||||
orm.Many2many("team_ids", "crm.team", orm.FieldOpts{String: "Sales Teams"}),
|
||||
orm.Text("requirements", orm.FieldOpts{String: "Requirements"}),
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
@@ -73,12 +74,14 @@ func initCrmAnalysis() {
|
||||
|
||||
// Win rate
|
||||
var total, won int64
|
||||
_ = env.Tx().QueryRow(env.Ctx(),
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COUNT(*), COALESCE(SUM(CASE WHEN s.is_won THEN 1 ELSE 0 END), 0)
|
||||
FROM crm_lead l
|
||||
JOIN crm_stage s ON s.id = l.stage_id
|
||||
WHERE l.type = 'opportunity'`,
|
||||
).Scan(&total, &won)
|
||||
).Scan(&total, &won); err != nil {
|
||||
log.Printf("warning: crm win rate query failed: %v", err)
|
||||
}
|
||||
|
||||
winRate := float64(0)
|
||||
if total > 0 {
|
||||
@@ -99,12 +102,14 @@ func initCrmAnalysis() {
|
||||
env := rs.Env()
|
||||
|
||||
var totalLeads, convertedLeads int64
|
||||
_ = env.Tx().QueryRow(env.Ctx(), `
|
||||
if err := env.Tx().QueryRow(env.Ctx(), `
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE type = 'lead'),
|
||||
COUNT(*) FILTER (WHERE type = 'opportunity' AND date_conversion IS NOT NULL)
|
||||
FROM crm_lead WHERE active = true`,
|
||||
).Scan(&totalLeads, &convertedLeads)
|
||||
).Scan(&totalLeads, &convertedLeads); err != nil {
|
||||
log.Printf("warning: crm conversion data query failed: %v", err)
|
||||
}
|
||||
|
||||
conversionRate := float64(0)
|
||||
if totalLeads > 0 {
|
||||
@@ -113,19 +118,23 @@ func initCrmAnalysis() {
|
||||
|
||||
// Average days to convert
|
||||
var avgDaysConvert float64
|
||||
_ = env.Tx().QueryRow(env.Ctx(), `
|
||||
if err := env.Tx().QueryRow(env.Ctx(), `
|
||||
SELECT COALESCE(AVG(EXTRACT(EPOCH FROM (date_conversion - create_date)) / 86400), 0)
|
||||
FROM crm_lead
|
||||
WHERE type = 'opportunity' AND date_conversion IS NOT NULL AND active = true`,
|
||||
).Scan(&avgDaysConvert)
|
||||
).Scan(&avgDaysConvert); err != nil {
|
||||
log.Printf("warning: crm avg days to convert query failed: %v", err)
|
||||
}
|
||||
|
||||
// Average days to close (won)
|
||||
var avgDaysClose float64
|
||||
_ = env.Tx().QueryRow(env.Ctx(), `
|
||||
if err := env.Tx().QueryRow(env.Ctx(), `
|
||||
SELECT COALESCE(AVG(EXTRACT(EPOCH FROM (date_closed - create_date)) / 86400), 0)
|
||||
FROM crm_lead
|
||||
WHERE state = 'won' AND date_closed IS NOT NULL`,
|
||||
).Scan(&avgDaysClose)
|
||||
).Scan(&avgDaysClose); err != nil {
|
||||
log.Printf("warning: crm avg days to close query failed: %v", err)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"total_leads": totalLeads,
|
||||
|
||||
@@ -2,6 +2,8 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
@@ -38,14 +40,32 @@ func initCRMLeadExtended() {
|
||||
}),
|
||||
|
||||
// ──── Tracking / timing fields ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py day_open, day_close
|
||||
orm.Integer("day_open", orm.FieldOpts{
|
||||
String: "Days to Assign",
|
||||
Help: "Number of days to assign this lead to a salesperson.",
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py date_open, day_open, day_close
|
||||
orm.Datetime("date_open", orm.FieldOpts{
|
||||
String: "Assignment Date",
|
||||
Help: "Date when the lead was first assigned to a salesperson.",
|
||||
}),
|
||||
orm.Integer("day_close", orm.FieldOpts{
|
||||
String: "Days to Close",
|
||||
Help: "Number of days to close this lead/opportunity.",
|
||||
orm.Float("day_open", orm.FieldOpts{
|
||||
String: "Days to Assign",
|
||||
Compute: "_compute_day_open",
|
||||
Help: "Number of days between creation and assignment.",
|
||||
}),
|
||||
orm.Float("day_close", orm.FieldOpts{
|
||||
String: "Days to Close",
|
||||
Compute: "_compute_day_close",
|
||||
Help: "Number of days between creation and closing.",
|
||||
}),
|
||||
|
||||
// ──── Kanban state ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py kanban_state (via mail.activity.mixin)
|
||||
orm.Selection("kanban_state", []orm.SelectionItem{
|
||||
{Value: "grey", Label: "No next activity planned"},
|
||||
{Value: "red", Label: "Next activity late"},
|
||||
{Value: "green", Label: "Next activity is planned"},
|
||||
}, orm.FieldOpts{
|
||||
String: "Kanban State",
|
||||
Compute: "_compute_kanban_state",
|
||||
Help: "Activity-based status indicator for kanban views.",
|
||||
}),
|
||||
|
||||
// ──── Additional contact/address fields ────
|
||||
@@ -76,6 +96,27 @@ func initCRMLeadExtended() {
|
||||
Help: "Second line of the street address.",
|
||||
}),
|
||||
|
||||
// ──── Computed timing fields ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _compute_days_in_stage
|
||||
orm.Float("days_in_stage", orm.FieldOpts{
|
||||
String: "Days in Current Stage",
|
||||
Compute: "_compute_days_in_stage",
|
||||
Help: "Number of days since the last stage change.",
|
||||
}),
|
||||
|
||||
// ──── Email scoring / contact address ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _compute_email_score
|
||||
orm.Char("email_domain_criterion", orm.FieldOpts{
|
||||
String: "Email Domain",
|
||||
Compute: "_compute_email_score",
|
||||
Help: "Domain part of the lead email (e.g. 'example.com').",
|
||||
}),
|
||||
orm.Text("contact_address_complete", orm.FieldOpts{
|
||||
String: "Contact Address",
|
||||
Compute: "_compute_contact_address",
|
||||
Help: "Full contact address assembled from partner data.",
|
||||
}),
|
||||
|
||||
// ──── Revenue fields ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py prorated_revenue
|
||||
orm.Monetary("prorated_revenue", orm.FieldOpts{
|
||||
@@ -135,35 +176,333 @@ func initCRMLeadExtended() {
|
||||
|
||||
var revenue float64
|
||||
var probability float64
|
||||
_ = env.Tx().QueryRow(env.Ctx(),
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(expected_revenue::float8, 0), COALESCE(probability, 0)
|
||||
FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&revenue, &probability)
|
||||
).Scan(&revenue, &probability); err != nil {
|
||||
log.Printf("warning: crm.lead _compute_prorated_revenue query failed: %v", err)
|
||||
}
|
||||
|
||||
prorated := revenue * probability / 100.0
|
||||
return orm.Values{"prorated_revenue": prorated}, nil
|
||||
})
|
||||
|
||||
// ──── Compute: day_open ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _compute_day_open
|
||||
m.RegisterCompute("day_open", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
var dayOpen *float64
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT CASE
|
||||
WHEN date_open IS NOT NULL AND create_date IS NOT NULL
|
||||
THEN ABS(EXTRACT(EPOCH FROM (date_open - create_date)) / 86400)
|
||||
ELSE NULL
|
||||
END
|
||||
FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&dayOpen)
|
||||
if err != nil {
|
||||
log.Printf("warning: crm.lead _compute_day_open query failed: %v", err)
|
||||
}
|
||||
|
||||
result := float64(0)
|
||||
if dayOpen != nil {
|
||||
result = *dayOpen
|
||||
}
|
||||
return orm.Values{"day_open": result}, nil
|
||||
})
|
||||
|
||||
// ──── Compute: day_close ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _compute_day_close
|
||||
m.RegisterCompute("day_close", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
var dayClose *float64
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT CASE
|
||||
WHEN date_closed IS NOT NULL AND create_date IS NOT NULL
|
||||
THEN ABS(EXTRACT(EPOCH FROM (date_closed - create_date)) / 86400)
|
||||
ELSE NULL
|
||||
END
|
||||
FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&dayClose)
|
||||
if err != nil {
|
||||
log.Printf("warning: crm.lead _compute_day_close query failed: %v", err)
|
||||
}
|
||||
|
||||
result := float64(0)
|
||||
if dayClose != nil {
|
||||
result = *dayClose
|
||||
}
|
||||
return orm.Values{"day_close": result}, nil
|
||||
})
|
||||
|
||||
// ──── Compute: kanban_state ────
|
||||
// Based on activity deadline: overdue=red, today/future=green, no activity=grey.
|
||||
// Mirrors: odoo/addons/mail/models/mail_activity_mixin.py _compute_kanban_state
|
||||
m.RegisterCompute("kanban_state", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
var deadline *string
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT activity_date_deadline FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&deadline)
|
||||
if err != nil {
|
||||
log.Printf("warning: crm.lead _compute_kanban_state query failed: %v", err)
|
||||
}
|
||||
|
||||
state := "grey" // no activity planned
|
||||
if deadline != nil && *deadline != "" {
|
||||
// Check if overdue
|
||||
var isOverdue bool
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT activity_date_deadline < CURRENT_DATE FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&isOverdue)
|
||||
if isOverdue {
|
||||
state = "red" // overdue
|
||||
} else {
|
||||
state = "green" // planned (today or future)
|
||||
}
|
||||
}
|
||||
|
||||
return orm.Values{"kanban_state": state}, nil
|
||||
})
|
||||
|
||||
// ──── Compute: days_in_stage ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _compute_days_in_stage
|
||||
m.RegisterCompute("days_in_stage", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
var days *float64
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT CASE
|
||||
WHEN date_last_stage_update IS NOT NULL
|
||||
THEN EXTRACT(DAY FROM NOW() - date_last_stage_update)
|
||||
ELSE 0
|
||||
END
|
||||
FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&days); err != nil {
|
||||
log.Printf("warning: crm.lead _compute_days_in_stage query failed: %v", err)
|
||||
}
|
||||
|
||||
result := float64(0)
|
||||
if days != nil {
|
||||
result = *days
|
||||
}
|
||||
return orm.Values{"days_in_stage": result}, nil
|
||||
})
|
||||
|
||||
// ──── Compute: email_score (email domain extraction) ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _compute_email_domain_criterion
|
||||
m.RegisterCompute("email_domain_criterion", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
var email *string
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT email_from FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&email); err != nil {
|
||||
log.Printf("warning: crm.lead _compute_email_score query failed: %v", err)
|
||||
}
|
||||
|
||||
domain := ""
|
||||
if email != nil && *email != "" {
|
||||
parts := strings.SplitN(*email, "@", 2)
|
||||
if len(parts) == 2 {
|
||||
domain = strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
return orm.Values{"email_domain_criterion": domain}, nil
|
||||
})
|
||||
|
||||
// ──── Compute: contact_address ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _compute_contact_address
|
||||
m.RegisterCompute("contact_address_complete", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
var street, street2, city, zip *string
|
||||
var partnerID *int64
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT street, street2, city, zip, partner_id
|
||||
FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&street, &street2, &city, &zip, &partnerID); err != nil {
|
||||
log.Printf("warning: crm.lead _compute_contact_address query failed: %v", err)
|
||||
}
|
||||
|
||||
// If partner exists, fetch address from partner instead
|
||||
if partnerID != nil {
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT street, street2, city, zip
|
||||
FROM res_partner WHERE id = $1`, *partnerID,
|
||||
).Scan(&street, &street2, &city, &zip); err != nil {
|
||||
log.Printf("warning: crm.lead _compute_contact_address partner query failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var parts []string
|
||||
if street != nil && *street != "" {
|
||||
parts = append(parts, *street)
|
||||
}
|
||||
if street2 != nil && *street2 != "" {
|
||||
parts = append(parts, *street2)
|
||||
}
|
||||
if zip != nil && *zip != "" && city != nil && *city != "" {
|
||||
parts = append(parts, *zip+" "+*city)
|
||||
} else if city != nil && *city != "" {
|
||||
parts = append(parts, *city)
|
||||
}
|
||||
|
||||
address := strings.Join(parts, "\n")
|
||||
return orm.Values{"contact_address_complete": address}, nil
|
||||
})
|
||||
|
||||
// ──── Business Methods ────
|
||||
|
||||
// action_schedule_activity: return a window action to schedule an activity.
|
||||
// action_schedule_activity: create a mail.activity record linked to the lead.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py action_schedule_activity
|
||||
m.RegisterMethod("action_schedule_activity", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
// Extract optional kwargs: summary, activity_type_id, date_deadline, user_id, note
|
||||
summary := ""
|
||||
note := ""
|
||||
dateDeadline := ""
|
||||
var userID int64
|
||||
var activityTypeID int64
|
||||
|
||||
if len(args) > 0 {
|
||||
if kwargs, ok := args[0].(map[string]interface{}); ok {
|
||||
if v, ok := kwargs["summary"].(string); ok {
|
||||
summary = v
|
||||
}
|
||||
if v, ok := kwargs["note"].(string); ok {
|
||||
note = v
|
||||
}
|
||||
if v, ok := kwargs["date_deadline"].(string); ok {
|
||||
dateDeadline = v
|
||||
}
|
||||
if v, ok := kwargs["user_id"]; ok {
|
||||
switch uid := v.(type) {
|
||||
case float64:
|
||||
userID = int64(uid)
|
||||
case int64:
|
||||
userID = uid
|
||||
case int:
|
||||
userID = int64(uid)
|
||||
}
|
||||
}
|
||||
if v, ok := kwargs["activity_type_id"]; ok {
|
||||
switch tid := v.(type) {
|
||||
case float64:
|
||||
activityTypeID = int64(tid)
|
||||
case int64:
|
||||
activityTypeID = tid
|
||||
case int:
|
||||
activityTypeID = int64(tid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Default user to current user
|
||||
if userID == 0 {
|
||||
userID = env.UID()
|
||||
}
|
||||
// Default deadline to tomorrow
|
||||
if dateDeadline == "" {
|
||||
dateDeadline = "CURRENT_DATE + INTERVAL '1 day'"
|
||||
}
|
||||
|
||||
var newID int64
|
||||
var err error
|
||||
if dateDeadline == "CURRENT_DATE + INTERVAL '1 day'" {
|
||||
err = env.Tx().QueryRow(env.Ctx(),
|
||||
`INSERT INTO mail_activity (res_model, res_id, summary, note, date_deadline, user_id, activity_type_id, state)
|
||||
VALUES ('crm.lead', $1, $2, $3, CURRENT_DATE + INTERVAL '1 day', $4, NULLIF($5, 0), 'planned')
|
||||
RETURNING id`,
|
||||
leadID, summary, note, userID, activityTypeID,
|
||||
).Scan(&newID)
|
||||
} else {
|
||||
err = env.Tx().QueryRow(env.Ctx(),
|
||||
`INSERT INTO mail_activity (res_model, res_id, summary, note, date_deadline, user_id, activity_type_id, state)
|
||||
VALUES ('crm.lead', $1, $2, $3, $6::date, $4, NULLIF($5, 0), 'planned')
|
||||
RETURNING id`,
|
||||
leadID, summary, note, userID, activityTypeID, dateDeadline,
|
||||
).Scan(&newID)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("action_schedule_activity: %w", err)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"name": "Schedule Activity",
|
||||
"res_model": "crm.lead",
|
||||
"res_id": rs.IDs()[0],
|
||||
"view_mode": "form",
|
||||
"views": [][]interface{}{{nil, "form"}},
|
||||
"target": "new",
|
||||
"activity_id": newID,
|
||||
"type": "ir.actions.act_window",
|
||||
"name": "Schedule Activity",
|
||||
"res_model": "mail.activity",
|
||||
"res_id": newID,
|
||||
"view_mode": "form",
|
||||
"views": [][]interface{}{{nil, "form"}},
|
||||
"target": "new",
|
||||
}, nil
|
||||
})
|
||||
|
||||
// action_merge: merge multiple leads into the first one.
|
||||
// Sums expected revenues from slave leads, deactivates them.
|
||||
// Mirrors: odoo/addons/crm/wizard/crm_merge_opportunities.py
|
||||
// action_merge: alias for action_merge_leads (delegates to the full implementation).
|
||||
m.RegisterMethod("action_merge", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
mergeMethod := orm.Registry.Get("crm.lead").Methods["action_merge_leads"]
|
||||
if mergeMethod != nil {
|
||||
return mergeMethod(rs, args...)
|
||||
}
|
||||
return nil, fmt.Errorf("crm.lead: action_merge_leads not found")
|
||||
})
|
||||
|
||||
// _get_opportunities_by_status: GROUP BY stage_id aggregation returning counts + sums.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _read_group (pipeline analysis)
|
||||
m.RegisterMethod("_get_opportunities_by_status", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
rows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT s.id, s.name, COUNT(l.id), COALESCE(SUM(l.expected_revenue::float8), 0),
|
||||
COALESCE(AVG(l.probability), 0)
|
||||
FROM crm_lead l
|
||||
JOIN crm_stage s ON s.id = l.stage_id
|
||||
WHERE l.active = true AND l.type = 'opportunity'
|
||||
GROUP BY s.id, s.name, s.sequence
|
||||
ORDER BY s.sequence`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("_get_opportunities_by_status: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []map[string]interface{}
|
||||
for rows.Next() {
|
||||
var stageID int64
|
||||
var stageName string
|
||||
var count int64
|
||||
var revenue, avgProb float64
|
||||
if err := rows.Scan(&stageID, &stageName, &count, &revenue, &avgProb); err != nil {
|
||||
return nil, fmt.Errorf("_get_opportunities_by_status scan: %w", err)
|
||||
}
|
||||
results = append(results, map[string]interface{}{
|
||||
"stage_id": stageID,
|
||||
"stage_name": stageName,
|
||||
"count": count,
|
||||
"total_revenue": revenue,
|
||||
"avg_probability": avgProb,
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
})
|
||||
|
||||
// action_merge_leads: merge multiple leads — sum revenues, keep first partner,
|
||||
// concatenate descriptions, delete merged records.
|
||||
// Mirrors: odoo/addons/crm/wizard/crm_merge_opportunities.py action_merge
|
||||
m.RegisterMethod("action_merge_leads", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
ids := rs.IDs()
|
||||
if len(ids) < 2 {
|
||||
@@ -172,25 +511,36 @@ func initCRMLeadExtended() {
|
||||
|
||||
masterID := ids[0]
|
||||
for _, slaveID := range ids[1:] {
|
||||
// Sum revenues from slave into master
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
// Sum revenues
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead
|
||||
SET expected_revenue = COALESCE(expected_revenue, 0) +
|
||||
(SELECT COALESCE(expected_revenue, 0) FROM crm_lead WHERE id = $1)
|
||||
WHERE id = $2`,
|
||||
slaveID, masterID)
|
||||
// Copy partner info if master has none
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
WHERE id = $2`, slaveID, masterID); err != nil {
|
||||
log.Printf("warning: crm.lead action_merge_leads revenue sum failed for slave %d: %v", slaveID, err)
|
||||
}
|
||||
// Keep first partner (master wins if set)
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET
|
||||
partner_id = COALESCE(partner_id, (SELECT partner_id FROM crm_lead WHERE id = $1))
|
||||
WHERE id = $2`, slaveID, masterID); err != nil {
|
||||
log.Printf("warning: crm.lead action_merge_leads partner copy failed for slave %d: %v", slaveID, err)
|
||||
}
|
||||
// Concatenate descriptions
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead
|
||||
SET partner_id = COALESCE(
|
||||
(SELECT partner_id FROM crm_lead WHERE id = $2),
|
||||
partner_id)
|
||||
WHERE id = $1 AND partner_id IS NULL`,
|
||||
masterID, slaveID)
|
||||
// Deactivate the slave lead
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET active = false WHERE id = $1`, slaveID)
|
||||
SET description = COALESCE(description, '') || E'\n---\n' ||
|
||||
COALESCE((SELECT description FROM crm_lead WHERE id = $1), '')
|
||||
WHERE id = $2`, slaveID, masterID); err != nil {
|
||||
log.Printf("warning: crm.lead action_merge_leads description concat failed for slave %d: %v", slaveID, err)
|
||||
}
|
||||
// Delete the merged (slave) lead
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`DELETE FROM crm_lead WHERE id = $1`, slaveID); err != nil {
|
||||
log.Printf("warning: crm.lead action_merge_leads delete failed for slave %d: %v", slaveID, err)
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"res_model": "crm.lead",
|
||||
@@ -201,6 +551,166 @@ func initCRMLeadExtended() {
|
||||
}, nil
|
||||
})
|
||||
|
||||
// _action_reschedule_calls: update activity dates for leads with overdue activities.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _action_reschedule_calls
|
||||
m.RegisterMethod("_action_reschedule_calls", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
// Default reschedule days = 7
|
||||
rescheduleDays := 7
|
||||
if len(args) > 0 {
|
||||
switch v := args[0].(type) {
|
||||
case float64:
|
||||
rescheduleDays = int(v)
|
||||
case int:
|
||||
rescheduleDays = v
|
||||
case int64:
|
||||
rescheduleDays = int(v)
|
||||
}
|
||||
}
|
||||
|
||||
// Update all overdue mail.activity records linked to crm.lead
|
||||
result, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE mail_activity
|
||||
SET date_deadline = CURRENT_DATE + ($1 || ' days')::interval,
|
||||
state = 'planned'
|
||||
WHERE res_model = 'crm.lead'
|
||||
AND date_deadline < CURRENT_DATE
|
||||
AND done = false`, rescheduleDays)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("_action_reschedule_calls: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected := result.RowsAffected()
|
||||
return map[string]interface{}{
|
||||
"rescheduled_count": rowsAffected,
|
||||
}, nil
|
||||
})
|
||||
|
||||
// action_lead_duplicate: copy lead with "(Copy)" suffix on name.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py copy()
|
||||
m.RegisterMethod("action_lead_duplicate", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
var newID int64
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`INSERT INTO crm_lead (name, type, partner_id, email_from, phone,
|
||||
stage_id, team_id, user_id, expected_revenue, probability,
|
||||
priority, company_id, currency_id, active, description,
|
||||
partner_name, street, city, zip, country_id, date_last_stage_update)
|
||||
SELECT name || ' (Copy)', type, partner_id, email_from, phone,
|
||||
stage_id, team_id, user_id, expected_revenue, probability,
|
||||
priority, company_id, currency_id, true, description,
|
||||
partner_name, street, city, zip, country_id, NOW()
|
||||
FROM crm_lead WHERE id = $1
|
||||
RETURNING id`, leadID,
|
||||
).Scan(&newID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("action_lead_duplicate: %w", err)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"res_model": "crm.lead",
|
||||
"res_id": newID,
|
||||
"view_mode": "form",
|
||||
"views": [][]interface{}{{nil, "form"}},
|
||||
"target": "current",
|
||||
}, nil
|
||||
})
|
||||
|
||||
// set_user_as_follower: create mail.followers entry for the lead's salesperson.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _create_lead_partner
|
||||
m.RegisterMethod("set_user_as_follower", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
for _, leadID := range rs.IDs() {
|
||||
// Get the user_id for the lead, then find the partner_id for that user
|
||||
var userID *int64
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT user_id FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&userID); err != nil || userID == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var partnerID *int64
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT partner_id FROM res_users WHERE id = $1`, *userID,
|
||||
).Scan(&partnerID); err != nil || partnerID == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if already a follower
|
||||
var exists bool
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT EXISTS(
|
||||
SELECT 1 FROM mail_followers
|
||||
WHERE res_model = 'crm.lead' AND res_id = $1 AND partner_id = $2
|
||||
)`, leadID, *partnerID,
|
||||
).Scan(&exists)
|
||||
|
||||
if !exists {
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`INSERT INTO mail_followers (res_model, res_id, partner_id)
|
||||
VALUES ('crm.lead', $1, $2)`, leadID, *partnerID); err != nil {
|
||||
log.Printf("warning: crm.lead set_user_as_follower failed for lead %d: %v", leadID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// message_subscribe: subscribe partners as followers on the lead.
|
||||
// Mirrors: odoo/addons/mail/models/mail_thread.py message_subscribe
|
||||
m.RegisterMethod("message_subscribe", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
if len(args) < 1 {
|
||||
return nil, fmt.Errorf("partner_ids required")
|
||||
}
|
||||
|
||||
// Accept partner_ids as []interface{} or []int64
|
||||
var partnerIDs []int64
|
||||
switch v := args[0].(type) {
|
||||
case []interface{}:
|
||||
for _, p := range v {
|
||||
switch pid := p.(type) {
|
||||
case float64:
|
||||
partnerIDs = append(partnerIDs, int64(pid))
|
||||
case int64:
|
||||
partnerIDs = append(partnerIDs, pid)
|
||||
case int:
|
||||
partnerIDs = append(partnerIDs, int64(pid))
|
||||
}
|
||||
}
|
||||
case []int64:
|
||||
partnerIDs = v
|
||||
}
|
||||
|
||||
for _, leadID := range rs.IDs() {
|
||||
for _, partnerID := range partnerIDs {
|
||||
// Check if already subscribed
|
||||
var exists bool
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT EXISTS(
|
||||
SELECT 1 FROM mail_followers
|
||||
WHERE res_model = 'crm.lead' AND res_id = $1 AND partner_id = $2
|
||||
)`, leadID, partnerID,
|
||||
).Scan(&exists)
|
||||
|
||||
if !exists {
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`INSERT INTO mail_followers (res_model, res_id, partner_id)
|
||||
VALUES ('crm.lead', $1, $2)`, leadID, partnerID); err != nil {
|
||||
log.Printf("warning: crm.lead message_subscribe failed for lead %d partner %d: %v", leadID, partnerID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_assign_salesperson: assign a salesperson to one or more leads.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _handle_salesmen_assignment
|
||||
m.RegisterMethod("action_assign_salesperson", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
@@ -213,8 +723,10 @@ func initCRMLeadExtended() {
|
||||
}
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET user_id = $1 WHERE id = $2`, int64(userID), id)
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET user_id = $1 WHERE id = $2`, int64(userID), id); err != nil {
|
||||
log.Printf("warning: crm.lead action_assign_salesperson failed for lead %d: %v", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
@@ -262,8 +774,10 @@ func initCRMLeadExtended() {
|
||||
}
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET priority = $1 WHERE id = $2`, priority, id)
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET priority = $1 WHERE id = $2`, priority, id); err != nil {
|
||||
log.Printf("warning: crm.lead action_set_priority failed for lead %d: %v", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
@@ -273,8 +787,10 @@ func initCRMLeadExtended() {
|
||||
m.RegisterMethod("action_archive", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET active = false WHERE id = $1`, id)
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET active = false WHERE id = $1`, id); err != nil {
|
||||
log.Printf("warning: crm.lead action_archive failed for lead %d: %v", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
@@ -284,8 +800,10 @@ func initCRMLeadExtended() {
|
||||
m.RegisterMethod("action_unarchive", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET active = true WHERE id = $1`, id)
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET active = true WHERE id = $1`, id); err != nil {
|
||||
log.Printf("warning: crm.lead action_unarchive failed for lead %d: %v", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
@@ -302,9 +820,11 @@ func initCRMLeadExtended() {
|
||||
}
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET stage_id = $1, date_last_stage_update = NOW() WHERE id = $2`,
|
||||
int64(stageID), id)
|
||||
int64(stageID), id); err != nil {
|
||||
log.Printf("warning: crm.lead action_set_stage failed for lead %d: %v", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
@@ -317,7 +837,7 @@ func initCRMLeadExtended() {
|
||||
var totalLeads, totalOpps, wonCount, lostCount int64
|
||||
var totalRevenue, avgProbability float64
|
||||
|
||||
_ = env.Tx().QueryRow(env.Ctx(), `
|
||||
if err := env.Tx().QueryRow(env.Ctx(), `
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE type = 'lead'),
|
||||
COUNT(*) FILTER (WHERE type = 'opportunity'),
|
||||
@@ -326,7 +846,9 @@ func initCRMLeadExtended() {
|
||||
COALESCE(SUM(expected_revenue::float8), 0),
|
||||
COALESCE(AVG(probability), 0)
|
||||
FROM crm_lead WHERE active = true`,
|
||||
).Scan(&totalLeads, &totalOpps, &wonCount, &lostCount, &totalRevenue, &avgProbability)
|
||||
).Scan(&totalLeads, &totalOpps, &wonCount, &lostCount, &totalRevenue, &avgProbability); err != nil {
|
||||
log.Printf("warning: crm.lead _get_lead_statistics query failed: %v", err)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"total_leads": totalLeads,
|
||||
@@ -338,7 +860,158 @@ func initCRMLeadExtended() {
|
||||
}, nil
|
||||
})
|
||||
|
||||
// Onchange: partner_id → populate contact/address fields from partner
|
||||
// action_schedule_meeting: return calendar action for scheduling a meeting.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py action_schedule_meeting
|
||||
m.RegisterMethod("action_schedule_meeting", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
// Fetch lead data for context
|
||||
var name string
|
||||
var partnerID, teamID *int64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(name, ''), partner_id, team_id FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&name, &partnerID, &teamID)
|
||||
|
||||
ctx := map[string]interface{}{
|
||||
"default_opportunity_id": leadID,
|
||||
"default_name": name,
|
||||
"search_default_opportunity_id": leadID,
|
||||
}
|
||||
if partnerID != nil {
|
||||
ctx["default_partner_id"] = *partnerID
|
||||
ctx["default_partner_ids"] = []int64{*partnerID}
|
||||
}
|
||||
if teamID != nil {
|
||||
ctx["default_team_id"] = *teamID
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"name": "Meeting",
|
||||
"res_model": "calendar.event",
|
||||
"view_mode": "calendar,tree,form",
|
||||
"context": ctx,
|
||||
}, nil
|
||||
})
|
||||
|
||||
// action_new_quotation: return action to create a sale.order linked to the lead.
|
||||
// Mirrors: odoo/addons/sale_crm/models/crm_lead.py action_new_quotation
|
||||
m.RegisterMethod("action_new_quotation", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
// Fetch lead context data
|
||||
var partnerID, teamID, companyID *int64
|
||||
var name string
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(name, ''), partner_id, team_id, company_id FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&name, &partnerID, &teamID, &companyID)
|
||||
|
||||
ctx := map[string]interface{}{
|
||||
"default_opportunity_id": leadID,
|
||||
"search_default_opportunity_id": leadID,
|
||||
"default_origin": name,
|
||||
}
|
||||
if partnerID != nil {
|
||||
ctx["default_partner_id"] = *partnerID
|
||||
}
|
||||
if teamID != nil {
|
||||
ctx["default_team_id"] = *teamID
|
||||
}
|
||||
if companyID != nil {
|
||||
ctx["default_company_id"] = *companyID
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"name": "New Quotation",
|
||||
"res_model": "sale.order",
|
||||
"view_mode": "form",
|
||||
"views": [][]interface{}{{nil, "form"}},
|
||||
"target": "current",
|
||||
"context": ctx,
|
||||
}, nil
|
||||
})
|
||||
|
||||
// merge_opportunity: alias for action_merge_leads.
|
||||
m.RegisterMethod("merge_opportunity", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
mergeMethod := orm.Registry.Get("crm.lead").Methods["action_merge_leads"]
|
||||
if mergeMethod != nil {
|
||||
return mergeMethod(rs, args...)
|
||||
}
|
||||
return nil, fmt.Errorf("crm.lead: action_merge_leads not found")
|
||||
})
|
||||
|
||||
// handle_partner_assignment: create or assign partner for leads.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _handle_partner_assignment
|
||||
m.RegisterMethod("handle_partner_assignment", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
// Optional force_partner_id from args
|
||||
var forcePartnerID int64
|
||||
if len(args) > 0 {
|
||||
if pid, ok := args[0].(float64); ok {
|
||||
forcePartnerID = int64(pid)
|
||||
}
|
||||
}
|
||||
|
||||
for _, id := range rs.IDs() {
|
||||
if forcePartnerID > 0 {
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET partner_id = $1 WHERE id = $2`, forcePartnerID, id)
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if lead already has a partner
|
||||
var existingPartnerID *int64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT partner_id FROM crm_lead WHERE id = $1`, id).Scan(&existingPartnerID)
|
||||
if existingPartnerID != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Create partner from lead data
|
||||
var email, phone, partnerName, street, city, zip, contactName string
|
||||
var countryID *int64
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(email_from,''), COALESCE(phone,''),
|
||||
COALESCE(partner_name,''), COALESCE(street,''),
|
||||
COALESCE(city,''), COALESCE(zip,''),
|
||||
COALESCE(contact_name,''), country_id
|
||||
FROM crm_lead WHERE id = $1`, id,
|
||||
).Scan(&email, &phone, &partnerName, &street, &city, &zip, &contactName, &countryID)
|
||||
|
||||
name := partnerName
|
||||
if name == "" {
|
||||
name = contactName
|
||||
}
|
||||
if name == "" {
|
||||
name = email
|
||||
}
|
||||
if name == "" {
|
||||
continue // cannot create partner without any identifying info
|
||||
}
|
||||
|
||||
var newPartnerID int64
|
||||
err := env.Tx().QueryRow(env.Ctx(),
|
||||
`INSERT INTO res_partner (name, email, phone, street, city, zip, country_id, active, is_company)
|
||||
VALUES ($1, NULLIF($2,''), NULLIF($3,''), NULLIF($4,''), NULLIF($5,''), NULLIF($6,''), $7, true,
|
||||
CASE WHEN $8 != '' THEN true ELSE false END)
|
||||
RETURNING id`,
|
||||
name, email, phone, street, city, zip, countryID, partnerName,
|
||||
).Scan(&newPartnerID)
|
||||
if err != nil {
|
||||
log.Printf("warning: crm.lead handle_partner_assignment create partner failed for lead %d: %v", id, err)
|
||||
continue
|
||||
}
|
||||
env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET partner_id = $1 WHERE id = $2`, newPartnerID, id)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// Onchange: partner_id -> populate contact/address fields from partner
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _onchange_partner_id
|
||||
m.RegisterOnchange("partner_id", func(env *orm.Environment, vals orm.Values) orm.Values {
|
||||
result := make(orm.Values)
|
||||
@@ -352,11 +1025,13 @@ func initCRMLeadExtended() {
|
||||
}
|
||||
|
||||
var email, phone, street, city, zip, name string
|
||||
_ = env.Tx().QueryRow(env.Ctx(),
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(email,''), COALESCE(phone,''), COALESCE(street,''),
|
||||
COALESCE(city,''), COALESCE(zip,''), COALESCE(name,'')
|
||||
FROM res_partner WHERE id = $1`, int64(pid),
|
||||
).Scan(&email, &phone, &street, &city, &zip, &name)
|
||||
).Scan(&email, &phone, &street, &city, &zip, &name); err != nil {
|
||||
log.Printf("warning: crm.lead onchange partner_id lookup failed: %v", err)
|
||||
}
|
||||
|
||||
if email != "" {
|
||||
result["email_from"] = email
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
@@ -81,6 +82,24 @@ func initCrmTeamExpanded() {
|
||||
}),
|
||||
)
|
||||
|
||||
// _compute_assignment_optout: count members who opted out of auto-assignment.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team.py _compute_assignment_optout
|
||||
m.RegisterCompute("assignment_optout_count", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
teamID := rs.IDs()[0]
|
||||
|
||||
var count int64
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COUNT(*) FROM crm_team_member
|
||||
WHERE crm_team_id = $1 AND active = true AND assignment_optout = true`,
|
||||
teamID,
|
||||
).Scan(&count); err != nil {
|
||||
log.Printf("warning: crm.team _compute_assignment_optout query failed: %v", err)
|
||||
}
|
||||
|
||||
return orm.Values{"assignment_optout_count": count}, nil
|
||||
})
|
||||
|
||||
// _compute_counts: compute dashboard KPIs for the sales team.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team.py _compute_opportunities_data
|
||||
m.RegisterCompute("opportunities_count", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
@@ -89,20 +108,24 @@ func initCrmTeamExpanded() {
|
||||
|
||||
var count int64
|
||||
var amount float64
|
||||
_ = env.Tx().QueryRow(env.Ctx(),
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COUNT(*), COALESCE(SUM(expected_revenue::float8), 0)
|
||||
FROM crm_lead
|
||||
WHERE team_id = $1 AND active = true AND type = 'opportunity'`,
|
||||
teamID,
|
||||
).Scan(&count, &amount)
|
||||
).Scan(&count, &amount); err != nil {
|
||||
log.Printf("warning: crm.team _compute_counts opportunities query failed: %v", err)
|
||||
}
|
||||
|
||||
var unassigned int64
|
||||
_ = env.Tx().QueryRow(env.Ctx(),
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COUNT(*)
|
||||
FROM crm_lead
|
||||
WHERE team_id = $1 AND active = true AND user_id IS NULL`,
|
||||
teamID,
|
||||
).Scan(&unassigned)
|
||||
).Scan(&unassigned); err != nil {
|
||||
log.Printf("warning: crm.team _compute_counts unassigned query failed: %v", err)
|
||||
}
|
||||
|
||||
return orm.Values{
|
||||
"opportunities_count": count,
|
||||
@@ -111,6 +134,69 @@ func initCrmTeamExpanded() {
|
||||
}, nil
|
||||
})
|
||||
|
||||
// get_crm_dashboard_data: KPIs — total pipeline value, won count, lost count, conversion rate.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team.py _compute_dashboard_data
|
||||
m.RegisterMethod("get_crm_dashboard_data", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
teamID := rs.IDs()[0]
|
||||
|
||||
var totalPipeline float64
|
||||
var wonCount, lostCount, totalOpps int64
|
||||
if err := env.Tx().QueryRow(env.Ctx(), `
|
||||
SELECT
|
||||
COALESCE(SUM(expected_revenue::float8), 0),
|
||||
COUNT(*) FILTER (WHERE state = 'won'),
|
||||
COUNT(*) FILTER (WHERE state = 'lost'),
|
||||
COUNT(*)
|
||||
FROM crm_lead
|
||||
WHERE team_id = $1 AND type = 'opportunity'`,
|
||||
teamID,
|
||||
).Scan(&totalPipeline, &wonCount, &lostCount, &totalOpps); err != nil {
|
||||
log.Printf("warning: crm.team get_crm_dashboard_data query failed: %v", err)
|
||||
}
|
||||
|
||||
conversionRate := float64(0)
|
||||
decided := wonCount + lostCount
|
||||
if decided > 0 {
|
||||
conversionRate = float64(wonCount) / float64(decided) * 100
|
||||
}
|
||||
|
||||
// Active pipeline (open opportunities only)
|
||||
var activePipeline float64
|
||||
var activeCount int64
|
||||
if err := env.Tx().QueryRow(env.Ctx(), `
|
||||
SELECT COALESCE(SUM(expected_revenue::float8), 0), COUNT(*)
|
||||
FROM crm_lead
|
||||
WHERE team_id = $1 AND type = 'opportunity' AND active = true AND state = 'open'`,
|
||||
teamID,
|
||||
).Scan(&activePipeline, &activeCount); err != nil {
|
||||
log.Printf("warning: crm.team get_crm_dashboard_data active pipeline query failed: %v", err)
|
||||
}
|
||||
|
||||
// Overdue activities count
|
||||
var overdueCount int64
|
||||
if err := env.Tx().QueryRow(env.Ctx(), `
|
||||
SELECT COUNT(DISTINCT l.id)
|
||||
FROM crm_lead l
|
||||
JOIN mail_activity a ON a.res_model = 'crm.lead' AND a.res_id = l.id
|
||||
WHERE l.team_id = $1 AND a.date_deadline < CURRENT_DATE AND a.done = false`,
|
||||
teamID,
|
||||
).Scan(&overdueCount); err != nil {
|
||||
log.Printf("warning: crm.team get_crm_dashboard_data overdue query failed: %v", err)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"total_pipeline": totalPipeline,
|
||||
"active_pipeline": activePipeline,
|
||||
"active_count": activeCount,
|
||||
"won_count": wonCount,
|
||||
"lost_count": lostCount,
|
||||
"total_opportunities": totalOpps,
|
||||
"conversion_rate": conversionRate,
|
||||
"overdue_activities": overdueCount,
|
||||
}, nil
|
||||
})
|
||||
|
||||
// action_assign_leads: trigger automatic lead assignment.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team.py action_assign_leads
|
||||
m.RegisterMethod("action_assign_leads", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
@@ -174,8 +260,10 @@ func initCrmTeamExpanded() {
|
||||
break
|
||||
}
|
||||
mc := &members[memberIdx]
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET user_id = $1 WHERE id = $2`, mc.userID, leadID)
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET user_id = $1 WHERE id = $2`, mc.userID, leadID); err != nil {
|
||||
log.Printf("warning: crm.team action_assign_leads update failed for lead %d: %v", leadID, err)
|
||||
}
|
||||
assigned++
|
||||
mc.capacity--
|
||||
if mc.capacity <= 0 {
|
||||
@@ -233,6 +321,15 @@ func initCrmTeamMember() {
|
||||
Index: true,
|
||||
}),
|
||||
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
|
||||
orm.Selection("role", []orm.SelectionItem{
|
||||
{Value: "member", Label: "Member"},
|
||||
{Value: "leader", Label: "Team Leader"},
|
||||
{Value: "manager", Label: "Sales Manager"},
|
||||
}, orm.FieldOpts{
|
||||
String: "Role",
|
||||
Default: "member",
|
||||
Help: "Role of this member within the sales team.",
|
||||
}),
|
||||
orm.Float("assignment_max", orm.FieldOpts{
|
||||
String: "Max Leads",
|
||||
Help: "Maximum number of leads this member should be assigned per month.",
|
||||
@@ -260,17 +357,21 @@ func initCrmTeamMember() {
|
||||
memberID := rs.IDs()[0]
|
||||
|
||||
var userID, teamID int64
|
||||
_ = env.Tx().QueryRow(env.Ctx(),
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT user_id, crm_team_id FROM crm_team_member WHERE id = $1`, memberID,
|
||||
).Scan(&userID, &teamID)
|
||||
).Scan(&userID, &teamID); err != nil {
|
||||
log.Printf("warning: crm.team.member _compute_lead_count member lookup failed: %v", err)
|
||||
}
|
||||
|
||||
var count int64
|
||||
_ = env.Tx().QueryRow(env.Ctx(),
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COUNT(*) FROM crm_lead
|
||||
WHERE user_id = $1 AND team_id = $2 AND active = true
|
||||
AND create_date >= date_trunc('month', CURRENT_DATE)`,
|
||||
userID, teamID,
|
||||
).Scan(&count)
|
||||
).Scan(&count); err != nil {
|
||||
log.Printf("warning: crm.team.member _compute_lead_count query failed: %v", err)
|
||||
}
|
||||
|
||||
return orm.Values{"lead_month_count": count}, nil
|
||||
})
|
||||
@@ -281,4 +382,90 @@ func initCrmTeamMember() {
|
||||
"UNIQUE(crm_team_id, user_id)",
|
||||
"A user can only be a member of a team once.",
|
||||
)
|
||||
|
||||
// action_assign_to_team: add a user to a team as a member.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team_member.py _assign_to_team
|
||||
m.RegisterMethod("action_assign_to_team", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
if len(args) < 2 {
|
||||
return nil, fmt.Errorf("user_id and team_id required")
|
||||
}
|
||||
var userID, teamID int64
|
||||
switch v := args[0].(type) {
|
||||
case float64:
|
||||
userID = int64(v)
|
||||
case int64:
|
||||
userID = v
|
||||
case int:
|
||||
userID = int64(v)
|
||||
}
|
||||
switch v := args[1].(type) {
|
||||
case float64:
|
||||
teamID = int64(v)
|
||||
case int64:
|
||||
teamID = v
|
||||
case int:
|
||||
teamID = int64(v)
|
||||
}
|
||||
|
||||
// Check if already a member
|
||||
var exists bool
|
||||
env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT EXISTS(SELECT 1 FROM crm_team_member WHERE user_id = $1 AND crm_team_id = $2)`,
|
||||
userID, teamID,
|
||||
).Scan(&exists)
|
||||
|
||||
if exists {
|
||||
// Ensure active
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_team_member SET active = true WHERE user_id = $1 AND crm_team_id = $2`,
|
||||
userID, teamID); err != nil {
|
||||
return nil, fmt.Errorf("action_assign_to_team reactivate: %w", err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
var newID int64
|
||||
if err := env.Tx().QueryRow(env.Ctx(),
|
||||
`INSERT INTO crm_team_member (user_id, crm_team_id, active, role)
|
||||
VALUES ($1, $2, true, 'member')
|
||||
RETURNING id`, userID, teamID,
|
||||
).Scan(&newID); err != nil {
|
||||
return nil, fmt.Errorf("action_assign_to_team insert: %w", err)
|
||||
}
|
||||
return map[string]interface{}{"id": newID}, nil
|
||||
})
|
||||
|
||||
// action_remove_from_team: deactivate membership (soft delete).
|
||||
// Mirrors: odoo/addons/crm/models/crm_team_member.py unlink
|
||||
m.RegisterMethod("action_remove_from_team", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_team_member SET active = false WHERE id = $1`, id); err != nil {
|
||||
log.Printf("warning: crm.team.member action_remove_from_team failed for member %d: %v", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_set_role: change the role of a team member.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team_member.py write
|
||||
m.RegisterMethod("action_set_role", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
if len(args) < 1 {
|
||||
return nil, fmt.Errorf("role value required ('member', 'leader', 'manager')")
|
||||
}
|
||||
role, ok := args[0].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("role must be a string")
|
||||
}
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
if _, err := env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_team_member SET role = $1 WHERE id = $2`, role, id); err != nil {
|
||||
log.Printf("warning: crm.team.member action_set_role failed for member %d: %v", id, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user