backend.js 5.42 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
odoo.define('lacagette_cpo.inv', function(require) {
    "use strict";
    var FormView = require('web.FormView')
    var Model = require('web.DataModel');
    var order = null
    var open_ckeckboxes_button = $('<button>').attr('type','button')
        .addClass('add-checkboxes-btn')
        .text('Ajout coches inventaire')
    var send_checked_button = $('<button>').attr('type','button')
        .addClass('send-checked-products')
        .text('Envoyer liste pour inventaire')
    var config_parameter = new Model('ir.config_parameter');
    var last_generation_call = 0

    FormView.include({

        load_record: function() {
          var self = this;
          return this._super.apply(this, arguments)
            .then(function() {
          order = self.get_fields_values()
            });
        }
    });

    function generate_checkboxes() {
        var table = $('table.oe_list_content')
        if (table.find('.products_to_check_col').length == 0) {
            //prepend void header column
            table.find('.oe_list_header_columns')
                .prepend($('<th>')
                        .addClass('products_to_check_col')
                        .css({'width':'5px', 'padding':'2px'})
                        .html('<input type="checkbox" id="check_all_for_inv"/>'))
            //adding checkboxes
            table.find('tbody tr').each(function(i,e) {
                if (typeof $(e).data('id') != "undefined")
                    $(e).prepend($('<td>').addClass('for_inv_cb')
                        .html('<input type="checkbox" name="for_inventory" class="cb_item_for_inv"/>'))
            })

            $('#check_all_for_inv').change(function() {
              if ($('#check_all_for_inv:checked').length > 0) {
                $('.cb_item_for_inv').prop('checked', true);
              } else {
                $('.cb_item_for_inv').prop('checked', false);
              }
            });
        }

    }

    function send_checked_products_for_inventory() {
        var d = new Date()
        var elapsed_since_last_call = d.getTime() - last_generation_call
        if (elapsed_since_last_call > 5000) {
            last_generation_call = d.getTime()

            var table = $('table.oe_list_content'),
            ids = []

            table.find('.cb_item_for_inv:checked').each(function(i,e) {
                    ids.push($(e).closest('tr').data('id'))
            })

            if (ids.length > 0) {
                config_parameter.call('get_param', ['lacagette_cpo.proxy_url'])
                                .then(function(url){
                                    if (url && url.length > 0) {
                                        //TODO show message
                                        $.ajax({url:url,
                                                method: 'POST',
                                                dataType: "json",
                                                data: {
                                                        lines: JSON.stringify(ids),
                                                        type: send_checked_button.data('type')
                                                      },
                                        })
                                        .done(function(rData){
                                                if (typeof rData.res != "undefined" && typeof rData.res.error === "undefined"){
                                                    alert("Fichier pour inventaire généré")
                                                } else {
                                                    alert(JSON.stringify(rData))
                                                }
                                        })
                                    }
                                })
            }
        }
    }
    function for_inv_changed(event) {
        //event.stopPropagation(); is useless !!
        $('.modal-dialog .close').trigger('click')
    }

    function locationHashChanged() {
        if (/purchase.order/.exec(window.location.href)) {
            if (order != null && order.id != false &&  order.state =='draft') {
                var  sheet = $('.oe_form_sheet')
                if (sheet.find('.add-checkboxes-btn').length == 0){
                    var buttons_wrapper = $('<div>').css('float','right')
                    if (typeof order.order_line === "undefined") {
                        send_checked_button.attr('data-type','cpo')
                    } else {
                        send_checked_button.attr('data-type','order')
                    }
                    buttons_wrapper.append(open_ckeckboxes_button).append(send_checked_button)
                    sheet.prepend(buttons_wrapper)
                    //https://stackoverflow.com/questions/30793066/how-to-avoid-memory-leaks-from-jquery
                    $(document)
                        .off('.generate_to_inv_checkboxes .send_checked_for_inv .for_inv')
                        .on('click.generate_to_inv_checkboxes', '.add-checkboxes-btn', generate_checkboxes)
                        .on('click.send_checked_for_inv', '.send-checked-products', send_checked_products_for_inventory)
                        .on('change.for_inv', '[name="for_inventory"]', for_inv_changed)
                }
            }

            order = null // absolutely needed !
        }
    }
    window.onhashchange = locationHashChanged; // not already used by any module -> be careful of side-effects if one use it one day !

});