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 ! });