Add parameter to load all lot from lines of object in cache

If you want use the lot stock selector in mutiple lines you can add
lines array. If you don't give a product id or a lines array, no lot
numbers will be loaded.
This commit is contained in:
fappels 2017-10-29 20:40:56 +01:00
parent cd5c678534
commit a9ceb46f97

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2008-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2016 Francis Appels <francis.appels@yahoo.com>
* Copyright (C) 2015-2017 Francis Appels <francis.appels@yahoo.com>
*
* 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
@ -338,8 +338,9 @@ class FormProduct
* @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
* @param int $fk_product show lot numbers of product with id fk_product. All if 0.
* @param int $fk_entrepot show lot numbers in warehouse with id fk_entrepot. All if 0.
* @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.
* @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
@ -347,15 +348,26 @@ class FormProduct
*
* @return string HTML select
*/
function selectLotStock($selected='',$htmlname='batch_id',$filterstatus='',$empty=0,$disabled=0,$fk_product=0,$fk_entrepot=0,$empty_label='', $forcecombo=0, $events=array(), $morecss='minwidth200')
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')
{
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='';
$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;
}
}
$nboflot = $this->loadLotStock($fk_product, $fk_entrepot);
$nboflot = $this->loadLotStock($productIdArray);
if ($conf->use_javascript_ajax && ! $forcecombo)
{
@ -366,15 +378,33 @@ class FormProduct
$out.='<select class="flat'.($morecss?' '.$morecss:'').'"'.($disabled?' disabled':'').' id="'.$htmlname.'" name="'.($htmlname.($disabled?'_disabled':'')).'">';
if ($empty) $out.='<option value="-1">'.($empty_label?$empty_label:'&nbsp;').'</option>';
foreach($this->cache_lot as $id => $arraytypes)
if (! empty($fk_product))
{
$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>';
$productIdArray = array($fk_product); // only show lot stock for product
}
else
{
foreach($this->cache_lot as $key => $value)
{
$productIdArray[] = $key;
}
}
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>';
}
}
}
$out.='</select>';
if ($disabled) $out.='<input type="hidden" name="'.$htmlname.'" value="'.(($selected>0)?$selected:'').'">';
@ -386,70 +416,73 @@ class FormProduct
* Load in cache array list of lot available in stock
* If fk_product is not 0, we do not use cache
*
* @param int $fk_product load lot of id fk_product, all if 0.
* @param array $productIdArray array of product id's from who to get lot numbers. A
* @param string $fk_entrepot load lot in fk_entrepot all if 0.
*
* @return int Nb of loaded lines, 0 if already loaded, <0 if KO
* @return int Nb of loaded lines, 0 if nothing loaded, <0 if KO
*/
function loadLotStock($fk_product = 0, $fk_entrepot = 0)
private function loadLotStock($productIdArray = array())
{
global $conf, $langs;
if (empty($fk_product) && empty($fk_product) && count($this->cache_lot))
$cacheLoaded = false;
if (empty($productIdArray))
{
return count($this->cache_lot); // Cache already loaded and we do not want a list with information specific to a product or warehouse
// only Load lot stock for given products
$this->cache_lot = array();
return 0;
}
if (count($productIdArray) && count($this->cache_lot))
{
// check cache already loaded for product id's
foreach ($productIdArray as $productId)
{
$cacheLoaded = ! empty($this->cache_lot[$productId]) ? true : false;
}
}
if ($cacheLoaded)
{
return count($this->cache_lot);
}
else
{
// clear cache;
// clear cache
$this->cache_lot = array();
}
$sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.label";
$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($fk_product) || !empty($fk_entrepot))
{
$sql.= " WHERE";
if (!empty($fk_product))
$productIdList = implode(',', $productIdArray);
$sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.label, ps.fk_product";
$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))
{
$sql.= " ps.fk_product = '".$fk_product."'";
if (!empty($fk_entrepot))
$sql.= " WHERE ps.fk_product IN (".$productIdList.")";
}
$sql.= " ORDER BY e.label, pb.batch";
dol_syslog(get_class($this).'::loadLotStock', LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$sql.= " AND";
$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++;
}
return $num;
}
if (!empty($fk_entrepot))
else
{
$sql.= " ps.fk_entrepot = '".$fk_entrepot."'";
dol_print_error($this->db);
return -1;
}
}
$sql.= " ORDER BY e.label, pb.batch";
dol_syslog(get_class($this).'::loadLotStock', LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$this->cache_lot[$obj->rowid]['id'] =$obj->rowid;
$this->cache_lot[$obj->rowid]['batch']=$obj->batch;
$this->cache_lot[$obj->rowid]['entrepot_id']=$obj->fk_entrepot;
$this->cache_lot[$obj->rowid]['entrepot_label']=$obj->label;
$this->cache_lot[$obj->rowid]['qty'] = $obj->qty;
$i++;
}
return $num;
}
else
{
dol_print_error($this->db);
return -1;
}
}
}