product.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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 (!empty($conf->project->enabled)) {
  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->rights->service->lire));
  103. $usercancreate = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->creer) || ($object->type == Product::TYPE_SERVICE && $user->rights->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' && !empty($user->rights->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' && !empty($user->rights->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' && !empty($user->rights->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' && !empty($user->rights->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 (!empty($conf->project->enabled)) {
  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 (empty($conf->global->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 (empty($conf->global->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->rights->produit->creer).'</td><td>';
  640. print $form->editfieldval("StockLimit", 'seuil_stock_alerte', $object->seuil_stock_alerte, $object, $user->rights->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->rights->produit->creer);
  644. print '</td><td>';
  645. print $form->editfieldval("DesiredStock", 'desiredstock', $object->desiredstock, $object, $user->rights->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 .= (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE) ? '- '.$langs->trans("DeStockOnShipment").'<br>' : '');
  651. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) ? '- '.$langs->trans("DeStockOnValidateOrder").'<br>' : '');
  652. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_BILL) ? '- '.$langs->trans("DeStockOnBill").'<br>' : '');
  653. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) ? '- '.$langs->trans("ReStockOnBill").'<br>' : '');
  654. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) ? '- '.$langs->trans("ReStockOnValidateOrder").'<br>' : '');
  655. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) ? '- '.$langs->trans("ReStockOnDispatchOrder").'<br>' : '');
  656. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_RECEPTION) || !empty($conf->global->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 (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)) {
  698. $filterShipmentStatus = Expedition::STATUS_VALIDATED.','.Expedition::STATUS_CLOSED;
  699. } elseif (!empty($conf->global->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("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || 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("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || 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 (!empty($user->rights->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 style="clear:both"></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->rights->stock->mouvement->creer) {
  817. if (!$variants || !empty($conf->global->VARIANT_ALLOW_STOCK_MOVEMENT_ON_VARIANT_PARENT)) {
  818. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;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->rights->stock->mouvement->creer) {
  826. if (!$variants || !empty($conf->global->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) {
  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 (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  865. $colspan--;
  866. print '<td class="center width100">'.$langs->trans("SellByDate").'</td>';
  867. }
  868. if (empty($conf->global->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 (empty($conf->global->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; $maxsellprice = null;
  933. print '<td class="right">';
  934. if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
  935. foreach ($object->multiprices as $priceforlevel) {
  936. if (is_numeric($priceforlevel)) {
  937. if (is_null($maxsellprice) || $priceforlevel > $maxsellprice) {
  938. $maxsellprice = $priceforlevel;
  939. }
  940. if (is_null($minsellprice) || $priceforlevel < $minsellprice) {
  941. $minsellprice = $priceforlevel;
  942. }
  943. }
  944. }
  945. print '<span class="valignmiddle">';
  946. if ($usercancreadprice) {
  947. if ($minsellprice != $maxsellprice) {
  948. print price(price2num($minsellprice, 'MU'), 1).' - '.price(price2num($maxsellprice, 'MU'), 1);
  949. } else {
  950. print price(price2num($minsellprice, 'MU'), 1);
  951. }
  952. }
  953. print '</span>';
  954. print $form->textwithpicto('', $langs->trans("Variable"));
  955. } elseif ($usercancreadprice) {
  956. print price(price2num($object->price, 'MU'), 1);
  957. }
  958. print '</td>';
  959. // Value sell
  960. print '<td class="right amount nowraponall">';
  961. if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
  962. print '<span class="valignmiddle">';
  963. if ($usercancreadprice) {
  964. if ($minsellprice != $maxsellprice) {
  965. print price(price2num($minsellprice * $obj->reel, 'MT'), 1).' - '.price(price2num($maxsellprice * $obj->reel, 'MT'), 1);
  966. } else {
  967. print price(price2num($minsellprice * $obj->reel, 'MT'), 1);
  968. }
  969. }
  970. print '</span>';
  971. print $form->textwithpicto('', $langs->trans("Variable"));
  972. } else {
  973. if ($usercancreadprice) {
  974. print price(price2num($object->price * $obj->reel, 'MT'), 1);
  975. }
  976. }
  977. print '</td>';
  978. print '<td></td>';
  979. print '<td></td>';
  980. print '</tr>';
  981. $total += $obj->reel;
  982. if (price2num($object->pmp)) {
  983. $totalwithpmp += $obj->reel;
  984. }
  985. $totalvalue = $totalvalue + ($object->pmp * $obj->reel);
  986. $totalvaluesell = $totalvaluesell + ($object->price * $obj->reel);
  987. // Batch Detail
  988. if ((isModEnabled('productbatch')) && $object->hasbatch()) {
  989. $details = Productbatch::findAll($db, $obj->product_stock_id, 0, $object->id);
  990. if ($details < 0) {
  991. dol_print_error($db);
  992. }
  993. foreach ($details as $pdluo) {
  994. $product_lot_static->id = $pdluo->lotid;
  995. $product_lot_static->batch = $pdluo->batch;
  996. $product_lot_static->eatby = $pdluo->eatby;
  997. $product_lot_static->sellby = $pdluo->sellby;
  998. if ($action == 'editline' && GETPOST('lineid', 'int') == $pdluo->id) { //Current line edit
  999. print "\n".'<tr>';
  1000. print '<td colspan="9">';
  1001. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1002. print '<input type="hidden" name="token" value="'.newToken().'">';
  1003. 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>';
  1004. print '<td class="right" width="10%"><input type="text" name="batch_number" value="'.$pdluo->batch.'"></td>';
  1005. if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  1006. print '<td class="center" width="10%">';
  1007. print $form->selectDate($pdluo->sellby, 'sellby', '', '', 1, '', 1, 0);
  1008. print '</td>';
  1009. }
  1010. if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  1011. print '<td class="center" width="10%">';
  1012. print $form->selectDate($pdluo->eatby, 'eatby', '', '', 1, '', 1, 0);
  1013. print '</td>';
  1014. }
  1015. print '<td class="right" colspan="3">'.$pdluo->qty.($pdluo->qty < 0 ? ' '.img_warning() : '').'</td>';
  1016. print '<td colspan="4"><input type="submit" class="button button-save" id="savelinebutton marginbottomonly" name="save" value="'.$langs->trans("Save").'">';
  1017. print '<input type="submit" class="button button-cancel" id="cancellinebutton" name="Cancel" value="'.$langs->trans("Cancel").'"></td></tr>';
  1018. print '</table>';
  1019. print '</form>';
  1020. print '</td>';
  1021. print '<td></td>';
  1022. print '<td></td>';
  1023. print '</tr>';
  1024. } else {
  1025. print "\n".'<tr style="display:'.(empty($conf->global->STOCK_SHOW_ALL_BATCH_BY_DEFAULT) ? 'none' : 'visible').';" class="batch_warehouse'.$entrepotstatic->id.'"><td class="left">';
  1026. print '</td>';
  1027. print '<td class="right nowraponall">';
  1028. print $product_lot_static->getNomUrl(1);
  1029. print '</td>';
  1030. $colspan = 3;
  1031. if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  1032. $colspan--;
  1033. print '<td class="center">'.dol_print_date($pdluo->sellby, 'day').'</td>';
  1034. }
  1035. if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  1036. $colspan--;
  1037. print '<td class="center">'.dol_print_date($pdluo->eatby, 'day').'</td>';
  1038. }
  1039. print '<td class="right" colspan="'.$colspan.'">'.$pdluo->qty.($pdluo->qty < 0 ? ' '.img_warning() : '').'</td>';
  1040. print '<td colspan="4"></td>';
  1041. print '<td class="center">';
  1042. if ($entrepotstatic->status != $entrepotstatic::STATUS_CLOSED) {
  1043. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;id_entrepot='.$entrepotstatic->id.'&amp;action=transfert&amp;pdluoid='.$pdluo->id.'">';
  1044. print img_picto($langs->trans("TransferStock"), 'add', 'class="hideonsmartphone paddingright" style="color: #a69944"');
  1045. print $langs->trans("TransferStock");
  1046. print '</a>';
  1047. // Disabled, because edition of stock content must use the "Correct stock menu".
  1048. // Do not use this, or data will be wrong (bad tracking of movement label, inventory code, ...
  1049. //print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=editline&token='.newToken().'&lineid='.$pdluo->id.'#'.$pdluo->id.'">';
  1050. //print img_edit().'</a>';
  1051. }
  1052. print '</td>';
  1053. print '<td class="center">';
  1054. if ($entrepotstatic->status != $entrepotstatic::STATUS_CLOSED) {
  1055. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;id_entrepot='.$entrepotstatic->id.'&amp;action=correction&amp;pdluoid='.$pdluo->id.'">';
  1056. print img_picto($langs->trans("CorrectStock"), 'add', 'class="hideonsmartphone paddingright" style="color: #a69944"');
  1057. print $langs->trans("CorrectStock");
  1058. print '</a>';
  1059. // Disabled, because edition of stock content must use the "Correct stock menu".
  1060. // Do not use this, or data will be wrong (bad tracking of movement label, inventory code, ...
  1061. //print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=editline&token='.newToken().'&lineid='.$pdluo->id.'#'.$pdluo->id.'">';
  1062. //print img_edit().'</a>';
  1063. }
  1064. print '</td>';
  1065. print '</tr>';
  1066. }
  1067. }
  1068. }
  1069. $i++;
  1070. }
  1071. } else {
  1072. dol_print_error($db);
  1073. }
  1074. // Total line
  1075. print '<tr class="liste_total"><td class="right liste_total" colspan="4">'.$langs->trans("Total").':</td>';
  1076. print '<td class="liste_total right">'.price2num($total, 'MS').'</td>';
  1077. print '<td class="liste_total right">';
  1078. if ($usercancreadprice) {
  1079. print ($totalwithpmp ? price(price2num($totalvalue / $totalwithpmp, 'MU')) : '&nbsp;'); // This value may have rounding errors
  1080. }
  1081. print '</td>';
  1082. // Value purchase
  1083. print '<td class="liste_total right">';
  1084. if ($usercancreadprice) {
  1085. print $totalvalue ? price(price2num($totalvalue, 'MT'), 1) : '&nbsp;';
  1086. }
  1087. print '</td>';
  1088. print '<td class="liste_total right">';
  1089. if ($num) {
  1090. if ($total) {
  1091. print '<span class="valignmiddle">';
  1092. if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
  1093. print $form->textwithpicto('', $langs->trans("Variable"));
  1094. } elseif ($usercancreadprice) {
  1095. print price($totalvaluesell / $total, 1);
  1096. }
  1097. print '</span>';
  1098. }
  1099. }
  1100. print '</td>';
  1101. // Value to sell
  1102. print '<td class="liste_total right amount">';
  1103. if ($num) {
  1104. print '<span class="valignmiddle">';
  1105. if (empty($conf->global->PRODUIT_MULTIPRICES) && $usercancreadprice) {
  1106. print price(price2num($totalvaluesell, 'MT'), 1);
  1107. } else {
  1108. print $form->textwithpicto('', $langs->trans("Variable"));
  1109. }
  1110. print '</span>';
  1111. }
  1112. print '</td>';
  1113. print '<td></td>';
  1114. print '<td></td>';
  1115. print "</tr>";
  1116. print "</table>";
  1117. print '</div>';
  1118. if (!empty($conf->global->STOCK_ALLOW_ADD_LIMIT_STOCK_BY_WAREHOUSE)) {
  1119. print '<br><br>';
  1120. print load_fiche_titre($langs->trans('AddNewProductStockWarehouse'));
  1121. if (!empty($user->rights->produit->creer)) {
  1122. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1123. print '<input type="hidden" name="token" value="'.newToken().'">';
  1124. print '<input type="hidden" name="action" value="addlimitstockwarehouse">';
  1125. print '<input type="hidden" name="id" value="'.$id.'">';
  1126. }
  1127. print '<table class="noborder centpercent">';
  1128. if (!empty($user->rights->produit->creer)) {
  1129. print '<tr class="liste_titre"><td>'.$formproduct->selectWarehouses('', 'fk_entrepot').'</td>';
  1130. print '<td class="right"><input name="seuil_stock_alerte" type="text" placeholder="'.$langs->trans("StockLimit").'" /></td>';
  1131. print '<td class="right"><input name="desiredstock" type="text" placeholder="'.$langs->trans("DesiredStock").'" /></td>';
  1132. print '<td class="right"><input type="submit" value="'.$langs->trans("Save").'" class="button button-save" /></td>';
  1133. print '</tr>';
  1134. } else {
  1135. print '<tr class="liste_titre"><td>'.$langs->trans("Warehouse").'</td>';
  1136. print '<td class="right">'.$langs->trans("StockLimit").'</td>';
  1137. print '<td class="right">'.$langs->trans("DesiredStock").'</td>';
  1138. print '</tr>';
  1139. }
  1140. $pse = new ProductStockEntrepot($db);
  1141. $lines = $pse->fetchAll($id);
  1142. if (!empty($lines)) {
  1143. $var = false;
  1144. foreach ($lines as $line) {
  1145. $ent = new Entrepot($db);
  1146. $ent->fetch($line['fk_entrepot']);
  1147. print '<tr class="oddeven"><td>'.$ent->getNomUrl(3).'</td>';
  1148. print '<td class="right">'.$line['seuil_stock_alerte'].'</td>';
  1149. print '<td class="right">'.$line['desiredstock'].'</td>';
  1150. if (!empty($user->rights->produit->creer)) {
  1151. print '<td class="right"><a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&fk_productstockwarehouse='.$line['id'].'&action=delete_productstockwarehouse&token='.newToken().'">'.img_delete().'</a></td>';
  1152. }
  1153. print '</tr>';
  1154. }
  1155. }
  1156. print "</table>";
  1157. if (!empty($user->rights->produit->creer)) {
  1158. print '</form>';
  1159. }
  1160. }
  1161. } else {
  1162. // List of variants
  1163. include_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
  1164. include_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
  1165. $prodstatic = new Product($db);
  1166. $prodcomb = new ProductCombination($db);
  1167. $comb2val = new ProductCombination2ValuePair($db);
  1168. $productCombinations = $prodcomb->fetchAllByFkProductParent($object->id);
  1169. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  1170. print '<input type="hidden" name="token" value="'.newToken().'">';
  1171. print '<input type="hidden" name="action" value="massaction">';
  1172. print '<input type="hidden" name="id" value="'.$id.'">';
  1173. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  1174. // load variants
  1175. $title = $langs->trans("ProductCombinations");
  1176. print_barre_liste($title, 0, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, '', 0);
  1177. print '<div class="div-table-responsive">';
  1178. ?>
  1179. <table class="liste">
  1180. <tr class="liste_titre">
  1181. <td class="liste_titre"><?php echo $langs->trans('Product') ?></td>
  1182. <td class="liste_titre"><?php echo $langs->trans('Combination') ?></td>
  1183. <td class="liste_titre center"><?php echo $langs->trans('OnSell') ?></td>
  1184. <td class="liste_titre center"><?php echo $langs->trans('OnBuy') ?></td>
  1185. <td class="liste_titre right"><?php echo $langs->trans('Stock') ?></td>
  1186. <td class="liste_titre"></td>
  1187. </tr>
  1188. <?php
  1189. if (count($productCombinations)) {
  1190. $stock_total = 0;
  1191. foreach ($productCombinations as $currcomb) {
  1192. $prodstatic->fetch($currcomb->fk_product_child);
  1193. $prodstatic->load_stock();
  1194. $stock_total += $prodstatic->stock_reel;
  1195. ?>
  1196. <tr class="oddeven">
  1197. <td><?php echo $prodstatic->getNomUrl(1) ?></td>
  1198. <td>
  1199. <?php
  1200. $productCombination2ValuePairs = $comb2val->fetchByFkCombination($currcomb->id);
  1201. $iMax = count($productCombination2ValuePairs);
  1202. for ($i = 0; $i < $iMax; $i++) {
  1203. echo dol_htmlentities($productCombination2ValuePairs[$i]);
  1204. if ($i !== ($iMax - 1)) {
  1205. echo ', ';
  1206. }
  1207. } ?>
  1208. </td>
  1209. <td style="text-align: center;"><?php echo $prodstatic->getLibStatut(2, 0) ?></td>
  1210. <td style="text-align: center;"><?php echo $prodstatic->getLibStatut(2, 1) ?></td>
  1211. <td class="right"><?php echo $prodstatic->stock_reel ?></td>
  1212. <td class="right">
  1213. <a class="paddingleft paddingright" href="<?php echo dol_buildpath('/product/stock/product.php?id='.$currcomb->fk_product_child, 2) ?>"><?php echo img_edit() ?></a>
  1214. </td>
  1215. <?php
  1216. ?>
  1217. </tr>
  1218. <?php
  1219. }
  1220. print '<tr class="liste_total">';
  1221. print '<td colspan="4" class="left">'.$langs->trans("Total").'</td>';
  1222. print '<td class="right">'.$stock_total.'</td>';
  1223. print '<td></td>';
  1224. print '</tr>';
  1225. } else {
  1226. print '<tr><td colspan="8"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  1227. }
  1228. ?>
  1229. </table>
  1230. <?php
  1231. print '</div>';
  1232. print '</form>';
  1233. }
  1234. // End of page
  1235. llxFooter();
  1236. $db->close();