backend.js 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
openerp.lacagette_pos_data = function (instance) {
    var numMedian = function(a) {
    	a = a.slice(0).sort(function(x, y) {
    	    return x - y;
    	});
    	var b = (a.length + 1) / 2;
    	return (a.length % 2) ? a[b - 1] : (a[b - 1.5] + a[b - 0.5]) / 2;
    }
    var averageQty = function(a, back_qty) {
    	//compute sum of the last "back_qty" elements of a
    	var ac = a,
    	    sum = average = 0,
    	    i = 0
    	while (i < back_qty && ac.length > 0) {
    	    i += 1
    	    sum += ac.pop()
    	}
    	if (i > 0) average = Math.round((sum / i) * 100) / 100
    	return average
    }

    // Declare which method has to be called according ir.actions.client
    instance.web.client_actions.add('custom_pos_sales', 'instance.lacagette_pos_data.ShowProductSales');

    instance.lacagette_pos_data.ShowProductSales = instance.web.Widget.extend({
      template: 'product_pos_sale_details',
      className: 'oe_pos_data',
    	context: null,
    	init: function(parent, action) {
    	    this._super(parent, action);
    	    this.context = action.context
    	    //console.log(action)
    	},
      start: function () {
    	    jQuery('.modal-title').text("Vente POS de l'article")

    	    new instance.web.Model('lacagette.pos_product_sales').call('get_product_tempate_sales', [this.context.active_id, {interval: 'week'}])
        		.then(function(result){
        		    if (typeof result.error == "undefined"){
            			var average_qty_per_week = Math.round((result.non_empty_weeks_sales / result.non_empty_weeks) * 100)/100
            			var weeks_without_sales_pc = Math.round(100 - (result.non_empty_weeks / result.labels.length) * 100)
            			var non_empty_qties = result.data.filter(function(val) {
                     return val !== 0;
                  });
            			var median_qty_without_empty = numMedian(non_empty_qties)
            			var average_last_4_non_empty_weeks = averageQty(non_empty_qties, 4)
            			var comment = "Semaines sans vente : " + weeks_without_sales_pc + "% <br/>"
            			comment += "Vente hebdomadaire moyenne (hors semaines vides) : "+average_qty_per_week + "<br/>"
            			comment += "Quantité hebdomadaire médiane (hors semaines vides) : " + median_qty_without_empty + "<br/>"
            			comment += "Quantité hebdomadaire moyenne (4 dernières semaines non vides) " + average_last_4_non_empty_weeks
            			jQuery('#pos_data_sales_comments').html(comment)

            			var config = {
            			    type: 'bar',
            			    data: {labels: result.labels,
            				   datasets: [{
            				       label: 'Quantités vendues',
            				       data: result.data
            				   }]
            				  },
            			    options: {
          				       title: {
              					   display: true,
              					   text: 'Graph. des ventes par semaine'
          				       },
                         scales: {
                    				yAxes: [{
                        			ticks: {
                                 beginAtZero: true
                               }
                             }]
                         },
            				     legend: { display: false }
            			    }
            			};
            			myChart =  new Chart(document.getElementById("product_sales_chart"), config);
        		    }
        		})

          return this._super()
      },
    	destroy: function(){
    	   return this._super()
    	}
    });

};