diff --git a/htdocs/contrat/card.php b/htdocs/contrat/card.php index b691524ad92..d26724aa618 100644 --- a/htdocs/contrat/card.php +++ b/htdocs/contrat/card.php @@ -424,11 +424,10 @@ if (empty($reshook)) { if (GETPOST('prod_entry_mode', 'alpha') == 'free') { $idprod = 0; - $tva_tx = (GETPOST('tva_tx', 'alpha') ? price2num(preg_replace('/\s*\(.*\)/', '', GETPOST('tva_tx', 'alpha'))) : 0); } else { $idprod = GETPOST('idprod', 'int'); - $tva_tx = ''; } + $tva_tx = (GETPOST('tva_tx', 'alpha') ? price2num(preg_replace('/\s*\(.*\)/', '', GETPOST('tva_tx', 'alpha'))) : 0); $qty = price2num(GETPOST('qty'.$predef, 'alpha'), 'MS'); $remise_percent = (GETPOSTISSET('remise_percent'.$predef) ? price2num(GETPOST('remise_percent'.$predef), 2) : 0); @@ -467,12 +466,8 @@ if (empty($reshook)) { // Clean parameters $date_start = dol_mktime(GETPOST('date_start'.$predef.'hour'), GETPOST('date_start'.$predef.'min'), GETPOST('date_start'.$predef.'sec'), GETPOST('date_start'.$predef.'month'), GETPOST('date_start'.$predef.'day'), GETPOST('date_start'.$predef.'year')); $date_end = dol_mktime(GETPOST('date_end'.$predef.'hour'), GETPOST('date_end'.$predef.'min'), GETPOST('date_end'.$predef.'sec'), GETPOST('date_end'.$predef.'month'), GETPOST('date_end'.$predef.'day'), GETPOST('date_end'.$predef.'year')); - $price_base_type = (GETPOST('price_base_type', 'alpha') ?GETPOST('price_base_type', 'alpha') : 'HT'); - // Ecrase $pu par celui du produit - // Ecrase $desc par celui du produit // Ecrase $tva_tx par celui du produit - // Ecrase $base_price_type par celui du produit if ($idprod > 0) { $prod = new Product($db); $prod->fetch($idprod); @@ -484,19 +479,13 @@ if (empty($reshook)) { $tva_npr = 0; } - $pu_ht = $prod->price; - $pu_ttc = $prod->price_ttc; $price_min = $prod->price_min; $price_min_ttc = $prod->price_min_ttc; - $price_base_type = $prod->price_base_type; // On defini prix unitaire if ($conf->global->PRODUIT_MULTIPRICES && $object->thirdparty->price_level) { - $pu_ht = $prod->multiprices[$object->thirdparty->price_level]; - $pu_ttc = $prod->multiprices_ttc[$object->thirdparty->price_level]; $price_min = $prod->multiprices_min[$object->thirdparty->price_level]; $price_min_ttc = $prod->multiprices_min_ttc[$object->thirdparty->price_level]; - $price_base_type = $prod->multiprices_base_type[$object->thirdparty->price_level]; } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) { // If price per customer require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php'; @@ -508,11 +497,8 @@ if (empty($reshook)) { $result = $prodcustprice->fetchAll('', '', 0, 0, $filter); if ($result) { if (count($prodcustprice->lines) > 0) { - $pu_ht = price($prodcustprice->lines[0]->price); - $pu_ttc = price($prodcustprice->lines[0]->price_ttc); $price_min = price($prodcustprice->lines[0]->price_min); $price_min_ttc = price($prodcustprice->lines[0]->price_min_ttc); - $price_base_type = $prodcustprice->lines[0]->price_base_type; $tva_tx = $prodcustprice->lines[0]->tva_tx; if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; @@ -532,16 +518,11 @@ if (empty($reshook)) { if (!empty($price_ht) || $price_ht === '0') { $pu_ht = price2num($price_ht, 'MU'); $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU'); + $price_base_type = 'HT'; } elseif (!empty($price_ttc) || $price_ttc === '0') { $pu_ttc = price2num($price_ttc, 'MU'); $pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU'); - } elseif ($tmpvat != $tmpprodvat) { - // Is this still used ? - if ($price_base_type != 'HT') { - $pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU'); - } else { - $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU'); - } + $price_base_type = 'TTC'; } $desc = $prod->description; @@ -571,8 +552,16 @@ if (empty($reshook)) { $pu_ht_devise = price2num($price_ht_devise, 'MU'); $pu_ttc_devise = price2num($price_ttc_devise, 'MU'); - $price_base_type = 'HT'; - if ($pu_ttc && !$pu_ht) { + $tmpvat = price2num(preg_replace('/\s*\(.*\)/', '', $tva_tx)); + + // Set unit price to use + if (!empty($price_ht) || $price_ht === '0') { + $pu_ht = price2num($price_ht, 'MU'); + $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU'); + $price_base_type = 'HT'; + } elseif (!empty($price_ttc) || $price_ttc === '0') { + $pu_ttc = price2num($price_ttc, 'MU'); + $pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU'); $price_base_type = 'TTC'; } } diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php index c0979a2b480..c0382a39de4 100644 --- a/htdocs/core/tpl/objectline_create.tpl.php +++ b/htdocs/core/tpl/objectline_create.tpl.php @@ -718,7 +718,8 @@ if (!empty($usemargins) && $user->rights->margins->creer) { { console.log("objectline_create.tpl Call method change() after change on #idprod or #idprodfournprice (senderissupplier=). this.val = "+$(this).val()); - setforpredef(); // TODO Keep vat combo visible and set it to first entry into list that match result of get_default_tva + setforpredef(); // TODO Keep vat combo visible and set it to first entry into list that match result of get_default_tva(product) + jQuery('#trlinefordates').show(); rights->margins->creer) { global->PRODUIT_CUSTOMER_PRICES_BY_QTY) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) { ?> if (isNaN(pbq)) { console.log("We use experimental option PRODUIT_CUSTOMER_PRICES_BY_QTY or PRODUIT_CUSTOMER_PRICES_BY_QTY but we could not get the id of pbq from product combo list, so load of price may be 0 if product has differet prices"); } - // Get the HT price for the product and display it + // Get the price for the product and display it console.log("Load unit price without tax and set it into #price_ht for product id="+$(this).val()+" socid=socid; ?>"); $.post('/product/ajax/products.php?action=fetch', { 'id': $(this).val(), 'socid': socid; ?>, 'token': '' }, @@ -755,7 +756,7 @@ if (!empty($usemargins) && $user->rights->margins->creer) { if (data.mandatory_period == 1 && data.type == 1) { jQuery('#date_start').addClass('inputmandatory'); jQuery('#date_end').addClass('inputmandatory'); - }else{ + } else { jQuery('#date_start').removeClass('inputmandatory'); jQuery('#date_end').removeClass('inputmandatory'); } @@ -767,6 +768,23 @@ if (!empty($usemargins) && $user->rights->margins->creer) { console.log("objectline_create.tpl set content of price_ht"); jQuery("#price_ht").val(data.price_ht); } + + var tva_tx = data.tva_tx; + var default_vat_code = data.default_vat_code; + + // Now set the VAT + var stringforvatrateselection = tva_tx; + if (typeof default_vat_code != 'undefined' && default_vat_code != null) { + stringforvatrateselection = stringforvatrateselection+' ('+default_vat_code+')'; + } + // Set vat rate if field is an input box + $('#tva_tx').val(tva_tx); + // Set vat rate by selecting the combo + //$('#tva_tx option').val(tva_tx); // This is bugged, it replaces the vat key of all options + $('#tva_tx option').removeAttr('selected'); + console.log("stringforvatrateselection="+stringforvatrateselection+" -> value of option label for this key="+$('#tva_tx option[value="'+stringforvatrateselection+'"]').val()); + $('#tva_tx option[value="'+stringforvatrateselection+'"]').prop('selected', true); + global->PRODUIT_AUTOFILL_DESC) && $conf->global->PRODUIT_AUTOFILL_DESC == 1) { if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) { ?> @@ -919,39 +937,41 @@ if (!empty($usemargins) && $user->rights->margins->creer) { ?> global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {?> - /* To process customer price per quantity (PRODUIT_CUSTOMER_PRICES_BY_QTY works only if combo product is not an ajax after x key pressed) */ - var pbq = parseInt($('option:selected', this).attr('data-pbq')); // When select is done from HTML select - if (isNaN(pbq)) { pbq = jQuery('#idprod').attr('data-pbq'); } // When select is done from HTML input with autocomplete - var pbqup = parseFloat($('option:selected', this).attr('data-pbqup')); - if (isNaN(pbqup)) { pbqup = jQuery('#idprod').attr('data-pbqup'); } - var pbqbase = $('option:selected', this).attr('data-pbqbase'); - if (isNaN(pbqbase)) { pbqbase = jQuery('#idprod').attr('data-pbqbase'); } - var pbqqty = parseFloat($('option:selected', this).attr('data-pbqqty')); - if (isNaN(pbqqty)) { pbqqty = jQuery('#idprod').attr('data-pbqqty'); } - var pbqpercent = parseFloat($('option:selected', this).attr('data-pbqpercent')); - if (isNaN(pbqpercent)) { pbqpercent = jQuery('#idprod').attr('data-pbqpercent'); } + if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) { + ?> + /* To process customer price per quantity (PRODUIT_CUSTOMER_PRICES_BY_QTY works only if combo product is not an ajax after x key pressed) */ + var pbq = parseInt($('option:selected', this).attr('data-pbq')); // When select is done from HTML select + if (isNaN(pbq)) { pbq = jQuery('#idprod').attr('data-pbq'); } // When select is done from HTML input with autocomplete + var pbqup = parseFloat($('option:selected', this).attr('data-pbqup')); + if (isNaN(pbqup)) { pbqup = jQuery('#idprod').attr('data-pbqup'); } + var pbqbase = $('option:selected', this).attr('data-pbqbase'); + if (isNaN(pbqbase)) { pbqbase = jQuery('#idprod').attr('data-pbqbase'); } + var pbqqty = parseFloat($('option:selected', this).attr('data-pbqqty')); + if (isNaN(pbqqty)) { pbqqty = jQuery('#idprod').attr('data-pbqqty'); } + var pbqpercent = parseFloat($('option:selected', this).attr('data-pbqpercent')); + if (isNaN(pbqpercent)) { pbqpercent = jQuery('#idprod').attr('data-pbqpercent'); } - if ((jQuery('#idprod').val() > 0) && ! isNaN(pbq) && pbq > 0) - { - var pbqupht = pbqup; /* TODO support of price per qty TTC not yet available */ + if ((jQuery('#idprod').val() > 0) && ! isNaN(pbq) && pbq > 0) + { + var pbqupht = pbqup; /* TODO support of price per qty TTC not yet available */ - console.log("We choose a price by quanty price_by_qty id = "+pbq+" price_by_qty upht = "+pbqupht+" price_by_qty qty = "+pbqqty+" price_by_qty percent = "+pbqpercent); - jQuery("#pbq").val(pbq); - jQuery("#price_ht").val(pbqupht); - if (jQuery("#qty").val() < pbqqty) - { - jQuery("#qty").val(pbqqty); - } - if (jQuery("#remise_percent").val() < pbqpercent) - { - jQuery("#remise_percent").val(pbqpercent); - } - } else { jQuery("#pbq").val(''); } + console.log("We choose a price by quanty price_by_qty id = "+pbq+" price_by_qty upht = "+pbqupht+" price_by_qty qty = "+pbqqty+" price_by_qty percent = "+pbqpercent); + jQuery("#pbq").val(pbq); + jQuery("#price_ht").val(pbqupht); + if (jQuery("#qty").val() < pbqqty) + { + jQuery("#qty").val(pbqqty); + } + if (jQuery("#remise_percent").val() < pbqpercent) + { + jQuery("#remise_percent").val(pbqpercent); + } + } else { jQuery("#pbq").val(''); } - // Deal with supplier + + // Deal with supplier ref price if (jQuery('#idprodfournprice').val() > 0) { console.log("objectline_create.tpl #idprodfournprice is > 0, so we set some properties into page"); @@ -975,7 +995,7 @@ if (!empty($usemargins) && $user->rights->margins->creer) { if (typeof default_vat_code === 'undefined') { default_vat_code = jQuery('#idprodfournprice').attr('data-default-vat-code');} // When select is done from HTML input with ajax autocomplete var stringforvatrateselection = tva_tx; - if (typeof default_vat_code != 'undefined') { + if (typeof default_vat_code != 'undefined' && default_vat_code != null) { stringforvatrateselection = stringforvatrateselection+' ('+default_vat_code+')'; } @@ -992,7 +1012,7 @@ if (!empty($usemargins) && $user->rights->margins->creer) { // Set vat rate by selecting the combo //$('#tva_tx option').val(tva_tx); // This is bugged, it replaces the vat key of all options $('#tva_tx option').removeAttr('selected'); - console.log("stringforvatrateselection="+stringforvatrateselection+" -> value of option for this selection="+$('#tva_tx option[value="'+stringforvatrateselection+'"]').val()); + console.log("stringforvatrateselection="+stringforvatrateselection+" -> value of option label for this key="+$('#tva_tx option[value="'+stringforvatrateselection+'"]').val()); $('#tva_tx option[value="'+stringforvatrateselection+'"]').prop('selected', true); if (jQuery("#qty").val() < qty) { diff --git a/htdocs/product/ajax/products.php b/htdocs/product/ajax/products.php index 24da89b7619..f61e92c56f3 100644 --- a/htdocs/product/ajax/products.php +++ b/htdocs/product/ajax/products.php @@ -93,6 +93,7 @@ if ($action == 'fetch' && !empty($id)) { $outprice_ht = null; $outprice_ttc = null; $outpricebasetype = null; + $outtva_tx_formated = 0; $outtva_tx = 0; $outdefault_vat_code = ''; $outqty = 1; @@ -140,7 +141,8 @@ if ($action == 'fetch' && !empty($id)) { $outprice_ttc = price($objp->unitprice * (1 + ($object->tva_tx / 100))); $outpricebasetype = $object->price_base_type; - $outtva_tx = $object->tva_tx; + $outtva_tx_formated = price($object->tva_tx); + $outtva_tx = price2num($object->tva_tx); $outdefault_vat_code = $object->default_vat_code; $outqty = $objp->quantity; @@ -165,15 +167,17 @@ if ($action == 'fetch' && !empty($id)) { $objp = $db->fetch_object($result); if ($objp) { $found = true; - $outprice_ht = price($objp->price); - $outprice_ttc = price($objp->price_ttc); + $outprice_ht = price($objp->price); // formated for langage user because is inserted into input field + $outprice_ttc = price($objp->price_ttc); // formated for langage user because is inserted into input field $outpricebasetype = $objp->price_base_type; if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) { - $outtva_tx = $objp->tva_tx; + $outtva_tx_formated = price($objp->tva_tx); // formated for langage user because is inserted into input field + $outtva_tx = price2num($objp->tva_tx); // international numeric $outdefault_vat_code = $objp->default_vat_code; } else { // The common and default behaviour. - $outtva_tx = $object->tva_tx; + $outtva_tx_formated = price($object->tva_tx); + $outtva_tx = price2num($object->tva_tx); $outdefault_vat_code = $object->default_vat_code; } } @@ -195,7 +199,8 @@ if ($action == 'fetch' && !empty($id)) { $outprice_ht = price($prodcustprice->lines[0]->price); $outprice_ttc = price($prodcustprice->lines[0]->price_ttc); $outpricebasetype = $prodcustprice->lines[0]->price_base_type; - $outtva_tx = $prodcustprice->lines[0]->tva_tx; + $outtva_tx_formated = price($prodcustprice->lines[0]->tva_tx); + $outtva_tx = price2num($prodcustprice->lines[0]->tva_tx); $outdefault_vat_code = $prodcustprice->lines[0]->default_vat_code; } } @@ -205,7 +210,8 @@ if ($action == 'fetch' && !empty($id)) { $outprice_ht = price($object->price); $outprice_ttc = price($object->price_ttc); $outpricebasetype = $object->price_base_type; - $outtva_tx = $object->tva_tx; + $outtva_tx_formated = price($object->tva_tx); + $outtva_tx = price2num($object->tva_tx); $outdefault_vat_code = $object->default_vat_code; } @@ -219,6 +225,7 @@ if ($action == 'fetch' && !empty($id)) { 'price_ht' => $outprice_ht, 'price_ttc' => $outprice_ttc, 'pricebasetype' => $outpricebasetype, + 'tva_tx_formated' => $outtva_tx_formated, 'tva_tx' => $outtva_tx, 'default_vat_code' => $outdefault_vat_code, 'qty' => $outqty,