product.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. <?php
  2. /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2020 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005 Simon TOSSER <simon@kornog-computing.com>
  6. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  7. * Copyright (C) 2013 Cédric Salvador <csalvador.gpcsolutions.fr>
  8. * Copyright (C) 2013-2018 Juanjo Menent <jmenent@2byte.es>
  9. * Copyright (C) 2014-2015 Cédric Gross <c.gross@kreiz-it.fr>
  10. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  11. * Copyright (C) 2018-2019 Frédéric France <frederic.france@netlogic.fr>
  12. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * \file htdocs/product/stock/product.php
  29. * \ingroup product stock
  30. * \brief Page to list detailed stock of a product
  31. */
  32. // Load Dolibarr environment
  33. require '../../main.inc.php';
  34. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  39. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productstockentrepot.class.php';
  41. if (isModEnabled('productbatch')) {
  42. require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
  43. }
  44. if (isModEnabled('project')) {
  45. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  46. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  47. }
  48. if (isModEnabled('variants')) {
  49. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
  50. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php';
  51. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
  52. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
  53. }
  54. // Load translation files required by the page
  55. $langs->loadlangs(array('products', 'suppliers', 'orders', 'bills', 'stocks', 'sendings', 'margins'));
  56. if (isModEnabled('productbatch')) {
  57. $langs->load("productbatch");
  58. }
  59. $backtopage = GETPOST('backtopage', 'alpha');
  60. $action = GETPOST('action', 'aZ09');
  61. $cancel = GETPOST('cancel', 'alpha');
  62. $id = GETPOST('id', 'int');
  63. $ref = GETPOST('ref', 'alpha');
  64. $stocklimit = GETPOST('seuil_stock_alerte');
  65. $desiredstock = GETPOST('desiredstock');
  66. $cancel = GETPOST('cancel', 'alpha');
  67. $fieldid = isset($_GET["ref"]) ? 'ref' : 'rowid';
  68. $d_eatby = dol_mktime(0, 0, 0, GETPOST('eatbymonth', 'int'), GETPOST('eatbyday', 'int'), GETPOST('eatbyyear', 'int'));
  69. $d_sellby = dol_mktime(0, 0, 0, GETPOST('sellbymonth', 'int'), GETPOST('sellbyday', 'int'), GETPOST('sellbyyear', 'int'));
  70. $pdluoid = GETPOST('pdluoid', 'int');
  71. $batchnumber = GETPOST('batch_number', 'san_alpha');
  72. if (!empty($batchnumber)) {
  73. $batchnumber = trim($batchnumber);
  74. }
  75. $cost_price = GETPOST('cost_price', 'alpha');
  76. // Security check
  77. if ($user->socid) {
  78. $socid = $user->socid;
  79. }
  80. $object = new Product($db);
  81. $extrafields = new ExtraFields($db);
  82. // fetch optionals attributes and labels
  83. $extrafields->fetch_name_optionals_label($object->table_element);
  84. if ($id > 0 || !empty($ref)) {
  85. $result = $object->fetch($id, $ref);
  86. }
  87. if (empty($id) && !empty($object->id)) {
  88. $id = $object->id;
  89. }
  90. $modulepart = 'product';
  91. // Get object canvas (By default, this is not defined, so standard usage of dolibarr)
  92. $canvas = !empty($object->canvas) ? $object->canvas : GETPOST("canvas");
  93. $objcanvas = null;
  94. if (!empty($canvas)) {
  95. require_once DOL_DOCUMENT_ROOT.'/core/class/canvas.class.php';
  96. $objcanvas = new Canvas($db, $action);
  97. $objcanvas->getCanvas('stockproduct', 'card', $canvas);
  98. }
  99. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  100. $hookmanager->initHooks(array('stockproductcard', 'globalcard'));
  101. $error = 0;
  102. $usercanread = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->lire) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'lire')));
  103. $usercancreate = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->creer) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'creer')));
  104. $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('product', 'product_advance', 'read_prices') : $user->hasRight('product', 'lire');
  105. if ($object->isService()) {
  106. $label = $langs->trans('Service');
  107. $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('service', 'service_advance', 'read_prices') : $user->hasRight('service', 'lire');
  108. }
  109. if ($object->id > 0) {
  110. if ($object->type == $object::TYPE_PRODUCT) {
  111. restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
  112. }
  113. if ($object->type == $object::TYPE_SERVICE) {
  114. restrictedArea($user, 'service', $object->id, 'product&product', '', '');
  115. }
  116. } else {
  117. restrictedArea($user, 'produit|service', $id, 'product&product', '', '', $fieldid);
  118. }
  119. /*
  120. * Actions
  121. */
  122. if ($cancel) {
  123. $action = '';
  124. }
  125. $parameters = array('id'=>$id, 'ref'=>$ref, 'objcanvas'=>$objcanvas);
  126. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  127. if ($reshook < 0) {
  128. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  129. }
  130. if ($action == 'setcost_price') {
  131. if ($id) {
  132. $result = $object->fetch($id);
  133. $object->cost_price = price2num($cost_price);
  134. $result = $object->update($object->id, $user);
  135. if ($result > 0) {
  136. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  137. $action = '';
  138. } else {
  139. $error++;
  140. setEventMessages($object->error, $object->errors, 'errors');
  141. }
  142. }
  143. }
  144. if ($action == 'addlimitstockwarehouse' && $user->hasRight('produit', 'creer')) {
  145. $seuil_stock_alerte = GETPOST('seuil_stock_alerte');
  146. $desiredstock = GETPOST('desiredstock');
  147. $maj_ok = true;
  148. if ($seuil_stock_alerte == '') {
  149. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("StockLimit")), null, 'errors');
  150. $maj_ok = false;
  151. }
  152. if ($desiredstock == '') {
  153. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DesiredStock")), null, 'errors');
  154. $maj_ok = false;
  155. }
  156. if ($maj_ok) {
  157. $pse = new ProductStockEntrepot($db);
  158. if ($pse->fetch(0, $id, GETPOST('fk_entrepot', 'int')) > 0) {
  159. // Update
  160. $pse->seuil_stock_alerte = $seuil_stock_alerte;
  161. $pse->desiredstock = $desiredstock;
  162. if ($pse->update($user) > 0) {
  163. setEventMessages($langs->trans('ProductStockWarehouseUpdated'), null, 'mesgs');
  164. }
  165. } else {
  166. // Create
  167. $pse->fk_entrepot = GETPOST('fk_entrepot', 'int');
  168. $pse->fk_product = $id;
  169. $pse->seuil_stock_alerte = GETPOST('seuil_stock_alerte');
  170. $pse->desiredstock = GETPOST('desiredstock');
  171. if ($pse->create($user) > 0) {
  172. setEventMessages($langs->trans('ProductStockWarehouseCreated'), null, 'mesgs');
  173. }
  174. }
  175. }
  176. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  177. exit;
  178. }
  179. if ($action == 'delete_productstockwarehouse' && $user->hasRight('produit', 'creer')) {
  180. $pse = new ProductStockEntrepot($db);
  181. $pse->fetch(GETPOST('fk_productstockwarehouse', 'int'));
  182. if ($pse->delete($user) > 0) {
  183. setEventMessages($langs->trans('ProductStockWarehouseDeleted'), null, 'mesgs');
  184. }
  185. $action = '';
  186. }
  187. // Set stock limit
  188. if ($action == 'setseuil_stock_alerte' && $user->hasRight('produit', 'creer')) {
  189. $object = new Product($db);
  190. $result = $object->fetch($id);
  191. $object->seuil_stock_alerte = $stocklimit;
  192. $result = $object->update($object->id, $user, 0, 'update');
  193. if ($result < 0) {
  194. setEventMessages($object->error, $object->errors, 'errors');
  195. }
  196. //else
  197. // setEventMessages($lans->trans("SavedRecordSuccessfully"), null, 'mesgs');
  198. $action = '';
  199. }
  200. // Set desired stock
  201. if ($action == 'setdesiredstock' && $user->hasRight('produit', 'creer')) {
  202. $object = new Product($db);
  203. $result = $object->fetch($id);
  204. $object->desiredstock = $desiredstock;
  205. $result = $object->update($object->id, $user, 0, 'update');
  206. if ($result < 0) {
  207. setEventMessages($object->error, $object->errors, 'errors');
  208. }
  209. $action = '';
  210. }
  211. // Correct stock
  212. if ($action == "correct_stock" && !$cancel) {
  213. if (!(GETPOST("id_entrepot", 'int') > 0)) {
  214. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
  215. $error++;
  216. $action = 'correction';
  217. }
  218. if (!GETPOST("nbpiece")) {
  219. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("NumberOfUnit")), null, 'errors');
  220. $error++;
  221. $action = 'correction';
  222. }
  223. if (isModEnabled('productbatch')) {
  224. $object = new Product($db);
  225. $result = $object->fetch($id);
  226. if ($object->hasbatch() && !$batchnumber) {
  227. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("batch_number")), null, 'errors');
  228. $error++;
  229. $action = 'correction';
  230. }
  231. }
  232. if (!$error) {
  233. $priceunit = price2num(GETPOST("unitprice"));
  234. $nbpiece = price2num(GETPOST("nbpiece", 'alphanohtml'));
  235. if (is_numeric($nbpiece) && $nbpiece != 0 && $id) {
  236. $origin_element = '';
  237. $origin_id = null;
  238. if (GETPOST('projectid', 'int')) {
  239. $origin_element = 'project';
  240. $origin_id = GETPOST('projectid', 'int');
  241. }
  242. if (empty($object)) {
  243. $object = new Product($db);
  244. $result = $object->fetch($id);
  245. }
  246. $disablestockchangeforsubproduct = 0;
  247. if (GETPOST('disablesubproductstockchange')) {
  248. $disablestockchangeforsubproduct = 1;
  249. }
  250. if ($object->hasbatch()) {
  251. $result = $object->correct_stock_batch(
  252. $user,
  253. GETPOST("id_entrepot", 'int'),
  254. $nbpiece,
  255. GETPOST("mouvement", 'int'),
  256. GETPOST("label", 'alphanohtml'), // label movement
  257. $priceunit,
  258. $d_eatby,
  259. $d_sellby,
  260. $batchnumber,
  261. GETPOST('inventorycode', 'alphanohtml'),
  262. $origin_element,
  263. $origin_id,
  264. $disablestockchangeforsubproduct
  265. ); // We do not change value of stock for a correction
  266. } else {
  267. $result = $object->correct_stock(
  268. $user,
  269. GETPOST("id_entrepot", 'int'),
  270. $nbpiece,
  271. GETPOST("mouvement", 'int'),
  272. GETPOST("label", 'alphanohtml'),
  273. $priceunit,
  274. GETPOST('inventorycode', 'alphanohtml'),
  275. $origin_element,
  276. $origin_id,
  277. $disablestockchangeforsubproduct
  278. ); // We do not change value of stock for a correction
  279. }
  280. if ($result > 0) {
  281. if ($backtopage) {
  282. header("Location: ".$backtopage);
  283. exit;
  284. } else {
  285. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
  286. exit;
  287. }
  288. } else {
  289. setEventMessages($object->error, $object->errors, 'errors');
  290. $action = 'correction';
  291. }
  292. }
  293. }
  294. }
  295. // Transfer stock from a warehouse to another warehouse
  296. if ($action == "transfert_stock" && !$cancel) {
  297. if (!(GETPOST("id_entrepot", 'int') > 0) || !(GETPOST("id_entrepot_destination", 'int') > 0)) {
  298. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
  299. $error++;
  300. $action = 'transfert';
  301. }
  302. if (!GETPOST("nbpiece", 'int')) {
  303. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("NumberOfUnit")), null, 'errors');
  304. $error++;
  305. $action = 'transfert';
  306. }
  307. if (GETPOST("id_entrepot", 'int') == GETPOST("id_entrepot_destination", 'int')) {
  308. setEventMessages($langs->trans("ErrorSrcAndTargetWarehouseMustDiffers"), null, 'errors');
  309. $error++;
  310. $action = 'transfert';
  311. }
  312. if (isModEnabled('productbatch')) {
  313. $object = new Product($db);
  314. $result = $object->fetch($id);
  315. if ($object->hasbatch() && !$batchnumber) {
  316. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("batch_number")), null, 'errors');
  317. $error++;
  318. $action = 'transfert';
  319. }
  320. }
  321. if (!$error) {
  322. if ($id) {
  323. $object = new Product($db);
  324. $result = $object->fetch($id);
  325. $db->begin();
  326. $object->load_stock('novirtual'); // Load array product->stock_warehouse
  327. // Define value of products moved
  328. $pricesrc = 0;
  329. if (isset($object->pmp)) {
  330. $pricesrc = $object->pmp;
  331. }
  332. $pricedest = $pricesrc;
  333. $nbpiece = price2num(GETPOST("nbpiece", 'alphanohtml'));
  334. if ($object->hasbatch()) {
  335. $pdluo = new Productbatch($db);
  336. if ($pdluoid > 0) {
  337. $result = $pdluo->fetch($pdluoid);
  338. if ($result) {
  339. $srcwarehouseid = $pdluo->warehouseid;
  340. $batch = $pdluo->batch;
  341. $eatby = $pdluo->eatby;
  342. $sellby = $pdluo->sellby;
  343. } else {
  344. setEventMessages($pdluo->error, $pdluo->errors, 'errors');
  345. $error++;
  346. }
  347. } else {
  348. $srcwarehouseid = GETPOST('id_entrepot', 'int');
  349. $batch = $batchnumber;
  350. $eatby = $d_eatby;
  351. $sellby = $d_sellby;
  352. }
  353. $nbpiece = price2num(GETPOST("nbpiece", 'alphanohtml'));
  354. if (!$error) {
  355. // Remove stock
  356. $result1 = $object->correct_stock_batch(
  357. $user,
  358. $srcwarehouseid,
  359. $nbpiece,
  360. 1,
  361. GETPOST("label", 'alphanohtml'),
  362. $pricesrc,
  363. $eatby,
  364. $sellby,
  365. $batch,
  366. GETPOST('inventorycode', 'alphanohtml')
  367. );
  368. if ($result1 < 0) {
  369. $error++;
  370. }
  371. }
  372. if (!$error) {
  373. // Add stock
  374. $result2 = $object->correct_stock_batch(
  375. $user,
  376. GETPOST("id_entrepot_destination", 'int'),
  377. $nbpiece,
  378. 0,
  379. GETPOST("label", 'alphanohtml'),
  380. $pricedest,
  381. $eatby,
  382. $sellby,
  383. $batch,
  384. GETPOST('inventorycode', 'alphanohtml')
  385. );
  386. if ($result2 < 0) {
  387. $error++;
  388. }
  389. }
  390. } else {
  391. if (!$error) {
  392. // Remove stock
  393. $result1 = $object->correct_stock(
  394. $user,
  395. GETPOST("id_entrepot", 'int'),
  396. $nbpiece,
  397. 1,
  398. GETPOST("label", 'alphanohtml'),
  399. $pricesrc,
  400. GETPOST('inventorycode', 'alphanohtml')
  401. );
  402. if ($result1 < 0) {
  403. $error++;
  404. }
  405. }
  406. if (!$error) {
  407. // Add stock
  408. $result2 = $object->correct_stock(
  409. $user,
  410. GETPOST("id_entrepot_destination", 'int'),
  411. $nbpiece,
  412. 0,
  413. GETPOST("label", 'alphanohtml'),
  414. $pricedest,
  415. GETPOST('inventorycode', 'alphanohtml')
  416. );
  417. if ($result2 < 0) {
  418. $error++;
  419. }
  420. }
  421. }
  422. if (!$error && $result1 >= 0 && $result2 >= 0) {
  423. $db->commit();
  424. if ($backtopage) {
  425. header("Location: ".$backtopage);
  426. exit;
  427. } else {
  428. header("Location: product.php?id=".$object->id);
  429. exit;
  430. }
  431. } else {
  432. setEventMessages($object->error, $object->errors, 'errors');
  433. $db->rollback();
  434. $action = 'transfert';
  435. }
  436. }
  437. }
  438. }
  439. // Update batch information
  440. if ($action == 'updateline' && GETPOST('save') == $langs->trans("Save")) {
  441. $pdluo = new Productbatch($db);
  442. $result = $pdluo->fetch(GETPOST('pdluoid', 'int'));
  443. if ($result > 0) {
  444. if ($pdluo->id) {
  445. if ((!GETPOST("sellby")) && (!GETPOST("eatby")) && (!$batchnumber)) {
  446. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("atleast1batchfield")), null, 'errors');
  447. } else {
  448. $d_eatby = dol_mktime(0, 0, 0, GETPOST('eatbymonth', 'int'), GETPOST('eatbyday', 'int'), GETPOST('eatbyyear', 'int'));
  449. $d_sellby = dol_mktime(0, 0, 0, GETPOST('sellbymonth', 'int'), GETPOST('sellbyday', 'int'), GETPOST('sellbyyear', 'int'));
  450. $pdluo->batch = $batchnumber;
  451. $pdluo->eatby = $d_eatby;
  452. $pdluo->sellby = $d_sellby;
  453. $result = $pdluo->update($user);
  454. if ($result < 0) {
  455. setEventMessages($pdluo->error, $pdluo->errors, 'errors');
  456. }
  457. }
  458. } else {
  459. setEventMessages($langs->trans('BatchInformationNotfound'), null, 'errors');
  460. }
  461. } else {
  462. setEventMessages($pdluo->error, null, 'errors');
  463. }
  464. header("Location: product.php?id=".$id);
  465. exit;
  466. }
  467. /*
  468. * View
  469. */
  470. $form = new Form($db);
  471. $formproduct = new FormProduct($db);
  472. if (isModEnabled('project')) {
  473. $formproject = new FormProjets($db);
  474. }
  475. if ($id > 0 || $ref) {
  476. $object = new Product($db);
  477. $result = $object->fetch($id, $ref);
  478. $variants = $object->hasVariants();
  479. $object->load_stock();
  480. $title = $langs->trans('ProductServiceCard');
  481. $helpurl = '';
  482. $shortlabel = dol_trunc($object->label, 16);
  483. if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
  484. $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Stock');
  485. $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  486. }
  487. if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
  488. $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Stock');
  489. $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  490. }
  491. llxHeader('', $title, $helpurl);
  492. if (!empty($conf->use_javascript_ajax)) {
  493. ?>
  494. <script type="text/javascript">
  495. $(document).ready(function() {
  496. $(".collapse_batch").click(function() {
  497. console.log("We click on collapse_batch");
  498. var id_entrepot = $(this).attr('id').replace('ent', '');
  499. if($(this).text().indexOf('+') > 0) {
  500. $(".batch_warehouse" + id_entrepot).show();
  501. $(this).html('(-)');
  502. jQuery("#show_all").hide();
  503. jQuery("#hide_all").show();
  504. }
  505. else {
  506. $(".batch_warehouse" + id_entrepot).hide();
  507. $(this).html('(+)');
  508. }
  509. return false;
  510. });
  511. $("#show_all").click(function() {
  512. console.log("We click on show_all");
  513. $("[class^=batch_warehouse]").show();
  514. $("[class^=collapse_batch]").html('(-)');
  515. jQuery("#show_all").hide();
  516. jQuery("#hide_all").show();
  517. return false;
  518. });
  519. $("#hide_all").click(function() {
  520. console.log("We click on hide_all");
  521. $("[class^=batch_warehouse]").hide();
  522. $("[class^=collapse_batch]").html('(+)');
  523. jQuery("#hide_all").hide();
  524. jQuery("#show_all").show();
  525. return false;
  526. });
  527. });
  528. </script>
  529. <?php
  530. }
  531. if ($result > 0) {
  532. $head = product_prepare_head($object);
  533. $titre = $langs->trans("CardProduct".$object->type);
  534. $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
  535. print dol_get_fiche_head($head, 'stock', $titre, -1, $picto);
  536. dol_htmloutput_events();
  537. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  538. $shownav = 1;
  539. if ($user->socid && !in_array('stock', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
  540. $shownav = 0;
  541. }
  542. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  543. if (!$variants) {
  544. print '<div class="fichecenter">';
  545. print '<div class="fichehalfleft">';
  546. print '<div class="underbanner clearboth"></div>';
  547. print '<table class="border tableforfield centpercent">';
  548. // Type
  549. if (isModEnabled("product") && isModEnabled("service")) {
  550. $typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
  551. print '<tr><td class="">';
  552. print (!getDolGlobalString('PRODUCT_DENY_CHANGE_PRODUCT_TYPE')) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, 0, $typeformat) : $langs->trans('Type');
  553. print '</td><td>';
  554. print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
  555. print '</td></tr>';
  556. }
  557. if (isModEnabled('productbatch')) {
  558. print '<tr><td class="">'.$langs->trans("ManageLotSerial").'</td><td>';
  559. print $object->getLibStatut(0, 2);
  560. print '</td></tr>';
  561. }
  562. // Cost price. Can be used for margin module for option "calculate margin on explicit cost price
  563. print '<tr><td>';
  564. $textdesc = $langs->trans("CostPriceDescription");
  565. $textdesc .= "<br>".$langs->trans("CostPriceUsage");
  566. $text = $form->textwithpicto($langs->trans("CostPrice"), $textdesc, 1, 'help', '');
  567. if (!$usercancreadprice) {
  568. print $form->editfieldkey($text, 'cost_price', '', $object, 0, 'amount:6');
  569. print '</td><td>';
  570. print $form->editfieldval($text, 'cost_price', '', $object, 0, 'amount:6');
  571. } else {
  572. print $form->editfieldkey($text, 'cost_price', $object->cost_price, $object, $usercancreate, 'amount:6');
  573. print '</td><td>';
  574. print $form->editfieldval($text, 'cost_price', $object->cost_price, $object, $usercancreate, 'amount:6');
  575. }
  576. print '</td></tr>';
  577. // AWP
  578. print '<tr><td class="titlefield">';
  579. print $form->textwithpicto($langs->trans("AverageUnitPricePMPShort"), $langs->trans("AverageUnitPricePMPDesc"));
  580. print '</td>';
  581. print '<td>';
  582. if ($object->pmp > 0 && $usercancreadprice) {
  583. print price($object->pmp).' '.$langs->trans("HT");
  584. }
  585. print '</td>';
  586. print '</tr>';
  587. // Minimum Price
  588. print '<tr><td>'.$langs->trans("BuyingPriceMin").'</td>';
  589. print '<td>';
  590. $product_fourn = new ProductFournisseur($db);
  591. if ($product_fourn->find_min_price_product_fournisseur($object->id) > 0) {
  592. if ($product_fourn->product_fourn_price_id > 0 && $usercancreadprice) {
  593. print $product_fourn->display_price_product_fournisseur();
  594. } else {
  595. print $langs->trans("NotDefined");
  596. }
  597. }
  598. print '</td></tr>';
  599. if (!getDolGlobalString('PRODUIT_MULTIPRICES')) {
  600. // Price
  601. print '<tr><td>'.$langs->trans("SellingPrice").'</td><td>';
  602. if ($usercancreadprice) {
  603. if ($object->price_base_type == 'TTC') {
  604. print price($object->price_ttc).' '.$langs->trans($object->price_base_type);
  605. } else {
  606. print price($object->price).' '.$langs->trans($object->price_base_type);
  607. }
  608. }
  609. print '</td></tr>';
  610. // Price minimum
  611. print '<tr><td>'.$langs->trans("MinPrice").'</td><td>';
  612. if ($usercancreadprice) {
  613. if ($object->price_base_type == 'TTC') {
  614. print price($object->price_min_ttc).' '.$langs->trans($object->price_base_type);
  615. } else {
  616. print price($object->price_min).' '.$langs->trans($object->price_base_type);
  617. }
  618. }
  619. print '</td></tr>';
  620. } else {
  621. // Price
  622. print '<tr><td>'.$langs->trans("SellingPrice").'</td><td>';
  623. print '<span class="opacitymedium">'.$langs->trans("Variable").'</span>';
  624. print '</td></tr>';
  625. // Price minimum
  626. print '<tr><td>'.$langs->trans("MinPrice").'</td><td>';
  627. print '<span class="opacitymedium">'.$langs->trans("Variable").'</span>';
  628. print '</td></tr>';
  629. }
  630. // Hook formObject
  631. $parameters = array();
  632. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  633. print $hookmanager->resPrint;
  634. print '</table>';
  635. print '</div>';
  636. print '<div class="fichehalfright"><div class="underbanner clearboth"></div>';
  637. print '<table class="border tableforfield centpercent">';
  638. // Stock alert threshold
  639. print '<tr><td>'.$form->editfieldkey($form->textwithpicto($langs->trans("StockLimit"), $langs->trans("StockLimitDesc"), 1), 'seuil_stock_alerte', $object->seuil_stock_alerte, $object, $user->hasRight('produit', 'creer')).'</td><td>';
  640. print $form->editfieldval("StockLimit", 'seuil_stock_alerte', $object->seuil_stock_alerte, $object, $user->hasRight('produit', 'creer'), 'string');
  641. print '</td></tr>';
  642. // Desired stock
  643. print '<tr><td>'.$form->editfieldkey($form->textwithpicto($langs->trans("DesiredStock"), $langs->trans("DesiredStockDesc"), 1), 'desiredstock', $object->desiredstock, $object, $user->hasRight('produit', 'creer'));
  644. print '</td><td>';
  645. print $form->editfieldval("DesiredStock", 'desiredstock', $object->desiredstock, $object, $user->hasRight('produit', 'creer'), 'string');
  646. print '</td></tr>';
  647. // Real stock
  648. $text_stock_options = $langs->trans("RealStockDesc").'<br>';
  649. $text_stock_options .= $langs->trans("RealStockWillAutomaticallyWhen").'<br>';
  650. $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT') || getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT_CLOSE') ? '- '.$langs->trans("DeStockOnShipment").'<br>' : '');
  651. $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_VALIDATE_ORDER') ? '- '.$langs->trans("DeStockOnValidateOrder").'<br>' : '');
  652. $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_BILL') ? '- '.$langs->trans("DeStockOnBill").'<br>' : '');
  653. $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL') ? '- '.$langs->trans("ReStockOnBill").'<br>' : '');
  654. $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER') ? '- '.$langs->trans("ReStockOnValidateOrder").'<br>' : '');
  655. $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER') ? '- '.$langs->trans("ReStockOnDispatchOrder").'<br>' : '');
  656. $text_stock_options .= (getDolGlobalString('STOCK_CALCULATE_ON_RECEPTION') || getDolGlobalString('STOCK_CALCULATE_ON_RECEPTION_CLOSE') ? '- '.$langs->trans("StockOnReception").'<br>' : '');
  657. $parameters = array();
  658. $reshook = $hookmanager->executeHooks('physicalStockTextStockOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  659. if ($reshook > 0) {
  660. $text_stock_options = $hookmanager->resPrint;
  661. } elseif ($reshook == 0) {
  662. $text_stock_options .= $hookmanager->resPrint;
  663. } else {
  664. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  665. }
  666. print '<tr><td>';
  667. print $form->textwithpicto($langs->trans("PhysicalStock"), $text_stock_options, 1);
  668. print '</td>';
  669. print '<td>'.price2num($object->stock_reel, 'MS');
  670. if ($object->seuil_stock_alerte != '' && ($object->stock_reel < $object->seuil_stock_alerte)) {
  671. print ' '.img_warning($langs->trans("StockLowerThanLimit", $object->seuil_stock_alerte));
  672. }
  673. print ' &nbsp; &nbsp;<a href="'.DOL_URL_ROOT.'/product/stock/stockatdate.php?productid='.$object->id.'">'.$langs->trans("StockAtDate").'</a>';
  674. print '</td>';
  675. print '</tr>';
  676. $stocktheo = price2num($object->stock_theorique, 'MS');
  677. $found = 0;
  678. $helpondiff = '<strong>'.$langs->trans("StockDiffPhysicTeoric").':</strong><br>';
  679. // Number of sales orders running
  680. if (isModEnabled('commande')) {
  681. if ($found) {
  682. $helpondiff .= '<br>';
  683. } else {
  684. $found = 1;
  685. }
  686. $helpondiff .= $langs->trans("ProductQtyInCustomersOrdersRunning").': '.$object->stats_commande['qty'];
  687. $result = $object->load_stats_commande(0, '0', 1);
  688. if ($result < 0) {
  689. dol_print_error($db, $object->error);
  690. }
  691. $helpondiff .= ' <span class="opacitymedium">('.$langs->trans("ProductQtyInDraft").': '.$object->stats_commande['qty'].')</span>';
  692. }
  693. // Number of product from sales order already sent (partial shipping)
  694. if (isModEnabled("expedition")) {
  695. require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
  696. $filterShipmentStatus = '';
  697. if (getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT')) {
  698. $filterShipmentStatus = Expedition::STATUS_VALIDATED.','.Expedition::STATUS_CLOSED;
  699. } elseif (getDolGlobalString('STOCK_CALCULATE_ON_SHIPMENT_CLOSE')) {
  700. $filterShipmentStatus = Expedition::STATUS_CLOSED;
  701. }
  702. if ($found) {
  703. $helpondiff .= '<br>';
  704. } else {
  705. $found = 1;
  706. }
  707. $result = $object->load_stats_sending(0, '2', 1, $filterShipmentStatus);
  708. $helpondiff .= $langs->trans("ProductQtyInShipmentAlreadySent").': '.$object->stats_expedition['qty'];
  709. }
  710. // Number of supplier order running
  711. if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
  712. if ($found) {
  713. $helpondiff .= '<br>';
  714. } else {
  715. $found = 1;
  716. }
  717. $result = $object->load_stats_commande_fournisseur(0, '3,4', 1);
  718. $helpondiff .= $langs->trans("ProductQtyInSuppliersOrdersRunning").': '.$object->stats_commande_fournisseur['qty'];
  719. $result = $object->load_stats_commande_fournisseur(0, '0,1,2', 1);
  720. if ($result < 0) {
  721. dol_print_error($db, $object->error);
  722. }
  723. $helpondiff .= ' <span class="opacitymedium">('.$langs->trans("ProductQtyInDraftOrWaitingApproved").': '.$object->stats_commande_fournisseur['qty'].')</span>';
  724. }
  725. // Number of product from supplier order already received (partial receipt)
  726. if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
  727. if ($found) {
  728. $helpondiff .= '<br>';
  729. } else {
  730. $found = 1;
  731. }
  732. $helpondiff .= $langs->trans("ProductQtyInSuppliersShipmentAlreadyRecevied").': '.$object->stats_reception['qty'];
  733. }
  734. // Number of product in production
  735. if (isModEnabled('mrp')) {
  736. if ($found) {
  737. $helpondiff .= '<br>';
  738. } else {
  739. $found = 1;
  740. }
  741. $helpondiff .= $langs->trans("ProductQtyToConsumeByMO").': '.$object->stats_mrptoconsume['qty'].'<br>';
  742. $helpondiff .= $langs->trans("ProductQtyToProduceByMO").': '.$object->stats_mrptoproduce['qty'];
  743. }
  744. $parameters = array('found' => &$found, 'id' => $object->id, 'includedraftpoforvirtual' => null);
  745. $reshook = $hookmanager->executeHooks('virtualStockHelpOnDiff', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  746. if ($reshook > 0) {
  747. $helpondiff = $hookmanager->resPrint;
  748. } elseif ($reshook == 0) {
  749. $helpondiff .= $hookmanager->resPrint;
  750. } else {
  751. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  752. }
  753. // Calculating a theorical value
  754. print '<tr><td>';
  755. print $form->textwithpicto($langs->trans("VirtualStock"), $langs->trans("VirtualStockDesc"));
  756. print '</td>';
  757. print "<td>";
  758. //print (empty($stocktheo)?0:$stocktheo);
  759. print $form->textwithpicto((empty($stocktheo) ? 0 : $stocktheo), $helpondiff);
  760. if ($object->seuil_stock_alerte != '' && ($object->stock_theorique < $object->seuil_stock_alerte)) {
  761. print ' '.img_warning($langs->trans("StockLowerThanLimit", $object->seuil_stock_alerte));
  762. }
  763. print ' &nbsp; &nbsp;<a href="'.DOL_URL_ROOT.'/product/stock/stockatdate.php?mode=future&productid='.$object->id.'">'.$langs->trans("VirtualStockAtDate").'</a>';
  764. print '</td>';
  765. print '</tr>';
  766. // Last movement
  767. if ($user->hasRight('stock', 'mouvement', 'lire')) {
  768. $sql = "SELECT max(m.datem) as datem";
  769. $sql .= " FROM ".MAIN_DB_PREFIX."stock_mouvement as m";
  770. $sql .= " WHERE m.fk_product = ".((int) $object->id);
  771. $resqlbis = $db->query($sql);
  772. if ($resqlbis) {
  773. $obj = $db->fetch_object($resqlbis);
  774. $lastmovementdate = $db->jdate($obj->datem);
  775. } else {
  776. dol_print_error($db);
  777. }
  778. print '<tr><td class="tdtop">'.$langs->trans("LastMovement").'</td><td>';
  779. if ($lastmovementdate) {
  780. print dol_print_date($lastmovementdate, 'dayhour').' ';
  781. print ' &nbsp; &nbsp; ';
  782. print img_picto($langs->trans("StockMovement"), 'movement', 'class="pictofixedwidth"');
  783. print '<a href="'.DOL_URL_ROOT.'/product/stock/movement_list.php?idproduct='.$object->id.'">'.$langs->trans("FullList").'</a>';
  784. } else {
  785. print img_picto($langs->trans("StockMovement"), 'movement', 'class="pictofixedwidth"');
  786. print '<a href="'.DOL_URL_ROOT.'/product/stock/movement_list.php?idproduct='.$object->id.'">'.$langs->trans("None").'</a>';
  787. }
  788. print "</td></tr>";
  789. }
  790. print "</table>";
  791. print '</div>';
  792. print '</div>';
  793. print '<div class="clearboth"></div>';
  794. }
  795. print dol_get_fiche_end();
  796. }
  797. // Correct stock
  798. if ($action == "correction") {
  799. include DOL_DOCUMENT_ROOT.'/product/stock/tpl/stockcorrection.tpl.php';
  800. print '<br><br>';
  801. }
  802. // Transfer of units
  803. if ($action == "transfert") {
  804. include DOL_DOCUMENT_ROOT.'/product/stock/tpl/stocktransfer.tpl.php';
  805. print '<br><br>';
  806. }
  807. } else {
  808. dol_print_error();
  809. }
  810. // Actions buttons
  811. $parameters = array();
  812. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  813. if (empty($reshook)) {
  814. if (empty($action) && $object->id) {
  815. print "<div class=\"tabsAction\">\n";
  816. if ($user->hasRight('stock', 'mouvement', 'creer')) {
  817. if (!$variants || getDolGlobalString('VARIANT_ALLOW_STOCK_MOVEMENT_ON_VARIANT_PARENT')) {
  818. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=transfert">'.$langs->trans("TransferStock").'</a>';
  819. } else {
  820. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("ActionAvailableOnVariantProductOnly").'">'.$langs->trans("TransferStock").'</a>';
  821. }
  822. } else {
  823. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("NotEnoughPermissions").'">'.$langs->trans("CorrectStock").'</a>';
  824. }
  825. if ($user->hasRight('stock', 'mouvement', 'creer')) {
  826. if (!$variants || getDolGlobalString('VARIANT_ALLOW_STOCK_MOVEMENT_ON_VARIANT_PARENT')) {
  827. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=correction">'.$langs->trans("CorrectStock").'</a>';
  828. } else {
  829. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("ActionAvailableOnVariantProductOnly").'">'.$langs->trans("CorrectStock").'</a>';
  830. }
  831. } else {
  832. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("NotEnoughPermissions").'">'.$langs->trans("CorrectStock").'</a>';
  833. }
  834. print '</div>';
  835. }
  836. }
  837. if (!$variants || getDolGlobalString('VARIANT_ALLOW_STOCK_MOVEMENT_ON_VARIANT_PARENT')) {
  838. /*
  839. * Stock detail (by warehouse). May go down into batch details.
  840. */
  841. print '<div class="div-table-responsive">';
  842. print '<table class="noborder centpercent">';
  843. print '<tr class="liste_titre">';
  844. print '<td colspan="4">'.$langs->trans("Warehouse").'</td>';
  845. print '<td class="right">'.$langs->trans("NumberOfUnit").'</td>';
  846. print '<td class="right">'.$form->textwithpicto($langs->trans("AverageUnitPricePMPShort"), $langs->trans("AverageUnitPricePMPDesc")).'</td>';
  847. print '<td class="right">'.$langs->trans("EstimatedStockValueShort").'</td>';
  848. print '<td class="right">'.$langs->trans("SellPriceMin").'</td>';
  849. print '<td class="right">'.$langs->trans("EstimatedStockValueSellShort").'</td>';
  850. print '<td></td>';
  851. print '<td></td>';
  852. print '</tr>';
  853. if ((isModEnabled('productbatch')) && $object->hasbatch()) {
  854. $colspan = 3;
  855. print '<tr class="liste_titre"><td class="minwidth200">';
  856. if (!empty($conf->use_javascript_ajax)) {
  857. print '<a id="show_all" href="#" class="hideobject">'.img_picto('', 'folder-open', 'class="paddingright"').$langs->trans("ShowAllLots").'</a>';
  858. //print ' &nbsp; ';
  859. print '<a id="hide_all" href="#">'.img_picto('', 'folder', 'class="paddingright"').$langs->trans("HideLots").'</a>';
  860. //print '&nbsp;'.$form->textwithpicto('', $langs->trans('CollapseBatchDetailHelp'), 1, 'help', '');
  861. }
  862. print '</td>';
  863. print '<td class="right">'.$langs->trans("batch_number").'</td>';
  864. if (!getDolGlobalString('PRODUCT_DISABLE_SELLBY')) {
  865. $colspan--;
  866. print '<td class="center width100">'.$langs->trans("SellByDate").'</td>';
  867. }
  868. if (!getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
  869. $colspan--;
  870. print '<td class="center width100">'.$langs->trans("EatByDate").'</td>';
  871. }
  872. print '<td colspan="'.$colspan.'"></td>';
  873. print '<td></td>';
  874. print '<td></td>';
  875. print '<td></td>';
  876. print '<td></td>';
  877. print '<td></td>';
  878. print '<td></td>';
  879. print '</tr>';
  880. }
  881. $sql = "SELECT e.rowid, e.ref, e.lieu, e.fk_parent, e.statut as status, ps.reel, ps.rowid as product_stock_id, p.pmp";
  882. $sql .= " FROM ".MAIN_DB_PREFIX."entrepot as e,";
  883. $sql .= " ".MAIN_DB_PREFIX."product_stock as ps";
  884. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = ps.fk_product";
  885. $sql .= " WHERE ps.reel != 0";
  886. $sql .= " AND ps.fk_entrepot = e.rowid";
  887. $sql .= " AND e.entity IN (".getEntity('stock').")";
  888. $sql .= " AND ps.fk_product = ".((int) $object->id);
  889. $sql .= " ORDER BY e.ref";
  890. $entrepotstatic = new Entrepot($db);
  891. $product_lot_static = new Productlot($db);
  892. $num = 0;
  893. $total = 0;
  894. $totalvalue = $totalvaluesell = 0;
  895. $totalwithpmp = 0;
  896. $resql = $db->query($sql);
  897. if ($resql) {
  898. $num = $db->num_rows($resql);
  899. $total = $totalwithpmp;
  900. $i = 0;
  901. $var = false;
  902. while ($i < $num) {
  903. $obj = $db->fetch_object($resql);
  904. $entrepotstatic->id = $obj->rowid;
  905. $entrepotstatic->ref = $obj->ref;
  906. $entrepotstatic->label = $obj->ref;
  907. $entrepotstatic->lieu = $obj->lieu;
  908. $entrepotstatic->fk_parent = $obj->fk_parent;
  909. $entrepotstatic->statut = $obj->status;
  910. $entrepotstatic->status = $obj->status;
  911. $stock_real = price2num($obj->reel, 'MS');
  912. print '<tr class="oddeven">';
  913. // Warehouse
  914. print '<td colspan="4">';
  915. print $entrepotstatic->getNomUrl(1);
  916. if (!empty($conf->use_javascript_ajax) && isModEnabled('productbatch') && $object->hasbatch()) {
  917. print '<a class="collapse_batch marginleftonly" id="ent' . $entrepotstatic->id . '" href="#">';
  918. print(!getDolGlobalString('STOCK_SHOW_ALL_BATCH_BY_DEFAULT') ? '(+)' : '(-)');
  919. print '</a>';
  920. }
  921. print '</td>';
  922. print '<td class="right">'.$stock_real.($stock_real < 0 ? ' '.img_warning() : '').'</td>';
  923. // PMP
  924. print '<td class="right nowraponall">'.(price2num($object->pmp) ? price2num($object->pmp, 'MU') : '').'</td>';
  925. // Value purchase
  926. if ($usercancreadprice) {
  927. print '<td class="right amount nowraponall">'.(price2num($object->pmp) ? price(price2num($object->pmp * $obj->reel, 'MT')) : '').'</td>';
  928. } else {
  929. print '<td class="right amount nowraponall"></td>';
  930. }
  931. // Sell price
  932. $minsellprice = null;
  933. $maxsellprice = null;
  934. print '<td class="right nowraponall">';
  935. if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
  936. foreach ($object->multiprices as $priceforlevel) {
  937. if (is_numeric($priceforlevel)) {
  938. if (is_null($maxsellprice) || $priceforlevel > $maxsellprice) {
  939. $maxsellprice = $priceforlevel;
  940. }
  941. if (is_null($minsellprice) || $priceforlevel < $minsellprice) {
  942. $minsellprice = $priceforlevel;
  943. }
  944. }
  945. }
  946. print '<span class="valignmiddle">';
  947. if ($usercancreadprice) {
  948. if ($minsellprice != $maxsellprice) {
  949. print price(price2num($minsellprice, 'MU'), 1).' - '.price(price2num($maxsellprice, 'MU'), 1);
  950. } else {
  951. print price(price2num($minsellprice, 'MU'), 1);
  952. }
  953. }
  954. print '</span>';
  955. print $form->textwithpicto('', $langs->trans("Variable"));
  956. } elseif ($usercancreadprice) {
  957. print price(price2num($object->price, 'MU'), 1);
  958. }
  959. print '</td>';
  960. // Value sell
  961. print '<td class="right amount nowraponall">';
  962. if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
  963. print '<span class="valignmiddle">';
  964. if ($usercancreadprice) {
  965. if ($minsellprice != $maxsellprice) {
  966. print price(price2num($minsellprice * $obj->reel, 'MT'), 1).' - '.price(price2num($maxsellprice * $obj->reel, 'MT'), 1);
  967. } else {
  968. print price(price2num($minsellprice * $obj->reel, 'MT'), 1);
  969. }
  970. }
  971. print '</span>';
  972. print $form->textwithpicto('', $langs->trans("Variable"));
  973. } else {
  974. if ($usercancreadprice) {
  975. print price(price2num($object->price * $obj->reel, 'MT'), 1);
  976. }
  977. }
  978. print '</td>';
  979. print '<td></td>';
  980. print '<td></td>';
  981. print '</tr>';
  982. $total += $obj->reel;
  983. if (price2num($object->pmp)) {
  984. $totalwithpmp += $obj->reel;
  985. }
  986. $totalvalue = $totalvalue + ($object->pmp * $obj->reel);
  987. $totalvaluesell = $totalvaluesell + ($object->price * $obj->reel);
  988. // Batch Detail
  989. if ((isModEnabled('productbatch')) && $object->hasbatch()) {
  990. $details = Productbatch::findAll($db, $obj->product_stock_id, 0, $object->id);
  991. if ($details < 0) {
  992. dol_print_error($db);
  993. }
  994. foreach ($details as $pdluo) {
  995. $product_lot_static->id = $pdluo->lotid;
  996. $product_lot_static->batch = $pdluo->batch;
  997. $product_lot_static->eatby = $pdluo->eatby;
  998. $product_lot_static->sellby = $pdluo->sellby;
  999. if ($action == 'editline' && GETPOST('lineid', 'int') == $pdluo->id) { //Current line edit
  1000. print "\n".'<tr>';
  1001. print '<td colspan="9">';
  1002. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1003. print '<input type="hidden" name="token" value="'.newToken().'">';
  1004. print '<input type="hidden" name="pdluoid" value="'.$pdluo->id.'"><input type="hidden" name="action" value="updateline"><input type="hidden" name="id" value="'.$id.'"><table class="noborder centpercent"><tr><td width="10%"></td>';
  1005. print '<td class="right" width="10%"><input type="text" name="batch_number" value="'.$pdluo->batch.'"></td>';
  1006. if (!getDolGlobalString('PRODUCT_DISABLE_SELLBY')) {
  1007. print '<td class="center" width="10%">';
  1008. print $form->selectDate($pdluo->sellby, 'sellby', '', '', 1, '', 1, 0);
  1009. print '</td>';
  1010. }
  1011. if (!getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
  1012. print '<td class="center" width="10%">';
  1013. print $form->selectDate($pdluo->eatby, 'eatby', '', '', 1, '', 1, 0);
  1014. print '</td>';
  1015. }
  1016. print '<td class="right" colspan="3">'.$pdluo->qty.($pdluo->qty < 0 ? ' '.img_warning() : '').'</td>';
  1017. print '<td colspan="4"><input type="submit" class="button button-save" id="savelinebutton marginbottomonly" name="save" value="'.$langs->trans("Save").'">';
  1018. print '<input type="submit" class="button button-cancel" id="cancellinebutton" name="Cancel" value="'.$langs->trans("Cancel").'"></td></tr>';
  1019. print '</table>';
  1020. print '</form>';
  1021. print '</td>';
  1022. print '<td></td>';
  1023. print '<td></td>';
  1024. print '</tr>';
  1025. } else {
  1026. print "\n".'<tr style="display:'.(!getDolGlobalString('STOCK_SHOW_ALL_BATCH_BY_DEFAULT') ? 'none' : 'visible').';" class="batch_warehouse'.$entrepotstatic->id.'"><td class="left">';
  1027. print '</td>';
  1028. print '<td class="right nowraponall">';
  1029. if ($product_lot_static->id > 0) {
  1030. print $product_lot_static->getNomUrl(1);
  1031. } else {
  1032. print $product_lot_static->getNomUrl(1, 'nolink');
  1033. }
  1034. print '</td>';
  1035. $colspan = 3;
  1036. if (!getDolGlobalString('PRODUCT_DISABLE_SELLBY')) {
  1037. $colspan--;
  1038. print '<td class="center">'.dol_print_date($pdluo->sellby, 'day').'</td>';
  1039. }
  1040. if (!getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
  1041. $colspan--;
  1042. print '<td class="center">'.dol_print_date($pdluo->eatby, 'day').'</td>';
  1043. }
  1044. print '<td class="right" colspan="'.$colspan.'">'.$pdluo->qty.($pdluo->qty < 0 ? ' '.img_warning() : (($pdluo->qty > 1 && $object->status_batch == 2) ? ' '.img_warning($langs->trans('IlligalQtyForSerialNumbers')) : '')).'</td>';
  1045. print '<td colspan="4"></td>';
  1046. print '<td class="center tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("TransferStock")).'">';
  1047. if ($entrepotstatic->status != $entrepotstatic::STATUS_CLOSED) {
  1048. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;id_entrepot='.$entrepotstatic->id.'&amp;action=transfert&amp;pdluoid='.$pdluo->id.'">';
  1049. print img_picto($langs->trans("TransferStock"), 'add', 'class="hideonsmartphone paddingright" style="color: #a69944"');
  1050. print $langs->trans("TransferStock");
  1051. print '</a>';
  1052. // Disabled, because edition of stock content must use the "Correct stock menu".
  1053. // Do not use this, or data will be wrong (bad tracking of movement label, inventory code, ...
  1054. //print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=editline&token='.newToken().'&lineid='.$pdluo->id.'#'.$pdluo->id.'">';
  1055. //print img_edit().'</a>';
  1056. }
  1057. print '</td>';
  1058. print '<td class="center tdoverflowmax125" title="'.dol_escape_htmltag($langs->trans("CorrectStock")).'">';
  1059. if ($entrepotstatic->status != $entrepotstatic::STATUS_CLOSED) {
  1060. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;id_entrepot='.$entrepotstatic->id.'&amp;action=correction&amp;pdluoid='.$pdluo->id.'">';
  1061. print img_picto($langs->trans("CorrectStock"), 'add', 'class="hideonsmartphone paddingright" style="color: #a69944"');
  1062. print $langs->trans("CorrectStock");
  1063. print '</a>';
  1064. // Disabled, because edition of stock content must use the "Correct stock menu".
  1065. // Do not use this, or data will be wrong (bad tracking of movement label, inventory code, ...
  1066. //print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=editline&token='.newToken().'&lineid='.$pdluo->id.'#'.$pdluo->id.'">';
  1067. //print img_edit().'</a>';
  1068. }
  1069. print '</td>';
  1070. print '</tr>';
  1071. }
  1072. }
  1073. }
  1074. $i++;
  1075. }
  1076. } else {
  1077. dol_print_error($db);
  1078. }
  1079. // Total line
  1080. print '<tr class="liste_total"><td class="right liste_total" colspan="4">'.$langs->trans("Total").':</td>';
  1081. print '<td class="liste_total right">'.price2num($total, 'MS').'</td>';
  1082. print '<td class="liste_total right">';
  1083. if ($usercancreadprice) {
  1084. print($totalwithpmp ? price(price2num($totalvalue / $totalwithpmp, 'MU')) : '&nbsp;'); // This value may have rounding errors
  1085. }
  1086. print '</td>';
  1087. // Value purchase
  1088. print '<td class="liste_total right">';
  1089. if ($usercancreadprice) {
  1090. print $totalvalue ? price(price2num($totalvalue, 'MT'), 1) : '&nbsp;';
  1091. }
  1092. print '</td>';
  1093. print '<td class="liste_total right">';
  1094. if ($num) {
  1095. if ($total) {
  1096. print '<span class="valignmiddle">';
  1097. if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
  1098. print $form->textwithpicto('', $langs->trans("Variable"));
  1099. } elseif ($usercancreadprice) {
  1100. print price($totalvaluesell / $total, 1);
  1101. }
  1102. print '</span>';
  1103. }
  1104. }
  1105. print '</td>';
  1106. // Value to sell
  1107. print '<td class="liste_total right amount">';
  1108. if ($num) {
  1109. print '<span class="valignmiddle">';
  1110. if (!getDolGlobalString('PRODUIT_MULTIPRICES') && $usercancreadprice) {
  1111. print price(price2num($totalvaluesell, 'MT'), 1);
  1112. } else {
  1113. print $form->textwithpicto('', $langs->trans("Variable"));
  1114. }
  1115. print '</span>';
  1116. }
  1117. print '</td>';
  1118. print '<td></td>';
  1119. print '<td></td>';
  1120. print "</tr>";
  1121. print "</table>";
  1122. print '</div>';
  1123. if (getDolGlobalString('STOCK_ALLOW_ADD_LIMIT_STOCK_BY_WAREHOUSE')) {
  1124. print '<br><br>';
  1125. print load_fiche_titre($langs->trans('AddNewProductStockWarehouse'));
  1126. if ($user->hasRight('produit', 'creer')) {
  1127. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1128. print '<input type="hidden" name="token" value="'.newToken().'">';
  1129. print '<input type="hidden" name="action" value="addlimitstockwarehouse">';
  1130. print '<input type="hidden" name="id" value="'.$id.'">';
  1131. }
  1132. print '<table class="noborder centpercent">';
  1133. if ($user->hasRight('produit', 'creer')) {
  1134. print '<tr class="liste_titre"><td>'.$formproduct->selectWarehouses('', 'fk_entrepot').'</td>';
  1135. print '<td class="right"><input name="seuil_stock_alerte" type="text" placeholder="'.$langs->trans("StockLimit").'" /></td>';
  1136. print '<td class="right"><input name="desiredstock" type="text" placeholder="'.$langs->trans("DesiredStock").'" /></td>';
  1137. print '<td class="right"><input type="submit" value="'.$langs->trans("Save").'" class="button button-save" /></td>';
  1138. print '</tr>';
  1139. } else {
  1140. print '<tr class="liste_titre"><td>'.$langs->trans("Warehouse").'</td>';
  1141. print '<td class="right">'.$langs->trans("StockLimit").'</td>';
  1142. print '<td class="right">'.$langs->trans("DesiredStock").'</td>';
  1143. print '</tr>';
  1144. }
  1145. $pse = new ProductStockEntrepot($db);
  1146. $lines = $pse->fetchAll($id);
  1147. if (!empty($lines)) {
  1148. $var = false;
  1149. foreach ($lines as $line) {
  1150. $ent = new Entrepot($db);
  1151. $ent->fetch($line['fk_entrepot']);
  1152. print '<tr class="oddeven"><td>'.$ent->getNomUrl(3).'</td>';
  1153. print '<td class="right">'.$line['seuil_stock_alerte'].'</td>';
  1154. print '<td class="right">'.$line['desiredstock'].'</td>';
  1155. if ($user->hasRight('produit', 'creer')) {
  1156. print '<td class="right"><a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&fk_productstockwarehouse='.$line['id'].'&action=delete_productstockwarehouse&token='.newToken().'">'.img_delete().'</a></td>';
  1157. }
  1158. print '</tr>';
  1159. }
  1160. }
  1161. print "</table>";
  1162. if ($user->hasRight('produit', 'creer')) {
  1163. print '</form>';
  1164. }
  1165. }
  1166. } else {
  1167. // List of variants
  1168. include_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
  1169. include_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
  1170. $prodstatic = new Product($db);
  1171. $prodcomb = new ProductCombination($db);
  1172. $comb2val = new ProductCombination2ValuePair($db);
  1173. $productCombinations = $prodcomb->fetchAllByFkProductParent($object->id);
  1174. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  1175. print '<input type="hidden" name="token" value="'.newToken().'">';
  1176. print '<input type="hidden" name="action" value="massaction">';
  1177. print '<input type="hidden" name="id" value="'.$id.'">';
  1178. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  1179. // load variants
  1180. $title = $langs->trans("ProductCombinations");
  1181. print_barre_liste($title, 0, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, '', 0);
  1182. print '<div class="div-table-responsive">'; ?>
  1183. <table class="liste">
  1184. <tr class="liste_titre">
  1185. <td class="liste_titre"><?php echo $langs->trans('Product') ?></td>
  1186. <td class="liste_titre"><?php echo $langs->trans('Combination') ?></td>
  1187. <td class="liste_titre center"><?php echo $langs->trans('OnSell') ?></td>
  1188. <td class="liste_titre center"><?php echo $langs->trans('OnBuy') ?></td>
  1189. <td class="liste_titre right"><?php echo $langs->trans('Stock') ?></td>
  1190. <td class="liste_titre"></td>
  1191. </tr>
  1192. <?php
  1193. if (count($productCombinations)) {
  1194. $stock_total = 0;
  1195. foreach ($productCombinations as $currcomb) {
  1196. $prodstatic->fetch($currcomb->fk_product_child);
  1197. $prodstatic->load_stock();
  1198. $stock_total += $prodstatic->stock_reel; ?>
  1199. <tr class="oddeven">
  1200. <td><?php echo $prodstatic->getNomUrl(1) ?></td>
  1201. <td>
  1202. <?php
  1203. $productCombination2ValuePairs = $comb2val->fetchByFkCombination($currcomb->id);
  1204. $iMax = count($productCombination2ValuePairs);
  1205. for ($i = 0; $i < $iMax; $i++) {
  1206. echo dol_htmlentities($productCombination2ValuePairs[$i]);
  1207. if ($i !== ($iMax - 1)) {
  1208. echo ', ';
  1209. }
  1210. } ?>
  1211. </td>
  1212. <td style="text-align: center;"><?php echo $prodstatic->getLibStatut(2, 0) ?></td>
  1213. <td style="text-align: center;"><?php echo $prodstatic->getLibStatut(2, 1) ?></td>
  1214. <td class="right"><?php echo $prodstatic->stock_reel ?></td>
  1215. <td class="right">
  1216. <a class="paddingleft paddingright editfielda" href="<?php echo dol_buildpath('/product/stock/product.php?id='.$currcomb->fk_product_child, 2) ?>"><?php echo img_edit() ?></a>
  1217. </td>
  1218. <?php
  1219. ?>
  1220. </tr>
  1221. <?php
  1222. }
  1223. print '<tr class="liste_total">';
  1224. print '<td colspan="4" class="left">'.$langs->trans("Total").'</td>';
  1225. print '<td class="right">'.$stock_total.'</td>';
  1226. print '<td></td>';
  1227. print '</tr>';
  1228. } else {
  1229. print '<tr><td colspan="8"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  1230. } ?>
  1231. </table>
  1232. <?php
  1233. print '</div>';
  1234. print '</form>';
  1235. }
  1236. // End of page
  1237. llxFooter();
  1238. $db->close();