stockatdate.php 25 KB

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