123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- function ranking_sales_product (widget) {
- "use strict";
- var model= openerp;
- widget.RankingSalesProductWidget = widget.Base.extend({
- template: 'RankingSalesProductTmpl',
- data : [],
- accountInvoice : [],
- invoiceLine : [],
- productProdcut : [],
- init: function (parent) {
- this._super(parent, {
- width : 6,
- height: 4
- });
- },
- start: function () {
- var self = this;
- self.fecthInitial();
- },
- fecthInitial: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(productProdcut){
- self.productProdcut = productProdcut;
- return self.fecthRankingProdcuto();
- });
- return false;
- },
- // Obtener factura
- fecthInvoice :function(){
- var self = this;
- var fecha = new Date
- var desde =fecha.getFullYear()+"-"+(fecha.getMonth()+1)+"-01";
- var hasta =fecha.getFullYear()+"-"+(fecha.getMonth()+2)+"-01";
- var defer =$.Deferred();
- var fields =['id','invoice_line', 'date_invoice'];
- var domain =[['type', '=', 'out_invoice'],['date_invoice', '>=', desde],['date_invoice', '<', hasta],['state', '=',['open','paid']]];
- // date_invoice
- 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','=', 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 prodcut_id = _.flatten(_.map(invoiceLine,function(item){return item.product_id[0]}));
- var fields = ['id', 'name_template', 'type'];
- var domain =[['id', '=', prodcut_id]];
- var prodcut= new model.web.Model('product.product');
- prodcut.query(fields).filter(domain).all().then(function(results){
- defer.resolve(results);
- });
- return defer;
- },
- fecthRankingProdcuto : function(){
- var self = this;
- var itemProduct;
- var itemLine;
- var ranking=[];
- var cat=0;
- var lineUnik;
- for (var i = 0; i < self.productProdcut.length; i++) {
- itemProduct = self.productProdcut[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 :lineUnik.product_id[1],qty:cat });
- }
- }
- }
- 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 Producto mas Vendido Mes de "+meses[fecha.getMonth()]);
- self.fecthChart(ranking);
- },
- // get line
- getInvoiceLine(product_id){
- var self = this;
- return _.flatten(_.filter(self.invoiceLine,function(line){return line.product_id[0] === product_id}));
- },
- // Generar Grafico
- fecthChart: function(ranking){
- var self = this;
- var label=[];
- var body=[];
- var item;
- var rank=10;
- for (var i = 0; i < rank; i++) {
- if (ranking[i]){
- item = ranking[i];
- }else{
- item={};
- item.product="N/A";
- item.qty=0;
- }
- label.push(item.product);
- body.push(item.qty);
- }
- var chart = new Chart(this.$el.find(".widget-content").find('canvas'), {
- // type: 'doughnut',
- type: 'horizontalBar',
- // type: '',
- data: {
- labels: label,
- datasets: [
- {
- backgroundColor: ['#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad'],
- data: body,
- }
- ]
- },
- options: {
- maintainAspectRatio: false,
- layout: {
- padding: 20
- },
- scales: {
- xAxes: [{ stacked: true }],
- yAxes: [{ stacked: true }],
- },
- legend: {
- display: false,
- },
- }
- });
- }
- });
- }
|