123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- function chart_purchase_expense (widget) {
- "use strict";
- var model = openerp;
- widget.ChartPurchaseExpenseWidget = widget.Base.extend({
- template: 'ChartPurchaseExpense',
- data: [],
- events: {
- 'click .month': 'showMonth',
- 'click .week': 'showWeek',
- 'click .days': 'showDays',
- },
-
- 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.fecthAccountInvoice().then(function (AccountInvoice) {
- return AccountInvoice;
- }).then(function (AccountInvoice) {
- self.AccountInvoice = AccountInvoice;
- return self.showWeek();
- });
- },
- // Usuario Logeado
- fetchCurrentUser: function() {
- var self = this;
- var ResUser = new model.web.Model('res.users');
- return ResUser.call('get_user', {
- context: new model.web.CompoundContext()
- });
- },
- // Lista de Graficos disponibles para el usuario
- fetchResUser: function(id) {
- var self = this;
- var defer = $.Deferred();
- var fields = ['id','name','chart_ids'];
- var domain = [['id','=',id]];
- var ResUser = new model.web.Model('res.users');
- ResUser.query(fields).filter(domain).all().then(function (results) {
- defer.resolve(results);
- });
- return defer;
- },
- // Obtener detalles de la lista de graficos
- fetchChartList: function(chart_ids) {
- var self = this;
- var defer = $.Deferred();
- var fields = ['id','name'];
- var domain = [['id','in',chart_ids]];
- var ChartList = new model.web.Model('chart.list');
- ChartList.query(fields).filter(domain).all().then(function (results) {
- defer.resolve(results);
- });
- return defer;
- },
- fecthAccountInvoice: function() {
- var self = this;
- var defer = $.Deferred();
- var fields = ['id','date_invoice','amount_total','origin'];
- var domain = [['state','in',['paid','open']],['type','=','in_invoice']];
- var AccountInvoice = new model.web.Model('account.invoice');
- AccountInvoice.query(fields).filter(domain).all().then(function (results) {
- defer.resolve(results);
- });
- return defer;
- },
- // Facturas de proveedor - Meses
- getMonthAccountInvoicePurchase:function(month) {
- var self = this;
- if (month < 10){
- var fecha = moment().format('YYYY')+'-'+'0'+month;
- }else{
- 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;
- }));
- },
- getMonthAccountInvoiceExpense:function(month) {
- var self = this;
- if (month < 10){
- var fecha = moment().format('YYYY')+'-'+'0'+month;
- }else{
- 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;
- }));
- },
- // Facturas de proveedor - Semana Actual
- 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;
- }));
- },
- // Facturas de proveedor - 15 dias
- getDaysAccountInvoicePurchase:function(day) {
- var self = this;
- var date = moment().subtract(day, 'days').format('YYYY-MM-DD');
- return _.flatten(_.filter(self.AccountInvoice,function (inv) {
- return moment(inv.date_invoice).format('YYYY-MM-DD') === date & inv.origin !== false;
- }));
- },
- getDaysAccountInvoiceExpense:function(day) {
- var self = this;
- var date = moment().subtract(day, 'days').format('YYYY-MM-DD');
- return _.flatten(_.filter(self.AccountInvoice,function (inv) {
- return moment(inv.date_invoice).format('YYYY-MM-DD') === date & inv.origin === false;
- }));
- },
-
- showMonth: function() {
- var self = this;
- var invoices;
- var title = ['Enero', 'Febrero', 'Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
- var dataPurchase = [];
- var dataExpense = [];
- for (var i = 1; i <= 12; i++) {
- invoices = self.getMonthAccountInvoicePurchase(i);
- var total = _.reduce(_.map(invoices,function(item) {
- return item.amount_total;
- }),function(memo, num) {
- return memo + num;
- },0);
- dataPurchase.push(total)
- }
-
- for (var i = 1; i <= 12; 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(total)
- }
- self.$el.unblock();
- self.$el.find('.widget-content.widget-loading').css('display','none');
- self.fetchChart(dataPurchase, dataExpense, title);
- },
- showWeek: function() {
- var self = this;
- var invoices;
- var title = ['Lunes','Martes','Miercoles','Jueves','Viernes','Sabado','Domingo'];
- 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(total);
- }
-
- 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) {
- return memo + num;
- },0);
- dataExpense.push(total);
- }
- self.$el.unblock();
- self.$el.find('.widget-content.widget-loading').css('display','none');
- self.fetchChart(dataPurchase, dataExpense, title);
- },
- showDays: function() {
- var self = this;
- var title = [];
- var invoices;
- var dataPurchase = [];
- var dataExpense = [];
- for (var i = 15; i >= 0; i--) {
- invoices = self.getDaysAccountInvoicePurchase(i);
- var total = _.reduce(_.map(invoices,function(item) {
- return item.amount_total;
- }),function(memo, num) {
- return memo + num;
- },0);
- dataPurchase.push(total);
- title.push(moment().subtract(i, 'days').format('DD / MM'));
- }
-
- for (var i = 15; i >= 0; i--) {
- invoices = self.getDaysAccountInvoiceExpense(i);
- var total = _.reduce(_.map(invoices,function(item) {
- return item.amount_total;
- }),function(memo, num) {
- return memo + num;
- },0);
- dataExpense.push(total);
- }
- self.$el.unblock();
- self.$el.find('.widget-content.widget-loading').css('display','none');
- self.fetchChart(dataPurchase, dataExpense, title);
- },
- fetchChart: function (dataPurchase, dataExpense, title) {
- var self = this;
- var label = title;
- var bodyPurchase = dataPurchase;
- var bodyExpense = dataExpense;
- // 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: 5,
- bottom: 20,
- left : 0,
- rigth: 0,
- }
- },
- }
- });
- },
- });
- }
|