reassortlot.php 17 KB

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