massstockmove.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. <?php
  2. /* Copyright (C) 2013-2021 Laurent Destaileur <ely@users.sourceforge.net>
  3. * Copyright (C) 2014 Regis Houssin <regis.houssin@inodbox.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/product/stock/massstockmove.php
  20. * \ingroup stock
  21. * \brief This page allows to select several products, then incoming warehouse and
  22. * outgoing warehouse and create all stock movements for this.
  23. */
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/modules/import/import_csv.modules.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/import.lib.php';
  34. $confirm = GETPOST('confirm', 'alpha');
  35. $filetoimport = GETPOST('filetoimport');
  36. // Load translation files required by the page
  37. $langs->loadLangs(array('products', 'stocks', 'orders', 'productbatch'));
  38. //init Hook
  39. $hookmanager->initHooks(array('massstockmove'));
  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. $id_product = GETPOST('productid', 'int');
  48. $id_sw = GETPOST('id_sw', 'int');
  49. $id_tw = GETPOST('id_tw', 'int');
  50. $batch = GETPOST('batch');
  51. $qty = GETPOST('qty');
  52. $idline = GETPOST('idline');
  53. $sortfield = GETPOST('sortfield', 'aZ09comma');
  54. $sortorder = GETPOST('sortorder', 'aZ09comma');
  55. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  56. if (empty($page) || $page == -1) {
  57. $page = 0;
  58. } // If $page is not defined, or '' or -1
  59. if (!$sortfield) {
  60. $sortfield = 'p.ref';
  61. }
  62. if (!$sortorder) {
  63. $sortorder = 'ASC';
  64. }
  65. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  66. $offset = $limit * $page;
  67. $listofdata = array();
  68. if (!empty($_SESSION['massstockmove'])) {
  69. $listofdata = json_decode($_SESSION['massstockmove'], true);
  70. }
  71. /*
  72. * Actions
  73. */
  74. if ($action == 'addline' && !empty($user->rights->stock->mouvement->creer)) {
  75. if (!($id_product > 0)) {
  76. $error++;
  77. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product")), null, 'errors');
  78. }
  79. if (!($id_sw > 0)) {
  80. $error++;
  81. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WarehouseSource")), null, 'errors');
  82. }
  83. if (!($id_tw > 0)) {
  84. $error++;
  85. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WarehouseTarget")), null, 'errors');
  86. }
  87. if ($id_sw > 0 && $id_tw == $id_sw) {
  88. $error++;
  89. $langs->load("errors");
  90. setEventMessages($langs->trans("ErrorWarehouseMustDiffers"), null, 'errors');
  91. }
  92. if (!$qty) {
  93. $error++;
  94. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Qty")), null, 'errors');
  95. }
  96. // Check a batch number is provided if product need it
  97. if (!$error) {
  98. $producttmp = new Product($db);
  99. $producttmp->fetch($id_product);
  100. if ($producttmp->hasbatch()) {
  101. if (empty($batch)) {
  102. $error++;
  103. $langs->load("errors");
  104. setEventMessages($langs->trans("ErrorTryToMakeMoveOnProductRequiringBatchData", $producttmp->ref), null, 'errors');
  105. }
  106. }
  107. }
  108. // TODO Check qty is ok for stock move. Note qty may not be enough yet, but we make a check now to report a warning.
  109. // What is more important is to have qty when doing action 'createmovements'
  110. if (!$error) {
  111. // Warning, don't forget lines already added into the $_SESSION['massstockmove']
  112. if ($producttmp->hasbatch()) {
  113. } else {
  114. }
  115. }
  116. if (!$error) {
  117. if (count(array_keys($listofdata)) > 0) {
  118. $id = max(array_keys($listofdata)) + 1;
  119. } else {
  120. $id = 1;
  121. }
  122. $listofdata[$id] = array('id'=>$id, 'id_product'=>$id_product, 'qty'=>$qty, 'id_sw'=>$id_sw, 'id_tw'=>$id_tw, 'batch'=>$batch);
  123. $_SESSION['massstockmove'] = json_encode($listofdata);
  124. //unset($id_sw);
  125. //unset($id_tw);
  126. unset($id_product);
  127. unset($batch);
  128. unset($qty);
  129. }
  130. }
  131. if ($action == 'delline' && $idline != '' && !empty($user->rights->stock->mouvement->creer)) {
  132. if (!empty($listofdata[$idline])) {
  133. unset($listofdata[$idline]);
  134. }
  135. if (count($listofdata) > 0) {
  136. $_SESSION['massstockmove'] = json_encode($listofdata);
  137. } else {
  138. unset($_SESSION['massstockmove']);
  139. }
  140. }
  141. if ($action == 'createmovements' && !empty($user->rights->stock->mouvement->creer)) {
  142. $error = 0;
  143. if (!GETPOST("label")) {
  144. $error++;
  145. setEventMessages($langs->trans("ErrorFieldRequired"), $langs->transnoentitiesnoconv("MovementLabel"), null, 'errors');
  146. }
  147. $db->begin();
  148. if (!$error) {
  149. $product = new Product($db);
  150. foreach ($listofdata as $key => $val) { // Loop on each movement to do
  151. $id = $val['id'];
  152. $id_product = $val['id_product'];
  153. $id_sw = $val['id_sw'];
  154. $id_tw = $val['id_tw'];
  155. $qty = price2num($val['qty']);
  156. $batch = $val['batch'];
  157. $dlc = -1; // They are loaded later from serial
  158. $dluo = -1; // They are loaded later from serial
  159. if (!$error && $id_sw <> $id_tw && is_numeric($qty) && $id_product) {
  160. $result = $product->fetch($id_product);
  161. $product->load_stock('novirtual'); // Load array product->stock_warehouse
  162. // Define value of products moved
  163. $pricesrc = 0;
  164. if (!empty($product->pmp)) {
  165. $pricesrc = $product->pmp;
  166. }
  167. $pricedest = $pricesrc;
  168. //print 'price src='.$pricesrc.', price dest='.$pricedest;exit;
  169. if (empty($conf->productbatch->enabled) || !$product->hasbatch()) { // If product does not need lot/serial
  170. // Remove stock
  171. $result1 = $product->correct_stock(
  172. $user,
  173. $id_sw,
  174. $qty,
  175. 1,
  176. GETPOST("label"),
  177. $pricesrc,
  178. GETPOST("codemove")
  179. );
  180. if ($result1 < 0) {
  181. $error++;
  182. setEventMessages($product->error, $product->errors, 'errors');
  183. }
  184. // Add stock
  185. $result2 = $product->correct_stock(
  186. $user,
  187. $id_tw,
  188. $qty,
  189. 0,
  190. GETPOST("label"),
  191. $pricedest,
  192. GETPOST("codemove")
  193. );
  194. if ($result2 < 0) {
  195. $error++;
  196. setEventMessages($product->error, $product->errors, 'errors');
  197. }
  198. } else {
  199. $arraybatchinfo = $product->loadBatchInfo($batch);
  200. if (count($arraybatchinfo) > 0) {
  201. $firstrecord = array_shift($arraybatchinfo);
  202. $dlc = $firstrecord['eatby'];
  203. $dluo = $firstrecord['sellby'];
  204. //var_dump($batch);
  205. //var_dump($arraybatchinfo);
  206. //var_dump($firstrecord);
  207. //var_dump($dlc);
  208. //var_dump($dluo); exit;
  209. } else {
  210. $dlc = '';
  211. $dluo = '';
  212. }
  213. // Remove stock
  214. $result1 = $product->correct_stock_batch(
  215. $user,
  216. $id_sw,
  217. $qty,
  218. 1,
  219. GETPOST("label"),
  220. $pricesrc,
  221. $dlc,
  222. $dluo,
  223. $batch,
  224. GETPOST("codemove")
  225. );
  226. if ($result1 < 0) {
  227. $error++;
  228. setEventMessages($product->error, $product->errors, 'errors');
  229. }
  230. // Add stock
  231. $result2 = $product->correct_stock_batch(
  232. $user,
  233. $id_tw,
  234. $qty,
  235. 0,
  236. GETPOST("label"),
  237. $pricedest,
  238. $dlc,
  239. $dluo,
  240. $batch,
  241. GETPOST("codemove")
  242. );
  243. if ($result2 < 0) {
  244. $error++;
  245. setEventMessages($product->error, $product->errors, 'errors');
  246. }
  247. }
  248. } else {
  249. // dol_print_error('',"Bad value saved into sessions");
  250. $error++;
  251. }
  252. }
  253. }
  254. if (!$error) {
  255. unset($_SESSION['massstockmove']);
  256. $db->commit();
  257. setEventMessages($langs->trans("StockMovementRecorded"), null, 'mesgs');
  258. header("Location: ".DOL_URL_ROOT.'/product/stock/index.php'); // Redirect to avoid pb when using back
  259. exit;
  260. } else {
  261. $db->rollback();
  262. setEventMessages($langs->trans("Error"), null, 'errors');
  263. }
  264. }
  265. if ($action == 'importCSV' && !empty($user->rights->stock->mouvement->creer)) {
  266. dol_mkdir($conf->stock->dir_temp);
  267. $nowyearmonth = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
  268. $fullpath = $conf->stock->dir_temp."/".$user->id.'-csvfiletotimport.csv';
  269. if (dol_move_uploaded_file($_FILES['userfile']['tmp_name'], $fullpath, 1) > 0) {
  270. dol_syslog("File ".$fullpath." was added for import");
  271. } else {
  272. $error++;
  273. $langs->load("errors");
  274. setEventMessages($langs->trans("ErrorFailedToSaveFile"), null, 'errors');
  275. }
  276. if (!$error) {
  277. $importcsv = new ImportCsv($db, 'massstocklist');
  278. //print $importcsv->separator;
  279. $nblinesrecord = $importcsv->import_get_nb_of_lines($fullpath)-1;
  280. $importcsv->import_open_file($fullpath);
  281. $labelsrecord = $importcsv->import_read_record();
  282. if ($nblinesrecord < 1) {
  283. setEventMessages($langs->trans("BadNumberOfLinesMustHaveAtLeastOneLinePlusTitle"), null, 'errors');
  284. } else {
  285. $i=0;
  286. $data = array();
  287. $productstatic = new Product($db);
  288. $warehousestatics = new Entrepot($db);
  289. $warehousestatict = new Entrepot($db);
  290. while (($i < $nblinesrecord) && !$error) {
  291. $data[] = $importcsv->import_read_record();
  292. if (count($data[$i]) == 1) {
  293. // Only 1 empty line
  294. unset($data[$i]);
  295. $i++;
  296. continue;
  297. }
  298. //var_dump($data);
  299. $tmp_id_sw = $data[$i][0]['val'];
  300. $tmp_id_tw = $data[$i][1]['val'];
  301. $tmp_id_product = $data[$i][2]['val'];
  302. $tmp_qty = $data[$i][3]['val'];
  303. $tmp_batch = $data[$i][4]['val'];
  304. if (!is_numeric($tmp_id_product)) {
  305. $result = fetchref($productstatic, $tmp_id_product);
  306. $tmp_id_product = $result;
  307. $data[$i][2]['val'] = $result;
  308. }
  309. if (!($tmp_id_product > 0)) {
  310. $error++;
  311. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product")), null, 'errors');
  312. }
  313. if (!is_numeric($tmp_id_sw)) {
  314. $result = fetchref($warehousestatics, $tmp_id_sw);
  315. $tmp_id_sw = $result;
  316. $data[$i][0]['val'] = $result;
  317. }
  318. if (!($tmp_id_sw > 0)) {
  319. $error++;
  320. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WarehouseSource")), null, 'errors');
  321. }
  322. if (!is_numeric($tmp_id_tw)) {
  323. $result = fetchref($warehousestatict, $tmp_id_tw);
  324. $tmp_id_tw = $result;
  325. $data[$i][1]['val'] = $result;
  326. }
  327. if (!($tmp_id_tw > 0)) {
  328. $error++;
  329. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WarehouseTarget")), null, 'errors');
  330. }
  331. if ($tmp_id_sw > 0 && $tmp_id_tw == $tmp_id_sw) {
  332. $error++;
  333. $langs->load("errors");
  334. setEventMessages($langs->trans("ErrorWarehouseMustDiffers"), null, 'errors');
  335. }
  336. if (!$tmp_qty) {
  337. $error++;
  338. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Qty")), null, 'errors');
  339. }
  340. // Check a batch number is provided if product need it
  341. if (!$error) {
  342. $producttmp = new Product($db);
  343. $producttmp->fetch($tmp_id_product);
  344. if ($producttmp->hasbatch()) {
  345. if (empty($tmp_batch)) {
  346. $error++;
  347. $langs->load("errors");
  348. setEventMessages($langs->trans("ErrorTryToMakeMoveOnProductRequiringBatchData", $producttmp->ref), null, 'errors');
  349. }
  350. }
  351. }
  352. $i++;
  353. }
  354. if (!$error) {
  355. foreach ($data as $key => $value) {
  356. if (count(array_keys($listofdata)) > 0) {
  357. $id = max(array_keys($listofdata)) + 1;
  358. } else {
  359. $id = 1;
  360. }
  361. $tmp_id_sw = $data[$key][0]['val'];
  362. $tmp_id_tw = $data[$key][1]['val'];
  363. $tmp_id_product = $data[$key][2]['val'];
  364. $tmp_qty = $data[$key][3]['val'];
  365. $tmp_batch = $data[$key][4]['val'];
  366. $listofdata[$key] = array('id'=>$key, 'id_sw'=>$tmp_id_sw, 'id_tw'=>$tmp_id_tw, 'id_product'=>$tmp_id_product, 'qty'=>$tmp_qty, 'batch'=>$tmp_batch);
  367. }
  368. }
  369. }
  370. }
  371. $_SESSION['massstockmove'] = json_encode($listofdata);
  372. }
  373. if ($action == 'confirm_deletefile' && $confirm == 'yes') {
  374. $langs->load("other");
  375. $param = '&datatoimport='.urlencode($datatoimport).'&format='.urlencode($format);
  376. if ($excludefirstline) {
  377. $param .= '&excludefirstline='.urlencode($excludefirstline);
  378. }
  379. if ($endatlinenb) {
  380. $param .= '&endatlinenb='.urlencode($endatlinenb);
  381. }
  382. $file = $conf->stock->dir_temp.'/'.GETPOST('urlfile'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  383. $ret = dol_delete_file($file);
  384. if ($ret) {
  385. setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
  386. } else {
  387. setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
  388. }
  389. Header('Location: '.$_SERVER["PHP_SELF"]);
  390. exit;
  391. }
  392. /*
  393. * View
  394. */
  395. $now = dol_now();
  396. $error = 0;
  397. $form = new Form($db);
  398. $formproduct = new FormProduct($db);
  399. $productstatic = new Product($db);
  400. $warehousestatics = new Entrepot($db);
  401. $warehousestatict = new Entrepot($db);
  402. $help_url = 'EN:Module_Stocks_En|FR:Module_Stock|ES:Módulo_Stocks|DE:Modul_Bestände';
  403. $title = $langs->trans('MassMovement');
  404. llxHeader('', $title, $help_url);
  405. print load_fiche_titre($langs->trans("MassStockTransferShort"), '', 'stock');
  406. $titletoadd = $langs->trans("Select");
  407. $buttonrecord = $langs->trans("RecordMovement");
  408. $titletoaddnoent = $langs->transnoentitiesnoconv("Select");
  409. $buttonrecordnoent = $langs->transnoentitiesnoconv("RecordMovement");
  410. print '<span class="opacitymedium">'.$langs->trans("SelectProductInAndOutWareHouse", $titletoaddnoent, $buttonrecordnoent).'</span><br>';
  411. print '<br>';
  412. // Form to upload a file
  413. print '<form name="userfile" action="'.$_SERVER["PHP_SELF"].'" enctype="multipart/form-data" METHOD="POST">';
  414. print '<input type="hidden" name="token" value="'.newToken().'">';
  415. print '<input type="hidden" name="action" value="importCSV">';
  416. print '<input type="hidden" name="max_file_size" value="'.$conf->maxfilesize.'">';
  417. print '<span class="opacitymedium">';
  418. print $langs->trans("or").' ';
  419. $importcsv = new ImportCsv($db, 'massstocklist');
  420. print $form->textwithpicto($langs->trans('SelectAStockMovementFileToImport'), $langs->transnoentitiesnoconv("InfoTemplateImport", $importcsv->separator));
  421. print '</span>';
  422. print '<input type="file" name="userfile" size="20" maxlength="80"> &nbsp; &nbsp; ';
  423. $out = (empty($conf->global->MAIN_UPLOAD_DOC) ? ' disabled' : '');
  424. print '<input type="submit" class="button" value="'.$langs->trans("ImportFromCSV").'"'.$out.' name="sendit">';
  425. $out = '';
  426. if (!empty($conf->global->MAIN_UPLOAD_DOC)) {
  427. $max = $conf->global->MAIN_UPLOAD_DOC; // In Kb
  428. $maxphp = @ini_get('upload_max_filesize'); // In unknown
  429. if (preg_match('/k$/i', $maxphp)) {
  430. $maxphp = preg_replace('/k$/i', '', $maxphp);
  431. $maxphp = $maxphp * 1;
  432. }
  433. if (preg_match('/m$/i', $maxphp)) {
  434. $maxphp = preg_replace('/m$/i', '', $maxphp);
  435. $maxphp = $maxphp * 1024;
  436. }
  437. if (preg_match('/g$/i', $maxphp)) {
  438. $maxphp = preg_replace('/g$/i', '', $maxphp);
  439. $maxphp = $maxphp * 1024 * 1024;
  440. }
  441. if (preg_match('/t$/i', $maxphp)) {
  442. $maxphp = preg_replace('/t$/i', '', $maxphp);
  443. $maxphp = $maxphp * 1024 * 1024 * 1024;
  444. }
  445. $maxphp2 = @ini_get('post_max_size'); // In unknown
  446. if (preg_match('/k$/i', $maxphp2)) {
  447. $maxphp2 = preg_replace('/k$/i', '', $maxphp2);
  448. $maxphp2 = $maxphp2 * 1;
  449. }
  450. if (preg_match('/m$/i', $maxphp2)) {
  451. $maxphp2 = preg_replace('/m$/i', '', $maxphp2);
  452. $maxphp2 = $maxphp2 * 1024;
  453. }
  454. if (preg_match('/g$/i', $maxphp2)) {
  455. $maxphp2 = preg_replace('/g$/i', '', $maxphp2);
  456. $maxphp2 = $maxphp2 * 1024 * 1024;
  457. }
  458. if (preg_match('/t$/i', $maxphp2)) {
  459. $maxphp2 = preg_replace('/t$/i', '', $maxphp2);
  460. $maxphp2 = $maxphp2 * 1024 * 1024 * 1024;
  461. }
  462. // Now $max and $maxphp and $maxphp2 are in Kb
  463. $maxmin = $max;
  464. $maxphptoshow = $maxphptoshowparam = '';
  465. if ($maxphp > 0) {
  466. $maxmin = min($max, $maxphp);
  467. $maxphptoshow = $maxphp;
  468. $maxphptoshowparam = 'upload_max_filesize';
  469. }
  470. if ($maxphp2 > 0) {
  471. $maxmin = min($max, $maxphp2);
  472. if ($maxphp2 < $maxphp) {
  473. $maxphptoshow = $maxphp2;
  474. $maxphptoshowparam = 'post_max_size';
  475. }
  476. }
  477. $langs->load('other');
  478. $out .= ' ';
  479. $out .= info_admin($langs->trans("ThisLimitIsDefinedInSetup", $max, $maxphptoshow), 1);
  480. } else {
  481. $out .= ' ('.$langs->trans("UploadDisabled").')';
  482. }
  483. print $out;
  484. print '</form>';
  485. print '<br><br>';
  486. // Form to add a line
  487. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" name="formulaire">';
  488. print '<input type="hidden" name="token" value="'.newToken().'">';
  489. print '<input type="hidden" name="action" value="addline">';
  490. print '<div class="div-table-responsive-no-min">';
  491. print '<table class="liste centpercent">';
  492. $param = '';
  493. print '<tr class="liste_titre">';
  494. print getTitleFieldOfList($langs->trans('WarehouseSource'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'tagtd maxwidthonsmartphone ');
  495. print getTitleFieldOfList($langs->trans('WarehouseTarget'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'tagtd maxwidthonsmartphone ');
  496. print getTitleFieldOfList($langs->trans('ProductRef'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'tagtd maxwidthonsmartphone ');
  497. if ($conf->productbatch->enabled) {
  498. print getTitleFieldOfList($langs->trans('Batch'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'tagtd maxwidthonsmartphone ');
  499. }
  500. print getTitleFieldOfList($langs->trans('Qty'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'center tagtd maxwidthonsmartphone ');
  501. print getTitleFieldOfList('', 0);
  502. print '</tr>';
  503. print '<tr class="oddeven">';
  504. // From warehouse
  505. print '<td>';
  506. print img_picto($langs->trans("WarehouseSource"), 'stock', 'class="paddingright"').$formproduct->selectWarehouses($id_sw, 'id_sw', 'warehouseopen,warehouseinternal', 1, 0, 0, '', 0, 0, array(), 'minwidth200imp maxwidth200');
  507. print '</td>';
  508. // To warehouse
  509. print '<td>';
  510. print img_picto($langs->trans("WarehouseTarget"), 'stock', 'class="paddingright"').$formproduct->selectWarehouses($id_tw, 'id_tw', 'warehouseopen,warehouseinternal', 1, 0, 0, '', 0, 0, array(), 'minwidth200imp maxwidth200');
  511. print '</td>';
  512. // Product
  513. print '<td>';
  514. $filtertype = 0;
  515. if (!empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
  516. $filtertype = '';
  517. }
  518. if ($conf->global->PRODUIT_LIMIT_SIZE <= 0) {
  519. $limit = '';
  520. } else {
  521. $limit = $conf->global->PRODUIT_LIMIT_SIZE;
  522. }
  523. print img_picto($langs->trans("Product"), 'product', 'class="paddingright"');
  524. print $form->select_produits($id_product, 'productid', $filtertype, $limit, 0, -1, 2, '', 1, array(), 0, '1', 0, 'minwidth200imp maxwidth300', 1, '', null, 1);
  525. print '</td>';
  526. // Batch number
  527. if ($conf->productbatch->enabled) {
  528. print '<td>';
  529. print img_picto($langs->trans("LotSerial"), 'lot', 'class="paddingright"');
  530. print '<input type="text" name="batch" class="flat maxwidth50" value="'.$batch.'">';
  531. print '</td>';
  532. }
  533. // Qty
  534. print '<td class="center"><input type="text" class="flat maxwidth50" name="qty" value="'.$qty.'"></td>';
  535. // Button to add line
  536. print '<td class="right"><input type="submit" class="button" name="addline" value="'.dol_escape_htmltag($titletoadd).'"></td>';
  537. print '</tr>';
  538. foreach ($listofdata as $key => $val) {
  539. $productstatic->fetch($val['id_product']);
  540. $warehousestatics->fetch($val['id_sw']);
  541. $warehousestatict->fetch($val['id_tw']);
  542. if ($productstatic->id <= 0) {
  543. $error++;
  544. setEventMessages($langs->trans("ObjectNotFound", $langs->transnoentitiesnoconv("Product")), null, 'errors');
  545. }
  546. if ($warehousestatics->id <= 0) {
  547. $error++;
  548. setEventMessages($langs->trans("ObjectNotFound", $langs->transnoentitiesnoconv("WarehouseSource")), null, 'errors');
  549. }
  550. if ($warehousestatics->id <= 0) {
  551. $error++;
  552. setEventMessages($langs->trans("ObjectNotFound", $langs->transnoentitiesnoconv("WarehouseTarget")), null, 'errors');
  553. }
  554. if (!$error) {
  555. print '<tr class="oddeven">';
  556. print '<td>';
  557. print $warehousestatics->getNomUrl(1);
  558. print '</td>';
  559. print '<td>';
  560. print $warehousestatict->getNomUrl(1);
  561. print '</td>';
  562. print '<td>';
  563. print $productstatic->getNomUrl(1).' - '.$productstatic->label;
  564. print '</td>';
  565. if ($conf->productbatch->enabled) {
  566. print '<td>';
  567. print $val['batch'];
  568. print '</td>';
  569. }
  570. print '<td class="center">'.$val['qty'].'</td>';
  571. print '<td class="right"><a href="'.$_SERVER["PHP_SELF"].'?action=delline&token='.newToken().'&idline='.$val['id'].'">'.img_delete($langs->trans("Remove")).'</a></td>';
  572. print '</tr>';
  573. }
  574. }
  575. print '</table>';
  576. print '</div>';
  577. print '</form>';
  578. print '<br>';
  579. // Form to validate all movements
  580. if (count($listofdata)) {
  581. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" name="formulaire2" class="formconsumeproduce">';
  582. print '<input type="hidden" name="token" value="'.newToken().'">';
  583. print '<input type="hidden" name="action" value="createmovements">';
  584. // Button to record mass movement
  585. $codemove = (GETPOSTISSET("codemove") ? GETPOST("codemove", 'alpha') : dol_print_date(dol_now(), '%Y%m%d%H%M%S'));
  586. $labelmovement = GETPOST("label") ? GETPOST('label') : $langs->trans("StockTransfer").' '.dol_print_date($now, '%Y-%m-%d %H:%M');
  587. print '<div class="center">';
  588. print '<span class="fieldrequired">'.$langs->trans("InventoryCode").':</span> ';
  589. print '<input type="text" name="codemove" class="maxwidth300" value="'.dol_escape_htmltag($codemove).'"> &nbsp; ';
  590. print '<span class="clearbothonsmartphone"></span>';
  591. print $langs->trans("MovementLabel").': ';
  592. print '<input type="text" name="label" class="minwidth300" value="'.dol_escape_htmltag($labelmovement).'"><br>';
  593. print '<br>';
  594. print '<div class="center"><input type="submit" class="button" name="valid" value="'.dol_escape_htmltag($buttonrecord).'"></div>';
  595. print '<br>';
  596. print '</div>';
  597. print '</form>';
  598. }
  599. if ($action == 'delete') {
  600. print $form->formconfirm($_SERVER["PHP_SELF"].'?urlfile='.urlencode(GETPOST('urlfile')).'&step=3'.$param, $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', 0, 1);
  601. }
  602. // End of page
  603. llxFooter();
  604. $db->close();
  605. /**
  606. * Verify if $haystack startswith $needle
  607. *
  608. * @param string $haystack string to test
  609. * @param string $needle string to find
  610. * @return bool false if Ko true else
  611. */
  612. function startsWith($haystack, $needle)
  613. {
  614. $length = strlen($needle);
  615. return substr($haystack, 0, $length) === $needle;
  616. }
  617. /**
  618. * Fetch object with ref
  619. *
  620. * @param Object $static_object static object to fetch
  621. * @param string $tmp_ref ref of the object to fetch
  622. * @return int <0 if Ko or Id of object
  623. */
  624. function fetchref($static_object, $tmp_ref)
  625. {
  626. if (startsWith($tmp_ref, 'ref:')) {
  627. $tmp_ref = str_replace('ref:', '', $tmp_ref);
  628. }
  629. $static_object->fetch('', $tmp_ref);
  630. return $static_object->id;
  631. }