Massive module expansion: Stock, CRM, HR — +2895 LOC
Stock (1193→2867 LOC): - Valuation layers (FIFO consumption, product valuation history) - Landed costs (split by equal/qty/cost/weight/volume, validation) - Stock reports (by product, by location, move history, valuation) - Forecasting (on_hand + incoming - outgoing per product) - Batch transfers (confirm/assign/done with picking delegation) - Barcode interface (scan product/lot/package/location, qty increment) CRM (233→1113 LOC): - Sales teams with dashboard KPIs (opportunity count/amount/unassigned) - Team members with lead capacity + round-robin auto-assignment - Lead extended: activities, UTM tracking, scoring, address fields - Lead methods: merge, duplicate, schedule activity, set priority/stage - Pipeline analysis (stages, win rate, conversion, team/salesperson perf) - Partner onchange (auto-populate contact from partner) HR (223→520 LOC): - Leave management: hr.leave.type, hr.leave, hr.leave.allocation with full approval workflow (draft→confirm→validate/refuse) - Attendance: check in/out with computed worked_hours - Expenses: hr.expense + hr.expense.sheet with state machine - Skills/Resume: skill types, employee skills, resume lines - Employee extensions: skills, attendance, leave count links Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
232
addons/crm/models/crm_analysis.go
Normal file
232
addons/crm/models/crm_analysis.go
Normal file
@@ -0,0 +1,232 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// initCrmAnalysis registers the crm.lead.analysis transient model
|
||||
// for pipeline reporting and dashboard data.
|
||||
// Mirrors: odoo/addons/crm/report/crm_activity_report.py (simplified)
|
||||
func initCrmAnalysis() {
|
||||
m := orm.NewModel("crm.lead.analysis", orm.ModelOpts{
|
||||
Description: "Pipeline Analysis",
|
||||
Type: orm.ModelTransient,
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Many2one("team_id", "crm.team", orm.FieldOpts{
|
||||
String: "Sales Team",
|
||||
Help: "Filter analysis by sales team.",
|
||||
}),
|
||||
orm.Many2one("user_id", "res.users", orm.FieldOpts{
|
||||
String: "Salesperson",
|
||||
Help: "Filter analysis by salesperson.",
|
||||
}),
|
||||
orm.Date("date_from", orm.FieldOpts{
|
||||
String: "From",
|
||||
Help: "Start date for the analysis period.",
|
||||
}),
|
||||
orm.Date("date_to", orm.FieldOpts{
|
||||
String: "To",
|
||||
Help: "End date for the analysis period.",
|
||||
}),
|
||||
orm.Many2one("company_id", "res.company", orm.FieldOpts{
|
||||
String: "Company",
|
||||
Help: "Filter analysis by company.",
|
||||
}),
|
||||
)
|
||||
|
||||
// get_pipeline_data: return pipeline statistics grouped by stage.
|
||||
// Mirrors: odoo/addons/crm/report/crm_activity_report.py read_group
|
||||
m.RegisterMethod("get_pipeline_data", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
// Pipeline by stage
|
||||
rows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT s.name, COUNT(l.id), COALESCE(SUM(l.expected_revenue::float8), 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_pipeline_data: stages query: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var stages []map[string]interface{}
|
||||
for rows.Next() {
|
||||
var name string
|
||||
var count int64
|
||||
var revenue float64
|
||||
if err := rows.Scan(&name, &count, &revenue); err != nil {
|
||||
return nil, fmt.Errorf("get_pipeline_data: scan stage: %w", err)
|
||||
}
|
||||
stages = append(stages, map[string]interface{}{
|
||||
"stage": name,
|
||||
"count": count,
|
||||
"revenue": revenue,
|
||||
})
|
||||
}
|
||||
|
||||
// Win rate
|
||||
var total, won int64
|
||||
_ = 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)
|
||||
|
||||
winRate := float64(0)
|
||||
if total > 0 {
|
||||
winRate = float64(won) / float64(total) * 100
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"stages": stages,
|
||||
"total": total,
|
||||
"won": won,
|
||||
"win_rate": winRate,
|
||||
}, nil
|
||||
})
|
||||
|
||||
// get_conversion_data: return lead-to-opportunity conversion statistics.
|
||||
// Mirrors: odoo/addons/crm/report/crm_activity_report.py (conversion metrics)
|
||||
m.RegisterMethod("get_conversion_data", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
var totalLeads, convertedLeads int64
|
||||
_ = 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)
|
||||
|
||||
conversionRate := float64(0)
|
||||
if totalLeads > 0 {
|
||||
conversionRate = float64(convertedLeads) / float64(totalLeads) * 100
|
||||
}
|
||||
|
||||
// Average days to convert
|
||||
var avgDaysConvert float64
|
||||
_ = 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)
|
||||
|
||||
// Average days to close (won)
|
||||
var avgDaysClose float64
|
||||
_ = 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)
|
||||
|
||||
return map[string]interface{}{
|
||||
"total_leads": totalLeads,
|
||||
"converted_leads": convertedLeads,
|
||||
"conversion_rate": conversionRate,
|
||||
"avg_days_convert": avgDaysConvert,
|
||||
"avg_days_close": avgDaysClose,
|
||||
}, nil
|
||||
})
|
||||
|
||||
// get_team_performance: return per-team performance comparison.
|
||||
// Mirrors: odoo/addons/crm/report/crm_activity_report.py (team grouping)
|
||||
m.RegisterMethod("get_team_performance", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
rows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT
|
||||
t.name,
|
||||
COUNT(l.id) AS opp_count,
|
||||
COALESCE(SUM(l.expected_revenue::float8), 0) AS total_revenue,
|
||||
COALESCE(AVG(l.probability), 0) AS avg_probability,
|
||||
COUNT(l.id) FILTER (WHERE l.state = 'won') AS won_count
|
||||
FROM crm_lead l
|
||||
JOIN crm_team t ON t.id = l.team_id
|
||||
WHERE l.active = true AND l.type = 'opportunity'
|
||||
GROUP BY t.id, t.name
|
||||
ORDER BY total_revenue DESC`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get_team_performance: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var teams []map[string]interface{}
|
||||
for rows.Next() {
|
||||
var name string
|
||||
var oppCount, wonCount int64
|
||||
var totalRevenue, avgProb float64
|
||||
if err := rows.Scan(&name, &oppCount, &totalRevenue, &avgProb, &wonCount); err != nil {
|
||||
return nil, fmt.Errorf("get_team_performance: scan: %w", err)
|
||||
}
|
||||
winRate := float64(0)
|
||||
if oppCount > 0 {
|
||||
winRate = float64(wonCount) / float64(oppCount) * 100
|
||||
}
|
||||
teams = append(teams, map[string]interface{}{
|
||||
"team": name,
|
||||
"opportunities": oppCount,
|
||||
"revenue": totalRevenue,
|
||||
"avg_probability": avgProb,
|
||||
"won": wonCount,
|
||||
"win_rate": winRate,
|
||||
})
|
||||
}
|
||||
|
||||
return map[string]interface{}{"teams": teams}, nil
|
||||
})
|
||||
|
||||
// get_salesperson_performance: return per-salesperson performance data.
|
||||
// Mirrors: odoo/addons/crm/report/crm_activity_report.py (user grouping)
|
||||
m.RegisterMethod("get_salesperson_performance", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
rows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT
|
||||
u.login,
|
||||
COUNT(l.id) AS opp_count,
|
||||
COALESCE(SUM(l.expected_revenue::float8), 0) AS total_revenue,
|
||||
COUNT(l.id) FILTER (WHERE l.state = 'won') AS won_count,
|
||||
COUNT(l.id) FILTER (WHERE l.state = 'lost') AS lost_count
|
||||
FROM crm_lead l
|
||||
JOIN res_users u ON u.id = l.user_id
|
||||
WHERE l.active = true AND l.type = 'opportunity'
|
||||
GROUP BY u.id, u.login
|
||||
ORDER BY total_revenue DESC`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get_salesperson_performance: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var users []map[string]interface{}
|
||||
for rows.Next() {
|
||||
var login string
|
||||
var oppCount, wonCount, lostCount int64
|
||||
var totalRevenue float64
|
||||
if err := rows.Scan(&login, &oppCount, &totalRevenue, &wonCount, &lostCount); err != nil {
|
||||
return nil, fmt.Errorf("get_salesperson_performance: scan: %w", err)
|
||||
}
|
||||
winRate := float64(0)
|
||||
if oppCount > 0 {
|
||||
winRate = float64(wonCount) / float64(oppCount) * 100
|
||||
}
|
||||
users = append(users, map[string]interface{}{
|
||||
"salesperson": login,
|
||||
"opportunities": oppCount,
|
||||
"revenue": totalRevenue,
|
||||
"won": wonCount,
|
||||
"lost": lostCount,
|
||||
"win_rate": winRate,
|
||||
})
|
||||
}
|
||||
|
||||
return map[string]interface{}{"salespersons": users}, nil
|
||||
})
|
||||
}
|
||||
381
addons/crm/models/crm_lead_ext.go
Normal file
381
addons/crm/models/crm_lead_ext.go
Normal file
@@ -0,0 +1,381 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// initCRMLeadExtended adds activity, address, contact, and scoring fields
|
||||
// to crm.lead, plus business methods (merge, assign, schedule, duplicate, etc.).
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py (extended CRM fields and methods)
|
||||
func initCRMLeadExtended() {
|
||||
m := orm.ExtendModel("crm.lead")
|
||||
|
||||
// ──── Activity fields ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py activity fields
|
||||
m.AddFields(
|
||||
orm.Date("activity_date_deadline", orm.FieldOpts{
|
||||
String: "Next Activity Deadline",
|
||||
Help: "Date by which the next activity should be completed.",
|
||||
}),
|
||||
orm.Char("activity_summary", orm.FieldOpts{
|
||||
String: "Next Activity Summary",
|
||||
Help: "Short description of the next scheduled activity.",
|
||||
}),
|
||||
orm.Selection("activity_type", []orm.SelectionItem{
|
||||
{Value: "email", Label: "Email"},
|
||||
{Value: "call", Label: "Call"},
|
||||
{Value: "meeting", Label: "Meeting"},
|
||||
{Value: "todo", Label: "To-Do"},
|
||||
}, orm.FieldOpts{
|
||||
String: "Activity Type",
|
||||
Help: "Type of the next scheduled activity.",
|
||||
}),
|
||||
orm.Many2one("activity_user_id", "res.users", orm.FieldOpts{
|
||||
String: "Activity Responsible",
|
||||
Help: "User responsible for the next scheduled activity.",
|
||||
}),
|
||||
|
||||
// ──── 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.",
|
||||
}),
|
||||
orm.Integer("day_close", orm.FieldOpts{
|
||||
String: "Days to Close",
|
||||
Help: "Number of days to close this lead/opportunity.",
|
||||
}),
|
||||
|
||||
// ──── Additional contact/address fields ────
|
||||
// Fields not already on crm.lead base definition.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py address & contact fields
|
||||
orm.Char("referred", orm.FieldOpts{
|
||||
String: "Referred By",
|
||||
Help: "Name or reference of who referred this lead.",
|
||||
}),
|
||||
orm.Char("title", orm.FieldOpts{
|
||||
String: "Title",
|
||||
Help: "Contact title (Mr., Mrs., etc.).",
|
||||
}),
|
||||
orm.Many2one("state_id", "res.country.state", orm.FieldOpts{
|
||||
String: "State",
|
||||
Help: "State/province of the lead address.",
|
||||
}),
|
||||
orm.Text("contact_name", orm.FieldOpts{
|
||||
String: "Contact Name",
|
||||
Help: "Name of the primary contact person.",
|
||||
}),
|
||||
orm.Char("mobile", orm.FieldOpts{
|
||||
String: "Mobile",
|
||||
Help: "Mobile phone number of the contact.",
|
||||
}),
|
||||
orm.Char("street2", orm.FieldOpts{
|
||||
String: "Street2",
|
||||
Help: "Second line of the street address.",
|
||||
}),
|
||||
|
||||
// ──── Revenue fields ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py prorated_revenue
|
||||
orm.Monetary("prorated_revenue", orm.FieldOpts{
|
||||
String: "Prorated Revenue",
|
||||
CurrencyField: "company_currency",
|
||||
Compute: "_compute_prorated_revenue",
|
||||
Help: "Expected revenue weighted by probability.",
|
||||
}),
|
||||
orm.Many2one("company_currency", "res.currency", orm.FieldOpts{
|
||||
String: "Company Currency",
|
||||
}),
|
||||
|
||||
// ──── Scoring / automated fields ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead_scoring.py (simplified)
|
||||
orm.Float("automated_probability", orm.FieldOpts{
|
||||
String: "Automated Probability (%)",
|
||||
Help: "Probability computed by the lead scoring engine.",
|
||||
}),
|
||||
orm.Boolean("is_automated_probability", orm.FieldOpts{
|
||||
String: "Use Automated Probability",
|
||||
Default: true,
|
||||
Help: "If true, probability is set automatically by scoring.",
|
||||
}),
|
||||
|
||||
// ──── Source / campaign tracking ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py utm fields
|
||||
orm.Many2one("source_id", "utm.source", orm.FieldOpts{
|
||||
String: "Source",
|
||||
Help: "Source of the lead (e.g. search engine, social media).",
|
||||
}),
|
||||
orm.Many2one("medium_id", "utm.medium", orm.FieldOpts{
|
||||
String: "Medium",
|
||||
Help: "Medium of the lead (e.g. email, banner).",
|
||||
}),
|
||||
orm.Many2one("campaign_id", "utm.campaign", orm.FieldOpts{
|
||||
String: "Campaign",
|
||||
Help: "Marketing campaign that generated this lead.",
|
||||
}),
|
||||
|
||||
// ──── Date tracking ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py date_conversion, date_closed
|
||||
orm.Datetime("date_conversion", orm.FieldOpts{
|
||||
String: "Conversion Date",
|
||||
Help: "Date when the lead was converted to an opportunity.",
|
||||
}),
|
||||
orm.Datetime("date_closed", orm.FieldOpts{
|
||||
String: "Closed Date",
|
||||
Help: "Date when the opportunity was won or lost.",
|
||||
}),
|
||||
)
|
||||
|
||||
// ──── Compute: prorated revenue ────
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py _compute_prorated_revenue
|
||||
m.RegisterCompute("prorated_revenue", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
leadID := rs.IDs()[0]
|
||||
|
||||
var revenue float64
|
||||
var probability float64
|
||||
_ = env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT COALESCE(expected_revenue::float8, 0), COALESCE(probability, 0)
|
||||
FROM crm_lead WHERE id = $1`, leadID,
|
||||
).Scan(&revenue, &probability)
|
||||
|
||||
prorated := revenue * probability / 100.0
|
||||
return orm.Values{"prorated_revenue": prorated}, nil
|
||||
})
|
||||
|
||||
// ──── Business Methods ────
|
||||
|
||||
// action_schedule_activity: return a window action to schedule an activity.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py action_schedule_activity
|
||||
m.RegisterMethod("action_schedule_activity", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
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",
|
||||
}, 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
|
||||
m.RegisterMethod("action_merge", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
ids := rs.IDs()
|
||||
if len(ids) < 2 {
|
||||
return nil, fmt.Errorf("need at least 2 leads to merge")
|
||||
}
|
||||
|
||||
masterID := ids[0]
|
||||
for _, slaveID := range ids[1:] {
|
||||
// Sum revenues from slave into master
|
||||
_, _ = 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(),
|
||||
`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)
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"res_model": "crm.lead",
|
||||
"res_id": masterID,
|
||||
"view_mode": "form",
|
||||
"views": [][]interface{}{{nil, "form"}},
|
||||
"target": "current",
|
||||
}, 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) {
|
||||
if len(args) < 1 {
|
||||
return nil, fmt.Errorf("user_id required")
|
||||
}
|
||||
userID, ok := args[0].(float64)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user_id must be a number")
|
||||
}
|
||||
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)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_duplicate: duplicate a lead with a modified name.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py copy()
|
||||
m.RegisterMethod("action_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)
|
||||
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
|
||||
FROM crm_lead WHERE id = $1
|
||||
RETURNING id`, leadID,
|
||||
).Scan(&newID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("action_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
|
||||
})
|
||||
|
||||
// action_set_priority: set priority on selected leads.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py (bulk actions)
|
||||
m.RegisterMethod("action_set_priority", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
if len(args) < 1 {
|
||||
return nil, fmt.Errorf("priority value required (0-3)")
|
||||
}
|
||||
priority, ok := args[0].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("priority must be a string ('0','1','2','3')")
|
||||
}
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET priority = $1 WHERE id = $2`, priority, id)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_archive: archive (deactivate) selected leads.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py toggle_active
|
||||
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)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_unarchive: restore (reactivate) selected leads.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py toggle_active
|
||||
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)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// action_set_stage: move leads to a specific stage.
|
||||
// Mirrors: odoo/addons/crm/models/crm_lead.py write() stage change logic
|
||||
m.RegisterMethod("action_set_stage", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
if len(args) < 1 {
|
||||
return nil, fmt.Errorf("stage_id required")
|
||||
}
|
||||
stageID, ok := args[0].(float64)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("stage_id must be a number")
|
||||
}
|
||||
env := rs.Env()
|
||||
for _, id := range rs.IDs() {
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET stage_id = $1, date_last_stage_update = NOW() WHERE id = $2`,
|
||||
int64(stageID), id)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// _get_lead_statistics: return summary statistics for a set of leads.
|
||||
// Internal helper used by dashboard views.
|
||||
m.RegisterMethod("_get_lead_statistics", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
env := rs.Env()
|
||||
|
||||
var totalLeads, totalOpps, wonCount, lostCount int64
|
||||
var totalRevenue, avgProbability float64
|
||||
|
||||
_ = env.Tx().QueryRow(env.Ctx(), `
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE type = 'lead'),
|
||||
COUNT(*) FILTER (WHERE type = 'opportunity'),
|
||||
COUNT(*) FILTER (WHERE state = 'won'),
|
||||
COUNT(*) FILTER (WHERE state = 'lost'),
|
||||
COALESCE(SUM(expected_revenue::float8), 0),
|
||||
COALESCE(AVG(probability), 0)
|
||||
FROM crm_lead WHERE active = true`,
|
||||
).Scan(&totalLeads, &totalOpps, &wonCount, &lostCount, &totalRevenue, &avgProbability)
|
||||
|
||||
return map[string]interface{}{
|
||||
"total_leads": totalLeads,
|
||||
"total_opportunities": totalOpps,
|
||||
"won_count": wonCount,
|
||||
"lost_count": lostCount,
|
||||
"total_revenue": totalRevenue,
|
||||
"avg_probability": avgProbability,
|
||||
}, 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)
|
||||
partnerID, ok := vals["partner_id"]
|
||||
if !ok || partnerID == nil {
|
||||
return result
|
||||
}
|
||||
pid, _ := partnerID.(float64)
|
||||
if pid == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
var email, phone, street, city, zip, name string
|
||||
_ = 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)
|
||||
|
||||
if email != "" {
|
||||
result["email_from"] = email
|
||||
}
|
||||
if phone != "" {
|
||||
result["phone"] = phone
|
||||
}
|
||||
if street != "" {
|
||||
result["street"] = street
|
||||
}
|
||||
if city != "" {
|
||||
result["city"] = city
|
||||
}
|
||||
if zip != "" {
|
||||
result["zip"] = zip
|
||||
}
|
||||
if name != "" {
|
||||
result["partner_name"] = name
|
||||
}
|
||||
return result
|
||||
})
|
||||
}
|
||||
284
addons/crm/models/crm_team.go
Normal file
284
addons/crm/models/crm_team.go
Normal file
@@ -0,0 +1,284 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"odoo-go/pkg/orm"
|
||||
)
|
||||
|
||||
// initCrmTeamExpanded extends the basic crm.team with CRM-specific fields
|
||||
// (pipeline dashboard, lead assignment, team configuration).
|
||||
// Mirrors: odoo/addons/crm/models/crm_team.py CrmTeam
|
||||
func initCrmTeamExpanded() {
|
||||
m := orm.ExtendModel("crm.team")
|
||||
|
||||
m.AddFields(
|
||||
// Team configuration
|
||||
orm.Many2one("user_id", "res.users", orm.FieldOpts{String: "Team Leader"}),
|
||||
orm.One2many("crm_team_member_ids", "crm.team.member", "crm_team_id", orm.FieldOpts{
|
||||
String: "Sales Team Members",
|
||||
}),
|
||||
orm.Boolean("use_leads", orm.FieldOpts{
|
||||
String: "Leads",
|
||||
Default: true,
|
||||
Help: "Check this box to filter and qualify leads.",
|
||||
}),
|
||||
orm.Boolean("use_opportunities", orm.FieldOpts{
|
||||
String: "Pipeline",
|
||||
Default: true,
|
||||
Help: "Check this box to manage a pipeline of opportunities.",
|
||||
}),
|
||||
orm.Many2one("alias_id", "mail.alias", orm.FieldOpts{
|
||||
String: "Email Alias",
|
||||
Help: "Incoming emails on this alias will create leads/opportunities.",
|
||||
}),
|
||||
|
||||
// Dashboard computed fields
|
||||
// Mirrors: _compute_opportunities_data in crm.team
|
||||
orm.Integer("opportunities_count", orm.FieldOpts{
|
||||
String: "Number of Opportunities",
|
||||
Compute: "_compute_counts",
|
||||
Help: "Number of active opportunities in this team's pipeline.",
|
||||
}),
|
||||
orm.Monetary("opportunities_amount", orm.FieldOpts{
|
||||
String: "Opportunities Revenue",
|
||||
Compute: "_compute_counts",
|
||||
CurrencyField: "currency_id",
|
||||
Help: "Total expected revenue of active opportunities.",
|
||||
}),
|
||||
orm.Many2one("currency_id", "res.currency", orm.FieldOpts{
|
||||
String: "Currency",
|
||||
}),
|
||||
orm.Integer("unassigned_leads_count", orm.FieldOpts{
|
||||
String: "Unassigned Leads",
|
||||
Compute: "_compute_counts",
|
||||
Help: "Number of leads without a salesperson assigned.",
|
||||
}),
|
||||
|
||||
// Assignment settings
|
||||
// Mirrors: odoo/addons/crm/models/crm_team.py assignment fields
|
||||
orm.Selection("assignment_domain", []orm.SelectionItem{
|
||||
{Value: "manual", Label: "Manual"},
|
||||
{Value: "auto", Label: "Auto Assignment"},
|
||||
}, orm.FieldOpts{
|
||||
String: "Assignment Method",
|
||||
Default: "manual",
|
||||
Help: "How new leads are distributed among team members.",
|
||||
}),
|
||||
orm.Integer("assignment_max", orm.FieldOpts{
|
||||
String: "Max Leads per Cycle",
|
||||
Default: 30,
|
||||
Help: "Maximum leads assigned to a member per assignment cycle.",
|
||||
}),
|
||||
orm.Boolean("assignment_enabled", orm.FieldOpts{
|
||||
String: "Auto-Assignment Active",
|
||||
Help: "Enable automatic lead assignment for this team.",
|
||||
}),
|
||||
orm.Integer("assignment_optout_count", orm.FieldOpts{
|
||||
String: "Members Opted-Out",
|
||||
Compute: "_compute_assignment_optout",
|
||||
Help: "Number of members who opted out of automatic assignment.",
|
||||
}),
|
||||
)
|
||||
|
||||
// _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) {
|
||||
env := rs.Env()
|
||||
teamID := rs.IDs()[0]
|
||||
|
||||
var count int64
|
||||
var amount float64
|
||||
_ = 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)
|
||||
|
||||
var unassigned int64
|
||||
_ = 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)
|
||||
|
||||
return orm.Values{
|
||||
"opportunities_count": count,
|
||||
"opportunities_amount": amount,
|
||||
"unassigned_leads_count": unassigned,
|
||||
}, 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) {
|
||||
env := rs.Env()
|
||||
teamID := rs.IDs()[0]
|
||||
|
||||
// Fetch active members with remaining capacity
|
||||
rows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT m.user_id, COALESCE(m.assignment_max, 30) AS max_leads,
|
||||
COALESCE((SELECT COUNT(*) FROM crm_lead l
|
||||
WHERE l.user_id = m.user_id AND l.team_id = $1
|
||||
AND l.active = true
|
||||
AND l.create_date >= date_trunc('month', CURRENT_DATE)), 0) AS current_count
|
||||
FROM crm_team_member m
|
||||
WHERE m.crm_team_id = $1 AND m.active = true
|
||||
ORDER BY current_count ASC`,
|
||||
teamID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("action_assign_leads: query members: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
type memberCap struct {
|
||||
userID int64
|
||||
capacity int64
|
||||
}
|
||||
var members []memberCap
|
||||
for rows.Next() {
|
||||
var uid int64
|
||||
var maxLeads, current int64
|
||||
if err := rows.Scan(&uid, &maxLeads, ¤t); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if remaining := maxLeads - current; remaining > 0 {
|
||||
members = append(members, memberCap{userID: uid, capacity: remaining})
|
||||
}
|
||||
}
|
||||
if len(members) == 0 {
|
||||
return map[string]interface{}{"assigned": 0}, nil
|
||||
}
|
||||
|
||||
// Fetch unassigned leads
|
||||
leadRows, err := env.Tx().Query(env.Ctx(), `
|
||||
SELECT id FROM crm_lead
|
||||
WHERE team_id = $1 AND active = true AND user_id IS NULL
|
||||
ORDER BY priority DESC, id ASC`,
|
||||
teamID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("action_assign_leads: query leads: %w", err)
|
||||
}
|
||||
defer leadRows.Close()
|
||||
|
||||
var assigned int64
|
||||
memberIdx := 0
|
||||
for leadRows.Next() {
|
||||
var leadID int64
|
||||
if err := leadRows.Scan(&leadID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if memberIdx >= len(members) {
|
||||
break
|
||||
}
|
||||
mc := &members[memberIdx]
|
||||
_, _ = env.Tx().Exec(env.Ctx(),
|
||||
`UPDATE crm_lead SET user_id = $1 WHERE id = $2`, mc.userID, leadID)
|
||||
assigned++
|
||||
mc.capacity--
|
||||
if mc.capacity <= 0 {
|
||||
memberIdx++
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]interface{}{"assigned": assigned}, nil
|
||||
})
|
||||
|
||||
// action_open_opportunities: return window action for team's opportunities.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team.py action_open_opportunities
|
||||
m.RegisterMethod("action_open_opportunities", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"name": "Opportunities",
|
||||
"res_model": "crm.lead",
|
||||
"view_mode": "kanban,tree,form",
|
||||
"domain": fmt.Sprintf("[('team_id','=',%d),('type','=','opportunity')]", rs.IDs()[0]),
|
||||
"context": map[string]interface{}{"default_team_id": rs.IDs()[0], "default_type": "opportunity"},
|
||||
}, nil
|
||||
})
|
||||
|
||||
// action_open_unassigned: return window action for unassigned leads.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team.py (dashboard actions)
|
||||
m.RegisterMethod("action_open_unassigned", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"type": "ir.actions.act_window",
|
||||
"name": "Unassigned Leads",
|
||||
"res_model": "crm.lead",
|
||||
"view_mode": "tree,form",
|
||||
"domain": fmt.Sprintf("[('team_id','=',%d),('user_id','=',False)]", rs.IDs()[0]),
|
||||
"context": map[string]interface{}{"default_team_id": rs.IDs()[0]},
|
||||
}, nil
|
||||
})
|
||||
}
|
||||
|
||||
// initCrmTeamMember registers the crm.team.member model.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team_member.py CrmTeamMember
|
||||
func initCrmTeamMember() {
|
||||
m := orm.NewModel("crm.team.member", orm.ModelOpts{
|
||||
Description: "Sales Team Member",
|
||||
})
|
||||
|
||||
m.AddFields(
|
||||
orm.Many2one("crm_team_id", "crm.team", orm.FieldOpts{
|
||||
String: "Sales Team",
|
||||
Required: true,
|
||||
OnDelete: orm.OnDeleteCascade,
|
||||
Index: true,
|
||||
}),
|
||||
orm.Many2one("user_id", "res.users", orm.FieldOpts{
|
||||
String: "Salesperson",
|
||||
Required: true,
|
||||
Index: true,
|
||||
}),
|
||||
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
|
||||
orm.Float("assignment_max", orm.FieldOpts{
|
||||
String: "Max Leads",
|
||||
Help: "Maximum number of leads this member should be assigned per month.",
|
||||
Default: float64(30),
|
||||
}),
|
||||
orm.Integer("lead_month_count", orm.FieldOpts{
|
||||
String: "Leads This Month",
|
||||
Compute: "_compute_lead_count",
|
||||
Help: "Number of leads assigned to this member in the current month.",
|
||||
}),
|
||||
orm.Boolean("assignment_optout", orm.FieldOpts{
|
||||
String: "Opt-Out",
|
||||
Help: "If checked, this member will not receive automatically assigned leads.",
|
||||
}),
|
||||
orm.Many2one("company_id", "res.company", orm.FieldOpts{
|
||||
String: "Company",
|
||||
Help: "Company of the sales team.",
|
||||
}),
|
||||
)
|
||||
|
||||
// _compute_lead_count: count leads assigned to this member this month.
|
||||
// Mirrors: odoo/addons/crm/models/crm_team_member.py _compute_lead_month_count
|
||||
m.RegisterCompute("lead_month_count", func(rs *orm.Recordset) (orm.Values, error) {
|
||||
env := rs.Env()
|
||||
memberID := rs.IDs()[0]
|
||||
|
||||
var userID, teamID int64
|
||||
_ = env.Tx().QueryRow(env.Ctx(),
|
||||
`SELECT user_id, crm_team_id FROM crm_team_member WHERE id = $1`, memberID,
|
||||
).Scan(&userID, &teamID)
|
||||
|
||||
var count int64
|
||||
_ = 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)
|
||||
|
||||
return orm.Values{"lead_month_count": count}, nil
|
||||
})
|
||||
|
||||
// SQL constraint: one member per team
|
||||
m.AddSQLConstraint(
|
||||
"unique_member_per_team",
|
||||
"UNIQUE(crm_team_id, user_id)",
|
||||
"A user can only be a member of a team once.",
|
||||
)
|
||||
}
|
||||
@@ -4,6 +4,11 @@ func Init() {
|
||||
initCRMTag()
|
||||
initCRMLostReason()
|
||||
initCRMTeam()
|
||||
initCrmTeamMember()
|
||||
initCRMStage()
|
||||
initCRMLead()
|
||||
// Extensions (must come after base models are registered)
|
||||
initCrmTeamExpanded()
|
||||
initCRMLeadExtended()
|
||||
initCrmAnalysis()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user