diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index d68d9c8d59f..d753803dc54 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -655,6 +655,72 @@ class Form if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1); } + + /** + * Return list of company for customer in Ajax if Ajax activated or go to select_thirparty_list + * + * @param int $selected Preselected products + * @param string $htmlname Name of HTML seletc field (must be unique in page) + * @param int $filtertype Filter on product type (''=nofilter, 0=product, 1=service) + * @param int $limit Limit on number of returned lines + * @param array $ajaxoptions Options for ajax_autocompleter + * @return void + */ + function select_thirdparty($selected='', $htmlname='productid', $filtertype='', $limit=20, $ajaxoptions=array()) + { + global $langs,$conf; + + if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT)) + { + $placeholder=''; + + if ($selected && empty($selected_input_value)) + { + require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; + $societe = new Societe($this->db); + $societe->fetch($selected); + $selected_input_value=$societe->ref; + } + // mode=1 means customers products + $urloption='htmlname='.$htmlname.'&outjson=1&price_level='.$price_level.'&type='.$filtertype.'&mode=1&status='.$status.'&finished='.$finished; + print ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/societe/ajax/company.php', $urloption, $conf->global->COMPANY_USE_SEARCH_TO_SELECT, 0, $ajaxoptions); + if (empty($hidelabel)) print $langs->trans("RefOrLabel").' : '; + else if ($hidelabel > 1) { + if (! empty($conf->global->MAIN_HTML5_PLACEHOLDER)) $placeholder=' placeholder="'.$langs->trans("RefOrLabel").'"'; + else $placeholder=' title="'.$langs->trans("RefOrLabel").'"'; + if ($hidelabel == 2) { + print img_picto($langs->trans("Search"), 'search'); + } + } + print ''; + if ($hidelabel == 3) { + print img_picto($langs->trans("Search"), 'search'); + } + } + else + { + print $this->select_thirdparty_list($selected,$htmlname,$filtertype,1,0,1,array(),'',0,$limit); + } + } + + /** + * Output html form to select a third party + * + * @param string $selected Preselected type + * @param string $htmlname Name of field in form + * @param string $filter Optionnal filters criteras (example: 's.rowid <> x') + * @param int $showempty Add an empty field + * @param int $showtype Show third party type in combolist (customer, prospect or supplier) + * @param int $forcecombo Force to use combo box + * @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled'))) + * @return string HTML string with + * @deprecated Use select_thirdparty instead + */ + function select_company($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array()) + { + return $this->select_thirdparty_list($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array()); + } + /** * Output html form to select a third party * @@ -665,13 +731,17 @@ class Form * @param int $showtype Show third party type in combolist (customer, prospect or supplier) * @param int $forcecombo Force to use combo box * @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled'))) + * @param string $filterkey Filter on key value + * @param int $outputmode 0=HTML select string, 1=Array + * @param int $limit Limit number of answers * @return string HTML string with */ - function select_company($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array()) + function select_thirdparty_list($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array(), $filterkey='', $outputmode=0, $limit=20) { global $conf,$user,$langs; $out=''; + $outarray=array(); // On recherche les societes $sql = "SELECT s.rowid, s.nom, s.client, s.fournisseur, s.code_client, s.code_fournisseur"; @@ -682,9 +752,33 @@ class Form if ($filter) $sql.= " AND (".$filter.")"; if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; if (! empty($conf->global->COMPANY_HIDE_INACTIVE_IN_COMBOBOX)) $sql.= " AND s.status<>0 "; + // Add criteria + if ($filterkey && $filterkey != '') + { + $sql.=" AND ("; + if (! empty($conf->global->COMPANY_DONOTSEARCH_ANYWHERE)) // Can use index + { + $sql.="(s.name LIKE '".$filterkey."%'"; + $sql.=")"; + } + else + { + // For natural search + $scrit = explode(' ', $filterkey); + foreach ($scrit as $crit) { + $sql.=" AND (s.name LIKE '%".$crit."%'"; + $sql.=")"; + } + } + if (! empty($conf->barcode->enabled)) + { + $sql .= " OR s.barcode LIKE '".$filterkey."'"; + } + $sql.=")"; + } $sql.= " ORDER BY nom ASC"; - dol_syslog(get_class($this)."::select_company sql=".$sql); + dol_syslog(get_class($this)."::select_thirdparty_list sql=".$sql); $resql=$this->db->query($sql); if ($resql) { @@ -692,32 +786,9 @@ class Form { //$minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT)?$conf->global->COMPANY_USE_SEARCH_TO_SELECT:2); $out.= ajax_combobox($htmlname, $event, $conf->global->COMPANY_USE_SEARCH_TO_SELECT); - /* -<<<<<<< HEAD - if ($selected && empty($selected_input_value)) - { - require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; - $product = new Product($this->db); - $product->fetch($selected); - $selected_input_value=$product->ref; - } -======= - if ($selected && empty($selected_input_value)) - { - require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; - $product = new Product($this->db); - $product->fetch($selected); - $selected_input_value=$product->ref; - } ->>>>>>> refs/remotes/origin/3.3 - // mode=1 means customers products - $ajaxoptions=array(); - $urloption='htmlname='.$htmlname.'&outjson=1&filter='.urlencode($filter).'&showtype='.$showtype; - $out.=ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/societe/ajax/company.php', $urloption, $conf->global->COMPANY_USE_SEARCH_TO_SELECT, 0, $ajaxoptions); - $out.=''; - */ } + // Construct $out and $outarray $out.= ''; - $outselect.=''; + $out.=''; + $out.=''; $this->db->free($result); - if (empty($disableout)) print $outselect; - return $outjson; + if (empty($outputmode)) return $out; + return $outarray; } else - { + { dol_print_error($db); } } /** * _construct_product_list_option - * + * * @param resultset &$objp Resultset of fetch * @param string $opt Option * @param string $optJson Option * @param int $price_level Price level * @param string $selected Preselected value - * @return + * @return */ - private function _construct_product_list_option(&$objp, &$opt, &$optJson, $price_level, $selected) + private function _construct_product_list_option(&$objp, &$opt, &$optJson, $price_level, $selected) { global $langs,$conf,$user,$db; @@ -1399,7 +1479,7 @@ class Form $sql.= " ORDER BY date_price"; $sql.= " DESC LIMIT 1"; - dol_syslog(get_class($this)."::select_produits_do search price for level '.$price_level.' sql=".$sql); + dol_syslog(get_class($this)."::_construct_product_list_option search price for level '.$price_level.' sql=".$sql); $result2 = $this->db->query($sql); if ($result2) { @@ -1430,7 +1510,8 @@ class Form } // Price by quantity - if (!empty($objp->quantity) && $objp->quantity >= 1) { + if (!empty($objp->quantity) && $objp->quantity >= 1) + { $found = 1; $outqty=$objp->quantity; $outdiscount=$objp->remise_percent; @@ -1511,7 +1592,7 @@ class Form } /** - * Return list of products for customer (in Ajax if Ajax activated or go to select_produits_fournisseurs_do) + * Return list of products for customer (in Ajax if Ajax activated or go to select_produits_fournisseurs_list) * * @param int $socid Id third party * @param string $selected Preselected product @@ -1536,7 +1617,7 @@ class Form } else { - $this->select_produits_fournisseurs_do($socid,$selected,$htmlname,$filtertype,$filtre,'',-1,0); + print $this->select_produits_fournisseurs_list($socid,$selected,$htmlname,$filtertype,$filtre,'',-1,0); } } @@ -1550,13 +1631,16 @@ class Form * @param string $filtre Pour filtre sql * @param string $filterkey Filtre des produits * @param int $statut -1=Return all products, 0=Products not on sell, 1=Products on sell - * @param int $disableout Disable print output + * @param int $outputmode 0=HTML select string, 1=Array * @return array Array of keys for json */ - function select_produits_fournisseurs_do($socid,$selected='',$htmlname='productid',$filtertype='',$filtre='',$filterkey='',$statut=-1,$disableout=0) + function select_produits_fournisseurs_list($socid,$selected='',$htmlname='productid',$filtertype='',$filtre='',$filterkey='',$statut=-1,$outputmode=0) { global $langs,$conf; + $out=''; + $outarray=array(); + $langs->load('stocks'); $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,"; @@ -1590,20 +1674,18 @@ class Form $sql.= " ORDER BY pfp.ref_fourn DESC, pfp.quantity ASC"; // Build output string - $outselect=''; - $outjson=array(); - dol_syslog(get_class($this)."::select_produits_fournisseurs_do sql=".$sql,LOG_DEBUG); + dol_syslog(get_class($this)."::select_produits_fournisseurs_list sql=".$sql,LOG_DEBUG); $result=$this->db->query($sql); if ($result) { $num = $this->db->num_rows($result); - //$outselect.=''; - if (! $selected) $outselect.=''; - else $outselect.=''; + //$out.=''; + if (! $selected) $out.=''; + else $out.=''; $i = 0; while ($i < $num) @@ -1694,9 +1776,9 @@ class Form // Add new entry // "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; - array_push($outjson, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'qty'=>$outqty, 'discount'=>$outdiscount, 'disabled'=>(empty($objp->idprodfournprice)?true:false))); - // Exemple of var_dump $outjson + $out.=$opt; + array_push($outarray, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'qty'=>$outqty, 'discount'=>$outdiscount, 'disabled'=>(empty($objp->idprodfournprice)?true:false))); + // Exemple of var_dump $outarray // array(1) {[0]=>array(6) {[key"]=>string(1) "2" ["value"]=>string(3) "ppp" // ["label"]=>string(76) "ppp (fff2) - ppp - 20,00 Euros/1unité (20,00 Euros/unité)" // ["qty"]=>string(1) "1" ["discount"]=>string(1) "0" ["disabled"]=>bool(false) @@ -1707,12 +1789,12 @@ class Form $i++; } - $outselect.=''; + $out.=''; $this->db->free($result); - if (empty($disableout)) print $outselect; - return $outjson; + if (empty($outputmode)) return $out; + return $outarray; } else { @@ -3524,7 +3606,7 @@ class Form function select_duration($prefix,$iSecond='',$disabled=0,$typehour='select') { global $langs; - + if ($iSecond) { require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; @@ -3532,8 +3614,8 @@ class Form $hourSelected = convertSecondToTime($iSecond,'hour'); $minSelected = convertSecondToTime($iSecond,'min'); } - - if ($typehour=='select') + + if ($typehour=='select') { print '"; } - elseif ($typehour=='text') + elseif ($typehour=='text') { $fullhours=convertSecondToTime($iSecond,'fullhour'); print ''; diff --git a/htdocs/societe/ajax/company.php b/htdocs/societe/ajax/company.php new file mode 100644 index 00000000000..d79fe3f4ee9 --- /dev/null +++ b/htdocs/societe/ajax/company.php @@ -0,0 +1,107 @@ + + * Copyright (C) 2005-2012 Regis Houssin + * Copyright (C) 2007-2011 Laurent Destailleur + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/product/ajax/products.php + * \brief File to return Ajax response on product list request + */ + +if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL',1); // Disables token renewal +if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); +if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); +if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); +if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); +if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); +if (empty($_GET['keysearch']) && ! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); + +require '../../main.inc.php'; + +$htmlname=GETPOST('htmlname','alpha'); +$socid=GETPOST('socid','int'); +$type=GETPOST('type','int'); +$mode=GETPOST('mode','int'); +$status=((GETPOST('status','int') >= 0) ? GETPOST('status','int') : -1); +$outjson=(GETPOST('outjson','int') ? GETPOST('outjson','int') : 0); +$price_level=GETPOST('price_level','int'); +$action=GETPOST('action', 'alpha'); +$id=GETPOST('id', 'int'); +$price_by_qty_rowid=GETPOST('pbq', 'int'); + +/* + * View + */ + +//print ''."\n"; + +dol_syslog(join(',',$_GET)); +//print_r($_GET); + +if (! empty($action) && $action == 'fetch' && ! empty($id)) +{ + require DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php'; + + $outjson=array(); + + $object = new Societe($db); + $ret=$object->fetch($id); + if ($ret > 0) + { + $outname=$object->name; + + $outjson = array('name'=>$outname); + } + + echo json_encode($outjson); +} +else +{ + require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; + + $langs->load("products"); + $langs->load("main"); + + top_httphead(); + + if (empty($htmlname)) return; + + $match = preg_grep('/('.$htmlname.'[0-9]+)/',array_keys($_GET)); + sort($match); + $id = (! empty($match[0]) ? $match[0] : ''); + + if (! GETPOST($htmlname) && ! GETPOST($id)) return; + + // When used from jQuery, the search term is added as GET param "term". + $searchkey=(GETPOST($id)?GETPOST($id):(GETPOST($htmlname)?GETPOST($htmlname):'')); + + $form = new Form($db); + if (empty($mode) || $mode == 'customer') + { + $arrayresult=$form->select_company_html($socid,$htmlname,"client IN (1,3)",0,0,0,null,$searchkey,$outjson); + } + elseif ($mode == 'supplier') + { + $arrayresult=$form->select_company_html($socid,$htmlname,"fournisseur=1",0,0,0,null,$searchkey,$outjson); + } + + $db->close(); + + if ($outjson) print json_encode($arrayresult); +} + +?> \ No newline at end of file