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"}),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user