stockatdate.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. <?php
  2. /* Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
  3. * Copyright (C) 2013-2020 Laurent Destaileur <ely@users.sourceforge.net>
  4. * Copyright (C) 2014 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2016 ATM Consulting <support@atm-consulting.fr>
  7. * Copyright (C) 2019-2021 Frédéric France <frederic.france@netlogic.fr>
  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 <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/product/stock/stockatdate.php
  24. * \ingroup stock
  25. * \brief Page to list stocks at a given date
  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/entrepot.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  34. require_once './lib/replenishment.lib.php';
  35. // Load translation files required by the page
  36. $langs->loadLangs(array('products', 'stocks', 'orders'));
  37. // Security check
  38. if ($user->socid) {
  39. $socid = $user->socid;
  40. }
  41. $result = restrictedArea($user, 'produit|service');
  42. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  43. $hookmanager->initHooks(array('stockatdate'));
  44. //checks if a product has been ordered
  45. $action = GETPOST('action', 'aZ09');
  46. $type = GETPOST('type', 'int');
  47. $mode = GETPOST('mode', 'alpha');
  48. $date = '';
  49. $dateendofday = '';
  50. if (GETPOSTISSET('dateday') && GETPOSTISSET('datemonth') && GETPOSTISSET('dateyear')) {
  51. $date = dol_mktime(0, 0, 0, GETPOST('datemonth', 'int'), GETPOST('dateday', 'int'), GETPOST('dateyear', 'int'));
  52. $dateendofday = dol_mktime(23, 59, 59, GETPOST('datemonth', 'int'), GETPOST('dateday', 'int'), GETPOST('dateyear', 'int'));
  53. }
  54. $search_ref = GETPOST('search_ref', 'alphanohtml');
  55. $search_nom = GETPOST('search_nom', 'alphanohtml');
  56. $now = dol_now();
  57. $productid = GETPOST('productid', 'int');
  58. $fk_warehouse = GETPOST('fk_warehouse', 'int');
  59. $sortfield = GETPOST('sortfield', 'aZ09comma');
  60. $sortorder = GETPOST('sortorder', 'aZ09comma');
  61. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  62. if (empty($page) || $page == -1) {
  63. $page = 0;
  64. } // If $page is not defined, or '' or -1
  65. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  66. $offset = $limit * $page;
  67. if (!$sortfield) {
  68. $sortfield = 'p.ref';
  69. }
  70. if (!$sortorder) {
  71. $sortorder = 'ASC';
  72. }
  73. $parameters = array();
  74. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  75. if ($reshook < 0) {
  76. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  77. }
  78. $dateIsValid = true;
  79. if ($mode == 'future') {
  80. if ($date && $date < $now) {
  81. setEventMessages($langs->trans("ErrorDateMustBeInFuture"), null, 'errors');
  82. $dateIsValid = false;
  83. }
  84. } else {
  85. if ($date && $date > $now) {
  86. setEventMessages($langs->trans("ErrorDateMustBeBeforeToday"), null, 'errors');
  87. $dateIsValid = false;
  88. }
  89. }
  90. /*
  91. * Actions
  92. */
  93. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // Both test are required to be compatible with all browsers
  94. $date = '';
  95. $productid = 0;
  96. $fk_warehouse = 0;
  97. $search_ref = '';
  98. $search_nom = '';
  99. }
  100. $warehouseStatus = array();
  101. if (!empty($conf->global->ENTREPOT_EXTRA_STATUS)) {
  102. //$warehouseStatus[] = Entrepot::STATUS_CLOSED;
  103. $warehouseStatus[] = Entrepot::STATUS_OPEN_ALL;
  104. $warehouseStatus[] = Entrepot::STATUS_OPEN_INTERNAL;
  105. }
  106. // Get array with current stock per product, warehouse
  107. $stock_prod_warehouse = array();
  108. $stock_prod = array();
  109. if ($date && $dateIsValid) { // Avoid heavy sql if mandatory date is not defined
  110. $sql = "SELECT ps.fk_product, ps.fk_entrepot as fk_warehouse,";
  111. $sql .= " SUM(ps.reel) AS stock";
  112. $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps";
  113. $sql .= ", ".MAIN_DB_PREFIX."entrepot as w";
  114. $sql .= " WHERE w.entity IN (".getEntity('stock').")";
  115. $sql .= " AND w.rowid = ps.fk_entrepot";
  116. if (!empty($conf->global->ENTREPOT_EXTRA_STATUS) && count($warehouseStatus)) {
  117. $sql .= " AND w.statut IN (".$db->sanitize(implode(',', $warehouseStatus)).")";
  118. }
  119. if ($productid > 0) {
  120. $sql .= " AND ps.fk_product = ".((int) $productid);
  121. }
  122. if ($fk_warehouse > 0) {
  123. $sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
  124. }
  125. $sql .= " GROUP BY fk_product, fk_entrepot";
  126. //print $sql;
  127. $resql = $db->query($sql);
  128. if ($resql) {
  129. $num = $db->num_rows($resql);
  130. $i = 0;
  131. while ($i < $num) {
  132. $obj = $db->fetch_object($resql);
  133. $tmp_fk_product = $obj->fk_product;
  134. $tmp_fk_warehouse = $obj->fk_warehouse;
  135. $stock = $obj->stock;
  136. $stock_prod_warehouse[$tmp_fk_product][$tmp_fk_warehouse] = $stock;
  137. $stock_prod[$tmp_fk_product] = (isset($stock_prod[$tmp_fk_product]) ? $stock_prod[$tmp_fk_product] : 0) + $stock;
  138. $i++;
  139. }
  140. $db->free($resql);
  141. } else {
  142. dol_print_error($db);
  143. }
  144. //var_dump($stock_prod_warehouse);
  145. } elseif ($action == 'filter') {
  146. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
  147. }
  148. // Get array with list of stock movements between date and now (for product/warehouse=
  149. $movements_prod_warehouse = array();
  150. $movements_prod = array();
  151. $movements_prod_warehouse_nb = array();
  152. $movements_prod_nb = array();
  153. if ($date && $dateIsValid) {
  154. $sql = "SELECT sm.fk_product, sm.fk_entrepot, SUM(sm.value) AS stock, COUNT(sm.rowid) AS nbofmovement";
  155. $sql .= " FROM ".MAIN_DB_PREFIX."stock_mouvement as sm";
  156. $sql .= ", ".MAIN_DB_PREFIX."entrepot as w";
  157. $sql .= " WHERE w.entity IN (".getEntity('stock').")";
  158. $sql .= " AND w.rowid = sm.fk_entrepot";
  159. if (!empty($conf->global->ENTREPOT_EXTRA_STATUS) && count($warehouseStatus)) {
  160. $sql .= " AND w.statut IN (".$db->sanitize(implode(',', $warehouseStatus)).")";
  161. }
  162. if ($mode == 'future') {
  163. $sql .= " AND sm.datem <= '".$db->idate($dateendofday)."'";
  164. } else {
  165. $sql .= " AND sm.datem >= '".$db->idate($dateendofday)."'";
  166. }
  167. if ($productid > 0) {
  168. $sql .= " AND sm.fk_product = ".((int) $productid);
  169. }
  170. if ($fk_warehouse > 0) {
  171. $sql .= " AND sm.fk_entrepot = ".((int) $fk_warehouse);
  172. }
  173. $sql .= " GROUP BY sm.fk_product, sm.fk_entrepot";
  174. $resql = $db->query($sql);
  175. if ($resql) {
  176. $num = $db->num_rows($resql);
  177. $i = 0;
  178. while ($i < $num) {
  179. $obj = $db->fetch_object($resql);
  180. $fk_product = $obj->fk_product;
  181. $fk_entrepot = $obj->fk_entrepot;
  182. $stock = $obj->stock;
  183. $nbofmovement = $obj->nbofmovement;
  184. // Pour llx_product_stock.reel
  185. $movements_prod_warehouse[$fk_product][$fk_entrepot] = $stock;
  186. $movements_prod_warehouse_nb[$fk_product][$fk_entrepot] = $nbofmovement;
  187. // Pour llx_product.stock
  188. $movements_prod[$fk_product] += $stock;
  189. $movements_prod_nb[$fk_product] += $nbofmovement;
  190. $i++;
  191. }
  192. $db->free($resql);
  193. } else {
  194. dol_print_error($db);
  195. }
  196. }
  197. //var_dump($movements_prod_warehouse);
  198. //var_dump($movements_prod);
  199. /*
  200. * View
  201. */
  202. $form = new Form($db);
  203. $formproduct = new FormProduct($db);
  204. $prod = new Product($db);
  205. $num = 0;
  206. $title = $langs->trans('StockAtDate');
  207. $sql = 'SELECT p.rowid, p.ref, p.label, p.description, p.price,';
  208. $sql .= ' p.price_ttc, p.price_base_type, p.fk_product_type, p.desiredstock, p.seuil_stock_alerte,';
  209. $sql .= ' p.tms as datem, p.duration, p.tobuy, p.stock, ';
  210. if ($fk_warehouse > 0) {
  211. $sql .= " SUM(p.pmp * ps.reel) as estimatedvalue, SUM(p.price * ps.reel) as sellvalue";
  212. $sql .= ', SUM(ps.reel) as stock_reel';
  213. } else {
  214. $sql .= " SUM(p.pmp * p.stock) as estimatedvalue, SUM(p.price * p.stock) as sellvalue";
  215. }
  216. // Add fields from hooks
  217. $parameters = array();
  218. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook
  219. $sql .= $hookmanager->resPrint;
  220. $sql .= ' FROM '.MAIN_DB_PREFIX.'product as p';
  221. if ($fk_warehouse > 0) {
  222. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as ps ON p.rowid = ps.fk_product AND ps.fk_entrepot = '.((int) $fk_warehouse);
  223. }
  224. // Add fields from hooks
  225. $parameters = array();
  226. $reshook = $hookmanager->executeHooks('printFieldListJoin', $parameters); // Note that $action and $object may have been modified by hook
  227. $sql .= $hookmanager->resPrint;
  228. $sql .= ' WHERE p.entity IN ('.getEntity('product').')';
  229. if ($productid > 0) {
  230. $sql .= " AND p.rowid = ".((int) $productid);
  231. }
  232. if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
  233. $sql .= " AND p.fk_product_type = 0";
  234. }
  235. if (!empty($canvas)) {
  236. $sql .= " AND p.canvas = '".$db->escape($canvas)."'";
  237. }
  238. if ($fk_warehouse > 0) {
  239. $sql .= ' GROUP BY p.rowid, p.ref, p.label, p.description, p.price, p.price_ttc, p.price_base_type, p.fk_product_type, p.desiredstock, p.seuil_stock_alerte,';
  240. $sql .= ' p.tms, p.duration, p.tobuy, p.stock';
  241. } else {
  242. $sql .= ' GROUP BY p.rowid, p.ref, p.label, p.description, p.price, p.price_ttc, p.price_base_type, p.fk_product_type, p.desiredstock, p.seuil_stock_alerte,';
  243. $sql .= ' p.tms, p.duration, p.tobuy, p.stock';
  244. }
  245. // Add where from hooks
  246. $parameters = array();
  247. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
  248. $sql .= $hookmanager->resPrint;
  249. if ($sortfield == 'stock_reel' && $fk_warehouse <= 0) {
  250. $sortfield = 'stock';
  251. }
  252. if ($sortfield == 'stock' && $fk_warehouse > 0) {
  253. $sortfield = 'stock_reel';
  254. }
  255. $sql .= $db->order($sortfield, $sortorder);
  256. $nbtotalofrecords = 0;
  257. if ($date && $dateIsValid) { // We avoid a heavy sql if mandatory parameter date not yet defined
  258. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  259. $result = $db->query($sql);
  260. $nbtotalofrecords = $db->num_rows($result);
  261. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  262. $page = 0;
  263. $offset = 0;
  264. }
  265. }
  266. $sql .= $db->plimit($limit + 1, $offset);
  267. //print $sql;
  268. $resql = $db->query($sql);
  269. if (empty($resql)) {
  270. dol_print_error($db);
  271. exit;
  272. }
  273. $num = $db->num_rows($resql);
  274. }
  275. $i = 0;
  276. //print $sql;
  277. $helpurl = 'EN:Module_Stocks_En|FR:Module_Stock|';
  278. $helpurl .= 'ES:M&oacute;dulo_Stocks';
  279. llxHeader('', $title, $helpurl, '');
  280. $head = array();
  281. $head[0][0] = DOL_URL_ROOT.'/product/stock/stockatdate.php';
  282. $head[0][1] = $langs->trans("StockAtDateInPast");
  283. $head[0][2] = 'stockatdatepast';
  284. $head[1][0] = DOL_URL_ROOT.'/product/stock/stockatdate.php?mode=future';
  285. $head[1][1] = $langs->trans("StockAtDateInFuture");
  286. $head[1][2] = 'stockatdatefuture';
  287. print load_fiche_titre($langs->trans('StockAtDate'), '', 'stock');
  288. print dol_get_fiche_head($head, ($mode == 'future' ? 'stockatdatefuture' : 'stockatdatepast'), '', -1, '');
  289. $desc = $langs->trans("StockAtDatePastDesc");
  290. if ($mode == 'future') {
  291. $desc = $langs->trans("StockAtDateFutureDesc");
  292. }
  293. print '<span class="opacitymedium">'.$desc.'</span><br>'."\n";
  294. print '<br>'."\n";
  295. print '<form name="formFilterWarehouse" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  296. print '<input type="hidden" name="token" value="'.newToken().'">';
  297. print '<input type="hidden" name="action" value="filter">';
  298. print '<input type="hidden" name="mode" value="'.$mode.'">';
  299. print '<div class="inline-block valignmiddle" style="padding-right: 20px;">';
  300. print '<span class="fieldrequired">'.$langs->trans('Date').'</span> '.$form->selectDate(($date ? $date : -1), 'date');
  301. print ' <span class="clearbothonsmartphone marginleftonly paddingleftonly marginrightonly paddingrightonly">&nbsp;</span> ';
  302. print img_picto('', 'product', 'class="pictofiwedwidth"').' ';
  303. print '</span> ';
  304. print $form->select_produits($productid, 'productid', '', 0, 0, -1, 2, '', 0, array(), 0, $langs->trans('Product'), 0, 'maxwidth300', 0, '', null, 1);
  305. print ' <span class="clearbothonsmartphone marginleftonly paddingleftonly marginrightonly paddingrightonly">&nbsp;</span> ';
  306. print img_picto('', 'stock', 'class="pictofiwedwidth"');
  307. print '</span> ';
  308. print $formproduct->selectWarehouses((GETPOSTISSET('fk_warehouse') ? $fk_warehouse : 'ifone'), 'fk_warehouse', '', 1, 0, 0, $langs->trans('Warehouse'), 0, 0, null, '', null, 1, false, 'e.ref');
  309. print '</div>';
  310. $parameters = array();
  311. $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
  312. if (empty($reshook)) {
  313. print $hookmanager->resPrint;
  314. }
  315. print '<div class="inline-block valignmiddle">';
  316. print '<input type="submit" class="button" name="valid" value="'.$langs->trans('Refresh').'">';
  317. print '</div>';
  318. //print '</form>';
  319. $param = '';
  320. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  321. $param .= '&contextpage='.urlencode($contextpage);
  322. }
  323. if ($limit > 0 && $limit != $conf->liste_limit) {
  324. $param .= '&limit='.urlencode($limit);
  325. }
  326. $param .= '&mode='.$mode;
  327. if ($fk_warehouse > 0) {
  328. $param .= '&fk_warehouse='.$fk_warehouse;
  329. }
  330. if ($productid > 0) {
  331. $param .= '&productid='.$productid;
  332. }
  333. if (GETPOST('dateday', 'int') > 0) {
  334. $param .= '&dateday='.GETPOST('dateday', 'int');
  335. }
  336. if (GETPOST('datemonth', 'int') > 0) {
  337. $param .= '&datemonth='.GETPOST('datemonth', 'int');
  338. }
  339. if (GETPOST('dateyear', 'int') > 0) {
  340. $param .= '&dateyear='.GETPOST('dateyear', 'int');
  341. }
  342. // TODO Move this into the title line ?
  343. print_barre_liste('', $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'stock', 0, '', '', $limit, 0, 0, 1);
  344. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  345. print '<table class="liste centpercent">';
  346. $stocklabel = $langs->trans('StockAtDate');
  347. if ($mode == 'future') {
  348. $stocklabel = $langs->trans("VirtualStockAtDate");
  349. }
  350. //print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" name="formulaire">';
  351. print '<input type="hidden" name="token" value="'.newToken().'">';
  352. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  353. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  354. print '<input type="hidden" name="type" value="'.$type.'">';
  355. print '<input type="hidden" name="mode" value="'.$mode.'">';
  356. // Fields title search
  357. print '<tr class="liste_titre_filter">';
  358. print '<td class="liste_titre"><input class="flat" type="text" name="search_ref" size="8" value="'.dol_escape_htmltag($search_ref).'"></td>';
  359. print '<td class="liste_titre"><input class="flat" type="text" name="search_nom" size="8" value="'.dol_escape_htmltag($search_nom).'"></td>';
  360. print '<td class="liste_titre"></td>';
  361. print '<td class="liste_titre"></td>';
  362. print '<td class="liste_titre"></td>';
  363. if ($mode == 'future') {
  364. print '<td class="liste_titre"></td>';
  365. } else {
  366. print '<td class="liste_titre"></td>';
  367. print '<td class="liste_titre"></td>';
  368. }
  369. // Fields from hook
  370. $parameters = array('param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
  371. $reshook = $hookmanager->executeHooks('printFieldListOption', $parameters); // Note that $action and $object may have been modified by hook
  372. print $hookmanager->resPrint;
  373. print '<td class="liste_titre maxwidthsearch">';
  374. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  375. print $searchpicto;
  376. print '</td>';
  377. print '</tr>';
  378. $fieldtosortcurrentstock = 'stock';
  379. if ($fk_warehouse > 0) {
  380. $fieldtosortcurrentstock = 'stock_reel';
  381. }
  382. // Lines of title
  383. print '<tr class="liste_titre">';
  384. print_liste_field_titre('Ref', $_SERVER["PHP_SELF"], 'p.ref', $param, '', '', $sortfield, $sortorder);
  385. print_liste_field_titre('Label', $_SERVER["PHP_SELF"], 'p.label', $param, '', '', $sortfield, $sortorder);
  386. if ($mode == 'future') {
  387. print_liste_field_titre('CurrentStock', $_SERVER["PHP_SELF"], $fieldtosortcurrentstock, $param, '', '', $sortfield, $sortorder, 'right ');
  388. print_liste_field_titre('', $_SERVER["PHP_SELF"]);
  389. print_liste_field_titre($stocklabel, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'right ', 'VirtualStockAtDateDesc');
  390. print_liste_field_titre('VirtualStock', $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'right ', 'VirtualStockDesc');
  391. } else {
  392. print_liste_field_titre($stocklabel, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'right ');
  393. print_liste_field_titre("EstimatedStockValue", $_SERVER["PHP_SELF"], "estimatedvalue", '', $param, '', $sortfield, $sortorder, 'right ', $langs->trans("AtDate"), 1);
  394. print_liste_field_titre("EstimatedStockValueSell", $_SERVER["PHP_SELF"], "", '', $param, '', $sortfield, $sortorder, 'right ', $langs->trans("AtDate"), 1);
  395. print_liste_field_titre('', $_SERVER["PHP_SELF"]);
  396. print_liste_field_titre('CurrentStock', $_SERVER["PHP_SELF"], $fieldtosortcurrentstock, $param, '', '', $sortfield, $sortorder, 'right ');
  397. }
  398. print_liste_field_titre('', $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'right ');
  399. // Hook fields
  400. $parameters = array('param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
  401. $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters); // Note that $action and $object may have been modified by hook
  402. print $hookmanager->resPrint;
  403. print "</tr>\n";
  404. $i = 0;
  405. while ($i < ($limit ? min($num, $limit) : $num)) {
  406. $objp = $db->fetch_object($resql);
  407. if (!empty($conf->global->STOCK_SUPPORTS_SERVICES) || $objp->fk_product_type == 0) {
  408. $prod->fetch($objp->rowid);
  409. // Multilangs
  410. /*if (!empty($conf->global->MAIN_MULTILANGS))
  411. {
  412. $sql = 'SELECT label,description';
  413. $sql .= ' FROM '.MAIN_DB_PREFIX.'product_lang';
  414. $sql .= ' WHERE fk_product = '.((int) $objp->rowid);
  415. $sql .= " AND lang = '".$db->escape($langs->getDefaultLang())."'";
  416. $sql .= ' LIMIT 1';
  417. $resqlm = $db->query($sql);
  418. if ($resqlm)
  419. {
  420. $objtp = $db->fetch_object($resqlm);
  421. if (!empty($objtp->description)) $objp->description = $objtp->description;
  422. if (!empty($objtp->label)) $objp->label = $objtp->label;
  423. }
  424. }*/
  425. $currentstock = '';
  426. if ($fk_warehouse > 0) {
  427. //if ($productid > 0) {
  428. $currentstock = $stock_prod_warehouse[$objp->rowid][$fk_warehouse];
  429. //} else {
  430. // $currentstock = $objp->stock_reel;
  431. //}
  432. } else {
  433. //if ($productid > 0) {
  434. $currentstock = $stock_prod[$objp->rowid];
  435. //} else {
  436. // $currentstock = $objp->stock;
  437. //}
  438. }
  439. if ($mode == 'future') {
  440. $prod->load_stock('warehouseopen, warehouseinternal', 0); // This call also ->load_virtual_stock()
  441. //$result = $prod->load_stats_reception(0, '4');
  442. //print $prod->stats_commande_fournisseur['qty'].'<br>'."\n";
  443. //print $prod->stats_reception['qty'];
  444. $stock = '<span class="opacitymedium">'.$langs->trans("FeatureNotYetAvailable").'</span>';
  445. $virtualstock = $prod->stock_theorique;
  446. } else {
  447. if ($fk_warehouse > 0) {
  448. $stock = $currentstock - $movements_prod_warehouse[$objp->rowid][$fk_warehouse];
  449. $nbofmovement = $movements_prod_warehouse_nb[$objp->rowid][$fk_warehouse];
  450. } else {
  451. $stock = $currentstock - $movements_prod[$objp->rowid];
  452. $nbofmovement = $movements_prod_nb[$objp->rowid];
  453. }
  454. }
  455. print '<tr class="oddeven">';
  456. // Product ref
  457. print '<td class="nowrap">'.$prod->getNomUrl(1, '').'</td>';
  458. // Product label
  459. print '<td>'.$objp->label;
  460. print '<input type="hidden" name="desc'.$i.'" value="'.dol_escape_htmltag($objp->description).'">'; // TODO Remove this and make a fetch to get description when creating order instead of a GETPOST
  461. print '</td>';
  462. if ($mode == 'future') {
  463. // Current stock
  464. print '<td class="right">'.$currentstock.'</td>';
  465. print '<td class="right"></td>';
  466. // Virtual stock at date
  467. print '<td class="right">'.$stock.'</td>';
  468. // Final virtual stock
  469. print '<td class="right">'.$virtualstock.'</td>';
  470. } else {
  471. // Stock at date
  472. print '<td class="right">'.($stock ? $stock : '<span class="opacitymedium">'.$stock.'</span>').'</td>';
  473. // PMP value
  474. print '<td class="right">';
  475. if (price2num($objp->estimatedvalue, 'MT')) {
  476. print price(price2num($objp->estimatedvalue, 'MT'), 1);
  477. } else {
  478. print '';
  479. }
  480. print '</td>';
  481. // Selling value
  482. print '<td class="right">';
  483. if (empty($conf->global->PRODUIT_MULTIPRICES)) {
  484. print price(price2num($objp->sellvalue, 'MT'), 1);
  485. } else {
  486. $htmltext = $langs->trans("OptionMULTIPRICESIsOn");
  487. print $form->textwithtooltip($langs->trans("Variable"), $htmltext);
  488. }
  489. print'</td>';
  490. print '<td class="right">';
  491. if ($nbofmovement > 0) {
  492. print '<a href="'.DOL_URL_ROOT.'/product/stock/movement_list.php?idproduct='.$objp->rowid.($fk_warehouse > 0 ? '&search_warehouse='.$fk_warehouse : '').'">'.$langs->trans("Movements").'</a>';
  493. print ' <span class="tabs"><span class="badge">'.$nbofmovement.'</span></span>';
  494. }
  495. print '</td>';
  496. // Current stock
  497. print '<td class="right">'.($currentstock ? $currentstock : '<span class="opacitymedium">0</span>').'</td>';
  498. }
  499. // Action
  500. print '<td class="right"></td>';
  501. // Fields from hook
  502. $parameters = array('objp'=>$objp);
  503. $reshook = $hookmanager->executeHooks('printFieldListValue', $parameters); // Note that $action and $object may have been modified by hook
  504. print $hookmanager->resPrint;
  505. print '</tr>'."\n";
  506. }
  507. $i++;
  508. }
  509. $parameters = array('sql'=>$sql);
  510. $reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters); // Note that $action and $object may have been modified by hook
  511. print $hookmanager->resPrint;
  512. if (empty($date) || !$dateIsValid) {
  513. $colspan = 8;
  514. if ($mode == 'future') {
  515. $colspan++;
  516. }
  517. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("EnterADateCriteria").'</span></td></tr>';
  518. }
  519. print '</table>';
  520. print '</div>';
  521. if (!empty($resql)) {
  522. $db->free($resql);
  523. }
  524. print dol_get_fiche_end();
  525. print '</form>';
  526. llxFooter();
  527. $db->close();