diff -r d8f4ca915468 -r be8691b7d634 www/js/prod_divide.js --- a/www/js/prod_divide.js Mon Oct 21 14:39:49 2019 +0200 +++ b/www/js/prod_divide.js Wed Oct 23 14:20:50 2019 +0200 @@ -40,10 +40,10 @@ for (i = 0; i < rows.length; i++) { row = rows[i]; leftover -= row.split_size; - //console.log('i:' + i + ' split_size:' + row.split_size); + console.log('i:' + i + ' split_size:' + row.split_size); } $('#leftover').val(leftover); - //console.log('calcLeftover():' + leftover); + console.log('calcLeftover():' + leftover); } // Calculate available volume but ignore the current row. @@ -308,25 +308,6 @@ } }); - durl = 'includes/db_divides.php', - - // Prepare the data - dividerec = { - datatype: 'json', - cache: false, - datafields: [ - // From prod_main - { name: 'record', type: 'number' }, - { name: 'divide_from', type: 'string' }, - { name: 'divide_type', type: 'int' }, - { name: 'divide_size', type: 'float' }, - { name: 'name', type: 'string' }, - { name: 'code', type: 'string' } - ], - id: 'record', - url: durl + '?record=' + my_record - }; - var editSplit = function(data) { var splitSource = { datatype: 'local', @@ -479,77 +460,51 @@ if (leftover != available) { console.log('Save and there are splits'); - // Record 0, the master data - div = {}; - div.divide_from = dataRecord.uuid; - div.divide_type = dataRecord.divide_type; - div.divide_size = leftover; - div.divide_part = 0; - div.name = dataRecord.name; - div.code = dataRecord.code; - data = 'insert=true&' + $.param(div); - $.ajax({ - dataType: 'json', - url: durl, - cache: false, - data: data, - type: 'POST', - success: function(data, status, xhr) { - console.log('insert divides: 0'); - }, - error: function(jqXHR, textStatus, errorThrown) { - console.log('insert divides: ' + textStatus); - } - }); + // Send all the info to the database. The server handles the splitting. + var divide_data = new Array(); + row = {}; + row.name = dataRecord.name; + row.code = dataRecord.code; + row.size = Round(leftover, 4); + row.factor = Round((leftover / available), 4); + row.part = 0; + divide_data.push(row); rows = $('#splitGrid').jqxGrid('getrows'); for (i = 0; i < rows.length; i++) { row = rows[i]; console.log('split ' + i); div = {}; - div.divide_from = dataRecord.uuid; - div.divide_type = dataRecord.divide_type; - div.divide_size = row.split_size; - div.divide_part = i + 1; + div.size = Round(row.split_size, 4); + div.factor = Round((row.split_size / available), 4); + div.part = i + 1; div.name = row.split_name; div.code = row.split_code; - data = 'insert=true&' + $.param(div); - $.ajax({ + divide_data.push(div); + } + + // Send the data to the server + div = {}; + div.record = dataRecord.record; + div.divide_type = dataRecord.divide_type; + div.divide_parts = i; + div.divide_data = divide_data; + data = $.param(div); + $.ajax({ dataType: 'json', - url: durl, + url: 'includes/db_divides.php', cache: false, data: data, type: 'POST', success: function(data, status, xhr) { - console.log('insert divides: ' + i); + console.log('insert divides: success'); + window.location.href = my_return; }, error: function(jqXHR, textStatus, errorThrown) { console.log('insert divides: ' + textStatus); } - }); - } + }); - div = {}; - div.record = dataRecord.record; - div.divide_type = dataRecord.divide_type; - div.divide_size = leftover; - div.divide_parts = i; - data = 'splitit=true&' + $.param(div); - $.ajax({ - dataType: 'json', - url: url, - cache: false, - data: data, - type: 'POST', - success: function(data, status, xhr) { - console.log('updated products'); - }, - error: function(jqXHR, textStatus, errorThrown) { - console.log('updated products: ' + textStatus); - } - }); } - window.location.href = my_return; }); - });