|
@@ -5,19 +5,20 @@ function ranking_sales_product (widget) {
|
|
|
|
|
|
widget.RankingSalesProductWidget = widget.Base.extend({
|
|
|
template: 'RankingSalesProductTmpl',
|
|
|
- data : [],
|
|
|
- accountInvoice : [],
|
|
|
- invoiceLine : [],
|
|
|
- productProduct : [],
|
|
|
- ranking:[],
|
|
|
- modelId:[],
|
|
|
+ data: [],
|
|
|
+ accountInvoice: [],
|
|
|
+ invoiceLine: [],
|
|
|
+ productProduct: [],
|
|
|
+ ranking: [],
|
|
|
+ modelId: [],
|
|
|
|
|
|
events: {
|
|
|
- 'click canvas': 'showCustomers'
|
|
|
+ // 'click canvas': 'showCustomers'
|
|
|
+ 'click h2': 'showCustomers'
|
|
|
},
|
|
|
init: function (parent) {
|
|
|
this._super(parent, {
|
|
|
- width : 6,
|
|
|
+ width: 6,
|
|
|
height: 4
|
|
|
});
|
|
|
},
|
|
@@ -25,7 +26,7 @@ function ranking_sales_product (widget) {
|
|
|
var self = this;
|
|
|
self.fetchInitial();
|
|
|
},
|
|
|
- fetchInitial:function(){
|
|
|
+ fetchInitial: function() {
|
|
|
var self = this;
|
|
|
self.$el.block({
|
|
|
message: null,
|
|
@@ -34,6 +35,7 @@ function ranking_sales_product (widget) {
|
|
|
}
|
|
|
});
|
|
|
self.$el.find('.widget-content.widget-loading').css('display','flex');
|
|
|
+
|
|
|
self.fecthInvoice().then(function (accountInvoice) {
|
|
|
return accountInvoice;
|
|
|
}).then(function (accountInvoice) {
|
|
@@ -46,28 +48,28 @@ function ranking_sales_product (widget) {
|
|
|
self.productProduct = productProduct;
|
|
|
return self.fetchGetModelId();
|
|
|
}).then(function(modelId){
|
|
|
- self.modelId=modelId;
|
|
|
+ self.modelId = modelId;
|
|
|
return self.fetchProductRanking();
|
|
|
});
|
|
|
},
|
|
|
// getModelId
|
|
|
- fetchGetModelId: function(){
|
|
|
+ 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){
|
|
|
+ this.alive(getObtjectReference('product', 'product_normal_form_view')).then(function(results) {
|
|
|
defer.resolve(results);
|
|
|
});
|
|
|
+
|
|
|
return defer;
|
|
|
},
|
|
|
// Obtener factura
|
|
|
- fecthInvoice: function(){
|
|
|
+ fecthInvoice: function() {
|
|
|
var self = this;
|
|
|
- var desde =moment().format('YYYY-MM-01');
|
|
|
- var hasta =moment().add(1,'months').format('YYYY-MM-01');
|
|
|
+ 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']]];
|
|
@@ -79,18 +81,19 @@ function ranking_sales_product (widget) {
|
|
|
return defer;
|
|
|
},
|
|
|
// Obtener linea de la factura
|
|
|
- fecthInvoiceLine: function(accountInvoice){
|
|
|
+ fecthInvoiceLine: function(accountInvoice) {
|
|
|
var self = this;
|
|
|
var defer = $.Deferred();
|
|
|
var invoice_line = _.flatten(_.map(accountInvoice, function (item) {
|
|
|
- return item.invoice_line
|
|
|
+ return item.invoice_line;
|
|
|
}));
|
|
|
- var fields =['id', 'product_id', 'quantity'];
|
|
|
+ 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
|
|
@@ -98,11 +101,11 @@ function ranking_sales_product (widget) {
|
|
|
var self = this;
|
|
|
var defer = $.Deferred();
|
|
|
var product_id = _.flatten(_.map(invoiceLine,function (item) {
|
|
|
- return item.product_id[0]
|
|
|
+ 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');
|
|
|
+ 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);
|
|
@@ -110,7 +113,7 @@ function ranking_sales_product (widget) {
|
|
|
|
|
|
return defer;
|
|
|
},
|
|
|
- fetchProductRanking: function(){
|
|
|
+ fetchProductRanking: function() {
|
|
|
var self = this;
|
|
|
var itemProduct;
|
|
|
var itemLine;
|
|
@@ -119,37 +122,43 @@ function ranking_sales_product (widget) {
|
|
|
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]
|
|
|
- });
|
|
|
- }
|
|
|
+ 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) {
|
|
|
+ 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 más vendidos del mes de " + meses[fecha.getMonth()]);
|
|
|
- self.ranking=ranking;
|
|
|
+
|
|
|
+ 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){
|
|
|
+ getInvoiceLine: function (product_id) {
|
|
|
var self = this;
|
|
|
return _.flatten(_.filter(self.invoiceLine,function (line) {
|
|
|
return line.product_id[0] === product_id
|
|
@@ -159,59 +168,83 @@ function ranking_sales_product (widget) {
|
|
|
fetchChart: function (ranking){
|
|
|
var self = this;
|
|
|
var label = [];
|
|
|
- var body = [];
|
|
|
- var item;
|
|
|
+ var bodyProducto = [];
|
|
|
+ var labelService = [];
|
|
|
+ var bodyService = [];
|
|
|
var rank = 7;
|
|
|
+ var item;
|
|
|
|
|
|
- if (ranking.length < rank && ranking.length > 0)
|
|
|
- rank= ranking.length;
|
|
|
+ // if (ranking.length < rank && ranking.length > 0)
|
|
|
+ // rank= ranking.length;
|
|
|
|
|
|
for (var i = 0; i < rank; i++) {
|
|
|
if (ranking[i]){
|
|
|
item = ranking[i];
|
|
|
- }
|
|
|
-
|
|
|
- if (ranking.length === 0){
|
|
|
+ }else{
|
|
|
item = {};
|
|
|
item.product = "N/A";
|
|
|
item.qty = 0;
|
|
|
}
|
|
|
- var name=item.product.split(' ');
|
|
|
+ // 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]+")");
|
|
|
}
|
|
|
-
|
|
|
- body.push(item.qty);
|
|
|
+ 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: 'pie',
|
|
|
+ // 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: body,
|
|
|
+ data: bodyService,
|
|
|
}
|
|
|
]
|
|
|
},
|
|
|
options: {
|
|
|
- scales: {
|
|
|
- xAxes: [{ stacked: true }],
|
|
|
- yAxes: [{ stacked: true }],
|
|
|
+ tooltips: {
|
|
|
+ mode: 'index',
|
|
|
+ intersect: true
|
|
|
},
|
|
|
legend: {
|
|
|
- display: false,
|
|
|
+ labels: {
|
|
|
+ boxWidth: 10,
|
|
|
+ fontSize: 12,
|
|
|
+ },
|
|
|
+ position: 'left',
|
|
|
},
|
|
|
layout: {
|
|
|
- padding:{
|
|
|
+ padding: {
|
|
|
top: 20,
|
|
|
- bottom:0,
|
|
|
- left : 0,
|
|
|
- rigth:0,
|
|
|
+ bottom: 0,
|
|
|
+ left: 0,
|
|
|
+ rigth: 0,
|
|
|
}
|
|
|
},
|
|
|
}
|
|
@@ -224,9 +257,9 @@ function ranking_sales_product (widget) {
|
|
|
model.web.notification.do_warn("Atención","Sin datos");
|
|
|
return
|
|
|
}
|
|
|
- var hoy =moment().format('YYYY-MM-DD');
|
|
|
+ var hoy = moment().format('YYYY-MM-DD');
|
|
|
|
|
|
- var product_id= _.map(self.ranking,function(map){
|
|
|
+ var product_id = _.map(self.ranking,function(map){
|
|
|
return map.id;
|
|
|
})
|
|
|
this.do_action({
|
|
@@ -235,7 +268,7 @@ function ranking_sales_product (widget) {
|
|
|
res_model: "product.product",
|
|
|
views: [[false, 'list'],[self.modelId[1],'form']],
|
|
|
target: 'current',
|
|
|
- domain: [['id', 'in', product_id],['type','=', 'product']],
|
|
|
+ domain: [['id', 'in', product_id]],
|
|
|
context: {},
|
|
|
});
|
|
|
}
|