sales.js 5.29 KB
Newer Older
Damien Moulard committed
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
var dateFormat = "yy-mm-dd",
    from_datepicker = null,
    to_datepicker = null,
    orders_table = null;

// Return a date from a string if valid, else return null
function getDate(element) {
    var date = null;

    try {
        date = $.datepicker.parseDate(dateFormat, element);
    } catch (error) {
        date = null;
    }

    return date;
}

// Enable validation button if all fields are valid
function enable_validation() {
    if (getDate(from_datepicker.val()) &&
        getDate(to_datepicker.val())) {

        $('#dates_selection_button').prop('disabled', false);
    } else {
        $('#dates_selection_button').prop('disabled', true);
    }
}

function display_orders(orders) {
    // Empty datatable if already exists
    if (orders_table) {
        orders_table.destroy();
    }

    orders_table = $('#orders_table').DataTable({
        data: orders,
        columns:[
            {
                data:"create_date",
41
                title:"Date enregistrement",
Damien Moulard committed
42 43
                width: "10%"
            },
44 45 46 47 48 49
            {
                data:"pos_order_name",
                title:"Ref. Caisse",
                width: "10%"
            },

Damien Moulard committed
50
            {
51
                data:"partner",
Damien Moulard committed
52
                title:"Membre"
Damien Moulard committed
53 54 55
            },
            {
                data:"total_amount",
56
                title: "Montant dû",
Damien Moulard committed
57 58 59 60 61 62 63 64 65 66
                render: function (data) {
                    return parseFloat(data).toFixed(2) + ' €';
                }
            },
            {
                data:"payments",
                title:"Paiements",
                orderable: false,
                render: function (data) {
                    let res = '<ul>';
67

Damien Moulard committed
68
                    for (p of data) {
69
                        res += `<li>${p.journal_id[1]} : ${p.amount} €</li>`;
Damien Moulard committed
70
                    }
71 72
                    res += "</ul>";

73
                    return res; 
Damien Moulard committed
74
                }
75 76 77 78 79 80 81 82
            },
            {
                data:"payments[0].meal_voucher_issuer",
                title: "Émetteur CB Déj",
                render: function (data) {
                    return data === 'false' || data == false ? '' : data;
                }
            },
Damien Moulard committed
83 84 85 86 87 88 89
        ],
        order: [
            [
                0,
                "asc"
            ]
        ],
Damien Moulard committed
90 91 92 93 94 95 96 97 98
        buttons: [
            {
                extend: 'excelHtml5',
                text: 'Export en Excel',
                className: 'btn--primary btn_export_movements'
            }
        ],
        dom: '<lr<t>ip><"clear"><B>',
        iDisplayLength: 100,
Damien Moulard committed
99 100 101 102 103 104 105 106 107 108 109
        language: {url : '/static/js/datatables/french.json'}
    });

    $('.main').show();
}


function get_sales() {
    openModal();

    var url = "/sales/get_sales";
110

Damien Moulard committed
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
    url += '?from=' + encodeURIComponent(from_datepicker.val());
    url += '&to=' + encodeURIComponent(to_datepicker.val());

    $.ajax({
        type: 'GET',
        url: url,
        dataType:"json",
        traditional: true,
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            display_orders(data.res);
            closeModal();
        },
        error: function(data) {
            err = {msg: "erreur serveur lors de la sélection des mouvements de stock", ctx: 'get_movements'};
            if (typeof data.responseJSON != 'undefined' && typeof data.responseJSON.error != 'undefined') {
                err.msg += ' : ' + data.responseJSON.error;
            }
            report_JS_error(err, 'stock');

            closeModal();
            alert('Erreur lors de la récupération, réessayez plus tard');
        }
    });
}


$(document).ready(function() {
    $.ajaxSetup({ headers: { "X-CSRFToken": getCookie('csrftoken') } });

    // Set datepicker
    $.datepicker.regional['fr'] = {
        monthNames: [
            'Janvier',
            'Fevrier',
            'Mars',
            'Avril',
            'Mai',
            'Juin',
            'Juillet',
            'Aout',
            'Septembre',
            'Octobre',
            'Novembre',
            'Decembre'
        ],
        dayNamesMin: [
            'Di',
            'Lu',
            'Ma',
            'Me',
            'Je',
            'Ve',
            'Sa'
        ],
        dateFormat: dateFormat,
        firstDay: 1,
        minDate: 1,
        maxDate: '+12M +0D'
    };
    $.datepicker.setDefaults($.datepicker.regional['fr']);

    from_datepicker = $("#from")
        .datepicker({
            defaultDate: "-1d",
            changeMonth: true,
            changeYear: true,
            yearRange: "-03:+00",
            minDate: new Date(2007, 1 - 1, 1),
            maxDate: new Date()
        })
        .on("change", function() {
            to_datepicker.datepicker("option", "minDate", getDate(this.value));
        });

    to_datepicker = $("#to")
        .datepicker({
            defaultDate: "-1d",
            changeMonth: true,
            changeYear: true,
            yearRange: "-03:+00",
            minDate: new Date(2007, 1 - 1, 1),
            maxDate: new Date()
        })
        .on("change", function() {
            from_datepicker.datepicker("option", "maxDate", getDate(this.value));
        });

    $('.select_sales_date_input').change(function() {
        enable_validation();
    });

203
    $("#sales_form").submit(function(event) {
Damien Moulard committed
204 205 206 207
        event.preventDefault();
        get_sales();
    });
});