diff --git a/htdocs/core/ajax/price.php b/htdocs/core/ajax/price.php index c1aab70882e..79f7d1f439b 100644 --- a/htdocs/core/ajax/price.php +++ b/htdocs/core/ajax/price.php @@ -29,9 +29,8 @@ if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); require('../../main.inc.php'); -$action = GETPOST('action','alpha'); -$pu_ht = price2num(GETPOST('pu_ht','alpha')); -$pu_ttc = price2num(GETPOST('pu_ttc','alpha')); +$output = GETPOST('output','alpha'); +$amount = price2num(GETPOST('amount','alpha')); $tva_tx = str_replace('*','',GETPOST('tva_tx','alpha')); /* @@ -43,23 +42,27 @@ top_httphead(); //print ''."\n"; // Load original field value -if (! empty($action) && isset($tva_tx)) +if (! empty($output) && isset($amount) && isset($tva_tx)) { $return=array(); $price=''; - if ($action == 'get_ttc' && isset($pu_ht) && is_numeric($pu_ht) && $pu_ht != '') { - - $price = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU'); - + if (is_numeric($amount) && $amount != '') + { + if ($output == 'price_ttc') { + + $price = price2num($amount * (1 + ($tva_tx/100)), 'MU'); + $return['price_ht'] = $amount; + $return['price_ttc'] = (isset($price) && $price != '' ? price($price) : ''); + + } + else if ($output == 'price_ht') { + + $price = price2num($amount / (1 + ($tva_tx/100)), 'MU'); + $return['price_ht'] = (isset($price) && $price != '' ? price($price) : ''); + $return['price_ttc'] = ($tva_tx == 0 ? $price : $amount); + } } - else if ($action == 'get_ht' && isset($pu_ttc) && is_numeric($pu_ttc) && $pu_ttc != '') { - - $price = price2num($pu_ttc / (1 + ($tva_tx/100)), 'MU'); - - } - - $return['price'] = (isset($price) && $price != '' ? price($price) : ''); echo json_encode($return); } diff --git a/htdocs/core/js/lib_head.js b/htdocs/core/js/lib_head.js index fc408e246e2..11e78f419b1 100644 --- a/htdocs/core/js/lib_head.js +++ b/htdocs/core/js/lib_head.js @@ -739,7 +739,7 @@ function hideMessage(fieldId,message) { $.widget("ui.onDelayedKeyup", { _init : function() { var self = this; - $(this.element).bind('change keyup input', function() { + $(this.element).bind('keyup', function() { if(typeof(window['inputTimeout']) != "undefined"){ window.clearTimeout(inputTimeout); } diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php index 8b0614a1b42..53fee4a5482 100644 --- a/htdocs/core/lib/ajax.lib.php +++ b/htdocs/core/lib/ajax.lib.php @@ -136,7 +136,7 @@ function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLengt } if (ui.item.textarea) { $.each(ui.item.textarea, function(key, value) { - if (typeof CKEDITOR == "object") { + if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined") { CKEDITOR.instances[key].setData(value); CKEDITOR.instances[key].focus(); } else { diff --git a/htdocs/core/tpl/objectline_add.tpl.php b/htdocs/core/tpl/objectline_add.tpl.php index a1f8ecbda40..49ca10820c2 100644 --- a/htdocs/core/tpl/objectline_add.tpl.php +++ b/htdocs/core/tpl/objectline_add.tpl.php @@ -76,10 +76,11 @@ if (! empty($conf->margin->enabled)) { 'product_label' => 'label2', 'origin_label_cache' => 'label2', 'origin_desc_cache' => 'desc', + 'price_base_type' => 'pricebasetype', 'price_ht' => 'price_ht', 'origin_price_ht_cache' => 'price_ht', - 'price_ttc' => 'price_ttc', - 'origin_price_ttc_cache' => 'price_ttc' + //'price_ttc' => 'price_ttc', + //'origin_price_ttc_cache' => 'price_ttc' ), 'show' => array( 'update_label_area', @@ -110,7 +111,6 @@ if (! empty($conf->margin->enabled)) { | textwithtooltip($langs->trans('UpdateOriginalProductPrice'), $langs->trans('HelpUpdateOriginalProductPrice'),1,0,'','',3); ?> - @@ -156,6 +156,7 @@ if (! empty($conf->margin->enabled)) { if ($buyer->tva_assuj == "0") echo '0'; else echo $form->load_tva('tva_tx', (GETPOST('tva_tx')?GETPOST('tva_tx'):-1), $seller, $buyer); ?> + @@ -220,7 +221,7 @@ $(document).ready(function() { $('#idprod').change(function() { if ($(this).val().length > 0) { - if (typeof CKEDITOR == 'object') { + if (typeof CKEDITOR == 'object' && typeof CKEDITOR.instances != 'undefined') { CKEDITOR.instances['product_desc'].focus(); } else { $('#product_desc').focus(); @@ -297,16 +298,18 @@ $(document).ready(function() { $('#update_price_checkbox').change(function() { if ($(this).attr('checked')) { $('#price_ht').removeAttr('disabled').focus(); - $('#price_ttc').removeAttr('disabled') + if ($('#tva_tx').val() > 0) { + $('#price_ttc').removeAttr('disabled') + } } else { $('#price_ht') .attr('disabled','disabled') .val($('#origin_price_ht_cache').val()) .trigger('change'); $('#price_ttc') - .attr('disabled','disabled') - .val($('#origin_price_ttc_cache').val()) - .trigger('change'); + .attr('disabled','disabled'); + //.val($('#origin_price_ttc_cache').val()) + //.trigger('change'); } }); @@ -336,7 +339,7 @@ $(document).ready(function() { var origin_desc = $('#origin_desc_cache').val(); - if (typeof CKEDITOR == 'object') { + if (typeof CKEDITOR == 'object' && typeof CKEDITOR.instances != 'undefined') { var freecontent = CKEDITOR.instances['product_desc'].getData(); if (origin_desc.length > 0) var content = origin_desc + '
' + freecontent; @@ -356,7 +359,7 @@ $(document).ready(function() { var content = $('#free_desc_cache').val(); } - if (typeof CKEDITOR == 'object') { + if (typeof CKEDITOR == 'object' && typeof CKEDITOR.instances != 'undefined') { CKEDITOR.instances['product_desc'].setData(content); } else { $('#product_desc').html(content); @@ -364,84 +367,67 @@ $(document).ready(function() { }); $('#price_ht').focusin(function() { - $('#price_base_type').html('HT'); + $('#price_base_type').val('HT'); }); - $('#price_ht').onDelayedKeyup({ - handler: function() { - $.post('/core/ajax/price.php', { - 'action': 'get_ttc', - 'pu_ht': $(this).val(), - 'tva_tx': $('#tva_tx').val() - }, - function(data) { - if (data && data.price.length > 0) { - $('#price_ttc').val(data.price); - if ($('#select_type').val() >= 0) { - $('#addlinebutton').removeAttr('disabled'); - } else { - $('#addlinebutton').attr('disabled','disabled'); - } - } else { - $('#price_ttc').val(''); - $('#addlinebutton').attr('disabled','disabled'); - } - }, 'json'); + $('#price_ht').bind('change keyup input', function() { + if ($('#tva_tx').val() > 0 && ($('#idprod').val().length == 0 && $('#price_base_type').val() == 'HT') || $('#idprod').val().length > 0) { + update_price('price_ht', 'price_ttc'); } }); $('#price_ttc').focusin(function() { - $('#price_base_type').html('TTC'); + $('#price_base_type').val('TTC'); }); - $('#price_ttc').onDelayedKeyup({ - handler: function() { - $.post('/core/ajax/price.php', { - 'action': 'get_ht', - 'pu_ttc': $(this).val(), - 'tva_tx': $('#tva_tx').val() - }, - function(data) { - if (data && data.price.length > 0) { - $('#price_ht').val(data.price); - if ($('#select_type').val() >= 0) { - $('#addlinebutton').removeAttr('disabled'); - } else { - $('#addlinebutton').attr('disabled','disabled'); - } - } else { - $('#price_ht').val(''); - $('#addlinebutton').attr('disabled','disabled'); - } - }, 'json'); + $('#price_ttc').bind('change keyup input', function() { + if ($('#price_base_type').val() == 'TTC') { + update_price('price_ttc', 'price_ht'); } }); + if ($('#tva_tx').val() == 0) { + $('#price_ttc').attr('disabled','disabled'); + } + $('#tva_tx').change(function() { - if ($('#price_base_type').html() == 'HT') { - $.post('/core/ajax/price.php', { - 'action': 'get_ttc', - 'pu_ht': $('#price_ht').val(), - 'tva_tx': $(this).val() - }, - function(data) { - if (data && data.price.length > 0) { - $('#price_ttc').val(data.price); - } - }, 'json'); - } else if ($('#price_base_type').html() == 'TTC') { - $.post('/core/ajax/price.php', { - 'action': 'get_ht', - 'pu_ttc': $('#price_ttc').val(), - 'tva_tx': $(this).val() - }, - function(data) { - if (data && data.price.length > 0) { - $('#price_ht').val(data.price); - } - }, 'json'); + if ($(this).val() == 0) { + $('#price_ttc').attr('disabled','disabled'); + $('#price_ttc').val(''); + } else { + if ($('#idprod').val().length == 0 || ($('#idprod').val().length > 0 && $('#update_price_checkbox').attr('checked') == 'checked')) { + $('#price_ttc').removeAttr('disabled'); + } + if ($('#price_base_type').val() == 'HT') { + update_price('price_ht', 'price_ttc'); + } else if ($('#price_base_type').val() == 'TTC') { + update_price('price_ttc', 'price_ht'); + } } }); + + function update_price(input, output) { + $.post('/core/ajax/price.php', { + 'amount': $('#' + input).val(), + 'output': output, + 'tva_tx': $('#tva_tx').val() + }, + function(data) { + if (typeof data[output] != 'undefined') { + $('#' + output).val(data[output]); + if ($('#select_type').val() >= 0) { + $('#addlinebutton').removeAttr('disabled'); + } else { + $('#addlinebutton').attr('disabled','disabled'); + } + } else { + $('#' + input).val(''); + $('#' + output).val(''); + $('#addlinebutton').attr('disabled','disabled'); + } + }, 'json'); + } + });