123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- function ranking_sales_product (widget) {
- "use strict";
- var model = openerp;
- widget.RankingSalesProductWidget = widget.Base.extend({
- template: 'RankingSalesProductTmpl',
- data: [],
- accountInvoice: [],
- invoiceLine: [],
- productProduct: [],
- ranking: [],
- modelId: [],
- events: {
- // 'click canvas': 'showCustomers'
- 'click h2': 'showCustomers'
- },
- init: function (parent) {
- this._super(parent, {
- width: 6,
- height: 4
- });
- },
- start: function () {
- var self = this;
- self.fetchInitial();
- },
- fetchInitial: function() {
- var self = this;
- self.$el.block({
- message: null,
- overlayCSS: {
- backgroundColor: '#FAFAFA'
- }
- });
- self.$el.find('.widget-content.widget-loading').css('display','flex');
- self.fecthInvoice().then(function (accountInvoice) {
- return accountInvoice;
- }).then(function (accountInvoice) {
- self.accountInvoice = accountInvoice;
- return self.fecthInvoiceLine(accountInvoice);
- }).then(function (invoiceLine) {
- self.invoiceLine = invoiceLine;
- return self.fecthProductProduct(invoiceLine);
- }).then(function(productProduct){
- self.productProduct = productProduct;
- return self.fetchGetModelId();
- }).then(function(modelId){
- self.modelId = modelId;
- return self.fetchProductRanking();
- });
- },
- // getModelId
- fetchGetModelId: function() {
- var self = this;
- var defer = $.Deferred();
- var irModelData = new model.web.Model('ir.model.data');
- var getObtjectReference = irModelData.get_func('get_object_reference');
- this.alive(getObtjectReference('product', 'product_normal_form_view')).then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- // Obtener factura
- fecthInvoice: function() {
- var self = this;
- var desde = moment().format('YYYY-MM-01');
- var hasta = moment().add(1,'months').format('YYYY-MM-01');
- var defer = $.Deferred();
- var fields = ['id', 'invoice_line', 'date_invoice'];
- var domain = [['type', '=', 'out_invoice'], ['date_invoice', '>=', desde], ['date_invoice', '<', hasta], ['state', 'in', ['open','paid']]];
- var invoice = new model.web.Model('account.invoice');
- invoice.query(fields).filter(domain).all().then(function (results) {
- defer.resolve(results);
- });
- return defer;
- },
- // Obtener linea de la factura
- fecthInvoiceLine: function(accountInvoice) {
- var self = this;
- var defer = $.Deferred();
- var invoice_line = _.flatten(_.map(accountInvoice, function (item) {
- return item.invoice_line;
- }));
- var fields = ['id', 'product_id', 'quantity'];
- var domain = [['id','in', invoice_line]];
- var invoiceLine = new model.web.Model('account.invoice.line');
- invoiceLine.query(fields).filter(domain).all().then(function (results) {
- defer.resolve(results);
- });
- return defer;
- },
- // Obtener Prodcutos
- fecthProductProduct: function (invoiceLine) {
- var self = this;
- var defer = $.Deferred();
- var product_id = _.flatten(_.map(invoiceLine,function (item) {
- return item.product_id[0];
- }));
- var fields = ['id', 'name_template', 'type'];
- var domain = [['id', 'in', product_id]];
- var product = new model.web.Model('product.product');
- product.query(fields).filter(domain).all().then(function (results) {
- defer.resolve(results);
- });
- return defer;
- },
- fetchProductRanking: function() {
- var self = this;
- var itemProduct;
- var itemLine;
- var ranking = [];
- var cat = 0;
- var lineUnik;
- for (var i = 0; i < self.productProduct.length; i++) {
- itemProduct = self.productProduct[i];
- itemLine = self.getInvoiceLine(itemProduct.id);
- // if (itemProduct.type === 'product') {
- if (itemLine.length > 0) {
- cat = _.reduce(_.map(itemLine, function (map) {
- return map.quantity
- }), function (meno,num) {
- return meno + num
- }, 0);
- lineUnik = itemLine.shift();
- ranking.push({
- product: itemProduct.name_template,
- qty: cat,
- id: lineUnik.product_id[0],
- type: itemProduct.type
- });
- }
- // }
- }
- ranking.sort(function(a, b) {
- return b.qty - a.qty
- });
- self.$el.unblock();
- self.$el.find('.widget-content.widget-loading').css('display','none');
- var fecha = new Date();
- var meses = new Array ("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
- self.$el.find('.widget-title').find('span').text("Ranking de productos y servicios del mes de " + meses[fecha.getMonth()]);
- // self.$el.find('.widget-title').find('span').text("Ranking de productos más vendidos del mes de " + meses[fecha.getMonth()]);
- self.ranking = ranking;
- self.fetchChart(ranking);
- },
- // get line
- getInvoiceLine: function (product_id) {
- var self = this;
- return _.flatten(_.filter(self.invoiceLine,function (line) {
- return line.product_id[0] === product_id
- }));
- },
- // Generar Grafico
- fetchChart: function (ranking){
- var self = this;
- var label = [];
- var bodyProducto = [];
- var labelService = [];
- var bodyService = [];
- var rank = 7;
- var item;
- // if (ranking.length < rank && ranking.length > 0)
- // rank= ranking.length;
- for (var i = 0; i < rank; i++) {
- if (ranking[i]){
- item = ranking[i];
- }else{
- item = {};
- item.product = "N/A";
- item.qty = 0;
- }
- // if (ranking.length === 0){
- // item = {};
- // item.product = "N/A";
- // item.qty = 0;
- // }
- var name = item.product.split(' ');
- if (name.length === 1) {
- label.push(name[0]);
- }else{
- label.push(name[0]+"("+name[name.length-1]+")");
- }
- if (item.type === 'product'){
- bodyService.push(0);
- bodyProducto.push(item.qty);
- }
- if (item.type === 'service'){
- bodyService.push(item.qty);
- bodyProducto.push(0);
- }
- }
- var chart = new Chart(this.$el.find(".widget-content").find('canvas'), {
- // type: 'line',
- type: 'bar',
- data: {
- labels: label,
- datasets: [
- {
- label: 'Productos ',
- borderWidth: 1,
- backgroundColor: [ '#EF5350', '#EC407A', '#AB47BC', '#7E57C2', '#5C6BC0', '#42A5F5', '#26C6DA', '#26A69A', '#66BB6A', '#9CCC65'],
- data: bodyProducto,
- },
- {
- label: 'Servicios ',
- borderWidth: 1,
- backgroundColor: [ '#EF5350', '#EC407A', '#AB47BC', '#7E57C2', '#5C6BC0', '#42A5F5', '#26C6DA', '#26A69A', '#66BB6A', '#9CCC65'],
- data: bodyService,
- }
- ]
- },
- options: {
- tooltips: {
- mode: 'index',
- intersect: true
- },
- legend: {
- labels: {
- boxWidth: 10,
- fontSize: 12,
- },
- position: 'left',
- },
- layout: {
- padding: {
- top: 20,
- bottom: 0,
- left: 0,
- rigth: 0,
- }
- },
- }
- });
- },
- showCustomers: function (e) {
- var self = this;
- if (self.ranking.length === 0){
- model.web.notification.do_warn("Atención","Sin datos");
- return
- }
- var hoy = moment().format('YYYY-MM-DD');
- var product_id = _.map(self.ranking,function(map){
- return map.id;
- })
- this.do_action({
- name:"Listado de productos más vendidos",
- type: 'ir.actions.act_window',
- res_model: "product.product",
- views: [[false, 'list'],[self.modelId[1],'form']],
- target: 'current',
- domain: [['id', 'in', product_id]],
- context: {},
- });
- }
- });
- }
|