Fix: disable "add" button and show error message if a product with no

supplier price is selected
This commit is contained in:
Regis Houssin 2012-08-17 16:58:57 +02:00
parent 3438305169
commit 7394525388
3 changed files with 36 additions and 21 deletions

View File

@ -1335,9 +1335,10 @@ class Form
* @param string $htmlname Name of HTML Select
* @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
* @param string $filtre For a SQL filter
* @param array $ajaxoptions Options for ajax_autocompleter
* @return void
*/
function select_produits_fournisseurs($socid,$selected='',$htmlname='productid',$filtertype='',$filtre='')
function select_produits_fournisseurs($socid, $selected='', $htmlname='productid', $filtertype='', $filtre='', $ajaxoptions=array())
{
global $langs,$conf;
global $price_level, $status, $finished;
@ -1345,7 +1346,8 @@ class Form
if ($conf->global->PRODUIT_USE_SEARCH_TO_SELECT)
{
// mode=2 means suppliers products
print ajax_autocompleter('', $htmlname, DOL_URL_ROOT.'/product/ajax/products.php', ($socid > 0?'socid='.$socid.'&':'').'htmlname='.$htmlname.'&outjson=1&price_level='.$price_level.'&type='.$filtertype.'&mode=2&status='.$status.'&finished='.$finished, $conf->global->PRODUIT_USE_SEARCH_TO_SELECT);
$urloption=($socid > 0?'socid='.$socid.'&':'').'htmlname='.$htmlname.'&outjson=1&price_level='.$price_level.'&type='.$filtertype.'&mode=2&status='.$status.'&finished='.$finished;
print ajax_autocompleter('', $htmlname, DOL_URL_ROOT.'/product/ajax/products.php', $urloption, $conf->global->PRODUIT_USE_SEARCH_TO_SELECT, 0, $ajaxoptions);
print $langs->trans("RefOrLabel").' : <input type="text" size="16" name="search_'.$htmlname.'" id="search_'.$htmlname.'">';
print '<br>';
}
@ -1486,10 +1488,7 @@ class Form
// "key" value of json key array is used by jQuery automatically as selected value
// "label" value of json key array is used by jQuery automatically as text for combo box
$outselect.=$opt;
// FIXME don't select with autocomplete
if (! empty($objp->idprodfournprice)) {
array_push($outjson,array('key'=>$outkey,'value'=>$outref,'label'=>$outval));
}
array_push($outjson, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'disabled'=>(empty($objp->idprodfournprice)?true:false)));
$i++;
}

View File

@ -30,48 +30,63 @@
* @param string $selected Preselecte value
* @param string $htmlname HTML name of input field
* @param string $url Url for request: /chemin/fichier.php
* @param string $option More parameters on URL request
* @param string $urloption More parameters on URL request
* @param int $minLength Minimum number of chars to trigger that Ajax search
* @param int $autoselect Automatic selection if just one value
* @param array $ajaxoptions Multiple options array
* @return string Script
*/
function ajax_autocompleter($selected,$htmlname,$url,$option='',$minLength=2,$autoselect=0)
function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLength=2, $autoselect=0, $ajaxoptions=array())
{
if (empty($minLength)) $minLength=1;
$script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="'.$selected.'" />';
$script.= '<script type="text/javascript">';
$script.= 'jQuery(document).ready(function() {
$script.= '$(document).ready(function() {
var autoselect = '.$autoselect.';
jQuery("input#search_'.$htmlname.'").blur(function() {
var options = '.json_encode($ajaxoptions).';
$("input#search_'.$htmlname.'").blur(function() {
//console.log(this.value.length);
if (this.value.length == 0)
{
jQuery("#search_'.$htmlname.'").val("");
jQuery("#'.$htmlname.'").val("").trigger("change");
$("#search_'.$htmlname.'").val("");
$("#'.$htmlname.'").val("").trigger("change");
if (options.disabled) {
$("#" + options.disabled).removeAttr("disabled");
}
}
});
jQuery("input#search_'.$htmlname.'").autocomplete({
$("input#search_'.$htmlname.'").autocomplete({
source: function( request, response ) {
jQuery.get("'.$url.($option?'?'.$option:'').'", { '.$htmlname.': request.term }, function(data){
$.get("'.$url.($urloption?'?'.$urloption:'').'", { '.$htmlname.': request.term }, function(data){
response( jQuery.map( data, function( item ) {
if (autoselect == 1 && data.length == 1) {
jQuery("#search_'.$htmlname.'").val(item.value);
jQuery("#'.$htmlname.'").val(item.key).trigger("change");
$("#search_'.$htmlname.'").val(item.value);
$("#'.$htmlname.'").val(item.key).trigger("change");
}
var label = item.label.toString();
return { label: label, value: item.value, id: item.key}
return { label: label, value: item.value, id: item.key, disabled: item.disabled }
}));
}, "json");
},
dataType: "json",
minLength: '.$minLength.',
select: function( event, ui ) {
jQuery("#'.$htmlname.'").val(ui.item.id).trigger("change");
$("#'.$htmlname.'").val(ui.item.id).trigger("change");
if (options.disabled) {
if (ui.item.disabled) {
$("#" + options.disabled).attr("disabled", "disabled");
if (options.error) {
$.jnotify(options.error, "error", true);
}
} else {
$("#" + options.disabled).removeAttr("disabled");
}
}
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return jQuery( "<li></li>" )
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( \'<a href="#"><span class="tag">\' + item.label + "</span></a>" )
.appendTo(ul);

View File

@ -1530,7 +1530,8 @@ if ($id > 0 || ! empty($ref))
print '<tr '.$bc[$var].'>';
print '<td colspan="3">';
$form->select_produits_fournisseurs($object->fourn_id,'','idprodfournprice');
$ajaxoptions=array('disabled' => 'addPredefinedProductButton', 'error' => $langs->trans("NoPriceDefinedForThisSupplier"));
$form->select_produits_fournisseurs($object->fourn_id, '', 'idprodfournprice', '', '', $ajaxoptions);
if (empty($conf->global->PRODUIT_USE_SEARCH_TO_SELECT)) print '<br>';
@ -1548,7 +1549,7 @@ if ($id > 0 || ! empty($ref))
print '</td>';
print '<td align="right"><input type="text" size="2" name="pqty" value="'.(GETPOST('pqty')?GETPOST('pqty'):'1').'"></td>';
print '<td align="right" nowrap="nowrap"><input type="text" size="1" name="p_remise_percent" value="'.(GETPOST('p_remise_percent')?GETPOST('p_remise_percent'):$soc->remise_client).'">%</td>';
print '<td align="center" colspan="4"><input type="submit" class="button" value="'.$langs->trans('Add').'"></td>';
print '<td align="center" colspan="4"><input type="submit" id="addPredefinedProductButton" class="button" value="'.$langs->trans('Add').'"></td>';
print '</tr>';
print '</form>';