2008-10-25 17:53:16 +02:00
< ? php
2009-02-14 01:52:30 +01:00
/* Copyright ( C ) 2008 - 2009 Laurent Destailleur < eldy @ users . sourceforge . net >
2017-10-29 20:40:56 +01:00
* Copyright ( C ) 2015 - 2017 Francis Appels < francis . appels @ yahoo . com >
2008-10-25 17:53:16 +02:00
*
* 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2008-10-25 17:53:16 +02:00
* ( 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
2011-08-01 01:19:04 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2008-10-25 17:53:16 +02:00
*/
/**
2010-07-21 14:35:56 +02:00
* \file htdocs / product / class / html . formproduct . class . php
2010-03-30 14:47:19 +02:00
* \brief Fichier de la classe des fonctions predefinie de composants html
2008-10-25 17:53:16 +02:00
*/
2016-12-05 11:42:03 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/entrepot.class.php' ;
2008-10-25 17:53:16 +02:00
/**
2015-05-14 15:59:09 +02:00
* Class with static methods for building HTML components related to products
* Only components common to products and services must be here .
2008-10-25 17:53:16 +02:00
*/
class FormProduct
{
2018-08-22 11:11:59 +02:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2018-08-29 21:25:22 +02:00
2018-08-22 10:37:16 +02:00
/**
* @ var string Error code ( or message )
*/
public $error = '' ;
2008-10-25 17:53:16 +02:00
// Cache arrays
var $cache_warehouses = array ();
2017-10-23 22:17:24 +02:00
var $cache_lot = array ();
2008-10-25 17:53:16 +02:00
/**
2011-11-06 15:42:53 +01:00
* Constructor
*
* @ param DoliDB $db Database handler
2008-10-25 17:53:16 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db )
2008-10-25 17:53:16 +02:00
{
2011-11-06 15:42:53 +01:00
$this -> db = $db ;
2008-10-25 17:53:16 +02:00
}
/**
2011-11-06 15:42:53 +01:00
* Load in cache array list of warehouses
* If fk_product is not 0 , we do not use cache
*
2015-12-18 18:00:31 +01:00
* @ param int $fk_product Add quantity of stock in label for product with id fk_product . Nothing if 0.
* @ param string $batch Add quantity of batch stock in label for product with batch name batch , batch name precedes batch_id . Nothing if '' .
2016-12-06 20:07:19 +01:00
* @ param string $status warehouse status filter , following comma separated filter options can be used
* 'warehouseopen' = select products from open warehouses ,
2017-09-08 13:23:12 +02:00
* 'warehouseclosed' = select products from closed warehouses ,
2016-12-06 20:07:19 +01:00
* 'warehouseinternal' = select products from warehouses for internal correct / transfer only
2015-12-18 18:00:31 +01:00
* @ param boolean $sumStock sum total stock of a warehouse , default true
2016-10-14 11:59:07 +02:00
* @ param array $exclude warehouses ids to exclude
2015-12-18 18:00:31 +01:00
* @ return int Nb of loaded lines , 0 if already loaded , < 0 if KO
2008-10-25 17:53:16 +02:00
*/
2016-12-06 20:07:19 +01:00
function loadWarehouses ( $fk_product = 0 , $batch = '' , $status = '' , $sumStock = true , $exclude = '' )
2008-10-25 17:53:16 +02:00
{
2012-02-11 10:18:09 +01:00
global $conf , $langs ;
2008-10-25 17:53:16 +02:00
2011-09-17 21:49:50 +02:00
if ( empty ( $fk_product ) && count ( $this -> cache_warehouses )) return 0 ; // Cache already loaded and we do not want a list with information specific to a product
2017-09-08 13:23:12 +02:00
2016-09-16 11:17:19 +02:00
if ( is_array ( $exclude )) $excludeGroups = implode ( " ',' " , $exclude );
2016-12-06 20:07:19 +01:00
$warehouseStatus = array ();
2017-09-08 13:23:12 +02:00
if ( preg_match ( '/warehouseclosed/' , $status ))
2016-12-06 20:07:19 +01:00
{
$warehouseStatus [] = Entrepot :: STATUS_CLOSED ;
}
2017-09-08 13:23:12 +02:00
if ( preg_match ( '/warehouseopen/' , $status ))
2016-12-06 20:07:19 +01:00
{
$warehouseStatus [] = Entrepot :: STATUS_OPEN_ALL ;
}
2017-09-08 13:23:12 +02:00
if ( preg_match ( '/warehouseinternal/' , $status ))
2016-12-06 20:07:19 +01:00
{
$warehouseStatus [] = Entrepot :: STATUS_OPEN_INTERNAL ;
}
2017-09-08 13:23:12 +02:00
2017-11-24 15:27:00 +01:00
$sql = " SELECT e.rowid, e.ref as label, e.description, e.fk_parent " ;
2017-09-08 13:23:12 +02:00
if ( ! empty ( $fk_product ))
2015-12-18 18:00:31 +01:00
{
2017-09-08 13:23:12 +02:00
if ( ! empty ( $batch ))
2015-12-18 18:00:31 +01:00
{
$sql .= " , pb.qty as stock " ;
}
else
{
$sql .= " , ps.reel as stock " ;
}
}
else if ( $sumStock )
{
$sql .= " , sum(ps.reel) as stock " ;
}
2010-06-17 22:24:11 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " entrepot as e " ;
2015-12-18 18:00:31 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product_stock as ps on ps.fk_entrepot = e.rowid " ;
if ( ! empty ( $fk_product ))
2010-06-17 22:24:11 +02:00
{
$sql .= " AND ps.fk_product = ' " . $fk_product . " ' " ;
2017-09-08 13:23:12 +02:00
if ( ! empty ( $batch ))
2015-12-18 18:00:31 +01:00
{
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product_batch as pb on pb.fk_product_stock = ps.rowid AND pb.batch = ' " . $batch . " ' " ;
2016-04-27 16:51:51 +02:00
}
2010-06-17 22:24:11 +02:00
}
2017-05-30 18:50:54 +02:00
$sql .= " WHERE e.entity IN ( " . getEntity ( 'stock' ) . " ) " ;
2016-12-06 20:07:19 +01:00
if ( count ( $warehouseStatus ))
2016-04-27 16:51:51 +02:00
{
2017-09-12 19:03:12 +02:00
$sql .= " AND e.statut IN ( " . $this -> db -> escape ( implode ( ',' , $warehouseStatus )) . " ) " ;
2016-04-27 16:51:51 +02:00
}
else
{
$sql .= " AND e.statut = 1 " ;
}
2017-09-08 13:23:12 +02:00
2017-09-12 19:03:12 +02:00
if ( ! empty ( $exclude )) $sql .= ' AND e.rowid NOT IN(' . $this -> db -> escape ( implode ( ',' , $exclude )) . ')' ;
2017-09-08 13:23:12 +02:00
2017-11-24 15:27:00 +01:00
if ( $sumStock && empty ( $fk_product )) $sql .= " GROUP BY e.rowid, e.ref, e.description, e.fk_parent " ;
$sql .= " ORDER BY e.ref " ;
2008-10-28 22:59:53 +01:00
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::loadWarehouses' , LOG_DEBUG );
2008-10-25 17:53:16 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
2015-12-18 18:00:31 +01:00
if ( $sumStock ) $obj -> stock = price2num ( $obj -> stock , 5 );
2008-10-25 17:53:16 +02:00
$this -> cache_warehouses [ $obj -> rowid ][ 'id' ] = $obj -> rowid ;
2016-10-20 14:29:51 +02:00
$this -> cache_warehouses [ $obj -> rowid ][ 'label' ] = $obj -> label ;
$this -> cache_warehouses [ $obj -> rowid ][ 'parent_id' ] = $obj -> fk_parent ;
2015-12-18 18:00:31 +01:00
$this -> cache_warehouses [ $obj -> rowid ][ 'description' ] = $obj -> description ;
$this -> cache_warehouses [ $obj -> rowid ][ 'stock' ] = $obj -> stock ;
2008-10-25 17:53:16 +02:00
$i ++ ;
}
2017-09-08 13:23:12 +02:00
2016-10-20 14:29:51 +02:00
// Full label init
foreach ( $this -> cache_warehouses as $obj_rowid => $tab ) {
$this -> cache_warehouses [ $obj_rowid ][ 'full_label' ] = $this -> get_parent_path ( $tab );
}
2008-10-25 17:53:16 +02:00
return $num ;
}
2008-10-28 22:59:53 +01:00
else
2008-10-25 17:53:16 +02:00
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-10-25 17:53:16 +02:00
return - 1 ;
}
}
2017-09-08 13:23:12 +02:00
2018-09-01 23:46:13 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-10-20 14:29:51 +02:00
/**
* Return full path to current warehouse in $tab ( recursive function )
2017-09-08 13:23:12 +02:00
*
2016-10-20 14:29:51 +02:00
* @ param array $tab warehouse data in $this -> cache_warehouses line
* @ param String $final_label full label with all parents , separated by ' >> ' ( completed on each call )
* @ return String full label with all parents , separated by ' >> '
*/
2018-08-15 14:28:34 +02:00
private function get_parent_path ( $tab , $final_label = '' )
{
2018-09-01 23:46:13 +02:00
//phpcs:enable
2016-10-20 14:29:51 +02:00
if ( empty ( $final_label )) $final_label = $tab [ 'label' ];
2017-09-08 13:23:12 +02:00
2016-10-20 14:29:51 +02:00
if ( empty ( $tab [ 'parent_id' ])) return $final_label ;
else {
if ( ! empty ( $this -> cache_warehouses [ $tab [ 'parent_id' ]])) {
$final_label = $this -> cache_warehouses [ $tab [ 'parent_id' ]][ 'label' ] . ' >> ' . $final_label ;
return $this -> get_parent_path ( $this -> cache_warehouses [ $tab [ 'parent_id' ]], $final_label );
}
}
2017-09-08 13:23:12 +02:00
2016-10-20 14:29:51 +02:00
return $final_label ;
}
2008-10-28 22:59:53 +01:00
2008-10-25 17:53:16 +02:00
/**
2013-06-24 19:34:56 +02:00
* Return list of warehouses
2011-11-06 15:42:53 +01:00
*
2013-06-24 19:34:56 +02:00
* @ param int $selected Id of preselected warehouse ( '' for no value , 'ifone' = select value if one value otherwise no value )
2011-11-06 15:42:53 +01:00
* @ param string $htmlname Name of html select html
2016-12-06 20:07:19 +01:00
* @ param string $filterstatus warehouse status filter , following comma separated filter options can be used
* 'warehouseopen' = select products from open warehouses ,
2017-09-08 13:23:12 +02:00
* 'warehouseclosed' = select products from closed warehouses ,
2016-12-06 20:07:19 +01:00
* 'warehouseinternal' = select products from warehouses for internal correct / transfer only
2011-11-06 15:42:53 +01:00
* @ param int $empty 1 = Can be empty , 0 if not
* @ param int $disabled 1 = Select is disabled
* @ param int $fk_product Add quantity of stock in label for product with id fk_product . Nothing if 0.
2015-05-14 15:59:09 +02:00
* @ param string $empty_label Empty label if needed ( only if $empty = 1 )
2016-10-29 17:02:48 +02:00
* @ param int $showstock 1 = Show stock count
* @ param int $forcecombo 1 = Force combo iso ajax select2
* @ param array $events Events to add to select2
* @ param string $morecss Add more css classes to HTML select
* @ param array $exclude Warehouses ids to exclude
* @ param int $showfullpath 1 = Show full path of name ( parent ref into label ), 0 = Show only ref of current warehouse
2011-11-06 15:42:53 +01:00
* @ return string HTML select
2008-10-25 17:53:16 +02:00
*/
2016-12-05 11:42:03 +01:00
function selectWarehouses ( $selected = '' , $htmlname = 'idwarehouse' , $filterstatus = '' , $empty = 0 , $disabled = 0 , $fk_product = 0 , $empty_label = '' , $showstock = 0 , $forcecombo = 0 , $events = array (), $morecss = 'minwidth200' , $exclude = '' , $showfullpath = 1 )
2008-10-25 17:53:16 +02:00
{
2015-12-18 18:00:31 +01:00
global $conf , $langs , $user ;
2008-10-25 17:53:16 +02:00
2016-12-05 11:42:03 +01:00
dol_syslog ( get_class ( $this ) . " ::selectWarehouses $selected , $htmlname , $filterstatus , $empty , $disabled , $fk_product , $empty_label , $showstock , $forcecombo , $morecss " , LOG_DEBUG );
2017-09-08 13:23:12 +02:00
2015-12-18 18:00:31 +01:00
$out = '' ;
2016-12-05 11:42:03 +01:00
if ( empty ( $conf -> global -> ENTREPOT_EXTRA_STATUS )) $filterstatus = '' ;
2016-12-06 20:07:19 +01:00
$this -> loadWarehouses ( $fk_product , '' , $filterstatus , true , $exclude );
2013-06-24 19:34:56 +02:00
$nbofwarehouses = count ( $this -> cache_warehouses );
2015-05-14 15:59:09 +02:00
2015-12-18 18:00:31 +01:00
if ( $conf -> use_javascript_ajax && ! $forcecombo )
{
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
$comboenhancement = ajax_combobox ( $htmlname , $events );
$out .= $comboenhancement ;
}
2017-09-08 13:23:12 +02:00
2017-09-24 01:36:20 +02:00
$out .= '<select class="flat' . ( $morecss ? ' ' . $morecss : '' ) . '"' . ( $disabled ? ' disabled' : '' ) . ' id="' . $htmlname . '" name="' . ( $htmlname . ( $disabled ? '_disabled' : '' )) . '">' ;
2013-04-20 00:10:20 +02:00
if ( $empty ) $out .= '<option value="-1">' . ( $empty_label ? $empty_label : ' ' ) . '</option>' ;
2008-10-25 17:53:16 +02:00
foreach ( $this -> cache_warehouses as $id => $arraytypes )
{
2011-11-06 15:42:53 +01:00
$out .= '<option value="' . $id . '"' ;
2015-05-07 12:20:20 +02:00
if ( $selected == $id || ( $selected == 'ifone' && $nbofwarehouses == 1 )) $out .= ' selected' ;
2011-11-06 15:42:53 +01:00
$out .= '>' ;
2016-10-29 17:02:48 +02:00
if ( $showfullpath ) $out .= $arraytypes [ 'full_label' ];
else $out .= $arraytypes [ 'label' ];
2016-04-01 11:05:15 +02:00
if (( $fk_product || ( $showstock > 0 )) && ( $arraytypes [ 'stock' ] != 0 || ( $showstock > 0 ))) $out .= ' (' . $langs -> trans ( " Stock " ) . ':' . $arraytypes [ 'stock' ] . ')' ;
2011-11-06 15:42:53 +01:00
$out .= '</option>' ;
2008-10-25 17:53:16 +02:00
}
2011-11-06 15:42:53 +01:00
$out .= '</select>' ;
2013-06-24 19:34:56 +02:00
if ( $disabled ) $out .= '<input type="hidden" name="' . $htmlname . '" value="' . (( $selected > 0 ) ? $selected : '' ) . '">' ;
2010-06-17 18:53:26 +02:00
2011-11-06 15:42:53 +01:00
return $out ;
2008-10-25 17:53:16 +02:00
}
2010-05-13 01:03:33 +02:00
2015-10-19 22:06:46 +02:00
/**
* Display form to select warehouse
*
* @ param string $page Page
* @ param int $selected Id of warehouse
* @ param string $htmlname Name of select html field
* @ param int $addempty 1 = Add an empty value in list , 2 = Add an empty value in list only if there is more than 2 entries .
* @ return void
*/
function formSelectWarehouses ( $page , $selected = '' , $htmlname = 'warehouse_id' , $addempty = 0 )
{
global $langs ;
if ( $htmlname != " none " ) {
print '<form method="POST" action="' . $page . '">' ;
print '<input type="hidden" name="action" value="setwarehouse">' ;
print '<input type="hidden" name="token" value="' . $_SESSION [ 'newtoken' ] . '">' ;
print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">' ;
print '<tr><td>' ;
print $this -> selectWarehouses ( $selected , $htmlname , '' , $addempty );
print '</td>' ;
print '<td align="left"><input type="submit" class="button" value="' . $langs -> trans ( " Modify " ) . '"></td>' ;
print '</tr></table></form>' ;
} else {
if ( $selected ) {
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/entrepot.class.php' ;
$warehousestatic = new Entrepot ( $this -> db );
$warehousestatic -> fetch ( $selected );
print $warehousestatic -> getNomUrl ();
} else {
print " " ;
}
}
}
2018-09-01 23:46:13 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2008-10-25 19:27:15 +02:00
/**
2012-01-04 13:54:07 +01:00
* Output a combo box with list of units
* pour l ' instant on ne definit pas les unites dans la base
*
* @ param string $name Name of HTML field
* @ param string $measuring_style Unit to show : weight , size , surface , volume
* @ param string $default Force unit
* @ param int $adddefault Add empty unit called " Default "
* @ return void
2008-10-25 19:27:15 +02:00
*/
function select_measuring_units ( $name = 'measuring_units' , $measuring_style = '' , $default = '0' , $adddefault = 0 )
2010-05-02 09:34:08 +02:00
{
2018-09-01 23:46:13 +02:00
//phpcs:enable
2010-05-02 09:34:08 +02:00
print $this -> load_measuring_units ( $name , $measuring_style , $default , $adddefault );
}
2010-05-13 01:03:33 +02:00
2018-09-01 23:46:13 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2010-05-02 09:34:08 +02:00
/**
2010-08-26 13:59:41 +02:00
* Return a combo box with list of units
2012-01-04 13:54:07 +01:00
* For the moment , units labels are defined in measuring_units_string
*
* @ param string $name Name of HTML field
* @ param string $measuring_style Unit to show : weight , size , surface , volume
* @ param string $default Force unit
* @ param int $adddefault Add empty unit called " Default "
2015-03-17 00:21:17 +01:00
* @ return string
2010-05-02 09:34:08 +02:00
*/
function load_measuring_units ( $name = 'measuring_units' , $measuring_style = '' , $default = '0' , $adddefault = 0 )
2008-10-25 19:27:15 +02:00
{
2018-09-01 23:46:13 +02:00
//phpcs:enable
2008-10-25 19:27:15 +02:00
global $langs , $conf , $mysoc ;
$langs -> load ( " other " );
2010-05-13 01:03:33 +02:00
2010-05-02 09:34:08 +02:00
$return = '' ;
2008-10-25 19:27:15 +02:00
2010-08-24 20:40:17 +02:00
$measuring_units = array ();
2017-06-17 10:18:59 +02:00
if ( $measuring_style == 'weight' ) $measuring_units = array ( - 6 => 1 , - 3 => 1 , 0 => 1 , 3 => 1 , 98 => 1 , 99 => 1 );
2012-08-27 20:02:23 +02:00
else if ( $measuring_style == 'size' ) $measuring_units = array ( - 3 => 1 , - 2 => 1 , - 1 => 1 , 0 => 1 , 98 => 1 , 99 => 1 );
else if ( $measuring_style == 'surface' ) $measuring_units = array ( - 6 => 1 , - 4 => 1 , - 2 => 1 , 0 => 1 , 98 => 1 , 99 => 1 );
else if ( $measuring_style == 'volume' ) $measuring_units = array ( - 9 => 1 , - 6 => 1 , - 3 => 1 , 0 => 1 , 88 => 1 , 89 => 1 , 97 => 1 , 99 => 1 , /* 98=>1 */ ); // Liter is not used as already available with dm3
2008-10-25 19:27:15 +02:00
2010-05-02 09:34:08 +02:00
$return .= '<select class="flat" name="' . $name . '">' ;
if ( $adddefault ) $return .= '<option value="0">' . $langs -> trans ( " Default " ) . '</option>' ;
2008-10-25 19:27:15 +02:00
foreach ( $measuring_units as $key => $value )
{
2010-05-02 09:34:08 +02:00
$return .= '<option value="' . $key . '"' ;
2008-10-25 19:27:15 +02:00
if ( $key == $default )
{
2015-05-07 12:20:20 +02:00
$return .= ' selected' ;
2008-10-25 19:27:15 +02:00
}
2010-08-24 20:40:17 +02:00
//$return.= '>'.$value.'</option>';
$return .= '>' . measuring_units_string ( $key , $measuring_style ) . '</option>' ;
2008-10-25 19:27:15 +02:00
}
2010-05-02 09:34:08 +02:00
$return .= '</select>' ;
2010-05-13 01:03:33 +02:00
2010-05-02 09:34:08 +02:00
return $return ;
2008-10-25 19:27:15 +02:00
}
2008-10-28 22:59:53 +01:00
2017-10-23 22:17:24 +02:00
/**
* Return list of lot numbers ( stock from product_batch ) with stock location and stock qty
*
* @ param int $selected Id of preselected lot stock id ( '' for no value , 'ifone' = select value if one value otherwise no value )
* @ param string $htmlname Name of html select html
* @ param string $filterstatus lot status filter , following comma separated filter options can be used
* @ param int $empty 1 = Can be empty , 0 if not
* @ param int $disabled 1 = Select is disabled
2017-10-29 20:40:56 +01:00
* @ param int $fk_product show lot numbers of product with id fk_product . All from objectLines if 0.
* @ param int $fk_entrepot filter lot numbers for warehouse with id fk_entrepot . All if 0.
* @ param array $objectLines Only cache lot numbers for products in lines of object . If no lines only for fk_product . If no fk_product , all .
2017-10-23 22:17:24 +02:00
* @ param string $empty_label Empty label if needed ( only if $empty = 1 )
* @ param int $forcecombo 1 = Force combo iso ajax select2
* @ param array $events Events to add to select2
* @ param string $morecss Add more css classes to HTML select
*
* @ return string HTML select
*/
2017-10-29 20:40:56 +01:00
function selectLotStock ( $selected = '' , $htmlname = 'batch_id' , $filterstatus = '' , $empty = 0 , $disabled = 0 , $fk_product = 0 , $fk_entrepot = 0 , $objectLines = array (), $empty_label = '' , $forcecombo = 0 , $events = array (), $morecss = 'minwidth200' )
2017-10-23 22:17:24 +02:00
{
global $langs ;
dol_syslog ( get_class ( $this ) . " ::selectLot $selected , $htmlname , $filterstatus , $empty , $disabled , $fk_product , $fk_entrepot , $empty_label , $showstock , $forcecombo , $morecss " , LOG_DEBUG );
$out = '' ;
2017-10-29 20:40:56 +01:00
$productIdArray = array ();
if ( ! is_array ( $objectLines ) || ! count ( $objectLines ))
{
if ( ! empty ( $fk_product )) $productIdArray [] = $fk_product ;
}
else
{
foreach ( $objectLines as $line ) {
if ( $line -> fk_product ) $productIdArray [] = $line -> fk_product ;
}
}
2017-10-23 22:17:24 +02:00
2017-10-29 20:40:56 +01:00
$nboflot = $this -> loadLotStock ( $productIdArray );
2017-10-23 22:17:24 +02:00
if ( $conf -> use_javascript_ajax && ! $forcecombo )
{
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
$comboenhancement = ajax_combobox ( $htmlname , $events );
$out .= $comboenhancement ;
}
2008-10-25 17:53:16 +02:00
2017-10-23 22:17:24 +02:00
$out .= '<select class="flat' . ( $morecss ? ' ' . $morecss : '' ) . '"' . ( $disabled ? ' disabled' : '' ) . ' id="' . $htmlname . '" name="' . ( $htmlname . ( $disabled ? '_disabled' : '' )) . '">' ;
if ( $empty ) $out .= '<option value="-1">' . ( $empty_label ? $empty_label : ' ' ) . '</option>' ;
2017-10-29 20:40:56 +01:00
if ( ! empty ( $fk_product ))
2017-10-23 22:17:24 +02:00
{
2017-10-29 20:40:56 +01:00
$productIdArray = array ( $fk_product ); // only show lot stock for product
}
2017-11-24 15:27:00 +01:00
else
2017-10-29 20:40:56 +01:00
{
foreach ( $this -> cache_lot as $key => $value )
{
$productIdArray [] = $key ;
}
}
2017-11-24 15:27:00 +01:00
2017-10-29 20:40:56 +01:00
foreach ( $productIdArray as $productId )
{
foreach ( $this -> cache_lot [ $productId ] as $id => $arraytypes )
{
if ( empty ( $fk_entrepot ) || $fk_entrepot == $arraytypes [ 'entrepot_id' ])
{
$out .= '<option value="' . $id . '"' ;
if ( $selected == $id || ( $selected == 'ifone' && $nboflot == 1 )) $out .= ' selected' ;
$out .= '>' ;
$out .= $arraytypes [ 'entrepot_label' ] . ' - ' ;
$out .= $arraytypes [ 'batch' ];
$out .= ' (' . $langs -> trans ( " Stock " ) . ':' . $arraytypes [ 'qty' ] . ')' ;
$out .= '</option>' ;
}
}
2017-10-23 22:17:24 +02:00
}
$out .= '</select>' ;
if ( $disabled ) $out .= '<input type="hidden" name="' . $htmlname . '" value="' . (( $selected > 0 ) ? $selected : '' ) . '">' ;
return $out ;
}
/**
2017-10-31 16:49:39 +01:00
* Load in cache array list of lot available in stock from a given list of products
2017-10-23 22:17:24 +02:00
*
2017-10-29 20:40:56 +01:00
* @ param array $productIdArray array of product id ' s from who to get lot numbers . A
2017-10-23 22:17:24 +02:00
*
2017-10-31 16:49:39 +01:00
* @ return int Nb of loaded lines , 0 if nothing loaded , < 0 if KO
2017-10-23 22:17:24 +02:00
*/
2017-10-29 20:40:56 +01:00
private function loadLotStock ( $productIdArray = array ())
2017-10-23 22:17:24 +02:00
{
global $conf , $langs ;
2017-10-29 20:40:56 +01:00
$cacheLoaded = false ;
if ( empty ( $productIdArray ))
2017-10-23 22:17:24 +02:00
{
2017-10-29 20:40:56 +01:00
// only Load lot stock for given products
$this -> cache_lot = array ();
return 0 ;
2017-10-23 22:17:24 +02:00
}
2017-10-29 20:40:56 +01:00
if ( count ( $productIdArray ) && count ( $this -> cache_lot ))
2017-10-23 22:17:24 +02:00
{
2017-10-29 20:40:56 +01:00
// check cache already loaded for product id's
2017-11-24 15:27:00 +01:00
foreach ( $productIdArray as $productId )
2017-10-29 20:40:56 +01:00
{
$cacheLoaded = ! empty ( $this -> cache_lot [ $productId ]) ? true : false ;
}
2017-10-23 22:17:24 +02:00
}
2017-11-24 15:27:00 +01:00
if ( $cacheLoaded )
2017-10-23 22:17:24 +02:00
{
2017-10-29 20:40:56 +01:00
return count ( $this -> cache_lot );
}
2017-11-24 15:27:00 +01:00
else
2017-10-29 20:40:56 +01:00
{
// clear cache
$this -> cache_lot = array ();
$productIdList = implode ( ',' , $productIdArray );
2017-11-24 15:27:00 +01:00
$sql = " SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.ref as label, ps.fk_product " ;
2017-10-29 20:40:56 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product_batch as pb " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product_stock as ps on ps.rowid = pb.fk_product_stock " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN ( " . getEntity ( 'stock' ) . " ) " ;
if ( ! empty ( $productIdList ))
2017-10-23 22:17:24 +02:00
{
2017-10-29 20:40:56 +01:00
$sql .= " WHERE ps.fk_product IN ( " . $productIdList . " ) " ;
2017-10-23 22:17:24 +02:00
}
2017-11-24 15:27:00 +01:00
$sql .= " ORDER BY e.ref, pb.batch " ;
2017-10-29 20:40:56 +01:00
dol_syslog ( get_class ( $this ) . '::loadLotStock' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
2017-10-23 22:17:24 +02:00
{
2017-10-29 20:40:56 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
$this -> cache_lot [ $obj -> fk_product ][ $obj -> rowid ][ 'id' ] = $obj -> rowid ;
$this -> cache_lot [ $obj -> fk_product ][ $obj -> rowid ][ 'batch' ] = $obj -> batch ;
$this -> cache_lot [ $obj -> fk_product ][ $obj -> rowid ][ 'entrepot_id' ] = $obj -> fk_entrepot ;
$this -> cache_lot [ $obj -> fk_product ][ $obj -> rowid ][ 'entrepot_label' ] = $obj -> label ;
$this -> cache_lot [ $obj -> fk_product ][ $obj -> rowid ][ 'qty' ] = $obj -> qty ;
$i ++ ;
}
2017-11-24 15:27:00 +01:00
2017-10-29 20:40:56 +01:00
return $num ;
2017-10-23 22:17:24 +02:00
}
2017-10-29 20:40:56 +01:00
else
2017-10-23 22:17:24 +02:00
{
2017-10-29 20:40:56 +01:00
dol_print_error ( $this -> db );
return - 1 ;
2017-10-23 22:17:24 +02:00
}
}
}
2018-08-13 17:26:32 +02:00
}