pos_product_sales.js 8.21 KB
Newer Older
1 2 3 4 5 6
var table = $('#sales_list');
var active_table = null;
var filter_pareto = $('#filter_pareto');
var raw_lines_data = [];
var pareto_sales_80_data = [];
var parsed_sum_up = {total: 0, sale_to_zero: []};
Administrator committed
7 8

var compute_price_delta_level = function(prices) {
9 10 11 12
    var min = 100000000000000;
    var max = -100000000000000;
    var level = 0;

Administrator committed
13
    for (i in prices) {
14 15
        if (prices[i] < min) min = prices[i];
        if (prices[i] > max) max = prices[i];
Administrator committed
16 17
    }
    if (min == 0) {
18
        level = 10;
Administrator committed
19
    } else {
20 21
        var ratio = max/min;

Administrator committed
22
        if (ratio > 10) {
23
            level = 9;
Administrator committed
24
        } else if (ratio > 5) {
25
            level = 8;
Administrator committed
26
        } else if (ratio > 2) {
27
            level = 7;
Administrator committed
28
        } else if (ratio > 1.75) {
29
            level = 6;
Administrator committed
30
        } else if (ratio > 1.5) {
31
            level = 5;
Administrator committed
32
        } else if (ratio > 1.4) {
33
            level = 4;
Administrator committed
34
        } else if (ratio > 1.3) {
35
            level = 3;
Administrator committed
36
        } else if (ratio > 1.2) {
37
            level = 2;
Administrator committed
38
        } else if (ratio > 1.1) {
39
            level = 1;
Administrator committed
40
        }
41 42 43 44
    }

    return level;
};
Administrator committed
45 46
//console.log(window.sales_data)

