|
@@ -6,22 +6,24 @@ function chart_purchase_expense (widget) {
|
|
|
widget.ChartPurchaseExpenseWidget = widget.Base.extend({
|
|
|
template: 'ChartPurchaseExpense',
|
|
|
data: [],
|
|
|
- resPartner: [],
|
|
|
- modelId: [],
|
|
|
|
|
|
events: {
|
|
|
-
|
|
|
+ 'click .month': 'showMonth',
|
|
|
+ 'click .week': 'showWeek',
|
|
|
},
|
|
|
+
|
|
|
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({
|
|
@@ -37,28 +39,10 @@ function chart_purchase_expense (widget) {
|
|
|
return AccountInvoice;
|
|
|
}).then(function (AccountInvoice) {
|
|
|
self.AccountInvoice = AccountInvoice;
|
|
|
- return self.fetchGetModelId();
|
|
|
- }).then(function(modelId) {
|
|
|
- self.modelId = modelId;
|
|
|
- return self.BuildChart();
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- // 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('base', 'view_partner_form')).then(function(results) {
|
|
|
- defer.resolve(results);
|
|
|
+ return self.showWeek();
|
|
|
});
|
|
|
-
|
|
|
- return defer;
|
|
|
},
|
|
|
|
|
|
- // Obtener facturas
|
|
|
fecthAccountInvoice: function() {
|
|
|
var self = this;
|
|
|
var defer = $.Deferred();
|
|
@@ -72,43 +56,55 @@ function chart_purchase_expense (widget) {
|
|
|
return defer;
|
|
|
},
|
|
|
|
|
|
- // Obtener facturas por cada proveedores
|
|
|
- getAccountInvoicePurchase:function(mes) {
|
|
|
+ getMonthAccountInvoicePurchase:function(month) {
|
|
|
var self = this;
|
|
|
- if (mes < 10){
|
|
|
- var fecha = moment().format('YYYY')+'-'+'0'+mes;
|
|
|
+ if (month < 10){
|
|
|
+ var fecha = moment().format('YYYY')+'-'+'0'+month;
|
|
|
}else{
|
|
|
- var fecha = moment().format('YYYY')+'-'+mes;
|
|
|
+ var fecha = moment().format('YYYY')+'-'+month;
|
|
|
}
|
|
|
return _.flatten(_.filter(self.AccountInvoice,function (inv) {
|
|
|
return moment(inv.date_invoice).format('YYYY-MM') === fecha & inv.origin !== false;
|
|
|
}));
|
|
|
},
|
|
|
|
|
|
- // Obtener facturas de clientes
|
|
|
- getAccountInvoiceExpense:function(mes) {
|
|
|
+ getMonthAccountInvoiceExpense:function(month) {
|
|
|
var self = this;
|
|
|
- if (mes < 10){
|
|
|
- var fecha = moment().format('YYYY')+'-'+'0'+mes;
|
|
|
+ if (month < 10){
|
|
|
+ var fecha = moment().format('YYYY')+'-'+'0'+month;
|
|
|
}else{
|
|
|
- var fecha = moment().format('YYYY')+'-'+mes;
|
|
|
+ var fecha = moment().format('YYYY')+'-'+month;
|
|
|
}
|
|
|
return _.flatten(_.filter(self.AccountInvoice,function (inv) {
|
|
|
return moment(inv.date_invoice).format('YYYY-MM') === fecha & inv.origin === false;
|
|
|
}));
|
|
|
},
|
|
|
-
|
|
|
- // Generar el Ranking
|
|
|
- BuildChart: function() {
|
|
|
+
|
|
|
+ getWeekAccountInvoicePurchase:function(day) {
|
|
|
+ var self = this;
|
|
|
+ var week = moment().week();
|
|
|
+ return _.flatten(_.filter(self.AccountInvoice,function (inv) {
|
|
|
+ return moment(inv.date_invoice).week() === week & inv.origin !== false & moment(inv.date_invoice).isoWeekday() === day;
|
|
|
+ }));
|
|
|
+ },
|
|
|
+
|
|
|
+ getWeekAccountInvoiceExpense:function(day) {
|
|
|
+ var self = this;
|
|
|
+ var week = moment().week();
|
|
|
+ return _.flatten(_.filter(self.AccountInvoice,function (inv) {
|
|
|
+ return moment(inv.date_invoice).week() === week & inv.origin === false & moment(inv.date_invoice).isoWeekday() === day;
|
|
|
+ }));
|
|
|
+ },
|
|
|
+
|
|
|
+ showMonth: function() {
|
|
|
var self = this;
|
|
|
- console.log(self);
|
|
|
var invoices;
|
|
|
var mes = 0;
|
|
|
var dataPurchase = [];
|
|
|
var dataExpense = [];
|
|
|
|
|
|
for (var i = 1; i <= 12; i++) {
|
|
|
- invoices = self.getAccountInvoicePurchase(i);
|
|
|
+ invoices = self.getMonthAccountInvoicePurchase(i);
|
|
|
var total = _.reduce(_.map(invoices,function(item) {
|
|
|
return item.amount_total;
|
|
|
}),function(memo, num) {
|
|
@@ -120,7 +116,43 @@ function chart_purchase_expense (widget) {
|
|
|
}
|
|
|
|
|
|
for (var i = 1; i <= 12; i++) {
|
|
|
- invoices = self.getAccountInvoiceExpense(i);
|
|
|
+ invoices = self.getMonthAccountInvoiceExpense(i);
|
|
|
+ var total = _.reduce(_.map(invoices,function(item) {
|
|
|
+ return item.amount_total;
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ dataExpense.push({
|
|
|
+ amount_total: accounting.formatNumber(total,0,".",","),
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ self.$el.unblock();
|
|
|
+ self.$el.find('.widget-content.widget-loading').css('display','none');
|
|
|
+ self.fetchMonthChart(dataPurchase, dataExpense);
|
|
|
+ },
|
|
|
+
|
|
|
+ showWeek: function() {
|
|
|
+ var self = this;
|
|
|
+ var invoices;
|
|
|
+ var mes = 0;
|
|
|
+ var dataPurchase = [];
|
|
|
+ var dataExpense = [];
|
|
|
+
|
|
|
+ for (var i = 1; i <= 7; i++) {
|
|
|
+ invoices = self.getWeekAccountInvoicePurchase(i);
|
|
|
+ var total = _.reduce(_.map(invoices,function(item) {
|
|
|
+ return item.amount_total;
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ dataPurchase.push({
|
|
|
+ amount_total: accounting.formatNumber(total,0,".",","),
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ for (var i = 1; i <= 7; i++) {
|
|
|
+ invoices = self.getWeekAccountInvoiceExpense(i);
|
|
|
var total = _.reduce(_.map(invoices,function(item) {
|
|
|
return item.amount_total;
|
|
|
}),function(memo, num) {
|
|
@@ -131,14 +163,12 @@ function chart_purchase_expense (widget) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- // self.data = data;
|
|
|
self.$el.unblock();
|
|
|
self.$el.find('.widget-content.widget-loading').css('display','none');
|
|
|
- self.fetchChart(dataPurchase, dataExpense);
|
|
|
+ self.fetchWeekChart(dataPurchase, dataExpense);
|
|
|
},
|
|
|
|
|
|
- // Generar Grafico
|
|
|
- fetchChart: function (dataPurchase,dataExpense) {
|
|
|
+ fetchMonthChart: function (dataPurchase,dataExpense) {
|
|
|
var self = this;
|
|
|
var label = ['Enero', 'Febrero', 'Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
|
|
|
var bodyPurchase = [];
|
|
@@ -146,7 +176,6 @@ function chart_purchase_expense (widget) {
|
|
|
var item;
|
|
|
var rank = 12;
|
|
|
|
|
|
- // InInvoice
|
|
|
if (dataPurchase.length < rank && dataPurchase.length > 0){
|
|
|
rank= ranking.length;
|
|
|
}
|
|
@@ -161,8 +190,6 @@ function chart_purchase_expense (widget) {
|
|
|
bodyPurchase.push(item.amount_total);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- // OutInvoice
|
|
|
if (dataExpense.length < rank && dataExpense.length > 0){
|
|
|
rank= ranking.length;
|
|
|
}
|
|
@@ -233,5 +260,98 @@ function chart_purchase_expense (widget) {
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
+
|
|
|
+ fetchWeekChart: function (dataPurchase,dataExpense) {
|
|
|
+ var self = this;
|
|
|
+ var label = ['Lunes', 'Martes', 'Miercoles','Jueves','Viernes','Sabado','Domingo'];
|
|
|
+ var bodyPurchase = [];
|
|
|
+ var bodyExpense = [];
|
|
|
+ var item;
|
|
|
+ var rank = 7;
|
|
|
+
|
|
|
+ if (dataPurchase.length < rank && dataPurchase.length > 0){
|
|
|
+ rank= ranking.length;
|
|
|
+ }
|
|
|
+ for (var i = 0; i < rank; i++) {
|
|
|
+ if (dataPurchase[i]) {
|
|
|
+ item = dataPurchase[i];
|
|
|
+ }
|
|
|
+ if (dataPurchase.length === 0) {
|
|
|
+ item = {};
|
|
|
+ item.amount_total = 0;
|
|
|
+ }
|
|
|
+ bodyPurchase.push(item.amount_total);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dataExpense.length < rank && dataExpense.length > 0){
|
|
|
+ rank= ranking.length;
|
|
|
+ }
|
|
|
+ for (var i = 0; i < rank; i++) {
|
|
|
+ if (dataExpense[i]) {
|
|
|
+ item = dataExpense[i];
|
|
|
+ }
|
|
|
+ if (dataExpense.length === 0) {
|
|
|
+ item = {};
|
|
|
+ item.amount_total = 0;
|
|
|
+ }
|
|
|
+ bodyExpense.push(item.amount_total);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Global method for setting Y axis number format.
|
|
|
+ Chart.scaleService.updateScaleDefaults('linear', {
|
|
|
+ ticks: {
|
|
|
+ callback: function(tick) {
|
|
|
+ return tick.toLocaleString('de-DE');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ Chart.defaults.global.tooltips.callbacks.label = function(tooltipItem, data) {
|
|
|
+ var dataset = data.datasets[tooltipItem.datasetIndex];
|
|
|
+ var datasetLabel = dataset.label || '';
|
|
|
+ return datasetLabel + dataset.data[tooltipItem.index].toLocaleString('de-DE');
|
|
|
+ };
|
|
|
+ var chart = new Chart(this.$el.find(".widget-content").find('canvas'), {
|
|
|
+ type: 'line',
|
|
|
+ data: {
|
|
|
+ labels: label,
|
|
|
+ datasets: [
|
|
|
+ {
|
|
|
+ label: 'Compras ',
|
|
|
+ data: bodyPurchase,
|
|
|
+ backgroundColor: '#e3f2fd',
|
|
|
+ borderColor: '#64b5f6',
|
|
|
+ borderWidth: 2,
|
|
|
+ fill: false,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: 'Gastos ',
|
|
|
+ data: bodyExpense,
|
|
|
+ backgroundColor: '#c8e6c9',
|
|
|
+ borderColor: '#66bb6a',
|
|
|
+ borderWidth: 2,
|
|
|
+ fill: false,
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ options: {
|
|
|
+ responsive: true,
|
|
|
+ title: {
|
|
|
+ display: true,
|
|
|
+ },
|
|
|
+ hover: {
|
|
|
+ mode: 'nearest',
|
|
|
+ intersect: true
|
|
|
+ },
|
|
|
+ layout: {
|
|
|
+ padding: {
|
|
|
+ top: 0,
|
|
|
+ bottom: 15,
|
|
|
+ left : 0,
|
|
|
+ rigth: 0,
|
|
|
+ }
|
|
|
+ },
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
});
|
|
|
}
|