Dre4m Shell
Server IP : 127.0.0.2  /  Your IP : 3.147.77.120
Web Server : Apache/2.4.18 (Ubuntu)
System :
User : www-data ( )
PHP Version : 7.0.33-0ubuntu0.16.04.16
Disable Function : disk_free_space,disk_total_space,diskfreespace,dl,exec,fpaththru,getmyuid,getmypid,highlight_file,ignore_user_abord,leak,listen,link,opcache_get_configuration,opcache_get_status,passthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,php_uname,phpinfo,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix,_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,pclose,popen,proc_open,proc_close,proc_get_status,proc_nice,proc_terminate,shell_exec,source,show_source,system,virtual
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /opt/odoo/addons/website_sale/controllers/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /opt/odoo/addons/website_sale/controllers/backend.py
# -*- coding: utf-8 -*-
from odoo import http, _
from odoo.http import request
from datetime import datetime, timedelta
import babel

from odoo.addons.website.controllers.backend import WebsiteBackend
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT


class WebsiteSaleBackend(WebsiteBackend):

    @http.route()
    def fetch_dashboard_data(self, date_from, date_to):

        results = super(WebsiteSaleBackend, self).fetch_dashboard_data(date_from, date_to)
        results['groups']['sale_salesman'] = request.env['res.users'].has_group('sales_team.group_sale_salesman')
        if not results['groups']['sale_salesman']:
            return results

        date_from = datetime.strptime(date_from, DEFAULT_SERVER_DATE_FORMAT)
        date_to = datetime.strptime(date_to, DEFAULT_SERVER_DATE_FORMAT)

        # Best seller products
        product_lines = request.env['sale.order.line'].read_group(
            domain=[
                ('product_id.website_published', '=', True),
                ('order_id.state', 'in', ['sent', 'sale', 'done']),
                ('order_id.team_id.website_ids', '!=', False),
                ('order_id.date_order', '>=', date_from.strftime(DEFAULT_SERVER_DATE_FORMAT)),
                ('order_id.date_order', '<=', date_to.strftime(DEFAULT_SERVER_DATE_FORMAT))],
            fields=['product_id', 'product_uom_qty', 'price_total'],
            groupby='product_id', orderby='product_uom_qty desc', limit=5)

        best_sellers = []
        for product_line in product_lines:
            product_id = request.env['product.product'].browse(product_line['product_id'][0])
            best_sellers.append({
                'id': product_id.id,
                'name': product_id.name,
                'qty': product_line['product_uom_qty'],
                'sales': product_line['price_total'],
            })

        # Graphes computation
        sales_domain = [
            ('team_id.website_ids', '!=', False),
            ('state', 'in', ['sent', 'sale', 'done']),
        ]
        sales_graph = self._compute_sale_graph(date_from, date_to, sales_domain)
        previous_sales_graph = self._compute_sale_graph(date_from - timedelta(days=(date_to - date_from).days), date_from, sales_domain, previous=True)

        results['dashboards']['sales'] = {
            'graph': [
                {
                    'values': sales_graph,
                    'key': _('Sales'),
                },
                {
                    'values': previous_sales_graph,
                    'key': _('Previous Sales'),
                },
            ],
            'best_sellers': best_sellers,
        }

        return results

    def _compute_sale_graph(self, date_from, date_to, sales_domain, previous=False):

        days_between = (date_to - date_from).days
        date_list = [(date_from + timedelta(days=x)) for x in range(0, days_between + 1)]

        daily_sales = request.env['sale.order'].read_group(
            domain=sales_domain + [
                ('date_order', '>=', date_from.strftime(DEFAULT_SERVER_DATE_FORMAT)),
                ('date_order', '<=', date_to.strftime(DEFAULT_SERVER_DATE_FORMAT))],
            fields=['date_order', 'amount_total'],
            groupby='date_order:day')

        daily_sales_dict = {p['date_order:day']: p['amount_total'] for p in daily_sales}

        sales_graph = [{
            '0': d.strftime(DEFAULT_SERVER_DATE_FORMAT) if not previous else (d + timedelta(days=days_between)).strftime(DEFAULT_SERVER_DATE_FORMAT),
            # Respect read_group format in models.py
            '1': daily_sales_dict.get(babel.dates.format_date(d, format='dd MMM yyyy', locale=request.env.context.get('lang') or 'en_US'), 0)
        } for d in date_list]

        return sales_graph

Anon7 - 2022
AnonSec Team