47
$.each(window.sales_data.sales.products, function(pid, elt) {
Administrator committed
48 49
    //clef elts = discount_x, details, qty, total
    try {
50 51 52
        product = window.sales_data.products[pid];
        raw_lines_data.push({id: pid, barcode: product.barcode, name: product.p_name, categ: window.sales_data.pcat[product.categ_id], qty: elt.qty, total: elt.total, details: elt.details});
        parsed_sum_up.total += elt.total;
Administrator committed
53
    } catch {
54
        console.log("Pb avec produit " + pid);
Administrator committed
55
    }
56
});
Administrator committed
57 58 59
// console.log(window.sales_data)
// console.log(parsed_sum_up.total)
var display_table = function (data) {
60 61 62 63 64 65 66 67
    var cols = [
        {data: 'barcode', title: "Code-barre", className: 'barcode'},
        {data: 'name', title: "Nom", className: 'name'},
        {data: 'categ', title: "Catégorie", className: 'categ'},
        {data: 'qty', title: "Qté"},
        {data: 'total', title: "Total TTC", className: 'ttc'},
        {data: 'details', title: "Détails", className: 'details'}
    ];
Administrator committed
68
    var select = {
69 70 71 72
        style:    'os',
        selector: 'td:first-child'

    };
Administrator committed
73 74

    var settings = {
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
        dom: '<lf<t>i><"clear"><B>',
        lengthMenu : [
            [
                10,
                25,
                50,
                100,
                -1
            ],
            [
                10,
                25,
                50,
                100,
                'Tout'
            ]
        ],
        buttons: [],
        columns: cols,
        select: select,
        rowId : "id",
        data : data,
        language: {url : '/static/js/datatables/french.json'},
        createdRow: function(row, rdata, index) {
            var details = '';
            var nb_dates = Object.keys(rdata.details).length;
            var anomalie_classes = [];

            if (nb_dates > 0) {
                details = $('<div>');
                list = $('<ul>').css('display', 'none');
                var prices = [];
                var price_delta_level = 0;
                var price_1 = false;
                var price_0 = false;

                for (d in rdata.details) {
                    var l = $('<li>');
                    var detail_info = rdata.details[d].date + ' : ' + rdata.details[d].val + '€, ';

                    detail_info += rdata.details[d].qty + ' en ' + rdata.details[d].transacs.length + ' transac.';
                    if (rdata.details[d].transacs.length > 1) {
                        detail_info = $('<a>').addClass('open_day_details')
                            .text(detail_info);
                        day_list = $('<ul>').css('display', 'none');
                        $.each(rdata.details[d].transacs, function(idx, transac) {
                            var det_line = $('<li>');
                            var pfound = false;

                            for (pidx in prices) {
                                if (prices[pidx] == transac.price) pfound = true;
                            }
                            if (pfound == false || prices.length == 0) prices.push(transac.price);
                            if (transac.price == 1) {
                                price_1 = true;
                                day_list.addClass('pr_1');
                                det_line.addClass('pr_1');
                                l.addClass('pr_1');
                            } else if (transac.price == 0) {
                                price_0 = true;
                                day_list.addClass('pr_0');
                                det_line.addClass('pr_0');
                                l.addClass('pr_0');
                            }
                            var full_d = transac.qty + ' x ' + transac.price + '€ = ' + transac.subtotal;

                            if (transac.discount > 0)
                                full_d += ' (remise=' + transac.discount +')';
                            det_line.text(full_d);
                            day_list.append(det_line);
                        });

                        detail_info.append(day_list);
Administrator committed
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
                    if (price_1 == true) anomalie_classes.push('pr_1');
                    if (price_0 == true) anomalie_classes.push('pr_0');
                    l.html(detail_info);
                    list.append(l);
                }

                if (prices.length > 1) price_delta_level = compute_price_delta_level(prices);
                if (price_delta_level > 0) anomalie_classes.push('pdl_'+price_delta_level);
                var date_msg = nb_dates + ' date';

                if (nb_dates > 1) date_msg += 's';
                text = $('<a>').addClass('open_details')
                    .text(date_msg);
                details.append(text);
                details.append(list);
            }
            $(row).addClass(anomalie_classes.join(' '));
            $(row).find('td')
                .last()
                .html(details);

        },
        initComplete: function() {


        }
    };

    active_table = table.DataTable(settings);
    table.show();
    table.off('click', '.open_details');
    table.off('click', '.open_day_details');

    table.on('click', '.open_details', function() {
        var clicked = $(this);
        var details = clicked.closest('td').find('ul')
            .first();

Administrator committed
187
        if (details.css('display') == 'none') {
188
            details.show();
Administrator committed
189
        } else {
190
            details.hide();
Administrator committed
191
        }
192 193 194 195 196 197

    });
    table.on('click', '.open_day_details', function() {
        var clicked = $(this);
        var details = clicked.find('ul');

Administrator committed
198
        if (details.css('display') == 'none') {
199
            details.show();
Administrator committed
200
        } else {
201
            details.hide();
Administrator committed
202
        }
203 204 205

    });
};
Administrator committed
206 207

function get_x_pc_sales_products() {
208 209 210 211 212
    var kept = [];
    var local_sum = 0;
    var ref_sum = window.sales_data.sales.full_total - window.sales_data.sales.full_discount;
    var pareto_num = $('#pareto_num').val();

Administrator committed
213
    if (isNaN(pareto_num))
214
        pareto_num = 0.8;
Administrator committed
215
    else
216 217 218 219 220 221
        pareto_num = pareto_num / 100;
    raw_lines_data.sort(function(a, b) {
        return parseFloat(b.total) - parseFloat(a.total);
    });
    $.each(raw_lines_data, function(i, e) {
        local_sum += e.total;
Administrator committed
222
        if (local_sum < ref_sum * pareto_num) {
223
            kept.push(e);
Administrator committed
224
        }
225 226 227
    });

    return kept;
Administrator committed
228 229 230
}
function display_sum_up() {
    // verifier delta parsed_sum_up.total et window.sales_data.sales.full_total - window.sales_data.sales.full_discount
231 232 233 234 235 236 237 238 239
    var ttc_ar = window.sales_data.sales.full_total;
    var total_r = window.sales_data.sales.full_discount;
    var msg = '<strong>Total TTC</strong> : ' + ttc_ar + ' - ' + total_r + ' (remises) ';

    msg += '= <strong>' + (ttc_ar - total_r) + ' €</strong>';
    msg += ' (' + window.sales_data.sales.orders + ' passages en caisse ';
    msg += ' --> ' + window.sales_data.sales.pol_nb + ' lignes de tickets)';
    msg += ' réalisé avec ' + window.sales_data.sales.partners + ' coopérateurs différents';
    $('#sum_up').html(msg);
Administrator committed
240 241 242
}

filter_pareto.click(function() {
243
    active_table.destroy();
Administrator committed
244
    if ($(this).is(':checked')) {
245
        display_table(get_x_pc_sales_products());
Administrator committed
246
    } else {
247
        display_table(raw_lines_data);
Administrator committed
248
    }
249 250 251
});
display_sum_up();
display_table(raw_lines_data);