reassort.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. /* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/product/reassort.php
  23. * \ingroup produit
  24. * \brief Page to list stocks
  25. */
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array('products', 'stocks'));
  33. // Security check
  34. if ($user->societe_id) $socid=$user->societe_id;
  35. $result=restrictedArea($user,'produit|service');
  36. $action=GETPOST('action','alpha');
  37. $sref=GETPOST("sref");
  38. $snom=GETPOST("snom");
  39. $sall=trim((GETPOST('search_all', 'alphanohtml')!='')?GETPOST('search_all', 'alphanohtml'):GETPOST('sall', 'alphanohtml'));
  40. $type=GETPOST("type","int");
  41. $search_barcode=GETPOST("search_barcode");
  42. $catid=GETPOST('catid','int');
  43. $toolowstock=GETPOST('toolowstock');
  44. $tosell = GETPOST("tosell");
  45. $tobuy = GETPOST("tobuy");
  46. $fourn_id = GETPOST("fourn_id",'int');
  47. $sortfield = GETPOST("sortfield",'alpha');
  48. $sortorder = GETPOST("sortorder",'alpha');
  49. $page = GETPOST("page",'int');
  50. if (empty($page) || $page < 0) $page = 0;
  51. if (! $sortfield) $sortfield="p.ref";
  52. if (! $sortorder) $sortorder="ASC";
  53. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  54. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  55. $offset = $limit * $page ;
  56. // Load sale and categ filters
  57. $search_sale = GETPOST("search_sale");
  58. $search_categ = GETPOST("search_categ");
  59. // Get object canvas (By default, this is not defined, so standard usage of dolibarr)
  60. $canvas=GETPOST("canvas");
  61. $objcanvas=null;
  62. if (! empty($canvas))
  63. {
  64. require_once DOL_DOCUMENT_ROOT.'/core/class/canvas.class.php';
  65. $objcanvas = new Canvas($db,$action);
  66. $objcanvas->getCanvas('product','list',$canvas);
  67. }
  68. // Define virtualdiffersfromphysical
  69. $virtualdiffersfromphysical=0;
  70. if (! empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER))
  71. {
  72. $virtualdiffersfromphysical=1; // According to increase/decrease stock options, virtual and physical stock may differs.
  73. }
  74. /*
  75. * Actions
  76. */
  77. 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
  78. {
  79. $sref="";
  80. $snom="";
  81. $sall="";
  82. $tosell="";
  83. $tobuy="";
  84. $search_sale="";
  85. $search_categ="";
  86. $type="";
  87. $catid='';
  88. $toolowstock='';
  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,';
  102. $sql.= ' SUM(s.reel) as stock_physique';
  103. $sql.= ' FROM '.MAIN_DB_PREFIX.'product as p';
  104. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as s on p.rowid = s.fk_product';
  105. // We'll need this table joined to the select in order to filter by categ
  106. if ($search_categ) $sql.= ", ".MAIN_DB_PREFIX."categorie_product as cp";
  107. $sql.= " WHERE p.entity IN (".getEntity('product').")";
  108. if ($search_categ) $sql.= " AND p.rowid = cp.fk_product"; // Join for the needed table to filter by categ
  109. if ($sall) $sql.=natural_search(array('p.ref', 'p.label', 'p.description', 'p.note'), $sall);
  110. // if the type is not 1, we show all products (type = 0,2,3)
  111. if (dol_strlen($type))
  112. {
  113. if ($type==1)
  114. {
  115. $sql.= " AND p.fk_product_type = '1'";
  116. }
  117. else
  118. {
  119. $sql.= " AND p.fk_product_type <> '1'";
  120. }
  121. }
  122. if ($sref) $sql.= natural_search('p.ref', $sref);
  123. if ($search_barcode) $sql.= natural_search('p.barcode', $search_barcode);
  124. if ($snom) $sql.= natural_search('p.label', $snom);
  125. if (! empty($tosell)) $sql.= " AND p.tosell = ".$tosell;
  126. if (! empty($tobuy)) $sql.= " AND p.tobuy = ".$tobuy;
  127. if (! empty($canvas)) $sql.= " AND p.canvas = '".$db->escape($canvas)."'";
  128. if($catid) $sql.= " AND cp.fk_categorie = ".$catid;
  129. if ($fourn_id > 0) $sql.= " AND p.rowid = pf.fk_product AND pf.fk_soc = ".$fourn_id;
  130. // Insert categ filter
  131. if ($search_categ) $sql .= " AND cp.fk_categorie = ".$db->escape($search_categ);
  132. $sql.= " GROUP BY p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type, p.entity,";
  133. $sql.= " p.fk_product_type, p.tms, p.duration, p.tosell, p.tobuy, p.seuil_stock_alerte, p.desiredstock";
  134. if ($toolowstock) $sql.= " HAVING SUM(".$db->ifsql('s.reel IS NULL', '0', 's.reel').") < p.seuil_stock_alerte";
  135. $sql.= $db->order($sortfield,$sortorder);
  136. // Count total nb of records
  137. $nbtotalofrecords = '';
  138. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  139. {
  140. $result = $db->query($sql);
  141. $nbtotalofrecords = $db->num_rows($result);
  142. if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0
  143. {
  144. $page = 0;
  145. $offset = 0;
  146. }
  147. }
  148. $sql.= $db->plimit($limit + 1, $offset);
  149. $resql = $db->query($sql);
  150. if ($resql)
  151. {
  152. $num = $db->num_rows($resql);
  153. $i = 0;
  154. if ($num == 1 && GETPOST('autojumpifoneonly') && ($sall || $snom || $sref))
  155. {
  156. $objp = $db->fetch_object($resql);
  157. header("Location: card.php?id=$objp->rowid");
  158. exit;
  159. }
  160. if (isset($type))
  161. {
  162. if ($type==1) { $texte = $langs->trans("Services"); }
  163. else { $texte = $langs->trans("Products"); }
  164. } else {
  165. $texte = $langs->trans("ProductsAndServices");
  166. }
  167. $texte.=' ('.$langs->trans("Stocks").')';
  168. $param='';
  169. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  170. if ($sall) $param.="&sall=".$sall;
  171. if ($tosell) $param.="&tosell=".$tosell;
  172. if ($tobuy) $param.="&tobuy=".$tobuy;
  173. if ($type) $param.="&type=".$type;
  174. if ($fourn_id) $param.="&fourn_id=".$fourn_id;
  175. if ($snom) $param.="&snom=".$snom;
  176. if ($sref) $param.="&sref=".$sref;
  177. if ($search_sale) $param.="&search_sale=".$search_sale;
  178. if ($search_categ) $param.="&search_categ=".$search_categ;
  179. if ($toolowstock) $param.="&toolowstock=".$toolowstock;
  180. if ($sbarcode) $param.="&sbarcode=".$sbarcode;
  181. if ($catid) $param.="&catid=".$catid;
  182. llxHeader("", $texte, $helpurl);
  183. print '<form action="'. $_SERVER["PHP_SELF"] .'" method="post" name="formulaire">';
  184. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  185. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  186. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  187. print '<input type="hidden" name="page" value="'.$page.'">';
  188. print '<input type="hidden" name="type" value="'.$type.'">';
  189. print_barre_liste($texte, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder,'',$num, $nbtotalofrecords, 'title_products', 0, '', '', $limit);
  190. if (! empty($catid))
  191. {
  192. print "<div id='ways'>";
  193. $c = new Categorie($db);
  194. $c->fetch($catid);
  195. $ways = $c->print_all_ways(' &gt; ','product/reassort.php');
  196. print " &gt; ".$ways[0]."<br>\n";
  197. print "</div><br>";
  198. }
  199. // Filter on categories
  200. $moreforfilter='';
  201. if (! empty($conf->categorie->enabled))
  202. {
  203. $moreforfilter.='<div class="divsearchfield">';
  204. $moreforfilter.=$langs->trans('Categories'). ': ';
  205. $moreforfilter.=$htmlother->select_categories(Categorie::TYPE_PRODUCT,$search_categ,'search_categ');
  206. $moreforfilter.='</div>';
  207. }
  208. $moreforfilter.='<div class="divsearchfield">';
  209. $moreforfilter.=$langs->trans("StockTooLow").' <input type="checkbox" name="toolowstock" value="1"'.($toolowstock?' checked':'').'>';
  210. $moreforfilter.='</div>';
  211. if (! empty($moreforfilter))
  212. {
  213. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  214. print $moreforfilter;
  215. $parameters=array();
  216. $reshook=$hookmanager->executeHooks('printFieldPreListTitle',$parameters); // Note that $action and $object may have been modified by hook
  217. print $hookmanager->resPrint;
  218. print '</div>';
  219. }
  220. $param='';
  221. if ($tosell) $param.="&tosell=".$tosell;
  222. if ($tobuy) $param.="&tobuy=".$tobuy;
  223. if ($type) $param.="&type=".$type;
  224. if ($fourn_id) $param.="&fourn_id=".$fourn_id;
  225. if ($snom) $param.="&snom=".$snom;
  226. if ($sref) $param.="&sref=".$sref;
  227. if ($toolowstock) $param.="&toolowstock=".$toolowstock;
  228. if ($search_categ) $param.="&search_categ=".$search_categ;
  229. $formProduct = new FormProduct($db);
  230. $formProduct->loadWarehouses();
  231. $warehouses_list = $formProduct->cache_warehouses;
  232. $nb_warehouse = count($warehouses_list);
  233. $colspan_warehouse = 1;
  234. if (! empty($conf->global->STOCK_DETAIL_ON_WAREHOUSE)) { $colspan_warehouse = $nb_warehouse > 1 ? $nb_warehouse+1 : 1; }
  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. // Duration
  246. if (! empty($conf->service->enabled) && $type == 1)
  247. {
  248. print '<td class="liste_titre">';
  249. print '&nbsp;';
  250. print '</td>';
  251. }
  252. // Stock limit
  253. print '<td class="liste_titre">&nbsp;</td>';
  254. print '<td class="liste_titre" align="right">&nbsp;</td>';
  255. print '<td class="liste_titre">&nbsp;</td>';
  256. if ($virtualdiffersfromphysical) print '<td class="liste_titre">&nbsp;</td>';
  257. print '<td class="liste_titre">&nbsp;</td>';
  258. print '<td class="liste_titre" colspan="'.$colspan_warehouse.'">&nbsp;</td>';
  259. print '<td class="liste_titre"></td>';
  260. print '<td class="liste_titre" align="right">';
  261. $searchpicto=$form->showFilterAndCheckAddButtons(0);
  262. print $searchpicto;
  263. print '</td>';
  264. print '</tr>';
  265. // Lignes des titres
  266. print "<tr class=\"liste_titre\">";
  267. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "p.ref",$param,"","",$sortfield,$sortorder);
  268. print_liste_field_titre("Label", $_SERVER["PHP_SELF"], "p.label",$param,"","",$sortfield,$sortorder);
  269. if (! empty($conf->service->enabled) && $type == 1) print_liste_field_titre("Duration", $_SERVER["PHP_SELF"], "p.duration",$param,"",'align="center"',$sortfield,$sortorder);
  270. print_liste_field_titre("StockLimit", $_SERVER["PHP_SELF"], "p.seuil_stock_alerte",$param,"",'align="right"',$sortfield,$sortorder);
  271. print_liste_field_titre("DesiredStock", $_SERVER["PHP_SELF"], "p.desiredstock",$param,"",'align="right"',$sortfield,$sortorder);
  272. print_liste_field_titre("PhysicalStock", $_SERVER["PHP_SELF"], "stock_physique",$param,"",'align="right"',$sortfield,$sortorder);
  273. // Details per warehouse
  274. if (! empty($conf->global->STOCK_DETAIL_ON_WAREHOUSE)) // TODO This should be moved into the selection of fields on page product/list (page product/stock will be removed and replaced with product/list with its own context)
  275. {
  276. if ($nb_warehouse>1) {
  277. foreach($warehouses_list as &$wh) {
  278. print_liste_field_titre($wh['label'], '', '','','','align="right"');
  279. }
  280. }
  281. }
  282. if ($virtualdiffersfromphysical) print_liste_field_titre("VirtualStock",$_SERVER["PHP_SELF"], "",$param,"",'align="right"',$sortfield,$sortorder);
  283. print_liste_field_titre('');
  284. print_liste_field_titre( $langs->trans("Status").' ('.$langs->trans("Sell").')',$_SERVER["PHP_SELF"], "p.tosell",$param,"",'align="right"',$sortfield,$sortorder);
  285. print_liste_field_titre( $langs->trans("Status").' ('.$langs->trans("Buy").')',$_SERVER["PHP_SELF"], "p.tobuy",$param,"",'align="right"',$sortfield,$sortorder);
  286. print_liste_field_titre('');
  287. print "</tr>\n";
  288. while ($i < min($num,$limit))
  289. {
  290. $objp = $db->fetch_object($resql);
  291. $product=new Product($db);
  292. $product->fetch($objp->rowid);
  293. $product->load_stock();
  294. print '<tr>';
  295. print '<td class="nowrap">';
  296. print $product->getNomUrl(1,'',16);
  297. //if ($objp->stock_theorique < $objp->seuil_stock_alerte) print ' '.img_warning($langs->trans("StockTooLow"));
  298. print '</td>';
  299. print '<td>'.$product->label.'</td>';
  300. if (! empty($conf->service->enabled) && $type == 1)
  301. {
  302. print '<td align="center">';
  303. if (preg_match('/([0-9]+)y/i',$objp->duration,$regs)) print $regs[1].' '.$langs->trans("DurationYear");
  304. elseif (preg_match('/([0-9]+)m/i',$objp->duration,$regs)) print $regs[1].' '.$langs->trans("DurationMonth");
  305. elseif (preg_match('/([0-9]+)d/i',$objp->duration,$regs)) print $regs[1].' '.$langs->trans("DurationDay");
  306. else print $objp->duration;
  307. print '</td>';
  308. }
  309. //print '<td align="right">'.$objp->stock_theorique.'</td>';
  310. print '<td align="right">'.$objp->seuil_stock_alerte.'</td>';
  311. print '<td align="right">'.$objp->desiredstock.'</td>';
  312. // Real stock
  313. print '<td align="right">';
  314. if ($objp->seuil_stock_alerte != '' && ($objp->stock_physique < $objp->seuil_stock_alerte)) print img_warning($langs->trans("StockTooLow")).' ';
  315. print $objp->stock_physique|0;
  316. print '</td>';
  317. // Details per warehouse
  318. if (! empty($conf->global->STOCK_DETAIL_ON_WAREHOUSE)) // TODO This should be moved into the selection of fields on page product/list (page product/stock will be removed and replaced with product/list with its own context)
  319. {
  320. if($nb_warehouse>1) {
  321. foreach($warehouses_list as &$wh) {
  322. print '<td align="right">';
  323. print empty($product->stock_warehouse[$wh['id']]->real) ? '0' : $product->stock_warehouse[$wh['id']]->real;
  324. print '</td>';
  325. }
  326. }
  327. }
  328. // Virtual stock
  329. if ($virtualdiffersfromphysical)
  330. {
  331. print '<td align="right">';
  332. if ($objp->seuil_stock_alerte != '' && ($product->stock_theorique < $objp->seuil_stock_alerte)) print img_warning($langs->trans("StockTooLow")).' ';
  333. print $product->stock_theorique;
  334. print '</td>';
  335. }
  336. print '<td align="right"><a href="'.DOL_URL_ROOT.'/product/stock/movement_list.php?idproduct='.$product->id.'">'.$langs->trans("Movements").'</a></td>';
  337. print '<td align="right" class="nowrap">'.$product->LibStatut($objp->statut,5,0).'</td>';
  338. print '<td align="right" class="nowrap">'.$product->LibStatut($objp->tobuy,5,1).'</td>';
  339. print '<td></td>';
  340. print "</tr>\n";
  341. $i++;
  342. }
  343. print "</table>";
  344. print '</div>';
  345. print '</form>';
  346. $db->free($resql);
  347. }
  348. else
  349. {
  350. dol_print_error($db);
  351. }
  352. // End of page
  353. llxFooter();
  354. $db->close();