export_compta.js 3.92 KB
Newer Older
1
jQuery(document).ready(function($) {
Administrator committed
2 3 4

    var month_sel = $("select[name$='_month']"),
        year_sel = $("select[name$='_year']"),
5
        default_month, default_year;
Administrator committed
6

7 8
    var now = new Date();
    var current_month = now.getMonth() + 1; //jan = 0
Administrator committed
9 10
    //Usually, export concerns previous month, so we select it as the default

11 12 13 14 15 16
    if (current_month == 1) {
        default_month = 12;
        default_year = now.getFullYear() - 1;
    } else {
        default_month = current_month - 1;
        default_year = now.getFullYear();
Administrator committed
17 18
    }

19 20
    month_sel.find("option[value='" + default_month + "']").attr('selected', 'selected');
    year_sel.find("option[value='" + default_year + "']").attr('selected', 'selected');
Administrator committed
21 22

    function wait_for_odoo_export_file (export_id, url) {
23 24
        openWaiting('La demande de génération du document Odoo avec toutes les lignes comptables est lancée.<br/>Cela peut prendre plusieurs minutes.');
        $.ajax({url : url + '?phase=1&export_id='+export_id,
Administrator committed
25 26
            dataType :'json'
        })
27 28 29 30 31 32 33 34 35 36
            .done(function(rData) {
                if (rData.response && rData.response == true) {
                    newWaitingMessage('Le document Odoo est terminé.<br/>Les ventes journalières sont compilées et les identifiants de coop. substitués.');
                    $.ajax({url : url + '?phase=2&export_id='+export_id+ '&final_format=' + $('[name="final_format"]').val(),
                        dataType :'json'
                    })
                        .done(function(rData) {
                            if (rData.response && rData.response.file_path) {
                                var download_link = $('<a>').attr('href', '/' + rData.response.file_path)
                                    .text('Télécharger le fichier');
Administrator committed
37

38 39 40 41 42
                                $('form').hide();
                                if (rData.response.sells_total) {
                                    var preambule = $('<p>').html('A titre indicatif:');
                                    var vente_ht = $('<p>').html('Ventes H.T: <strong>' + parseFloat(rData.response.sells_total).toFixed(2) + '</strong> € ');
                                    var tva = $('<p>').html('TVA sur ventes: <strong>' + parseFloat(rData.response.vat_total).toFixed(2) + '</strong> € ');
Administrator committed
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57
                                    $('body')
                                        .append(preambule)
                                        .append(vente_ht)
                                        .append(tva)
                                        .append(download_link);
                                } else {
                                    $('body').append(download_link);
                                }
                            }
                            closeWaiting();
                        });
                }

            });
Administrator committed
58 59
    }

60 61 62 63 64 65 66
    $('form').submit(function() {
        var form = $(this);
        var month = month_sel.val();
        var year = year_sel.val();

        if (month > 0 && year > 0 && is_time_to('request_compta', 120000)) {
            month = new String(month).pad('0', 2);
Administrator committed
67
            data = {'from': year + '-' + month + '-01',
68 69 70 71
                'to': year + '-' + month + '-' + new Date(year, month, 0).getDate()
            };
            url = form.attr('action');
            post_form(url, data, function(error, rData) {
Administrator committed
72
                if (rData.response && !isNaN(rData.response))
73
                    wait_for_odoo_export_file(rData.response, url);
Administrator committed
74
                else
75 76
                    alert("L'export ne peut pas se faire, merci de contacter le service informatique.");
            });
Administrator committed
77 78

        } else {
79 80
            if (month > 0 && year > 0) {
                alert('Délai trop court entre 2 demandes...');
Administrator committed
81
            } else {
82
                alert('La sélection du mois n\'est pas valable');
Administrator committed
83 84 85
            }
        }
        //always return false because form is processed through ajax call
86
        return false;
Administrator committed
87

88 89
    });
});