From db35822c35ea506aaccd63293ba77ac199d3ba02 Mon Sep 17 00:00:00 2001 From: Denis Vermylen Date: Wed, 4 Jan 2017 11:40:10 +0100 Subject: [PATCH] [IMP] pos_sale: add group by point of sale option for dashboard graphs of sales channel of type point of sale. --- addons/pos_sale/models/crm_team.py | 48 ++++++++++++++++++---- addons/pos_sale/views/sales_team_views.xml | 6 +++ addons/sales_team/models/crm_team.py | 8 +++- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/addons/pos_sale/models/crm_team.py b/addons/pos_sale/models/crm_team.py index d8ccf08ca51..f2af268a0db 100644 --- a/addons/pos_sale/models/crm_team.py +++ b/addons/pos_sale/models/crm_team.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import fields, models, _ +from odoo import api, fields, models, _ from datetime import datetime @@ -12,6 +12,20 @@ class CrmTeam(models.Model): pos_config_ids = fields.One2many('pos.config', 'crm_team_id', string="Point of Sales") pos_sessions_open_count = fields.Integer(string='Open POS Sessions', compute='_compute_pos_sessions_open_count') pos_order_amount_total = fields.Float(string="Session Sale Amount", compute='_compute_pos_order_amount_total') + dashboard_graph_group_pos = fields.Selection([ + ('day', 'Day'), + ('week', 'Week'), + ('month', 'Month'), + ('user', 'Salesperson'), + ('pos', 'Point of Sale'), + ], string='Group by', default='day', help="How this channel's dashboard graph will group the results.") + + @api.onchange('dashboard_graph_group_pos') + def _onchange_dashboard_graph_group_pos(self): + if self.dashboard_graph_group_pos == 'pos': + self.dashboard_graph_group = False + else: + self.dashboard_graph_group = self.dashboard_graph_group_pos def _compute_pos_sessions_open_count(self): for team in self.filtered(lambda t: t.team_type == 'pos'): @@ -29,7 +43,24 @@ class CrmTeam(models.Model): """ if self.team_type == 'pos': result = [] - if self.dashboard_graph_group == 'user': + if self.dashboard_graph_group_pos == 'pos': + order_data = self.env['report.pos.order'].read_group( + domain=[ + ('date', '>=', fields.Date.to_string(start_date)), + ('date', '<=', fields.Datetime.to_string(datetime.combine(end_date, datetime.max.time()))), + ('config_id', 'in', self.pos_config_ids.ids), + ('state', 'in', ['paid', 'done', 'invoiced'])], + fields=['config_id', 'price_total'], + groupby=['config_id'] + ) + appended_config_ids = set() + for data_point in order_data: + result.append({'x_value': self.env['pos.config'].browse(data_point.get('config_id')[0]).name, 'y_value': data_point.get('price_total')}) + appended_config_ids.add(data_point.get('config_id')) + for config_id in set(self.pos_config_ids.ids) - appended_config_ids: + result.append({'x_value': self.env['pos.config'].browse(config_id).name, 'y_value': 0}) + + elif self.dashboard_graph_group_pos == 'user': order_data = self.env['report.pos.order'].read_group( domain=[ ('date', '>=', fields.Date.to_string(start_date)), @@ -41,6 +72,7 @@ class CrmTeam(models.Model): ) for data_point in order_data: result.append({'x_value': data_point.get('user_id')[0], 'y_value': data_point.get('price_total')}) + else: order_data = self.env['report.pos.order'].read_group( domain=[ @@ -49,16 +81,18 @@ class CrmTeam(models.Model): ('config_id', 'in', self.pos_config_ids.ids), ('state', 'in', ['paid', 'done', 'invoiced'])], fields=['date', 'price_total'], - groupby=['date:' + self.dashboard_graph_group] + groupby=['date:' + self.dashboard_graph_group_pos] ) - if self.dashboard_graph_group == 'day': + if self.dashboard_graph_group_pos == 'day': for data_point in order_data: result.append({'x_value': fields.Date.to_string((fields.datetime.strptime(data_point.get('date:day'), "%d %b %Y"))), 'y_value': data_point.get('price_total')}) - if self.dashboard_graph_group == 'week': + elif self.dashboard_graph_group_pos == 'week': for data_point in order_data: # non-standard groupby formatting requires garbage formatting here, hope this'll hold ... - result.append({'x_value': int(data_point.get('date:week')[1:3]) - 1, 'y_value': data_point.get('price_total')}) - if self.dashboard_graph_group == 'month': + # also this week result is non-standard, so week numbers might be wrong here... + # for example 2016-12-31 -> week number 53 from read_group, while both isocalendar() and strftime('%W') return 52. + result.append({'x_value': int(data_point.get('date:week')[1:3]), 'y_value': data_point.get('price_total')}) + elif self.dashboard_graph_group_pos == 'month': for data_point in order_data: result.append({'x_value': fields.datetime.strptime(data_point.get('date:month'), "%B %Y").month, 'y_value': data_point.get('price_total')}) return result diff --git a/addons/pos_sale/views/sales_team_views.xml b/addons/pos_sale/views/sales_team_views.xml index 2740a8656a9..0ecb5c08140 100644 --- a/addons/pos_sale/views/sales_team_views.xml +++ b/addons/pos_sale/views/sales_team_views.xml @@ -8,6 +8,12 @@ {'invisible': [('team_type', '=', 'pos')]} + + {'invisible': [('team_type', '=', 'pos')]} + + + + diff --git a/addons/sales_team/models/crm_team.py b/addons/sales_team/models/crm_team.py index 703cfb62fa0..95f451086fd 100644 --- a/addons/sales_team/models/crm_team.py +++ b/addons/sales_team/models/crm_team.py @@ -71,8 +71,8 @@ class CrmTeam(models.Model): @api.depends('dashboard_graph_group', 'dashboard_graph_model', 'dashboard_graph_period') def _compute_dashboard_graph(self): for team in self.filtered('dashboard_graph_model'): - # might want to discuss this - if team.dashboard_graph_group == 'user' or team.dashboard_graph_period == 'week' and team.dashboard_graph_group != 'day' or team.dashboard_graph_period == 'month' and team.dashboard_graph_group != 'day': + if team.dashboard_graph_group in (False, 'user') or team.dashboard_graph_period == 'week' and team.dashboard_graph_group != 'day' \ + or team.dashboard_graph_period == 'month' and team.dashboard_graph_group != 'day': team.dashboard_graph_type = 'bar' else: team.dashboard_graph_type = 'line' @@ -217,6 +217,10 @@ class CrmTeam(models.Model): for data_item in graph_data: values.append({x_field: self.env['res.users'].browse(data_item.get('x_value')).name, y_field: data_item.get('y_value')}) + else: + for data_item in graph_data: + values.append({x_field: data_item.get('x_value'), y_field: data_item.get('y_value')}) + [graph_title, graph_key] = self._graph_title_and_key() color = '#875A7B' if '+e' in version else '#7c7bad' return [{'values': values, 'area': True, 'title': graph_title, 'key': graph_key, 'color': color}]