You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过JSON文件在Odoo 16仪表盘计分卡展示年月度销售总额?

Odoo 16 仪表盘独立计分卡展示年度/月度销售总额实现方案

复制JSON后只显示空计分卡,核心问题是没绑定能返回有效数据的数据源——Odoo的计分卡需要通过Python方法计算销售总额,再在JSON里调用这个方法。下面是具体实现步骤:

一、编写Python方法计算销售数据

在销售模块或自定义模块中,添加计算月度、年度销售总额的方法,直接操作sale.order模型筛选有效订单:

from odoo import models, fields, api
from datetime import datetime, timedelta

class SaleDashboardExtension(models.Model):
    _inherit = 'sale.order'

    @api.model
    def get_monthly_sales_total(self):
        # 计算当月起止日期
        today = fields.Date.today()
        month_start = today.replace(day=1)
        month_end = (month_start + timedelta(days=32)).replace(day=1) - timedelta(days=1)
        
        # 筛选已确认/完成的订单,求和总额
        valid_orders = self.search([
            ('state', 'in', ['sale', 'done']),
            ('date_order', '>=', month_start),
            ('date_order', '<=', month_end)
        ])
        return sum(order.amount_total for order in valid_orders)

    @api.model
    def get_yearly_sales_total(self):
        # 计算当年起止日期
        today = fields.Date.today()
        year_start = today.replace(month=1, day=1)
        year_end = today.replace(month=12, day=31)
        
        valid_orders = self.search([
            ('state', 'in', ['sale', 'done']),
            ('date_order', '>=', year_start),
            ('date_order', '<=', year_end)
        ])
        return sum(order.amount_total for order in valid_orders)

说明:继承sale.order模型后新增两个类方法,仅计算已确认(sale)和已完成(done)的订单总额,避免无效数据干扰。

二、配置计分卡JSON

将以下JSON配置导入仪表盘的独立计分卡组件,核心是通过action字段绑定上面的Python方法:

[
    {
        "name": "月度销售总额",
        "type": "number",
        "icon": "fa fa-line-chart",
        "action": "sale.order.get_monthly_sales_total",
        "suffix": "元",
        "thresholds": [
            {"value": 0, "operator": ">", "class": "text-success"},
            {"value": 0, "operator": "<", "class": "text-danger"}
        ]
    },
    {
        "name": "年度销售总额",
        "type": "number",
        "icon": "fa fa-bar-chart",
        "action": "sale.order.get_yearly_sales_total",
        "suffix": "元",
        "thresholds": [
            {"value": 0, "operator": ">", "class": "text-success"},
            {"value": 0, "operator": "<", "class": "text-danger"}
        ]
    }
]

关键字段解释

  • action:必须写成模型名.方法名格式,比如sale.order.get_monthly_sales_total,对应上述Python方法
  • type:固定填number,表示该计分卡为数值类型
  • icon:使用Font Awesome图标类,可根据喜好更换
  • suffix:数值后缀,比如货币单位
  • thresholds:设置数值样式规则,比如正数显示绿色、负数显示红色

三、部署与验证

  1. 将Python代码放入自定义模块的models文件夹,更新模块
  2. 进入仪表盘编辑模式,添加「独立计分卡」组件并导入上述JSON配置
  3. 刷新页面,即可看到计分卡展示计算后的销售总额

额外注意事项

  • 确保当前用户拥有sale.order模型的读取权限,否则方法调用会返回空值
  • 多公司环境下,需在Python方法中添加('company_id', '=', self.env.company.id)的过滤条件
  • 可根据需求调整日期逻辑,比如改用订单确认时间confirmation_date而非下单时间date_order进行过滤

内容的提问来源于stack exchange,提问作者vish555

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 21:05:03