backend.js 8.98 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
openerp.lacagette_sales_helper = 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_product_losses', 'instance.lacagette_sales_helper.ShowProductLosses');
    instance.web.client_actions.add('custom_product_autoconso', 'instance.lacagette_sales_helper.ShowProductAutoconso');
    instance.web.client_actions.add('custom_product_meals', 'instance.lacagette_sales_helper.ShowProductMeals');

    instance.lacagette_sales_helper.ShowProductLosses = instance.web.Widget.extend({
      template: 'product_losses_details',
      className: 'oe_stock_movements_data',
    	context: null,
    	init: function(parent, action) {
    	    this._super(parent, action);
    	    this.context = action.context
    	    //console.log(action)
    	},
      start: function () {
    	    jQuery('.modal-title').text("Pertes de l'article")

    	    new instance.web.Model('lacagette.sales_helper')
            .call('get_product_template_data', [this.context.active_id, {interval: 'week', type: 'losses'}])
        		.then(function(result){
        		    if (typeof result.error == "undefined"){
                  var average_qty_moved_per_week = Math.round((result.non_empty_weeks_qty / result.nb_non_empty_weeks) * 100)/100
                  var weeks_without_movement = Math.round(100 - (result.nb_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 pertes : " + weeks_without_movement + "% <br/>"
                  comment += "Perte hebdomadaire moyenne (hors semaines vides) : " + average_qty_moved_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('#product_losses_comments').html(comment)

            			var config = {
            			    type: 'bar',
            			    data: {
                        labels: result.labels,
              				  datasets: [{
            				      label: 'Quantités en pertes',
            				      data: result.data
				                }]
            				  },
            			    options: {
          				       title: {
              					   display: true,
              					   text: 'Graph. des pertes par semaine'
          				       },
                         scales: {
                    				yAxes: [{
                        			ticks: {
                                 beginAtZero: true
                               }
                             }]
                         },
            				     legend: { display: false }
            			    }
            			};

            			myChart =  new Chart(document.getElementById("product_losses_chart"), config);
        		    }
        		})

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

    instance.lacagette_sales_helper.ShowProductAutoconso = instance.web.Widget.extend({
      template: 'product_autoconso_details',
      className: 'oe_stock_movements_data',
    	context: null,
    	init: function(parent, action) {
    	    this._super(parent, action);
    	    this.context = action.context
    	    //console.log(action)
    	},
      start: function () {
    	    jQuery('.modal-title').text("Autoconsommation de l'article")

    	    new instance.web.Model('lacagette.sales_helper')
            .call('get_product_template_data', [this.context.active_id, {interval: 'week', type: 'autoconso'}])
        		.then(function(result){
        		    if (typeof result.error == "undefined"){
                  var average_qty_moved_per_week = Math.round((result.non_empty_weeks_qty / result.nb_non_empty_weeks) * 100)/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 = "Autoconsommation hebdomadaire moyenne (hors semaines vides) : " + average_qty_moved_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('#product_autoconso_comments').html(comment)

            			var config = {
            			    type: 'bar',
            			    data: {
                        labels: result.labels,
              				  datasets: [{
            				      label: 'Quantités en autoconso',
            				      data: result.data
				                }]
            				  },
            			    options: {
          				       title: {
              					   display: true,
              					   text: 'Graph. des autoconso par semaine'
          				       },
                         scales: {
                    				yAxes: [{
                        			ticks: {
                                 beginAtZero: true
                               }
                             }]
                         },
            				     legend: { display: false }
            			    }
            			};

            			myChart =  new Chart(document.getElementById("product_autoconso_chart"), config);
        		    }
        		})

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

    instance.lacagette_sales_helper.ShowProductMeals = instance.web.Widget.extend({
      template: 'product_meals_details',
      className: 'oe_stock_movements_data',
    	context: null,
    	init: function(parent, action) {
    	    this._super(parent, action);
    	    this.context = action.context
    	    //console.log(action)
    	},
      start: function () {
    	    jQuery('.modal-title').text("Repas salariés de l'article")

    	    new instance.web.Model('lacagette.sales_helper')
            .call('get_product_template_data', [this.context.active_id, {interval: 'week', type: 'meals'}])
        		.then(function(result){
        		    if (typeof result.error == "undefined"){
                  var average_qty_moved_per_week = Math.round((result.non_empty_weeks_qty / result.nb_non_empty_weeks) * 100)/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 = "Qté en Repas salarié hebdomadaire moyenne (hors semaines vides) : " + average_qty_moved_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('#product_meals_comments').html(comment)

            			var config = {
            			    type: 'bar',
            			    data: {
                        labels: result.labels,
              				  datasets: [{
            				      label: 'Quantités en repas salarié',
            				      data: result.data
				                }]
            				  },
            			    options: {
          				       title: {
              					   display: true,
              					   text: 'Graph. des repas salariés par semaine'
          				       },
                         scales: {
                    				yAxes: [{
                        			ticks: {
                                 beginAtZero: true
                               }
                             }]
                         },
            				     legend: { display: false }
            			    }
            			};

            			myChart =  new Chart(document.getElementById("product_meals_chart"), config);
        		    }
        		})

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

};