inventory.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. <?php
  2. /* Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/product/inventory/inventory.php
  19. * \ingroup inventory
  20. * \brief Tabe to enter counting
  21. */
  22. // Load Dolibarr environment
  23. require '../../main.inc.php';
  24. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  25. include_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php';
  28. include_once DOL_DOCUMENT_ROOT.'/product/inventory/lib/inventory.lib.php';
  29. include_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  30. include_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array("stocks", "other", "productbatch"));
  33. // Get parameters
  34. $id = GETPOST('id', 'int');
  35. $ref = GETPOST('ref', 'alpha');
  36. $action = GETPOST('action', 'aZ09');
  37. $confirm = GETPOST('confirm', 'alpha');
  38. $cancel = GETPOST('cancel', 'aZ09');
  39. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'inventorycard'; // To manage different context of search
  40. $backtopage = GETPOST('backtopage', 'alpha');
  41. $listoffset = GETPOST('listoffset', 'alpha');
  42. $limit = GETPOST('limit', 'int') > 0 ?GETPOST('limit', 'int') : $conf->liste_limit;
  43. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  44. if (empty($page) || $page == -1) {
  45. $page = 0;
  46. }
  47. $offset = $limit * $page;
  48. $pageprev = $page - 1;
  49. $pagenext = $page + 1;
  50. $fk_warehouse = GETPOST('fk_warehouse', 'int');
  51. $fk_product = GETPOST('fk_product', 'int');
  52. $lineid = GETPOST('lineid', 'int');
  53. $batch = GETPOST('batch', 'alphanohtml');
  54. $totalExpectedValuation = 0;
  55. $totalRealValuation = 0;
  56. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  57. $result = restrictedArea($user, 'stock', $id);
  58. } else {
  59. $result = restrictedArea($user, 'stock', $id, '', 'inventory_advance');
  60. }
  61. // Initialize technical objects
  62. $object = new Inventory($db);
  63. $extrafields = new ExtraFields($db);
  64. $diroutputmassaction = $conf->stock->dir_output.'/temp/massgeneration/'.$user->id;
  65. $hookmanager->initHooks(array('inventorycard')); // Note that conf->hooks_modules contains array
  66. // Fetch optionals attributes and labels
  67. $extrafields->fetch_name_optionals_label($object->table_element);
  68. $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
  69. // Initialize array of search criterias
  70. $search_all = GETPOST("search_all", 'alpha');
  71. $search = array();
  72. foreach ($object->fields as $key => $val) {
  73. if (GETPOST('search_'.$key, 'alpha')) {
  74. $search[$key] = GETPOST('search_'.$key, 'alpha');
  75. }
  76. }
  77. if (empty($action) && empty($id) && empty($ref)) {
  78. $action = 'view';
  79. }
  80. // Load object
  81. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  82. // Security check - Protection if external user
  83. //if ($user->socid > 0) accessforbidden();
  84. //if ($user->socid > 0) $socid = $user->socid;
  85. //$result = restrictedArea($user, 'mymodule', $id);
  86. //Parameters Page
  87. $param = '&id='.$object->id;
  88. if ($limit > 0 && $limit != $conf->liste_limit) {
  89. $param .= '&limit='.((int) $limit);
  90. }
  91. $paramwithsearch = $param;
  92. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  93. $permissiontoadd = $user->rights->stock->creer;
  94. $permissiontodelete = $user->rights->stock->supprimer;
  95. } else {
  96. $permissiontoadd = $user->rights->stock->inventory_advance->write;
  97. $permissiontodelete = $user->rights->stock->inventory_advance->write;
  98. }
  99. $now = dol_now();
  100. /*
  101. * Actions
  102. */
  103. if ($cancel) {
  104. $action = '';
  105. }
  106. $parameters = array();
  107. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  108. if ($reshook < 0) {
  109. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  110. }
  111. if (empty($reshook)) {
  112. $error = 0;
  113. if ($action == 'cancel_record' && $permissiontoadd) {
  114. $object->setCanceled($user);
  115. }
  116. // Close inventory by recording the stock movements
  117. if ($action == 'update' && !empty($user->rights->stock->mouvement->creer) && $object->status == $object::STATUS_VALIDATED) {
  118. $stockmovment = new MouvementStock($db);
  119. $stockmovment->setOrigin($object->element, $object->id);
  120. $cacheOfProducts = array();
  121. $db->begin();
  122. $sql = 'SELECT id.rowid, id.datec as date_creation, id.tms as date_modification, id.fk_inventory, id.fk_warehouse,';
  123. $sql .= ' id.fk_product, id.batch, id.qty_stock, id.qty_view, id.qty_regulated, id.pmp_real';
  124. $sql .= ' FROM '.MAIN_DB_PREFIX.'inventorydet as id';
  125. $sql .= ' WHERE id.fk_inventory = '.((int) $object->id);
  126. $resql = $db->query($sql);
  127. if ($resql) {
  128. $num = $db->num_rows($resql);
  129. $i = 0;
  130. $totalarray = array();
  131. while ($i < $num) {
  132. $line = $db->fetch_object($resql);
  133. $qty_stock = $line->qty_stock;
  134. $qty_view = $line->qty_view; // The quantity viewed by inventorier, the qty we target
  135. // Load real stock we have now.
  136. if (isset($cacheOfProducts[$line->fk_product])) {
  137. $product_static = $cacheOfProducts[$line->fk_product];
  138. } else {
  139. $product_static = new Product($db);
  140. $result = $product_static->fetch($line->fk_product, '', '', '', 1, 1, 1);
  141. //$option = 'nobatch';
  142. $option .= ',novirtual';
  143. $product_static->load_stock($option); // Load stock_reel + stock_warehouse.
  144. $cacheOfProducts[$product_static->id] = $product_static;
  145. }
  146. // Get the real quantity in stock now, but before the stock move for inventory.
  147. $realqtynow = $product_static->stock_warehouse[$line->fk_warehouse]->real;
  148. if (isModEnabled('productbatch') && $product_static->hasbatch()) {
  149. $realqtynow = $product_static->stock_warehouse[$line->fk_warehouse]->detail_batch[$line->batch]->qty;
  150. }
  151. if (!is_null($qty_view)) {
  152. $stock_movement_qty = price2num($qty_view - $realqtynow, 'MS');
  153. if ($stock_movement_qty != 0) {
  154. if ($stock_movement_qty < 0) {
  155. $movement_type = 1;
  156. } else {
  157. $movement_type = 0;
  158. }
  159. $datemovement = '';
  160. //$inventorycode = 'INV'.$object->id;
  161. $inventorycode = 'INV-'.$object->ref;
  162. $price = 0;
  163. if (!empty($line->pmp_real) && !empty($conf->global->INVENTORY_MANAGE_REAL_PMP)) $price = $line->pmp_real;
  164. $idstockmove = $stockmovment->_create($user, $line->fk_product, $line->fk_warehouse, $stock_movement_qty, $movement_type, $price, $langs->trans('LabelOfInventoryMovemement', $object->ref), $inventorycode, $datemovement, '', '', $line->batch);
  165. if ($idstockmove < 0) {
  166. $error++;
  167. setEventMessages($stockmovment->error, $stockmovment->errors, 'errors');
  168. break;
  169. }
  170. // Update line with id of stock movement (and the start quantity if it has changed this last recording)
  171. $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."inventorydet";
  172. $sqlupdate .= " SET fk_movement = ".((int) $idstockmove);
  173. if ($qty_stock != $realqtynow) {
  174. $sqlupdate .= ", qty_stock = ".((float) $realqtynow);
  175. }
  176. $sqlupdate .= " WHERE rowid = ".((int) $line->rowid);
  177. $resqlupdate = $db->query($sqlupdate);
  178. if (! $resqlupdate) {
  179. $error++;
  180. setEventMessages($db->lasterror(), null, 'errors');
  181. break;
  182. }
  183. }
  184. if (!empty($line->pmp_real) && !empty($conf->global->INVENTORY_MANAGE_REAL_PMP)) {
  185. $sqlpmp = 'UPDATE '.MAIN_DB_PREFIX.'product SET pmp = '.((float) $line->pmp_real).' WHERE rowid = '.((int) $line->fk_product);
  186. $resqlpmp = $db->query($sqlpmp);
  187. if (! $resqlpmp) {
  188. $error++;
  189. setEventMessages($db->lasterror(), null, 'errors');
  190. break;
  191. }
  192. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  193. $sqlpmp = 'UPDATE '.MAIN_DB_PREFIX.'product_perentity SET pmp = '.((float) $line->pmp_real).' WHERE fk_product = '.((int) $line->fk_product).' AND entity='.$conf->entity;
  194. $resqlpmp = $db->query($sqlpmp);
  195. if (! $resqlpmp) {
  196. $error++;
  197. setEventMessages($db->lasterror(), null, 'errors');
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. $i++;
  204. }
  205. if (!$error) {
  206. $object->setRecorded($user);
  207. }
  208. } else {
  209. setEventMessages($db->lasterror, null, 'errors');
  210. $error++;
  211. }
  212. if (! $error) {
  213. $db->commit();
  214. } else {
  215. $db->rollback();
  216. }
  217. }
  218. // Save quantity found during inventory (when we click on Save button on inventory page)
  219. if ($action =='updateinventorylines' && $permissiontoadd) {
  220. $sql = 'SELECT id.rowid, id.datec as date_creation, id.tms as date_modification, id.fk_inventory, id.fk_warehouse,';
  221. $sql .= ' id.fk_product, id.batch, id.qty_stock, id.qty_view, id.qty_regulated';
  222. $sql .= ' FROM '.MAIN_DB_PREFIX.'inventorydet as id';
  223. $sql .= ' WHERE id.fk_inventory = '.((int) $object->id);
  224. $sql .= $db->plimit($limit, $offset);
  225. $db->begin();
  226. $resql = $db->query($sql);
  227. if ($resql) {
  228. $num = $db->num_rows($resql);
  229. $i = 0;
  230. $totalarray = array();
  231. $inventoryline = new InventoryLine($db);
  232. while ($i < $num) {
  233. $line = $db->fetch_object($resql);
  234. $lineid = $line->rowid;
  235. $result = 0;
  236. $resultupdate = 0;
  237. if (GETPOST("id_".$lineid, 'alpha') != '') { // If a value was set ('0' or something else)
  238. $qtytoupdate = price2num(GETPOST("id_".$lineid, 'alpha'), 'MS');
  239. $result = $inventoryline->fetch($lineid);
  240. if ($qtytoupdate < 0) {
  241. $result = -1;
  242. setEventMessages($langs->trans("FieldCannotBeNegative", $langs->transnoentitiesnoconv("RealQty")), null, 'errors');
  243. }
  244. if ($result > 0) {
  245. $inventoryline->qty_stock = price2num(GETPOST('stock_qty_'.$lineid, 'alpha'), 'MS'); // The new value that was set in as hidden field
  246. $inventoryline->qty_view = $qtytoupdate; // The new value we want
  247. $inventoryline->pmp_real = price2num(GETPOST('realpmp_'.$lineid, 'alpha'), 'MS');
  248. $inventoryline->pmp_expected = price2num(GETPOST('expectedpmp_'.$lineid, 'alpha'), 'MS');
  249. $resultupdate = $inventoryline->update($user);
  250. }
  251. } elseif (GETPOSTISSET('id_' . $lineid)) {
  252. // Delete record
  253. $result = $inventoryline->fetch($lineid);
  254. if ($result > 0) {
  255. $inventoryline->qty_view = null; // The new value we want
  256. $inventoryline->pmp_real = price2num(GETPOST('realpmp_'.$lineid, 'alpha'), 'MS');
  257. $inventoryline->pmp_expected = price2num(GETPOST('expectedpmp_'.$lineid, 'alpha'), 'MS');
  258. $resultupdate = $inventoryline->update($user);
  259. }
  260. }
  261. if ($result < 0 || $resultupdate < 0) {
  262. $error++;
  263. }
  264. $i++;
  265. }
  266. }
  267. // Update line with id of stock movement (and the start quantity if it has changed this last recording)
  268. if (! $error) {
  269. $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."inventory";
  270. $sqlupdate .= " SET fk_user_modif = ".((int) $user->id);
  271. $sqlupdate .= " WHERE rowid = ".((int) $object->id);
  272. $resqlupdate = $db->query($sqlupdate);
  273. if (! $resqlupdate) {
  274. $error++;
  275. setEventMessages($db->lasterror(), null, 'errors');
  276. }
  277. }
  278. if (!$error) {
  279. $db->commit();
  280. } else {
  281. $db->rollback();
  282. }
  283. }
  284. $backurlforlist = DOL_URL_ROOT.'/product/inventory/list.php';
  285. $backtopage = DOL_URL_ROOT.'/product/inventory/inventory.php?id='.$object->id.'&page='.$page.$paramwithsearch;
  286. // Actions cancel, add, update, delete or clone
  287. include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
  288. // Actions when linking object each other
  289. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php';
  290. // Actions when printing a doc from card
  291. include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
  292. // Actions to send emails
  293. /*$triggersendname = 'MYOBJECT_SENTBYMAIL';
  294. $autocopy='MAIN_MAIL_AUTOCOPY_MYOBJECT_TO';
  295. $trackid='stockinv'.$object->id;
  296. include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';*/
  297. if (GETPOST('addline', 'alpha')) {
  298. $qty= (GETPOST('qtytoadd') != '' ? price2num(GETPOST('qtytoadd', 'MS')) : null);
  299. if ($fk_warehouse <= 0) {
  300. $error++;
  301. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
  302. }
  303. if ($fk_product <= 0) {
  304. $error++;
  305. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product")), null, 'errors');
  306. }
  307. if (price2num(GETPOST('qtytoadd'), 'MS') < 0) {
  308. $error++;
  309. setEventMessages($langs->trans("FieldCannotBeNegative", $langs->transnoentitiesnoconv("RealQty")), null, 'errors');
  310. }
  311. if (!$error && isModEnabled('productbatch')) {
  312. $tmpproduct = new Product($db);
  313. $result = $tmpproduct->fetch($fk_product);
  314. if (empty($error) && $tmpproduct->status_batch>0 && empty($batch)) {
  315. $error++;
  316. $langs->load("errors");
  317. setEventMessages($langs->trans("ErrorProductNeedBatchNumber", $tmpproduct->ref), null, 'errors');
  318. }
  319. if (empty($error) && $tmpproduct->status_batch==2 && !empty($batch) && $qty>1) {
  320. $error++;
  321. $langs->load("errors");
  322. setEventMessages($langs->trans("TooManyQtyForSerialNumber", $tmpproduct->ref, $batch), null, 'errors');
  323. }
  324. if (empty($error) && empty($tmpproduct->status_batch) && !empty($batch)) {
  325. $error++;
  326. $langs->load("errors");
  327. setEventMessages($langs->trans("ErrorProductDoesNotNeedBatchNumber", $tmpproduct->ref), null, 'errors');
  328. }
  329. }
  330. if (!$error) {
  331. $tmp = new InventoryLine($db);
  332. $tmp->fk_inventory = $object->id;
  333. $tmp->fk_warehouse = $fk_warehouse;
  334. $tmp->fk_product = $fk_product;
  335. $tmp->batch = $batch;
  336. $tmp->datec = $now;
  337. $tmp->qty_view = $qty;
  338. $result = $tmp->create($user);
  339. if ($result < 0) {
  340. if ($db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  341. $langs->load("errors");
  342. setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
  343. } else {
  344. dol_print_error($db, $tmp->error, $tmp->errors);
  345. }
  346. } else {
  347. // Clear var
  348. $_POST['batch'] = '';
  349. $_POST['qtytoadd'] = '';
  350. }
  351. }
  352. }
  353. }
  354. /*
  355. * View
  356. */
  357. $form = new Form($db);
  358. $formproduct = new FormProduct($db);
  359. $help_url = '';
  360. llxHeader('', $langs->trans('Inventory'), $help_url);
  361. // Part to show record
  362. if ($object->id <= 0) {
  363. dol_print_error('', 'Bad value for object id');
  364. exit;
  365. }
  366. $res = $object->fetch_optionals();
  367. $head = inventoryPrepareHead($object);
  368. print dol_get_fiche_head($head, 'inventory', $langs->trans("Inventory"), -1, 'stock');
  369. $formconfirm = '';
  370. // Confirmation to delete
  371. if ($action == 'delete') {
  372. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteInventory'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete', '', 0, 1);
  373. }
  374. // Confirmation to delete line
  375. if ($action == 'deleteline') {
  376. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid.'&page='.$page.$paramwithsearch, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_deleteline', '', 0, 1);
  377. }
  378. // Clone confirmation
  379. if ($action == 'clone') {
  380. // Create an array for form
  381. $formquestion = array();
  382. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneMyObject', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  383. }
  384. // Confirmation to close
  385. if ($action == 'record') {
  386. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('Close'), $langs->trans('ConfirmFinish'), 'update', '', 0, 1);
  387. $action = 'view';
  388. }
  389. // Confirmation to close
  390. if ($action == 'confirm_cancel') {
  391. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('Cancel'), $langs->trans('ConfirmCancel'), 'cancel_record', '', 0, 1);
  392. $action = 'view';
  393. }
  394. // Call Hook formConfirm
  395. $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
  396. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  397. if (empty($reshook)) {
  398. $formconfirm .= $hookmanager->resPrint;
  399. } elseif ($reshook > 0) {
  400. $formconfirm = $hookmanager->resPrint;
  401. }
  402. // Print form confirm
  403. print $formconfirm;
  404. // Object card
  405. // ------------------------------------------------------------
  406. $linkback = '<a href="'.DOL_URL_ROOT.'/product/inventory/list.php">'.$langs->trans("BackToList").'</a>';
  407. $morehtmlref = '<div class="refidno">';
  408. /*
  409. // Ref bis
  410. $morehtmlref.=$form->editfieldkey("RefBis", 'ref_client', $object->ref_client, $object, $user->rights->inventory->creer, 'string', '', 0, 1);
  411. $morehtmlref.=$form->editfieldval("RefBis", 'ref_client', $object->ref_client, $object, $user->rights->inventory->creer, 'string', '', null, null, '', 1);
  412. // Thirdparty
  413. $morehtmlref.='<br>'.$langs->trans('ThirdParty') . ' : ' . $soc->getNomUrl(1);
  414. // Project
  415. if (isModEnabled('project'))
  416. {
  417. $langs->load("projects");
  418. $morehtmlref.='<br>'.$langs->trans('Project') . ' ';
  419. if ($user->rights->inventory->creer)
  420. {
  421. if ($action != 'classify')
  422. {
  423. $morehtmlref.='<a class="editfielda" href="' . $_SERVER['PHP_SELF'] . '?action=classify&token='.newToken().'&id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
  424. if ($action == 'classify') {
  425. //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
  426. $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
  427. $morehtmlref.='<input type="hidden" name="action" value="classin">';
  428. $morehtmlref.='<input type="hidden" name="token" value="'.newToken().'">';
  429. $morehtmlref.=$formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
  430. $morehtmlref.='<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  431. $morehtmlref.='</form>';
  432. } else {
  433. $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
  434. }
  435. }
  436. } else {
  437. if (!empty($object->fk_project)) {
  438. $proj = new Project($db);
  439. $proj->fetch($object->fk_project);
  440. $morehtmlref.=$proj->getNomUrl();
  441. } else {
  442. $morehtmlref.='';
  443. }
  444. }
  445. }
  446. */
  447. $morehtmlref .= '</div>';
  448. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  449. print '<div class="fichecenter">';
  450. print '<div class="fichehalfleft">';
  451. print '<div class="underbanner clearboth"></div>';
  452. print '<table class="border centpercent tableforfield">'."\n";
  453. // Common attributes
  454. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
  455. // Other attributes. Fields from hook formObjectOptions and Extrafields.
  456. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  457. //print '<tr><td class="titlefield fieldname_invcode">'.$langs->trans("InventoryCode").'</td><td>INV'.$object->id.'</td></tr>';
  458. print '</table>';
  459. print '</div>';
  460. print '</div>';
  461. print '<div class="clearboth"></div>';
  462. print dol_get_fiche_end();
  463. print '<form id="formrecord" name="formrecord" method="POST" action="'.$_SERVER["PHP_SELF"].'?page='.$page.'&id='.$object->id.'">';
  464. print '<input type="hidden" name="token" value="'.newToken().'">';
  465. print '<input type="hidden" name="action" value="updateinventorylines">';
  466. print '<input type="hidden" name="id" value="'.$object->id.'">';
  467. if ($backtopage) {
  468. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  469. }
  470. // Buttons for actions
  471. if ($action != 'record') {
  472. print '<div class="tabsAction">'."\n";
  473. $parameters = array();
  474. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  475. if ($reshook < 0) {
  476. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  477. }
  478. if (empty($reshook)) {
  479. if ($object->status == Inventory::STATUS_DRAFT) {
  480. if ($permissiontoadd) {
  481. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=confirm_validate&confirm=yes&token='.newToken().'">'.$langs->trans("Validate").' ('.$langs->trans("Start").')</a>'."\n";
  482. } else {
  483. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">'.$langs->trans('Validate').' ('.$langs->trans("Start").')</a>'."\n";
  484. }
  485. }
  486. // Save
  487. if ($object->status == $object::STATUS_VALIDATED) {
  488. if ($permissiontoadd) {
  489. print '<a class="butAction classfortooltip" id="idbuttonmakemovementandclose" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=record&token='.newToken().'" title="'.dol_escape_htmltag($langs->trans("MakeMovementsAndClose")).'">'.$langs->trans("MakeMovementsAndClose").'</a>'."\n";
  490. } else {
  491. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">'.$langs->trans('MakeMovementsAndClose').'</a>'."\n";
  492. }
  493. if ($permissiontoadd) {
  494. print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=confirm_cancel&token='.newToken().'">'.$langs->trans("Cancel").'</a>'."\n";
  495. }
  496. }
  497. }
  498. print '</div>'."\n";
  499. if ($object->status != Inventory::STATUS_DRAFT && $object->status != Inventory::STATUS_VALIDATED) {
  500. print '<br><br>';
  501. }
  502. }
  503. if ($object->status == Inventory::STATUS_VALIDATED) {
  504. print '<center>';
  505. if (!empty($conf->use_javascript_ajax)) {
  506. if ($permissiontoadd) {
  507. // Link to launch scan tool
  508. if (isModEnabled('barcode') || isModEnabled('productbatch')) {
  509. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=updatebyscaning&token='.currentToken().'" class="marginrightonly paddingright marginleftonly paddingleft">'.img_picto('', 'barcode', 'class="paddingrightonly"').$langs->trans("UpdateByScaning").'</a>';
  510. }
  511. // Link to autofill
  512. print '<a id="fillwithexpected" class="marginrightonly paddingright marginleftonly paddingleft" href="#">'.img_picto('', 'autofill', 'class="paddingrightonly"').$langs->trans('AutofillWithExpected').'</a>';
  513. print '<script>';
  514. print '$( document ).ready(function() {';
  515. print ' $("#fillwithexpected").on("click",function fillWithExpected(){
  516. $(".expectedqty").each(function(){
  517. var object = $(this)[0];
  518. var objecttofill = $("#"+object.id+"_input")[0];
  519. objecttofill.value = object.innerText;
  520. jQuery(".realqty").trigger("change");
  521. })
  522. console.log("Values filled (after click on fillwithexpected)");
  523. /* disablebuttonmakemovementandclose(); */
  524. return false;
  525. });';
  526. print '});';
  527. print '</script>';
  528. // Link to reset qty
  529. print '<a href="#" id="clearqty" class="marginrightonly paddingright marginleftonly paddingleft">'.img_picto('', 'eraser', 'class="paddingrightonly"').$langs->trans("ClearQtys").'</a>';
  530. } else {
  531. print '<a class="classfortooltip marginrightonly paddingright marginleftonly paddingleft" href="#" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">'.$langs->trans("Save").'</a>'."\n";
  532. }
  533. }
  534. print '<br>';
  535. print '<br>';
  536. print '</center>';
  537. }
  538. // Popup for mass barcode scanning
  539. if ($action == 'updatebyscaning') {
  540. if ($permissiontoadd) {
  541. // Output the javascript to manage the scanner tool.
  542. print '<script>';
  543. print '
  544. var duplicatedbatchcode = [];
  545. var errortab1 = [];
  546. var errortab2 = [];
  547. var errortab3 = [];
  548. var errortab4 = [];
  549. function barcodescannerjs(){
  550. console.log("We catch inputs in scanner box");
  551. jQuery("#scantoolmessage").text();
  552. var selectaddorreplace = $("select[name=selectaddorreplace]").val();
  553. var barcodemode = $("input[name=barcodemode]:checked").val();
  554. var barcodeproductqty = $("input[name=barcodeproductqty]").val();
  555. var textarea = $("textarea[name=barcodelist]").val();
  556. var textarray = textarea.split(/[\s,;]+/);
  557. var tabproduct = [];
  558. duplicatedbatchcode = [];
  559. errortab1 = [];
  560. errortab2 = [];
  561. errortab3 = [];
  562. errortab4 = [];
  563. textarray = textarray.filter(function(value){
  564. return value != "";
  565. });
  566. if(textarray.some((element) => element != "")){
  567. $(".expectedqty").each(function(){
  568. id = this.id;
  569. console.log("Analyze the line "+id+" in inventory, barcodemode="+barcodemode);
  570. warehouse = $("#"+id+"_warehouse").attr(\'data-ref\');
  571. //console.log(warehouse);
  572. productbarcode = $("#"+id+"_product").attr(\'data-barcode\');
  573. //console.log(productbarcode);
  574. productbatchcode = $("#"+id+"_batch").attr(\'data-batch\');
  575. //console.log(productbatchcode);
  576. if (barcodemode != "barcodeforproduct") {
  577. tabproduct.forEach(product=>{
  578. console.log("product.Batch="+product.Batch+" productbatchcode="+productbatchcode);
  579. if(product.Batch != "" && product.Batch == productbatchcode){
  580. console.log("duplicate batch code found for batch code "+productbatchcode);
  581. duplicatedbatchcode.push(productbatchcode);
  582. }
  583. })
  584. }
  585. productinput = $("#"+id+"_input").val();
  586. if(productinput == ""){
  587. productinput = 0
  588. }
  589. tabproduct.push({\'Id\':id,\'Warehouse\':warehouse,\'Barcode\':productbarcode,\'Batch\':productbatchcode,\'Qty\':productinput,\'fetched\':false});
  590. });
  591. console.log("Loop on each record entered in the textarea");
  592. textarray.forEach(function(element,index){
  593. console.log("Process record element="+element+" id="+id);
  594. var verify_batch = false;
  595. var verify_barcode = false;
  596. switch(barcodemode){
  597. case "barcodeforautodetect":
  598. verify_barcode = barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"barcode",true);
  599. verify_batch = barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"lotserial",true);
  600. break;
  601. case "barcodeforproduct":
  602. verify_barcode = barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"barcode");
  603. break;
  604. case "barcodeforlotserial":
  605. verify_batch = barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"lotserial");
  606. break;
  607. default:
  608. alert(\''.dol_escape_js($langs->trans("ErrorWrongBarcodemode")).' "\'+barcodemode+\'"\');
  609. throw \''.dol_escape_js($langs->trans('ErrorWrongBarcodemode')).' "\'+barcodemode+\'"\';
  610. }
  611. if (verify_batch == false && verify_barcode == false) { /* If the 2 flags are false, not found error */
  612. errortab2.push(element);
  613. } else if (verify_batch == true && verify_barcode == true) { /* If the 2 flags are true, error: we don t know which one to take */
  614. errortab3.push(element);
  615. } else if (verify_batch == true) {
  616. console.log("element="+element);
  617. console.log(duplicatedbatchcode);
  618. if (duplicatedbatchcode.includes(element)) {
  619. errortab1.push(element);
  620. }
  621. }
  622. });
  623. if (Object.keys(errortab1).length < 1 && Object.keys(errortab2).length < 1 && Object.keys(errortab3).length < 1) {
  624. tabproduct.forEach(product => {
  625. if(product.Qty!=0){
  626. console.log("We change #"+product.Id+"_input to match input in scanner box");
  627. if(product.hasOwnProperty("reelqty")){
  628. $.ajax({ url: \''.DOL_URL_ROOT.'/product/inventory/ajax/searchfrombarcode.php\',
  629. data: { "token":"'.newToken().'", "action":"addnewlineproduct", "fk_entrepot":product.Warehouse, "batch":product.Batch, "fk_inventory":'.dol_escape_js($object->id).', "fk_product":product.fk_product, "reelqty":product.reelqty},
  630. type: \'POST\',
  631. async: false,
  632. success: function(response) {
  633. response = JSON.parse(response);
  634. if(response.status == "success"){
  635. console.log(response.message);
  636. $("<input type=\'text\' value=\'"+product.Qty+"\' />")
  637. .attr("id", "id_"+response.id_line+"_input")
  638. .attr("name", "id_"+response.id_line)
  639. .appendTo("#formrecord");
  640. }else{
  641. console.error(response.message);
  642. }
  643. },
  644. error : function(output) {
  645. console.error("Error on line creation function");
  646. },
  647. });
  648. } else {
  649. $("#"+product.Id+"_input").val(product.Qty);
  650. }
  651. }
  652. });
  653. jQuery("#scantoolmessage").text("'.dol_escape_js($langs->transnoentities("QtyWasAddedToTheScannedBarcode")).'\n");
  654. /* document.forms["formrecord"].submit(); */
  655. } else {
  656. let stringerror = "";
  657. if (Object.keys(errortab1).length > 0) {
  658. stringerror += "<br>'.dol_escape_js($langs->transnoentities('ErrorSameBatchNumber')).': ";
  659. errortab1.forEach(element => {
  660. stringerror += (element + ", ")
  661. });
  662. stringerror = stringerror.slice(0, -2); /* Remove last ", " */
  663. }
  664. if (Object.keys(errortab2).length > 0) {
  665. stringerror += "<br>'.dol_escape_js($langs->transnoentities('ErrorCantFindCodeInInventory')).': ";
  666. errortab2.forEach(element => {
  667. stringerror += (element + ", ")
  668. });
  669. stringerror = stringerror.slice(0, -2); /* Remove last ", " */
  670. }
  671. if (Object.keys(errortab3).length > 0) {
  672. stringerror += "<br>'.dol_escape_js($langs->transnoentities('ErrorCodeScannedIsBothProductAndSerial')).': ";
  673. errortab3.forEach(element => {
  674. stringerror += (element + ", ")
  675. });
  676. stringerror = stringerror.slice(0, -2); /* Remove last ", " */
  677. }
  678. if (Object.keys(errortab4).length > 0) {
  679. stringerror += "<br>'.dol_escape_js($langs->transnoentities('ErrorBarcodeNotFoundForProductWarehouse')).': ";
  680. errortab4.forEach(element => {
  681. stringerror += (element + ", ")
  682. });
  683. stringerror = stringerror.slice(0, -2); /* Remove last ", " */
  684. }
  685. jQuery("#scantoolmessage").html(\''.dol_escape_js($langs->transnoentities("ErrorOnElementsInventory")).'\' + stringerror);
  686. //alert("'.dol_escape_js($langs->trans("ErrorOnElementsInventory")).' :\n" + stringerror);
  687. }
  688. }
  689. }
  690. /* This methode is called by parent barcodescannerjs() */
  691. function barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,mode,autodetect=false){
  692. BarcodeIsInProduct=0;
  693. newproductrow=0
  694. result=false;
  695. tabproduct.forEach(product => {
  696. $.ajax({ url: \''.DOL_URL_ROOT.'/product/inventory/ajax/searchfrombarcode.php\',
  697. data: { "token":"'.newToken().'", "action":"existbarcode", '.(!empty($object->fk_warehouse)?'"fk_entrepot":'.$object->fk_warehouse.', ':'').(!empty($object->fk_product)?'"fk_product":'.$object->fk_product.', ':'').'"barcode":element, "product":product, "mode":mode},
  698. type: \'POST\',
  699. async: false,
  700. success: function(response) {
  701. response = JSON.parse(response);
  702. if (response.status == "success"){
  703. console.log(response.message);
  704. if(!newproductrow){
  705. newproductrow = response.object;
  706. }
  707. }else{
  708. if (mode!="lotserial" && autodetect==false && !errortab4.includes(element)){
  709. errortab4.push(element);
  710. console.error(response.message);
  711. }
  712. }
  713. },
  714. error : function(output) {
  715. console.error("Error on barcodeserialforproduct function");
  716. },
  717. });
  718. console.log("Product "+(index+=1)+": "+element);
  719. if(mode == "barcode"){
  720. testonproduct = product.Barcode
  721. }else if (mode == "lotserial"){
  722. testonproduct = product.Batch
  723. }
  724. if(testonproduct == element){
  725. if(selectaddorreplace == "add"){
  726. productqty = parseInt(product.Qty,10);
  727. product.Qty = productqty + parseInt(barcodeproductqty,10);
  728. }else if(selectaddorreplace == "replace"){
  729. if(product.fetched == false){
  730. product.Qty = barcodeproductqty
  731. product.fetched=true
  732. }else{
  733. productqty = parseInt(product.Qty,10);
  734. product.Qty = productqty + parseInt(barcodeproductqty,10);
  735. }
  736. }
  737. BarcodeIsInProduct+=1;
  738. }
  739. })
  740. if(BarcodeIsInProduct==0 && newproductrow!=0){
  741. tabproduct.push({\'Id\':tabproduct.length-1,\'Warehouse\':newproductrow.fk_warehouse,\'Barcode\':mode=="barcode"?element:null,\'Batch\':mode=="lotserial"?element:null,\'Qty\':barcodeproductqty,\'fetched\':true,\'reelqty\':newproductrow.reelqty,\'fk_product\':newproductrow.fk_product,\'mode\':mode});
  742. result = true;
  743. }
  744. if(BarcodeIsInProduct > 0){
  745. result = true;
  746. }
  747. return result;
  748. }
  749. ';
  750. print '</script>';
  751. }
  752. include DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  753. $formother = new FormOther($db);
  754. print $formother->getHTMLScannerForm("barcodescannerjs", 'all');
  755. }
  756. //Call method to undo changes in real qty
  757. print '<script>';
  758. print 'jQuery(document).ready(function() {
  759. $("#clearqty").on("click", function() {
  760. console.log("Clear all values");
  761. /* disablebuttonmakemovementandclose(); */
  762. jQuery(".realqty").val("");
  763. jQuery(".realqty").trigger("change");
  764. return false; /* disable submit */
  765. });
  766. $(".undochangesqty").on("click", function undochangesqty() {
  767. console.log("Clear value of inventory line");
  768. id = this.id;
  769. id = id.split("_")[1];
  770. tmpvalue = $("#id_"+id+"_input_tmp").val()
  771. $("#id_"+id+"_input")[0].value = tmpvalue;
  772. /* disablebuttonmakemovementandclose(); */
  773. return false; /* disable submit */
  774. });
  775. });';
  776. print '</script>';
  777. print '<div class="fichecenter">';
  778. //print '<div class="fichehalfleft">';
  779. print '<div class="clearboth"></div>';
  780. //print load_fiche_titre($langs->trans('Consumption'), '', '');
  781. print '<div class="div-table-responsive-no-min">';
  782. print '<table id="tablelines" class="noborder noshadow centpercent">';
  783. print '<tr class="liste_titre">';
  784. print '<td>'.$langs->trans("Warehouse").'</td>';
  785. print '<td>'.$langs->trans("Product").'</td>';
  786. if (isModEnabled('productbatch')) {
  787. print '<td>';
  788. print $langs->trans("Batch");
  789. print '</td>';
  790. }
  791. print '<td class="right">'.$langs->trans("ExpectedQty").'</td>';
  792. if (!empty($conf->global->INVENTORY_MANAGE_REAL_PMP)) {
  793. print '<td class="right">'.$langs->trans('PMPExpected').'</td>';
  794. print '<td class="right">'.$langs->trans('ExpectedValuation').'</td>';
  795. print '<td class="right">'.$form->textwithpicto($langs->trans("RealQty"), $langs->trans("InventoryRealQtyHelp")).'</td>';
  796. print '<td class="right">'.$langs->trans('PMPReal').'</td>';
  797. print '<td class="right">'.$langs->trans('RealValuation').'</td>';
  798. } else {
  799. print '<td class="right">';
  800. print $form->textwithpicto($langs->trans("RealQty"), $langs->trans("InventoryRealQtyHelp"));
  801. print '</td>';
  802. }
  803. if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) {
  804. // Actions or link to stock movement
  805. print '<td class="center">';
  806. print '</td>';
  807. } else {
  808. // Actions or link to stock movement
  809. print '<td class="right">';
  810. //print $langs->trans("StockMovement");
  811. print '</td>';
  812. }
  813. print '</tr>';
  814. // Line to add a new line in inventory
  815. if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) {
  816. print '<tr>';
  817. print '<td>';
  818. print $formproduct->selectWarehouses((GETPOSTISSET('fk_warehouse') ? GETPOST('fk_warehouse', 'int') : $object->fk_warehouse), 'fk_warehouse', 'warehouseopen', 1, 0, 0, '', 0, 0, array(), 'maxwidth300');
  819. print '</td>';
  820. print '<td>';
  821. print $form->select_produits((GETPOSTISSET('fk_product') ? GETPOST('fk_product', 'int') : $object->fk_product), 'fk_product', '', 0, 0, -1, 2, '', 0, null, 0, '1', 0, 'maxwidth300');
  822. print '</td>';
  823. if (isModEnabled('productbatch')) {
  824. print '<td>';
  825. print '<input type="text" name="batch" class="maxwidth100" value="'.(GETPOSTISSET('batch') ? GETPOST('batch') : '').'">';
  826. print '</td>';
  827. }
  828. print '<td class="right"></td>';
  829. if (!empty($conf->global->INVENTORY_MANAGE_REAL_PMP)) {
  830. print '<td class="right">';
  831. print '</td>';
  832. print '<td class="right">';
  833. print '</td>';
  834. print '<td class="right">';
  835. print '<input type="text" name="qtytoadd" class="maxwidth75" value="">';
  836. print '</td>';
  837. print '<td class="right">';
  838. print '</td>';
  839. print '<td class="right">';
  840. print '</td>';
  841. } else {
  842. print '<td class="right">';
  843. print '<input type="text" name="qtytoadd" class="maxwidth75" value="">';
  844. print '</td>';
  845. }
  846. // Actions
  847. print '<td class="center">';
  848. print '<input type="submit" class="button paddingright" name="addline" value="'.$langs->trans("Add").'">';
  849. print '</td>';
  850. print '</tr>';
  851. }
  852. // Request to show lines of inventory (prefilled after start/validate step)
  853. $sql = 'SELECT id.rowid, id.datec as date_creation, id.tms as date_modification, id.fk_inventory, id.fk_warehouse,';
  854. $sql .= ' id.fk_product, id.batch, id.qty_stock, id.qty_view, id.qty_regulated, id.fk_movement, id.pmp_real, id.pmp_expected';
  855. $sql .= ' FROM '.MAIN_DB_PREFIX.'inventorydet as id';
  856. $sql .= ' WHERE id.fk_inventory = '.((int) $object->id);
  857. $sql .= $db->order('id.rowid', 'ASC');
  858. $sql .= $db->plimit($limit, $offset);
  859. $cacheOfProducts = array();
  860. $cacheOfWarehouses = array();
  861. //$sql = '';
  862. $resql = $db->query($sql);
  863. if ($resql) {
  864. $num = $db->num_rows($resql);
  865. if (!empty($limit != 0) || $num > $limit || $page) {
  866. print_fleche_navigation($page, $_SERVER["PHP_SELF"], $paramwithsearch, ($num >= $limit), '<li class="pagination"><span>' . $langs->trans("Page") . ' ' . ($page + 1) . '</span></li>', '', $limit);
  867. }
  868. $i = 0;
  869. $hasinput = false;
  870. $totalarray = array();
  871. while ($i < $num) {
  872. $obj = $db->fetch_object($resql);
  873. if (isset($cacheOfWarehouses[$obj->fk_warehouse])) {
  874. $warehouse_static = $cacheOfWarehouses[$obj->fk_warehouse];
  875. } else {
  876. $warehouse_static = new Entrepot($db);
  877. $warehouse_static->fetch($obj->fk_warehouse);
  878. $cacheOfWarehouses[$warehouse_static->id] = $warehouse_static;
  879. }
  880. // Load real stock we have now
  881. $option = '';
  882. if (isset($cacheOfProducts[$obj->fk_product])) {
  883. $product_static = $cacheOfProducts[$obj->fk_product];
  884. } else {
  885. $product_static = new Product($db);
  886. $result = $product_static->fetch($obj->fk_product, '', '', '', 1, 1, 1);
  887. //$option = 'nobatch';
  888. $option .= ',novirtual';
  889. $product_static->load_stock($option); // Load stock_reel + stock_warehouse.
  890. $cacheOfProducts[$product_static->id] = $product_static;
  891. }
  892. print '<tr class="oddeven">';
  893. print '<td id="id_'.$obj->rowid.'_warehouse" data-ref="'.dol_escape_htmltag($warehouse_static->ref).'">';
  894. print $warehouse_static->getNomUrl(1);
  895. print '</td>';
  896. print '<td id="id_'.$obj->rowid.'_product" data-ref="'.dol_escape_htmltag($product_static->ref).'" data-barcode="'.dol_escape_htmltag($product_static->barcode).'">';
  897. print $product_static->getNomUrl(1).' - '.$product_static->label;
  898. print '</td>';
  899. if (isModEnabled('productbatch')) {
  900. print '<td id="id_'.$obj->rowid.'_batch" data-batch="'.dol_escape_htmltag($obj->batch).'">';
  901. $batch_static = new Productlot($db);
  902. $res = $batch_static->fetch(0, $product_static->id, $obj->batch);
  903. if ($res) {
  904. print $batch_static->getNomUrl(1);
  905. } else {
  906. print dol_escape_htmltag($obj->batch);
  907. }
  908. print '</td>';
  909. }
  910. // Expected quantity = Quantity in stock when we start inventory
  911. print '<td class="right expectedqty" id="id_'.$obj->rowid.'" title="Stock viewed at last update: '.$obj->qty_stock.'">';
  912. $valuetoshow = $obj->qty_stock;
  913. // For inventory not yet close, we overwrite with the real value in stock now
  914. if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) {
  915. if (isModEnabled('productbatch') && $product_static->hasbatch()) {
  916. $valuetoshow = $product_static->stock_warehouse[$obj->fk_warehouse]->detail_batch[$obj->batch]->qty;
  917. } else {
  918. $valuetoshow = $product_static->stock_warehouse[$obj->fk_warehouse]->real;
  919. }
  920. }
  921. print price2num($valuetoshow, 'MS');
  922. print '<input type="hidden" name="stock_qty_'.$obj->rowid.'" value="'.$valuetoshow.'">';
  923. print '</td>';
  924. // Real quantity
  925. if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) {
  926. $qty_view = GETPOST("id_".$obj->rowid) && price2num(GETPOST("id_".$obj->rowid), 'MS') >= 0 ? GETPOST("id_".$obj->rowid) : $obj->qty_view;
  927. //if (!$hasinput && $qty_view !== null && $obj->qty_stock != $qty_view) {
  928. if ($qty_view != '') {
  929. $hasinput = true;
  930. }
  931. if (!empty($conf->global->INVENTORY_MANAGE_REAL_PMP)) {
  932. //PMP Expected
  933. if (!empty($obj->pmp_expected)) $pmp_expected = $obj->pmp_expected;
  934. else $pmp_expected = $product_static->pmp;
  935. $pmp_valuation = $pmp_expected * $valuetoshow;
  936. print '<td class="right">';
  937. print price($pmp_expected);
  938. print '<input type="hidden" name="expectedpmp_'.$obj->rowid.'" value="'.$pmp_expected.'"/>';
  939. print '</td>';
  940. print '<td class="right">';
  941. print price($pmp_valuation);
  942. print '</td>';
  943. print '<td class="right">';
  944. print '<a id="undochangesqty_'.$obj->rowid.'" href="#" class="undochangesqty reposition marginrightonly" title="'.dol_escape_htmltag($langs->trans("Clear")).'">';
  945. print img_picto('', 'eraser', 'class="opacitymedium"');
  946. print '</a>';
  947. print '<input type="text" class="maxwidth50 right realqty" name="id_'.$obj->rowid.'" id="id_'.$obj->rowid.'_input" value="'.$qty_view.'">';
  948. print '</td>';
  949. //PMP Real
  950. print '<td class="right">';
  951. if (!empty($obj->pmp_real)) $pmp_real = $obj->pmp_real;
  952. else $pmp_real = $product_static->pmp;
  953. $pmp_valuation_real = $pmp_real * $qty_view;
  954. print '<input type="text" class="maxwidth75 right realpmp'.$obj->fk_product.'" name="realpmp_'.$obj->rowid.'" id="id_'.$obj->rowid.'_input_pmp" value="'.price2num($pmp_real).'">';
  955. print '</td>';
  956. print '<td class="right">';
  957. print '<input type="text" class="maxwidth75 right realvaluation'.$obj->fk_product.'" name="realvaluation_'.$obj->rowid.'" id="id_'.$obj->rowid.'_input_real_valuation" value="'.$pmp_valuation_real.'">';
  958. print '</td>';
  959. $totalExpectedValuation += $pmp_valuation;
  960. $totalRealValuation += $pmp_valuation_real;
  961. } else {
  962. print '<td class="right">';
  963. print '<a id="undochangesqty_'.$obj->rowid.'" href="#" class="undochangesqty reposition marginrightonly" title="'.dol_escape_htmltag($langs->trans("Clear")).'">';
  964. print img_picto('', 'eraser', 'class="opacitymedium"');
  965. print '</a>';
  966. print '<input type="text" class="maxwidth50 right realqty" name="id_'.$obj->rowid.'" id="id_'.$obj->rowid.'_input" value="'.$qty_view.'">';
  967. print '</td>';
  968. }
  969. // Picto delete line
  970. print '<td class="right">';
  971. print '<a class="reposition" href="'.DOL_URL_ROOT.'/product/inventory/inventory.php?id='.$object->id.'&lineid='.$obj->rowid.'&action=deleteline&page='.$page.$paramwithsearch.'&token='.newToken().'">'.img_delete().'</a>';
  972. $qty_tmp = price2num(GETPOST("id_".$obj->rowid."_input_tmp", 'MS')) >= 0 ? GETPOST("id_".$obj->rowid."_input_tmp") : $qty_view;
  973. print '<input type="hidden" class="maxwidth50 right realqty" name="id_'.$obj->rowid.'_input_tmp" id="id_'.$obj->rowid.'_input_tmp" value="'.$qty_tmp.'">';
  974. print '</td>';
  975. } else {
  976. if (!empty($conf->global->INVENTORY_MANAGE_REAL_PMP)) {
  977. //PMP Expected
  978. if (!empty($obj->pmp_expected)) $pmp_expected = $obj->pmp_expected;
  979. else $pmp_expected = $product_static->pmp;
  980. $pmp_valuation = $pmp_expected * $valuetoshow;
  981. print '<td class="right">';
  982. print price($pmp_expected);
  983. print '</td>';
  984. print '<td class="right">';
  985. print price($pmp_valuation);
  986. print '</td>';
  987. print '<td class="right nowraponall">';
  988. print $obj->qty_view; // qty found
  989. print '</td>';
  990. //PMP Real
  991. print '<td class="right">';
  992. if (!empty($obj->pmp_real)) $pmp_real = $obj->pmp_real;
  993. else $pmp_real = $product_static->pmp;
  994. $pmp_valuation_real = $pmp_real * $obj->qty_view;
  995. print price($pmp_real);
  996. print '</td>';
  997. print '<td class="right">';
  998. print price($pmp_valuation_real);
  999. print '</td>';
  1000. print '<td class="nowraponall right">';
  1001. $totalExpectedValuation += $pmp_valuation;
  1002. $totalRealValuation += $pmp_valuation_real;
  1003. } else {
  1004. print '<td class="right nowraponall">';
  1005. print $obj->qty_view; // qty found
  1006. print '</td>';
  1007. }
  1008. if ($obj->fk_movement > 0) {
  1009. $stockmovment = new MouvementStock($db);
  1010. $stockmovment->fetch($obj->fk_movement);
  1011. print $stockmovment->getNomUrl(1, 'movements');
  1012. }
  1013. print '</td>';
  1014. }
  1015. print '</tr>';
  1016. $i++;
  1017. }
  1018. } else {
  1019. dol_print_error($db);
  1020. }
  1021. if (!empty($conf->global->INVENTORY_MANAGE_REAL_PMP)) {
  1022. print '<tr class="liste_total">';
  1023. print '<td colspan="4">'.$langs->trans("Total").'</td>';
  1024. print '<td class="right" colspan="2">'.price($totalExpectedValuation).'</td>';
  1025. print '<td class="right" id="totalRealValuation" colspan="3">'.price($totalRealValuation).'</td>';
  1026. print '<td></td>';
  1027. print '</tr>';
  1028. }
  1029. print '</table>';
  1030. print '</div>';
  1031. if ($object->status == $object::STATUS_VALIDATED) {
  1032. print '<center><input id="submitrecord" type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'"></center>';
  1033. }
  1034. print '</div>';
  1035. // Call method to disable the button if no qty entered yet for inventory
  1036. /*
  1037. if ($object->status != $object::STATUS_VALIDATED || !$hasinput) {
  1038. print '<script type="text/javascript">
  1039. jQuery(document).ready(function() {
  1040. console.log("Call disablebuttonmakemovementandclose because status = '.((int) $object->status).' or $hasinput = '.((int) $hasinput).'");
  1041. disablebuttonmakemovementandclose();
  1042. });
  1043. </script>';
  1044. }
  1045. */
  1046. print '</form>';
  1047. print '<script type="text/javascript">
  1048. $(document).ready(function() {
  1049. $(".paginationnext:last").click(function(e){
  1050. var form = $("#formrecord");
  1051. var actionURL = "'.$_SERVER['PHP_SELF']."?page=".($page).$paramwithsearch.'";
  1052. $.ajax({
  1053. url: actionURL,
  1054. data: form.serialize(),
  1055. cache: false,
  1056. success: function(result){
  1057. window.location.href = "'.$_SERVER['PHP_SELF']."?page=".($page + 1).$paramwithsearch.'";
  1058. }});
  1059. });
  1060. $(".paginationprevious:last").click(function(e){
  1061. var form = $("#formrecord");
  1062. var actionURL = "'.$_SERVER['PHP_SELF']."?page=".($page).$paramwithsearch.'";
  1063. $.ajax({
  1064. url: actionURL,
  1065. data: form.serialize(),
  1066. cache: false,
  1067. success: function(result){
  1068. window.location.href = "'.$_SERVER['PHP_SELF']."?page=".($page - 1).$paramwithsearch.'";
  1069. }});
  1070. });
  1071. $("#idbuttonmakemovementandclose").click(function(e){
  1072. var form = $("#formrecord");
  1073. var actionURL = "'.$_SERVER['PHP_SELF']."?page=".($page).$paramwithsearch.'";
  1074. $.ajax({
  1075. url: actionURL,
  1076. data: form.serialize(),
  1077. cache: false,
  1078. success: function(result){
  1079. window.location.href = "'.$_SERVER['PHP_SELF']."?page=".($page - 1).$paramwithsearch.'&action=record";
  1080. }});
  1081. });
  1082. });
  1083. </script>';
  1084. if (!empty($conf->global->INVENTORY_MANAGE_REAL_PMP)) {
  1085. ?>
  1086. <script type="text/javascript">
  1087. $('.realqty').on('change', function () {
  1088. let realqty = $(this).closest('tr').find('.realqty').val();
  1089. let inputPmp = $(this).closest('tr').find('input[class*=realpmp]');
  1090. let realpmp = $(inputPmp).val();
  1091. if (!isNaN(realqty) && !isNaN(realpmp)) {
  1092. let realval = realqty * realpmp;
  1093. $(this).closest('tr').find('input[name^=realvaluation]').val(realval.toFixed(2));
  1094. }
  1095. updateTotalValuation();
  1096. });
  1097. $('input[class*=realpmp]').on('change', function () {
  1098. let inputQtyReal = $(this).closest('tr').find('.realqty');
  1099. let realqty = $(inputQtyReal).val();
  1100. let inputPmp = $(this).closest('tr').find('input[class*=realpmp]');
  1101. console.log(inputPmp);
  1102. let realPmpClassname = $(inputPmp).attr('class').match(/[\w-]*realpmp[\w-]*/g)[0];
  1103. let realpmp = $(inputPmp).val();
  1104. if (!isNaN(realpmp)) {
  1105. $('.'+realPmpClassname).val(realpmp); //For batch case if pmp is changed we change it everywhere it's same product and calc back everything
  1106. if (!isNaN(realqty)) {
  1107. let realval = realqty * realpmp;
  1108. $(this).closest('tr').find('input[name^=realvaluation]').val(realval.toFixed(2));
  1109. }
  1110. $('.realqty').trigger('change');
  1111. updateTotalValuation();
  1112. }
  1113. });
  1114. $('input[name^=realvaluation]').on('change', function () {
  1115. let inputQtyReal = $(this).closest('tr').find('.realqty');
  1116. let realqty = $(inputQtyReal).val();
  1117. let inputPmp = $(this).closest('tr').find('input[class*=realpmp]');
  1118. let inputRealValuation = $(this).closest('tr').find('input[name^=realvaluation]');
  1119. let realPmpClassname = $(inputPmp).attr('class').match(/[\w-]*realpmp[\w-]*/g)[0];
  1120. let realvaluation = $(inputRealValuation).val();
  1121. if (!isNaN(realvaluation) && !isNaN(realqty) && realvaluation !== '' && realqty !== '' && realqty !== 0) {
  1122. let realpmp = realvaluation / realqty
  1123. $('.'+realPmpClassname).val(realpmp); //For batch case if pmp is changed we change it everywhere it's same product and calc back everything
  1124. $('.realqty').trigger('change');
  1125. updateTotalValuation();
  1126. }
  1127. });
  1128. function updateTotalValuation() {
  1129. let total = 0;
  1130. $('input[name^=realvaluation]').each(function( index ) {
  1131. let val = $(this).val();
  1132. if(!isNaN(val)) total += parseFloat($(this).val());
  1133. });
  1134. let currencyFractionDigits = new Intl.NumberFormat('fr-FR', {
  1135. style: 'currency',
  1136. currency: 'EUR',
  1137. }).resolvedOptions().maximumFractionDigits;
  1138. $('#totalRealValuation').html(total.toLocaleString('fr-FR', {
  1139. maximumFractionDigits: currencyFractionDigits
  1140. }));
  1141. }
  1142. </script>
  1143. <?php
  1144. }
  1145. // End of page
  1146. llxFooter();
  1147. $db->close();