1<?php
2/* Copyright (C) 2001-2006  Rodolphe Quiedeville    <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2016  Laurent Destailleur     <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2018  Regis Houssin           <regis.houssin@inodbox.com>
5 * Copyright (C) 2013       Cédric Salvador         <csalvador@gpcsolutions.fr>
6 * Copyright (C) 2015       Raphaël Doursenaud      <rdoursenaud@gpcsolutions.fr>
7 * Copyright (C) 2016       Ferran Marcet			<fmarcet@2byte.es>
8 * Copyright (C) 2019       Juanjo Menent			<jmenent@2byte.es>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24/**
25 *  \file       htdocs/product/reassortlot.php
26 *  \ingroup    produit
27 *  \brief      Page to list stocks
28 */
29
30require '../main.inc.php';
31require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
32require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
34require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
35require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
36
37// Load translation files required by the page
38$langs->loadLangs(array('products', 'stocks', 'productbatch'));
39
40// Security check
41if ($user->socid) $socid = $user->socid;
42$result = restrictedArea($user, 'produit|service');
43
44
45$action = GETPOST('action', 'aZ09');
46$sref = GETPOST("sref", 'alpha');
47$snom = GETPOST("snom", 'alpha');
48$sall = trim((GETPOST('search_all', 'alphanohtml') != '') ?GETPOST('search_all', 'alphanohtml') : GETPOST('sall', 'alphanohtml'));
49$type = GETPOST("type", "int");
50$search_barcode = GETPOST("search_barcode", 'alpha');
51$search_warehouse = GETPOST('search_warehouse', 'alpha');
52$search_batch = GETPOST('search_batch', 'alpha');
53$catid = GETPOST('catid', 'int');
54$toolowstock = GETPOST('toolowstock');
55$tosell = GETPOST("tosell");
56$tobuy = GETPOST("tobuy");
57$fourn_id = GETPOST("fourn_id", 'int');
58
59$sortfield = GETPOST("sortfield", 'alpha');
60$sortorder = GETPOST("sortorder", 'alpha');
61$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
62if (empty($page) || $page < 0) $page = 0;
63if (!$sortfield) $sortfield = "p.ref";
64if (!$sortorder) $sortorder = "ASC";
65$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
66if (empty($page) || $page == -1) { $page = 0; }     // If $page is not defined, or '' or -1
67$offset = $limit * $page;
68
69// Load sale and categ filters
70$search_sale = GETPOST("search_sale");
71$search_categ = GETPOST("search_categ");
72
73// Get object canvas (By default, this is not defined, so standard usage of dolibarr)
74$canvas = GETPOST("canvas");
75$objcanvas = null;
76if (!empty($canvas))
77{
78	require_once DOL_DOCUMENT_ROOT.'/core/class/canvas.class.php';
79	$objcanvas = new Canvas($db, $action);
80	$objcanvas->getCanvas('product', 'list', $canvas);
81}
82
83
84
85/*
86 * Actions
87 */
88
89if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) // All tests are required to be compatible with all browsers
90{
91	$sref = "";
92	$snom = "";
93	$sall = "";
94	$tosell = "";
95	$tobuy = "";
96	$search_sale = "";
97	$search_categ = "";
98	$type = "";
99	$catid = '';
100	$toolowstock = '';
101	$search_batch = '';
102	$search_warehouse = '';
103	$fourn_id = '';
104	$sbarcode = '';
105}
106
107
108/*
109 * View
110 */
111
112$form = new Form($db);
113$htmlother = new FormOther($db);
114
115$helpurl = 'EN:Module_Stocks_En|FR:Module_Stock|ES:M&oacute;dulo_Stocks';
116$title = $langs->trans("ProductsAndServices");
117
118$sql = 'SELECT p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type, p.entity,';
119$sql .= ' p.fk_product_type, p.tms as datem,';
120$sql .= ' p.duration, p.tosell as statut, p.tobuy, p.seuil_stock_alerte, p.desiredstock, p.stock, p.tosell, p.tobuy, p.tobatch,';
121$sql .= ' ps.fk_entrepot,';
122$sql .= ' e.ref as warehouse_ref, e.lieu as warehouse_lieu, e.fk_parent as warehouse_parent,';
123$sql .= ' pb.batch, pb.eatby as oldeatby, pb.sellby as oldsellby,';
124$sql .= ' pl.rowid as lotid, pl.eatby, pl.sellby,';
125$sql .= ' SUM(pb.qty) as stock_physique, COUNT(pb.rowid) as nbinbatchtable';
126$sql .= ' FROM '.MAIN_DB_PREFIX.'product as p';
127$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as ps on p.rowid = ps.fk_product'; // Detail for each warehouse
128$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'entrepot as e on ps.fk_entrepot = e.rowid'; // Link on unique key
129$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_batch as pb on pb.fk_product_stock = ps.rowid'; // Detail for each lot on each warehouse
130$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_lot as pl on pl.fk_product = p.rowid AND pl.batch = pb.batch'; // Link on unique key
131// We'll need this table joined to the select in order to filter by categ
132if ($search_categ) $sql .= ", ".MAIN_DB_PREFIX."categorie_product as cp";
133$sql .= " WHERE p.entity IN (".getEntity('product').")";
134if ($search_categ) $sql .= " AND p.rowid = cp.fk_product"; // Join for the needed table to filter by categ
135if ($sall) $sql .= natural_search(array('p.ref', 'p.label', 'p.description', 'p.note'), $sall);
136// if the type is not 1, we show all products (type = 0,2,3)
137if (dol_strlen($type))
138{
139	if ($type == 1)
140	{
141		$sql .= " AND p.fk_product_type = '1'";
142	} else {
143		$sql .= " AND p.fk_product_type <> '1'";
144	}
145}
146if ($sref)     $sql .= natural_search("p.ref", $sref);
147if ($search_barcode) $sql .= natural_search("p.barcode", $search_barcode);
148if ($snom)     $sql .= natural_search("p.label", $snom);
149if (!empty($tosell)) $sql .= " AND p.tosell = ".$tosell;
150if (!empty($tobuy))  $sql .= " AND p.tobuy = ".$tobuy;
151if (!empty($canvas)) $sql .= " AND p.canvas = '".$db->escape($canvas)."'";
152if ($catid) $sql .= " AND cp.fk_categorie = ".$catid;
153if ($fourn_id > 0) $sql .= " AND p.rowid = pf.fk_product AND pf.fk_soc = ".$fourn_id;
154// Insert categ filter
155if ($search_categ) $sql .= " AND cp.fk_categorie = ".$db->escape($search_categ);
156if ($search_warehouse) $sql .= natural_search("e.ref", $search_warehouse);
157if ($search_batch) $sql .= natural_search("pb.batch", $search_batch);
158$sql .= " GROUP BY p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type, p.entity,";
159$sql .= " p.fk_product_type, p.tms,";
160$sql .= " p.duration, p.tosell, p.tobuy, p.seuil_stock_alerte, p.desiredstock, p.stock, p.tosell, p.tobuy, p.tobatch,";
161$sql .= " ps.fk_entrepot,";
162$sql .= " e.ref, e.lieu, e.fk_parent,";
163$sql .= " pb.batch, pb.eatby, pb.sellby,";
164$sql .= " pl.rowid, pl.eatby, pl.sellby";
165if ($toolowstock) $sql .= " HAVING SUM(".$db->ifsql('ps.reel IS NULL', '0', 'ps.reel').") < p.seuil_stock_alerte"; // Not used yet
166$sql .= $db->order($sortfield, $sortorder);
167
168$nbtotalofrecords = '';
169if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
170{
171	$result = $db->query($sql);
172	$nbtotalofrecords = $db->num_rows($result);
173	if (($page * $limit) > $nbtotalofrecords)	// if total resultset is smaller then paging size (filtering), goto and load page 0
174	{
175		$page = 0;
176		$offset = 0;
177	}
178}
179
180$sql .= $db->plimit($limit + 1, $offset);
181
182$resql = $db->query($sql);
183if ($resql)
184{
185	$num = $db->num_rows($resql);
186
187	$i = 0;
188
189	if ($num == 1 && GETPOST('autojumpifoneonly') && ($sall or $snom or $sref))
190	{
191		$objp = $db->fetch_object($resql);
192		header("Location: card.php?id=$objp->rowid");
193		exit;
194	}
195
196	if (isset($type))
197	{
198		if ($type == 1) { $texte = $langs->trans("Services"); } else { $texte = $langs->trans("Products"); }
199	} else {
200		$texte = $langs->trans("ProductsAndServices");
201	}
202	$texte .= ' ('.$langs->trans("StocksByLotSerial").')';
203
204	$param = '';
205	if ($limit > 0 && $limit != $conf->liste_limit) $param .= '&limit='.urlencode($limit);
206	if ($sall)		$param .= "&sall=".urlencode($sall);
207	if ($tosell)		$param .= "&tosell=".urlencode($tosell);
208	if ($tobuy)			$param .= "&tobuy=".urlencode($tobuy);
209	if ($type)			$param .= "&type=".urlencode($type);
210	if ($fourn_id)		$param .= "&fourn_id=".urlencode($fourn_id);
211	if ($snom)			$param .= "&snom=".urlencode($snom);
212	if ($sref)			$param .= "&sref=".urlencode($sref);
213	if ($search_batch)	$param .= "&search_batch=".urlencode($search_batch);
214	if ($sbarcode)		$param .= "&sbarcode=".urlencode($sbarcode);
215	if ($search_warehouse)	$param .= "&search_warehouse=".urlencode($search_warehouse);
216	if ($catid)			$param .= "&catid=".urlencode($catid);
217	if ($toolowstock)	$param .= "&toolowstock=".urlencode($toolowstock);
218	if ($search_sale)	$param .= "&search_sale=".urlencode($search_sale);
219	if ($search_categ)	$param .= "&search_categ=".urlencode($search_categ);
220	/*if ($eatby)		$param.="&eatby=".$eatby;
221	if ($sellby)	$param.="&sellby=".$sellby;*/
222
223	llxHeader("", $title, $helpurl, $texte);
224
225	print '<form action="'.$_SERVER["PHP_SELF"].'" method="post" name="formulaire">';
226	print '<input type="hidden" name="token" value="'.newToken().'">';
227	print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
228	print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
229	print '<input type="hidden" name="type" value="'.$type.'">';
230
231	print_barre_liste($texte, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'product', 0, '', '', $limit, 0, 0, 1);
232
233
234	if (!empty($catid))
235	{
236		print "<div id='ways'>";
237		$c = new Categorie($db);
238		$c->fetch($catid);
239		$ways = $c->print_all_ways(' &gt; ', 'product/reassortlot.php');
240		print " &gt; ".$ways[0]."<br>\n";
241		print "</div><br>";
242	}
243
244	// Filter on categories
245 	$moreforfilter = '';
246	if (!empty($conf->categorie->enabled))
247	{
248	 	$moreforfilter .= '<div class="divsearchfield">';
249	 	$moreforfilter .= $langs->trans('Categories').': ';
250		$moreforfilter .= $htmlother->select_categories(Categorie::TYPE_PRODUCT, $search_categ, 'search_categ');
251	 	$moreforfilter .= '</div>';
252	}
253	//$moreforfilter.=$langs->trans("StockTooLow").' <input type="checkbox" name="toolowstock" value="1"'.($toolowstock?' checked':'').'>';
254
255	if (!empty($moreforfilter))
256	{
257		print '<div class="liste_titre liste_titre_bydiv centpercent">';
258		print $moreforfilter;
259		$parameters = array();
260		$reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
261		print $hookmanager->resPrint;
262		print '</div>';
263	}
264
265
266	print '<div class="div-table-responsive">';
267	print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">';
268
269	// Fields title search
270	print '<tr class="liste_titre_filter">';
271	print '<td class="liste_titre">';
272	print '<input class="flat" type="text" name="sref" size="6" value="'.$sref.'">';
273	print '</td>';
274	print '<td class="liste_titre">';
275	print '<input class="flat" type="text" name="snom" size="8" value="'.$snom.'">';
276	print '</td>';
277	if (!empty($conf->service->enabled) && $type == 1)
278	{
279		print '<td class="liste_titre">';
280		print '&nbsp;';
281		print '</td>';
282	}
283	print '<td class="liste_titre"><input class="flat" type="text" name="search_warehouse" size="6" value="'.$search_warehouse.'"></td>';
284	print '<td class="liste_titre center"><input class="flat" type="text" name="search_batch" size="6" value="'.$search_batch.'"></td>';
285	print '<td class="liste_titre right">&nbsp;</td>';
286	print '<td class="liste_titre">&nbsp;</td>';
287	if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
288		print '<td class="liste_titre">&nbsp;</td>';
289	}
290	if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
291		print '<td class="liste_titre">&nbsp;</td>';
292	}
293	print '<td class="liste_titre">&nbsp;</td>';
294	print '<td class="liste_titre">&nbsp;</td>';
295	print '<td class="liste_titre maxwidthsearch">';
296	$searchpicto = $form->showFilterAndCheckAddButtons(0);
297	print $searchpicto;
298	print '</td>';
299	print '</tr>';
300
301	//Line for column titles
302	print "<tr class=\"liste_titre\">";
303	print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "p.ref", $param, "", "", $sortfield, $sortorder);
304	print_liste_field_titre("Label", $_SERVER["PHP_SELF"], "p.label", $param, "", "", $sortfield, $sortorder);
305	if (!empty($conf->service->enabled) && $type == 1) print_liste_field_titre("Duration", $_SERVER["PHP_SELF"], "p.duration", $param, "", '', $sortfield, $sortorder, 'center ');
306	print_liste_field_titre("Warehouse", $_SERVER["PHP_SELF"], "e.ref", $param, "", '', $sortfield, $sortorder);
307	//print_liste_field_titre("DesiredStock", $_SERVER["PHP_SELF"], "p.desiredstock",$param,"",'',$sortfield,$sortorder, 'right );
308	print_liste_field_titre("Batch", $_SERVER["PHP_SELF"], "pb.batch", $param, "", '', $sortfield, $sortorder, 'center ');
309	if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
310		print_liste_field_titre("SellByDate", $_SERVER["PHP_SELF"], "pb.sellby", $param, "", '', $sortfield, $sortorder, 'center ');
311	}
312	if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
313		print_liste_field_titre("EatByDate", $_SERVER["PHP_SELF"], "pb.eatby", $param, "", '', $sortfield, $sortorder, 'center ');
314	}
315	print_liste_field_titre("PhysicalStock", $_SERVER["PHP_SELF"], "stock_physique", $param, "", '', $sortfield, $sortorder, 'right ');
316	// TODO Add info of running suppliers/customers orders
317	//print_liste_field_titre("TheoreticalStock",$_SERVER["PHP_SELF"], "stock_theorique",$param,"",'',$sortfield,$sortorder, 'right ');
318	print_liste_field_titre('');
319	print_liste_field_titre("ProductStatusOnSell", $_SERVER["PHP_SELF"], "p.tosell", "", $param, '', $sortfield, $sortorder, 'right ');
320	print_liste_field_titre("ProductStatusOnBuy", $_SERVER["PHP_SELF"], "p.tobuy", "", $param, '', $sortfield, $sortorder, 'right ');
321	print_liste_field_titre('');
322	print "</tr>\n";
323
324	$product_static = new Product($db);
325	$product_lot_static = new Productlot($db);
326	$warehousetmp = new Entrepot($db);
327
328	while ($i < min($num, $limit))
329	{
330		$objp = $db->fetch_object($resql);
331
332		// Multilangs
333		if (!empty($conf->global->MAIN_MULTILANGS)) // si l'option est active
334		{
335			$sql = "SELECT label";
336			$sql .= " FROM ".MAIN_DB_PREFIX."product_lang";
337			$sql .= " WHERE fk_product=".$objp->rowid;
338			$sql .= " AND lang='".$db->escape($langs->getDefaultLang())."'";
339			$sql .= " LIMIT 1";
340
341			$result = $db->query($sql);
342			if ($result)
343			{
344				$objtp = $db->fetch_object($result);
345				if (!empty($objtp->label)) $objp->label = $objtp->label;
346			}
347		}
348
349
350		$product_static->ref = $objp->ref;
351		$product_static->id = $objp->rowid;
352		$product_static->label = $objp->label;
353		$product_static->type = $objp->fk_product_type;
354		$product_static->entity = $objp->entity;
355		$product_static->status = $objp->tosell;
356		$product_static->status_buy = $objp->tobuy;
357		$product_static->status_batch = $objp->tobatch;
358
359		$product_lot_static->batch = $objp->batch;
360		$product_lot_static->product_id = $objp->rowid;
361		$product_lot_static->id = $objp->lotid;
362		$product_lot_static->eatby = $objp->eatby;
363		$product_lot_static->sellby = $objp->sellby;
364
365
366		$warehousetmp->id = $objp->fk_entrepot;
367		$warehousetmp->ref = $objp->warehouse_ref;
368		$warehousetmp->label = $objp->warehouse_ref;
369		$warehousetmp->fk_parent = $objp->warehouse_parent;
370
371		print '<tr>';
372
373		// Ref
374		print '<td class="nowrap">';
375		print $product_static->getNomUrl(1, '', 16);
376		//if ($objp->stock_theorique < $objp->seuil_stock_alerte) print ' '.img_warning($langs->trans("StockTooLow"));
377		print '</td>';
378
379		// Label
380		print '<td>'.$objp->label.'</td>';
381
382		if (!empty($conf->service->enabled) && $type == 1)
383		{
384			print '<td class="center">';
385			if (preg_match('/([0-9]+)y/i', $objp->duration, $regs)) print $regs[1].' '.$langs->trans("DurationYear");
386			elseif (preg_match('/([0-9]+)m/i', $objp->duration, $regs)) print $regs[1].' '.$langs->trans("DurationMonth");
387			elseif (preg_match('/([0-9]+)d/i', $objp->duration, $regs)) print $regs[1].' '.$langs->trans("DurationDay");
388			else print $objp->duration;
389			print '</td>';
390		}
391		//print '<td class="right">'.$objp->stock_theorique.'</td>';
392		//print '<td class="right">'.$objp->seuil_stock_alerte.'</td>';
393		//print '<td class="right">'.$objp->desiredstock.'</td>';
394
395		// Warehouse
396		print '<td class="nowrap">';
397		if ($objp->fk_entrepot > 0)
398		{
399			print $warehousetmp->getNomUrl(1);
400		}
401		print '</td>';
402
403		// Lot
404		print '<td class="center nowrap">';
405		if ($product_lot_static->batch)
406		{
407			print $product_lot_static->getNomUrl(1);
408		}
409		print '</td>';
410
411		if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
412			print '<td class="center">'.dol_print_date($db->jdate($objp->sellby), 'day').'</td>';
413		}
414
415		if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
416			print '<td class="center">'.dol_print_date($db->jdate($objp->eatby), 'day').'</td>';
417		}
418
419		print '<td class="right">';
420		//if ($objp->seuil_stock_alerte && ($objp->stock_physique < $objp->seuil_stock_alerte)) print img_warning($langs->trans("StockTooLow")).' ';
421		print $objp->stock_physique;
422		print '</td>';
423
424		print '<td class="right"><a href="'.DOL_URL_ROOT.'/product/stock/movement_list.php?idproduct='.$product_static->id.'&search_warehouse='.$objp->fk_entrepot.'&search_batch='.($objp->batch != 'Undefined' ? $objp->batch : 'Undefined').'">'.$langs->trans("Movements").'</a></td>';
425		print '<td class="right nowrap">'.$product_static->LibStatut($objp->statut, 5, 0).'</td>';
426		print '<td class="right nowrap">'.$product_static->LibStatut($objp->tobuy, 5, 1).'</td>';
427		print '<td></td>';
428		print "</tr>\n";
429		$i++;
430	}
431
432	print "</table>";
433	print '</div>';
434	print '</form>';
435
436	$db->free($resql);
437} else {
438	dol_print_error($db);
439}
440
441// End of page
442llxFooter();
443$db->close();
444