mo_production.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. <?php
  2. /* Copyright (C) 2019-2020 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2023 Christian Humpel <christian.humpel@gmail.com>
  4. * Copyright (C) 2023 Vincent de Grandpré <vincent@de-grandpre.quebec>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file mo_production.php
  21. * \ingroup mrp
  22. * \brief Page to make production on a MO
  23. */
  24. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  35. dol_include_once('/mrp/class/mo.class.php');
  36. dol_include_once('/bom/class/bom.class.php');
  37. dol_include_once('/mrp/lib/mrp_mo.lib.php');
  38. // Load translation files required by the page
  39. $langs->loadLangs(array("mrp", "stocks", "other", "product", "productbatch"));
  40. // Get parameters
  41. $id = GETPOST('id', 'int');
  42. $ref = GETPOST('ref', 'alpha');
  43. $action = GETPOST('action', 'aZ09');
  44. $confirm = GETPOST('confirm', 'alpha');
  45. $cancel = GETPOST('cancel', 'aZ09');
  46. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'mocard'; // To manage different context of search
  47. $backtopage = GETPOST('backtopage', 'alpha');
  48. $lineid = GETPOST('lineid', 'int');
  49. $fk_movement = GETPOST('fk_movement', 'int');
  50. $fk_default_warehouse = GETPOST('fk_default_warehouse', 'int');
  51. $collapse = GETPOST('collapse', 'aZ09comma');
  52. // Initialize technical objects
  53. $object = new Mo($db);
  54. $extrafields = new ExtraFields($db);
  55. $diroutputmassaction = $conf->mrp->dir_output.'/temp/massgeneration/'.$user->id;
  56. $hookmanager->initHooks(array('mocard', 'globalcard')); // Note that conf->hooks_modules contains array
  57. // Fetch optionals attributes and labels
  58. $extrafields->fetch_name_optionals_label($object->table_element);
  59. $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
  60. // Initialize array of search criterias
  61. $search_all = GETPOST("search_all", 'alpha');
  62. $search = array();
  63. foreach ($object->fields as $key => $val) {
  64. if (GETPOST('search_'.$key, 'alpha')) {
  65. $search[$key] = GETPOST('search_'.$key, 'alpha');
  66. }
  67. }
  68. if (empty($action) && empty($id) && empty($ref)) {
  69. $action = 'view';
  70. }
  71. // Load object
  72. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  73. // Security check - Protection if external user
  74. //if ($user->socid > 0) accessforbidden();
  75. //if ($user->socid > 0) $socid = $user->socid;
  76. $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
  77. $result = restrictedArea($user, 'mrp', $object->id, 'mrp_mo', '', 'fk_soc', 'rowid', $isdraft);
  78. $permissionnote = $user->rights->mrp->write; // Used by the include of actions_setnotes.inc.php
  79. $permissiondellink = $user->rights->mrp->write; // Used by the include of actions_dellink.inc.php
  80. $permissiontoadd = $user->rights->mrp->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  81. $permissiontodelete = $user->rights->mrp->delete || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
  82. $upload_dir = $conf->mrp->multidir_output[isset($object->entity) ? $object->entity : 1];
  83. $permissiontoproduce = $permissiontoadd;
  84. $permissiontoupdatecost = $user->hasRight('bom', 'read'); // User who can define cost must have knowledge of pricing
  85. /*
  86. * Actions
  87. */
  88. $parameters = array();
  89. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  90. if ($reshook < 0) {
  91. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  92. }
  93. if (empty($reshook)) {
  94. $error = 0;
  95. $backurlforlist = dol_buildpath('/mrp/mo_list.php', 1);
  96. if (empty($backtopage) || ($cancel && empty($id))) {
  97. //var_dump($backurlforlist);exit;
  98. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  99. $backtopage = $backurlforlist;
  100. } else {
  101. $backtopage = DOL_URL_ROOT.'/mrp/mo_production.php?id='.($id > 0 ? $id : '__ID__');
  102. }
  103. }
  104. $triggermodname = 'MO_MODIFY'; // Name of trigger action code to execute when we modify record
  105. // Actions cancel, add, update, delete or clone
  106. include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
  107. // Actions when linking object each other
  108. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php';
  109. // Actions when printing a doc from card
  110. include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
  111. // Actions to send emails
  112. $triggersendname = 'MO_SENTBYMAIL';
  113. $autocopy = 'MAIN_MAIL_AUTOCOPY_MO_TO';
  114. $trackid = 'mo'.$object->id;
  115. include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
  116. // Action to move up and down lines of object
  117. //include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
  118. if ($action == 'set_thirdparty' && $permissiontoadd) {
  119. $object->setValueFrom('fk_soc', GETPOST('fk_soc', 'int'), '', '', 'date', '', $user, $triggermodname);
  120. }
  121. if ($action == 'classin' && $permissiontoadd) {
  122. $object->setProject(GETPOST('projectid', 'int'));
  123. }
  124. if ($action == 'confirm_reopen' && $permissiontoadd) {
  125. $result = $object->setStatut($object::STATUS_INPROGRESS, 0, '', 'MRP_REOPEN');
  126. }
  127. if (($action == 'confirm_addconsumeline' && GETPOST('addconsumelinebutton') && $permissiontoadd)
  128. || ($action == 'confirm_addproduceline' && GETPOST('addproducelinebutton') && $permissiontoadd)) {
  129. $moline = new MoLine($db);
  130. // Line to produce
  131. $moline->fk_mo = $object->id;
  132. $moline->qty = GETPOST('qtytoadd', 'int');
  133. $moline->fk_product = GETPOST('productidtoadd', 'int');
  134. if (GETPOST('addconsumelinebutton')) {
  135. $moline->role = 'toconsume';
  136. } else {
  137. $moline->role = 'toproduce';
  138. }
  139. $moline->origin_type = 'free'; // free consume line
  140. $moline->position = 0;
  141. // Is it a product or a service ?
  142. if (!empty($moline->fk_product)) {
  143. $tmpproduct = new Product($db);
  144. $tmpproduct->fetch($moline->fk_product);
  145. if ($tmpproduct->type == Product::TYPE_SERVICE) {
  146. $moline->fk_default_workstation = $tmpproduct->fk_default_workstation;
  147. }
  148. $moline->disable_stock_change = ($tmpproduct->type == Product::TYPE_SERVICE ? 1 : 0);
  149. }
  150. $resultline = $moline->create($user, false); // Never use triggers here
  151. if ($resultline <= 0) {
  152. $error++;
  153. setEventMessages($moline->error, $molines->errors, 'errors');
  154. }
  155. $action = '';
  156. // Redirect to refresh the tab information
  157. header("Location: ".$_SERVER["PHP_SELF"].'?id='.$object->id);
  158. exit;
  159. }
  160. if (in_array($action, array('confirm_consumeorproduce', 'confirm_consumeandproduceall')) && $permissiontoproduce) {
  161. $stockmove = new MouvementStock($db);
  162. $labelmovement = GETPOST('inventorylabel', 'alphanohtml');
  163. $codemovement = GETPOST('inventorycode', 'alphanohtml');
  164. $db->begin();
  165. $pos = 0;
  166. // Process line to consume
  167. foreach ($object->lines as $line) {
  168. if ($line->role == 'toconsume') {
  169. $tmpproduct = new Product($db);
  170. $tmpproduct->fetch($line->fk_product);
  171. $i = 1;
  172. while (GETPOSTISSET('qty-'.$line->id.'-'.$i)) {
  173. $qtytoprocess = price2num(GETPOST('qty-'.$line->id.'-'.$i));
  174. if ($qtytoprocess != 0) {
  175. // Check warehouse is set if we should have to
  176. if (GETPOSTISSET('idwarehouse-'.$line->id.'-'.$i)) { // If there is a warehouse to set
  177. if (!(GETPOST('idwarehouse-'.$line->id.'-'.$i) > 0)) { // If there is no warehouse set.
  178. $langs->load("errors");
  179. setEventMessages($langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Warehouse"), $tmpproduct->ref), null, 'errors');
  180. $error++;
  181. }
  182. if ($tmpproduct->status_batch && (!GETPOST('batch-'.$line->id.'-'.$i))) {
  183. $langs->load("errors");
  184. setEventMessages($langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Batch"), $tmpproduct->ref), null, 'errors');
  185. $error++;
  186. }
  187. }
  188. $idstockmove = 0;
  189. if (!$error && GETPOST('idwarehouse-'.$line->id.'-'.$i) > 0) {
  190. // Record stock movement
  191. $id_product_batch = 0;
  192. $stockmove->setOrigin($object->element, $object->id);
  193. if ($qtytoprocess >= 0) {
  194. $idstockmove = $stockmove->livraison($user, $line->fk_product, GETPOST('idwarehouse-'.$line->id.'-'.$i), $qtytoprocess, 0, $labelmovement, dol_now(), '', '', GETPOST('batch-'.$line->id.'-'.$i), $id_product_batch, $codemovement);
  195. } else {
  196. $idstockmove = $stockmove->reception($user, $line->fk_product, GETPOST('idwarehouse-'.$line->id.'-'.$i), $qtytoprocess * -1, 0, $labelmovement, dol_now(), '', '', GETPOST('batch-'.$line->id.'-'.$i), $id_product_batch, $codemovement);
  197. }
  198. if ($idstockmove < 0) {
  199. $error++;
  200. setEventMessages($stockmove->error, $stockmove->errors, 'errors');
  201. }
  202. }
  203. if (!$error) {
  204. // Record consumption
  205. $moline = new MoLine($db);
  206. $moline->fk_mo = $object->id;
  207. $moline->position = $pos;
  208. $moline->fk_product = $line->fk_product;
  209. $moline->fk_warehouse = GETPOST('idwarehouse-'.$line->id.'-'.$i);
  210. $moline->qty = $qtytoprocess;
  211. $moline->batch = GETPOST('batch-'.$line->id.'-'.$i);
  212. $moline->role = 'consumed';
  213. $moline->fk_mrp_production = $line->id;
  214. $moline->fk_stock_movement = $idstockmove == 0 ? null : $idstockmove;
  215. $moline->fk_user_creat = $user->id;
  216. $resultmoline = $moline->create($user);
  217. if ($resultmoline <= 0) {
  218. $error++;
  219. setEventMessages($moline->error, $moline->errors, 'errors');
  220. }
  221. $pos++;
  222. }
  223. }
  224. $i++;
  225. }
  226. }
  227. }
  228. // Process line to produce
  229. $pos = 0;
  230. foreach ($object->lines as $line) {
  231. if ($line->role == 'toproduce') {
  232. $tmpproduct = new Product($db);
  233. $tmpproduct->fetch($line->fk_product);
  234. $i = 1;
  235. while (GETPOSTISSET('qtytoproduce-'.$line->id.'-'.$i)) {
  236. $qtytoprocess = price2num(GETPOST('qtytoproduce-'.$line->id.'-'.$i));
  237. $pricetoprocess = GETPOST('pricetoproduce-'.$line->id.'-'.$i) ? price2num(GETPOST('pricetoproduce-'.$line->id.'-'.$i)) : 0;
  238. if ($qtytoprocess != 0) {
  239. // Check warehouse is set if we should have to
  240. if (GETPOSTISSET('idwarehousetoproduce-'.$line->id.'-'.$i)) { // If there is a warehouse to set
  241. if (!(GETPOST('idwarehousetoproduce-'.$line->id.'-'.$i) > 0)) { // If there is no warehouse set.
  242. $langs->load("errors");
  243. setEventMessages($langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Warehouse"), $tmpproduct->ref), null, 'errors');
  244. $error++;
  245. }
  246. if (isModEnabled('productbatch') && $tmpproduct->status_batch && (!GETPOST('batchtoproduce-'.$line->id.'-'.$i))) {
  247. $langs->load("errors");
  248. setEventMessages($langs->trans("ErrorFieldRequiredForProduct", $langs->transnoentitiesnoconv("Batch"), $tmpproduct->ref), null, 'errors');
  249. $error++;
  250. }
  251. }
  252. $idstockmove = 0;
  253. if (!$error && GETPOST('idwarehousetoproduce-'.$line->id.'-'.$i) > 0) {
  254. // Record stock movement
  255. $id_product_batch = 0;
  256. $stockmove->origin_type = $object->element;
  257. $stockmove->origin_id = $object->id;
  258. $idstockmove = $stockmove->reception($user, $line->fk_product, GETPOST('idwarehousetoproduce-'.$line->id.'-'.$i), $qtytoprocess, $pricetoprocess, $labelmovement, '', '', GETPOST('batchtoproduce-'.$line->id.'-'.$i), dol_now(), $id_product_batch, $codemovement);
  259. if ($idstockmove < 0) {
  260. $error++;
  261. setEventMessages($stockmove->error, $stockmove->errors, 'errors');
  262. }
  263. }
  264. if (!$error) {
  265. // Record production
  266. $moline = new MoLine($db);
  267. $moline->fk_mo = $object->id;
  268. $moline->position = $pos;
  269. $moline->fk_product = $line->fk_product;
  270. $moline->fk_warehouse = GETPOST('idwarehousetoproduce-'.$line->id.'-'.$i);
  271. $moline->qty = $qtytoprocess;
  272. $moline->batch = GETPOST('batchtoproduce-'.$line->id.'-'.$i);
  273. $moline->role = 'produced';
  274. $moline->fk_mrp_production = $line->id;
  275. $moline->fk_stock_movement = $idstockmove;
  276. $moline->fk_user_creat = $user->id;
  277. $resultmoline = $moline->create($user);
  278. if ($resultmoline <= 0) {
  279. $error++;
  280. setEventMessages($moline->error, $moline->errors, 'errors');
  281. }
  282. $pos++;
  283. }
  284. }
  285. $i++;
  286. }
  287. }
  288. }
  289. if (!$error) {
  290. $consumptioncomplete = true;
  291. $productioncomplete = true;
  292. if (GETPOST('autoclose', 'int')) {
  293. foreach ($object->lines as $line) {
  294. if ($line->role == 'toconsume') {
  295. $arrayoflines = $object->fetchLinesLinked('consumed', $line->id);
  296. $alreadyconsumed = 0;
  297. foreach ($arrayoflines as $line2) {
  298. $alreadyconsumed += $line2['qty'];
  299. }
  300. if ($alreadyconsumed < $line->qty) {
  301. $consumptioncomplete = false;
  302. }
  303. }
  304. if ($line->role == 'toproduce') {
  305. $arrayoflines = $object->fetchLinesLinked('produced', $line->id);
  306. $alreadyproduced = 0;
  307. foreach ($arrayoflines as $line2) {
  308. $alreadyproduced += $line2['qty'];
  309. }
  310. if ($alreadyproduced < $line->qty) {
  311. $productioncomplete = false;
  312. }
  313. }
  314. }
  315. } else {
  316. $consumptioncomplete = false;
  317. $productioncomplete = false;
  318. }
  319. // Update status of MO
  320. dol_syslog("consumptioncomplete = ".$consumptioncomplete." productioncomplete = ".$productioncomplete);
  321. //var_dump("consumptioncomplete = ".$consumptioncomplete." productioncomplete = ".$productioncomplete);
  322. if ($consumptioncomplete && $productioncomplete) {
  323. $result = $object->setStatut($object::STATUS_PRODUCED, 0, '', 'MRP_MO_PRODUCED');
  324. } else {
  325. $result = $object->setStatut($object::STATUS_INPROGRESS, 0, '', 'MRP_MO_PRODUCED');
  326. }
  327. if ($result <= 0) {
  328. $error++;
  329. setEventMessages($object->error, $object->errors, 'errors');
  330. }
  331. }
  332. if ($error) {
  333. $action = str_replace('confirm_', '', $action);
  334. $db->rollback();
  335. } else {
  336. $db->commit();
  337. // Redirect to avoid to action done a second time if we make a back from browser
  338. header("Location: ".$_SERVER["PHP_SELF"].'?id='.$object->id);
  339. exit;
  340. }
  341. }
  342. // Action close produced
  343. if ($action == 'confirm_produced' && $confirm == 'yes' && $permissiontoadd) {
  344. $result = $object->setStatut($object::STATUS_PRODUCED, 0, '', 'MRP_MO_PRODUCED');
  345. if ($result >= 0) {
  346. // Define output language
  347. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  348. $outputlangs = $langs;
  349. $newlang = '';
  350. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  351. $newlang = GETPOST('lang_id', 'aZ09');
  352. }
  353. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  354. $newlang = $object->thirdparty->default_lang;
  355. }
  356. if (!empty($newlang)) {
  357. $outputlangs = new Translate("", $conf);
  358. $outputlangs->setDefaultLang($newlang);
  359. }
  360. $model = $object->model_pdf;
  361. $ret = $object->fetch($id); // Reload to get new records
  362. $object->generateDocument($model, $outputlangs, 0, 0, 0);
  363. }
  364. } else {
  365. setEventMessages($object->error, $object->errors, 'errors');
  366. }
  367. }
  368. }
  369. /*
  370. * View
  371. */
  372. $form = new Form($db);
  373. $formproject = new FormProjets($db);
  374. $formproduct = new FormProduct($db);
  375. $tmpwarehouse = new Entrepot($db);
  376. $tmpbatch = new Productlot($db);
  377. $tmpstockmovement = new MouvementStock($db);
  378. $help_url = 'EN:Module_Manufacturing_Orders|FR:Module_Ordres_de_Fabrication|DE:Modul_Fertigungsauftrag';
  379. $morejs = array('/mrp/js/lib_dispatch.js.php');
  380. llxHeader('', $langs->trans('Mo'), $help_url, '', 0, 0, $morejs);
  381. $newToken = newToken();
  382. // Part to show record
  383. if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
  384. $res = $object->fetch_thirdparty();
  385. $res = $object->fetch_optionals();
  386. if (!empty($conf->global->STOCK_CONSUMPTION_FROM_MANUFACTURING_WAREHOUSE) && $object->fk_warehouse > 0) {
  387. $tmpwarehouse->fetch($object->fk_warehouse);
  388. $fk_default_warehouse = $object->fk_warehouse;
  389. }
  390. $head = moPrepareHead($object);
  391. print dol_get_fiche_head($head, 'production', $langs->trans("ManufacturingOrder"), -1, $object->picto);
  392. $formconfirm = '';
  393. // Confirmation to delete
  394. if ($action == 'delete') {
  395. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteMo'), $langs->trans('ConfirmDeleteMo'), 'confirm_delete', '', 0, 1);
  396. }
  397. // Confirmation to delete line
  398. if ($action == 'deleteline') {
  399. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid.'&fk_movement='.$fk_movement, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_deleteline', '', 0, 1);
  400. }
  401. // Clone confirmation
  402. if ($action == 'clone') {
  403. // Create an array for form
  404. $formquestion = array();
  405. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneMo', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  406. }
  407. // Confirmation of validation
  408. if ($action == 'validate') {
  409. // We check that object has a temporary ref
  410. $ref = substr($object->ref, 1, 4);
  411. if ($ref == 'PROV') {
  412. $object->fetch_product();
  413. $numref = $object->getNextNumRef($object->fk_product);
  414. } else {
  415. $numref = $object->ref;
  416. }
  417. $text = $langs->trans('ConfirmValidateMo', $numref);
  418. /*if (isModEnabled('notification'))
  419. {
  420. require_once DOL_DOCUMENT_ROOT . '/core/class/notify.class.php';
  421. $notify = new Notify($db);
  422. $text .= '<br>';
  423. $text .= $notify->confirmMessage('BOM_VALIDATE', $object->socid, $object);
  424. }*/
  425. $formquestion = array();
  426. if (isModEnabled('mrp')) {
  427. $langs->load("mrp");
  428. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  429. $formproduct = new FormProduct($db);
  430. $forcecombo = 0;
  431. if ($conf->browser->name == 'ie') {
  432. $forcecombo = 1; // There is a bug in IE10 that make combo inside popup crazy
  433. }
  434. $formquestion = array(
  435. // 'text' => $langs->trans("ConfirmClone"),
  436. // array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value' => 1),
  437. // array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans("PuttingPricesUpToDate"), 'value' => 1),
  438. );
  439. }
  440. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('Validate'), $text, 'confirm_validate', $formquestion, 0, 1, 220);
  441. }
  442. // Call Hook formConfirm
  443. $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
  444. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  445. if (empty($reshook)) {
  446. $formconfirm .= $hookmanager->resPrint;
  447. } elseif ($reshook > 0) {
  448. $formconfirm = $hookmanager->resPrint;
  449. }
  450. // Print form confirm
  451. print $formconfirm;
  452. // MO file
  453. // ------------------------------------------------------------
  454. $linkback = '<a href="'.DOL_URL_ROOT.'/mrp/mo_list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  455. $morehtmlref = '<div class="refidno">';
  456. /*
  457. // Ref bis
  458. $morehtmlref.=$form->editfieldkey("RefBis", 'ref_client', $object->ref_client, $object, $user->rights->mrp->creer, 'string', '', 0, 1);
  459. $morehtmlref.=$form->editfieldval("RefBis", 'ref_client', $object->ref_client, $object, $user->rights->mrp->creer, 'string', '', null, null, '', 1);*/
  460. // Thirdparty
  461. if (is_object($object->thirdparty)) {
  462. $morehtmlref .= $object->thirdparty->getNomUrl(1, 'customer');
  463. if (empty($conf->global->MAIN_DISABLE_OTHER_LINK) && $object->thirdparty->id > 0) {
  464. $morehtmlref .= ' (<a href="'.DOL_URL_ROOT.'/commande/list.php?socid='.$object->thirdparty->id.'&search_societe='.urlencode($object->thirdparty->name).'">'.$langs->trans("OtherOrders").'</a>)';
  465. }
  466. }
  467. // Project
  468. if (isModEnabled('project')) {
  469. $langs->load("projects");
  470. if (is_object($object->thirdparty)) {
  471. $morehtmlref .= '<br>';
  472. }
  473. if ($permissiontoadd) {
  474. $morehtmlref .= img_picto($langs->trans("Project"), 'project', 'class="pictofixedwidth"');
  475. if ($action != 'classify') {
  476. $morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> ';
  477. }
  478. $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, $object->socid, $object->fk_project, ($action == 'classify' ? 'projectid' : 'none'), 0, 0, 0, 1, '', 'maxwidth300');
  479. } else {
  480. if (!empty($object->fk_project)) {
  481. $proj = new Project($db);
  482. $proj->fetch($object->fk_project);
  483. $morehtmlref .= $proj->getNomUrl(1);
  484. if ($proj->title) {
  485. $morehtmlref .= '<span class="opacitymedium"> - '.dol_escape_htmltag($proj->title).'</span>';
  486. }
  487. }
  488. }
  489. }
  490. $morehtmlref .= '</div>';
  491. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  492. print '<div class="fichecenter">';
  493. print '<div class="fichehalfleft">';
  494. print '<div class="underbanner clearboth"></div>';
  495. print '<table class="border centpercent tableforfield">'."\n";
  496. // Common attributes
  497. $keyforbreak = 'fk_warehouse';
  498. unset($object->fields['fk_project']);
  499. unset($object->fields['fk_soc']);
  500. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
  501. // Other attributes
  502. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  503. print '</table>';
  504. print '</div>';
  505. print '</div>';
  506. print '<div class="clearboth"></div>';
  507. print dol_get_fiche_end();
  508. if (!in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  509. print '<div class="tabsAction">';
  510. $parameters = array();
  511. // Note that $action and $object may be modified by hook
  512. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action);
  513. if (empty($reshook)) {
  514. // Validate
  515. if ($object->status == $object::STATUS_DRAFT) {
  516. if ($permissiontoadd) {
  517. if (empty($object->table_element_line) || (is_array($object->lines) && count($object->lines) > 0)) {
  518. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=validate&token='.$newToken.'">'.$langs->trans("Validate").'</a>';
  519. } else {
  520. $langs->load("errors");
  521. print '<a class="butActionRefused" href="" title="'.$langs->trans("ErrorAddAtLeastOneLineFirst").'">'.$langs->trans("Validate").'</a>';
  522. }
  523. }
  524. }
  525. // Consume or produce
  526. if ($object->status == Mo::STATUS_VALIDATED || $object->status == Mo::STATUS_INPROGRESS) {
  527. if ($permissiontoproduce) {
  528. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=consumeorproduce&token='.$newToken.'">'.$langs->trans('ConsumeOrProduce').'</a>';
  529. } else {
  530. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("NotEnoughPermissions").'">'.$langs->trans('ConsumeOrProduce').'</a>';
  531. }
  532. } elseif ($object->status == Mo::STATUS_DRAFT) {
  533. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("ValidateBefore").'">'.$langs->trans('ConsumeOrProduce').'</a>';
  534. }
  535. // ConsumeAndProduceAll
  536. if ($object->status == Mo::STATUS_VALIDATED || $object->status == Mo::STATUS_INPROGRESS) {
  537. if ($permissiontoproduce) {
  538. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=consumeandproduceall&token='.$newToken.'">'.$langs->trans('ConsumeAndProduceAll').'</a>';
  539. } else {
  540. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("NotEnoughPermissions").'">'.$langs->trans('ConsumeAndProduceAll').'</a>';
  541. }
  542. } elseif ($object->status == Mo::STATUS_DRAFT) {
  543. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("ValidateBefore").'">'.$langs->trans('ConsumeAndProduceAll').'</a>';
  544. }
  545. // Cancel - Reopen
  546. if ($permissiontoadd) {
  547. if ($object->status == $object::STATUS_VALIDATED || $object->status == $object::STATUS_INPROGRESS) {
  548. $arrayproduced = $object->fetchLinesLinked('produced', 0);
  549. $nbProduced = 0;
  550. foreach ($arrayproduced as $lineproduced) {
  551. $nbProduced += $lineproduced['qty'];
  552. }
  553. if ($nbProduced > 0) { // If production has started, we can close it
  554. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=confirm_produced&confirm=yes&token='.$newToken.'">'.$langs->trans("Close").'</a>'."\n";
  555. } else {
  556. print '<a class="butActionRefused" href="#" title="'.$langs->trans("GoOnTabProductionToProduceFirst", $langs->transnoentitiesnoconv("Production")).'">'.$langs->trans("Close").'</a>'."\n";
  557. }
  558. print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=confirm_close&confirm=yes&token='.$newToken.'">'.$langs->trans("Cancel").'</a>'."\n";
  559. }
  560. if ($object->status == $object::STATUS_CANCELED) {
  561. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=confirm_reopen&confirm=yes&token='.$newToken.'">'.$langs->trans("Re-Open").'</a>'."\n";
  562. }
  563. if ($object->status == $object::STATUS_PRODUCED) {
  564. if ($permissiontoproduce) {
  565. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=confirm_reopen&token='.$newToken.'">'.$langs->trans('ReOpen').'</a>';
  566. } else {
  567. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("NotEnoughPermissions").'">'.$langs->trans('ReOpen').'</a>';
  568. }
  569. }
  570. }
  571. }
  572. print '</div>';
  573. }
  574. if (in_array($action, array('consumeorproduce', 'consumeandproduceall', 'addconsumeline', 'addproduceline'))) {
  575. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  576. print '<input type="hidden" name="token" value="'.newToken().'">';
  577. print '<input type="hidden" name="action" value="confirm_'.$action.'">';
  578. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  579. print '<input type="hidden" name="id" value="'.$id.'">';
  580. // Note: closing form is add end of page
  581. if (in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  582. $defaultstockmovementlabel = GETPOST('inventorylabel', 'alphanohtml') ? GETPOST('inventorylabel', 'alphanohtml') : $langs->trans("ProductionForRef", $object->ref);
  583. $defaultstockmovementcode = GETPOST('inventorycode', 'alphanohtml') ? GETPOST('inventorycode', 'alphanohtml') : dol_print_date(dol_now(), 'dayhourlog');
  584. print '<div class="center'.(in_array($action, array('consumeorproduce', 'consumeandproduceall')) ? ' formconsumeproduce' : '').'">';
  585. print '<div class="opacitymedium hideonsmartphone paddingbottom">'.$langs->trans("ConfirmProductionDesc", $langs->transnoentitiesnoconv("Confirm")).'<br></div>';
  586. print '<span class="fieldrequired">'.$langs->trans("InventoryCode").':</span> <input type="text" class="minwidth150 maxwidth200" name="inventorycode" value="'.$defaultstockmovementcode.'"> &nbsp; ';
  587. print '<span class="clearbothonsmartphone"></span>';
  588. print $langs->trans("MovementLabel").': <input type="text" class="minwidth300" name="inventorylabel" value="'.$defaultstockmovementlabel.'"><br><br>';
  589. print '<input type="checkbox" id="autoclose" name="autoclose" value="1"'.(GETPOSTISSET('inventorylabel') ? (GETPOST('autoclose') ? ' checked="checked"' : '') : ' checked="checked"').'> <label for="autoclose">'.$langs->trans("AutoCloseMO").'</label><br>';
  590. print '<input type="submit" class="button" value="'.$langs->trans("Confirm").'" name="confirm">';
  591. print ' &nbsp; ';
  592. print '<input class="button button-cancel" type="submit" value="'.$langs->trans("Cancel").'" name="cancel">';
  593. print '<br><br>';
  594. print '</div>';
  595. print '<br>';
  596. }
  597. }
  598. /*
  599. * Lines
  600. */
  601. $collapse = 1;
  602. if (!empty($object->table_element_line)) {
  603. // Show object lines
  604. $object->fetchLines();
  605. $bomcost = 0;
  606. if ($object->fk_bom > 0) {
  607. $bom = new Bom($db);
  608. $res = $bom->fetch($object->fk_bom);
  609. if ($res > 0) {
  610. $bom->calculateCosts();
  611. $bomcost = $bom->unit_cost;
  612. }
  613. }
  614. // Lines to consume
  615. print '<div class="fichecenter">';
  616. print '<div class="fichehalfleft">';
  617. print '<div class="clearboth"></div>';
  618. $url = $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=addconsumeline&token='.newToken();
  619. $permissiontoaddaconsumeline = $object->status != $object::STATUS_PRODUCED && $object->status != $object::STATUS_CANCELED;
  620. $parameters = array('morecss'=>'reposition');
  621. $newcardbutton = '';
  622. if ($action != 'consumeorproduce' && $action != 'consumeandproduceall') {
  623. $newcardbutton = dolGetButtonTitle($langs->trans('AddNewConsumeLines'), '', 'fa fa-plus-circle size15x', $url, '', $permissiontoaddaconsumeline, $parameters);
  624. }
  625. print load_fiche_titre($langs->trans('Consumption'), $newcardbutton, '', 0, '', '', '');
  626. print '<div class="div-table-responsive-no-min">';
  627. print '<table class="noborder noshadow centpercent nobottom">';
  628. print '<tr class="liste_titre">';
  629. // Product
  630. print '<td>'.$langs->trans("Product").'</td>';
  631. // Qty
  632. print '<td class="right">'.$langs->trans("Qty").'</td>';
  633. // Cost price
  634. if ($permissiontoupdatecost && !empty($conf->global->MRP_SHOW_COST_FOR_CONSUMPTION)) {
  635. print '<td class="right">'.$langs->trans("UnitCost").'</td>';
  636. }
  637. // Qty already consumed
  638. print '<td class="right">'.$langs->trans("QtyAlreadyConsumed").'</td>';
  639. // Warehouse
  640. print '<td>';
  641. if ($collapse || in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  642. print $langs->trans("Warehouse");
  643. // Select warehouse to force it everywhere
  644. if (in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  645. $listwarehouses = $tmpwarehouse->list_array(1);
  646. if (count($listwarehouses) > 1) {
  647. print '<br>'.$form->selectarray('fk_default_warehouse', $listwarehouses, $fk_default_warehouse, $langs->trans("ForceTo"), 0, 0, '', 0, 0, 0, '', 'minwidth100 maxwidth200', 1);
  648. } elseif (count($listwarehouses) == 1) {
  649. print '<br>'.$form->selectarray('fk_default_warehouse', $listwarehouses, $fk_default_warehouse, 0, 0, 0, '', 0, 0, 0, '', 'minwidth100 maxwidth200', 1);
  650. }
  651. }
  652. }
  653. print '</td>';
  654. if (isModEnabled('stock')) {
  655. // Available
  656. print '<td align="right">';
  657. if ($collapse || in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  658. print $langs->trans("Stock");
  659. }
  660. print '</td>';
  661. }
  662. // Lot - serial
  663. if (isModEnabled('productbatch')) {
  664. print '<td>';
  665. if ($collapse || in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  666. print $langs->trans("Batch");
  667. }
  668. print '</td>';
  669. }
  670. // Action
  671. if ($permissiontodelete) {
  672. print '<td></td>';
  673. }
  674. // Split
  675. print '<td></td>';
  676. // SplitAll
  677. print '<td></td>';
  678. print '</tr>';
  679. if ($action == 'addconsumeline') {
  680. print '<!-- Add line to consume -->'."\n";
  681. print '<tr class="liste_titre">';
  682. print '<td>';
  683. print $form->select_produits('', 'productidtoadd', '', 0, 0, -1, 2, '', 1, array(), 0, '1', 0, 'maxwidth300');
  684. print '</td>';
  685. // Qty
  686. print '<td class="right"><input type="text" name="qtytoadd" value="1" class="width50 right"></td>';
  687. // Cost price
  688. if ($permissiontoupdatecost && !empty($conf->global->MRP_SHOW_COST_FOR_CONSUMPTION)) {
  689. print '<td></td>';
  690. }
  691. // Qty already consumed
  692. print '<td colspan="2">';
  693. // Warehouse
  694. print '<input type="submit" class="button buttongen button-add" name="addconsumelinebutton" value="'.$langs->trans("Add").'">';
  695. print '<input type="submit" class="button buttongen button-cancel" name="canceladdconsumelinebutton" value="'.$langs->trans("Cancel").'">';
  696. print '</td>';
  697. if (isModEnabled('stock')) {
  698. print '<td></td>';
  699. }
  700. // Lot - serial
  701. if (isModEnabled('productbatch')) {
  702. print '<td></td>';
  703. }
  704. // Action
  705. if ($permissiontodelete) {
  706. print '<td></td>';
  707. }
  708. // Split
  709. print '<td></td>';
  710. // SplitAll
  711. print '<td></td>';
  712. print '</tr>';
  713. }
  714. // Lines to consume
  715. $bomcostupdated = 0; // We will recalculate the unitary cost to produce a product using the real "products to consume into MO"
  716. if (!empty($object->lines)) {
  717. $nblinetoconsume = 0;
  718. foreach ($object->lines as $line) {
  719. if ($line->role == 'toconsume') {
  720. $nblinetoconsume++;
  721. }
  722. }
  723. $nblinetoconsumecursor = 0;
  724. foreach ($object->lines as $line) {
  725. if ($line->role == 'toconsume') {
  726. $nblinetoconsumecursor++;
  727. $tmpproduct = new Product($db);
  728. $tmpproduct->fetch($line->fk_product);
  729. $linecost = price2num($tmpproduct->pmp, 'MT');
  730. if ($object->qty > 0) {
  731. // add free consume line cost to $bomcostupdated
  732. $costprice = price2num((!empty($tmpproduct->cost_price)) ? $tmpproduct->cost_price : $tmpproduct->pmp);
  733. if (empty($costprice)) {
  734. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  735. $productFournisseur = new ProductFournisseur($db);
  736. if ($productFournisseur->find_min_price_product_fournisseur($line->fk_product) > 0) {
  737. $costprice = $productFournisseur->fourn_unitprice;
  738. } else {
  739. $costprice = 0;
  740. }
  741. }
  742. $linecost = price2num(($line->qty * $costprice) / $object->qty, 'MT'); // price for line for all quantities
  743. $bomcostupdated += price2num(($line->qty * $costprice) / $object->qty, 'MU'); // same but with full accuracy
  744. }
  745. $bomcostupdated = price2num($bomcostupdated, 'MU');
  746. $arrayoflines = $object->fetchLinesLinked('consumed', $line->id);
  747. $alreadyconsumed = 0;
  748. foreach ($arrayoflines as $line2) {
  749. $alreadyconsumed += $line2['qty'];
  750. }
  751. $suffix = '_'.$line->id;
  752. print '<!-- Line to dispatch '.$suffix.' -->'."\n";
  753. // hidden fields for js function
  754. print '<input id="qty_ordered'.$suffix.'" type="hidden" value="'.$line->qty.'">';
  755. print '<input id="qty_dispatched'.$suffix.'" type="hidden" value="'.$alreadyconsumed.'">';
  756. print '<tr data-line-id="'.$line->id.'">';
  757. // Product
  758. print '<td>'.$tmpproduct->getNomUrl(1);
  759. print '<br><div class="opacitymedium small tdoverflowmax150" title="'.dol_escape_htmltag($tmpproduct->label).'">'.$tmpproduct->label.'</div>';
  760. print '</td>';
  761. // Qty
  762. print '<td class="right nowraponall">';
  763. $help = ''; $picto = 'help';
  764. if ($line->qty_frozen) {
  765. $help = ($help ? '<br>' : '').'<strong>'.$langs->trans("QuantityFrozen").'</strong>: '.yn(1).' ('.$langs->trans("QuantityConsumedInvariable").')';
  766. print $form->textwithpicto('', $help, -1, 'lock').' ';
  767. }
  768. if ($line->disable_stock_change) {
  769. $help = ($help ? '<br>' : '').'<strong>'.$langs->trans("DisableStockChange").'</strong>: '.yn(1).' ('.(($tmpproduct->type == Product::TYPE_SERVICE && empty($conf->global->STOCK_SUPPORTS_SERVICES)) ? $langs->trans("NoStockChangeOnServices") : $langs->trans("DisableStockChangeHelp")).')';
  770. print $form->textwithpicto('', $help, -1, 'help').' ';
  771. }
  772. print price2num($line->qty, 'MS');
  773. print '</td>';
  774. // Cost price
  775. if ($permissiontoupdatecost && !empty($conf->global->MRP_SHOW_COST_FOR_CONSUMPTION)) {
  776. print '<td class="right nowraponall">';
  777. print price($linecost);
  778. print '</td>';
  779. }
  780. // Already consumed
  781. print '<td class="right">';
  782. if ($alreadyconsumed) {
  783. print '<script>';
  784. print 'jQuery(document).ready(function() {
  785. jQuery("#expandtoproduce'.$line->id.'").click(function() {
  786. console.log("Expand mrp_production line '.$line->id.'");
  787. jQuery(".expanddetail'.$line->id.'").toggle();';
  788. if ($nblinetoconsume == $nblinetoconsumecursor) { // If it is the last line
  789. print 'if (jQuery("#tablelines").hasClass("nobottom")) { jQuery("#tablelines").removeClass("nobottom"); } else { jQuery("#tablelines").addClass("nobottom"); }';
  790. }
  791. print '
  792. });
  793. });';
  794. print '</script>';
  795. if (empty($conf->use_javascript_ajax)) {
  796. print '<a href="'.$_SERVER["PHP_SELF"].'?collapse='.$collapse.','.$line->id.'">';
  797. }
  798. print img_picto($langs->trans("ShowDetails"), "chevron-down", 'id="expandtoproduce'.$line->id.'"');
  799. if (empty($conf->use_javascript_ajax)) {
  800. print '</a>';
  801. }
  802. } else {
  803. if ($nblinetoconsume == $nblinetoconsumecursor) { // If it is the last line
  804. print '<script>jQuery("#tablelines").removeClass("nobottom");</script>';
  805. }
  806. }
  807. print ' '.price2num($alreadyconsumed, 'MS');
  808. print '</td>';
  809. // Warehouse
  810. print '<td>';
  811. if (!empty($conf->global->STOCK_CONSUMPTION_FROM_MANUFACTURING_WAREHOUSE) && $tmpwarehouse->id > 0) {
  812. print img_picto('', $tmpwarehouse->picto)." ".$tmpwarehouse->label;
  813. }
  814. print '</td>';
  815. // Stock
  816. if (isModEnabled('stock')) {
  817. print '<td class="nowraponall right">';
  818. if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $tmpproduct->type != PRODUCT::TYPE_SERVICE) {
  819. if (!$line->disable_stock_change && $tmpproduct->stock_reel < ($line->qty - $alreadyconsumed)) {
  820. print img_warning($langs->trans('StockTooLow')) . ' ';
  821. }
  822. if (empty($conf->global->STOCK_CONSUMPTION_FROM_MANUFACTURING_WAREHOUSE) || empty($tmpwarehouse->id)) {
  823. print price2num($tmpproduct->stock_reel, 'MS'); // Available
  824. } else {
  825. // Print only the stock in the selected warehouse
  826. $tmpproduct->load_stock();
  827. $wh_stock = $tmpproduct->stock_warehouse[$tmpwarehouse->id];
  828. if (!empty($wh_stock)) {
  829. print price2num($wh_stock->real, 'MS');
  830. } else {
  831. print "0";
  832. }
  833. }
  834. }
  835. print '</td>';
  836. }
  837. // Lot
  838. if (isModEnabled('productbatch')) {
  839. print '<td></td>';
  840. }
  841. // Split
  842. print '<td></td>';
  843. // Split All
  844. print '<td></td>';
  845. // Action delete line
  846. if ($permissiontodelete) {
  847. $href = $_SERVER["PHP_SELF"].'?id='.((int) $object->id).'&action=deleteline&token='.newToken().'&lineid='.((int) $line->id);
  848. print '<td class="center">';
  849. print '<a class="reposition" href="'.$href.'">';
  850. print img_picto($langs->trans('TooltipDeleteAndRevertStockMovement'), 'delete');
  851. print '</a>';
  852. print '</td>';
  853. }
  854. print '</tr>';
  855. // Show detailed of already consumed with js code to collapse
  856. foreach ($arrayoflines as $line2) {
  857. print '<tr class="expanddetail'.$line->id.' hideobject opacitylow">';
  858. // Date
  859. print '<td>';
  860. $tmpstockmovement->id = $line2['fk_stock_movement'];
  861. print '<a href="'.DOL_URL_ROOT.'/product/stock/movement_list.php?search_ref='.$tmpstockmovement->id.'">'.img_picto($langs->trans("StockMovement"), 'movement', 'class="paddingright"').'</a>';
  862. print dol_print_date($line2['date'], 'dayhour', 'tzuserrel');
  863. print '</td>';
  864. // Already consumed
  865. print '<td></td>';
  866. // Qty
  867. print '<td class="right">'.$line2['qty'].'</td>';
  868. // Cost price
  869. if ($permissiontoupdatecost && !empty($conf->global->MRP_SHOW_COST_FOR_CONSUMPTION)) {
  870. print '<td></td>';
  871. }
  872. // Warehouse
  873. print '<td class="tdoverflowmax150">';
  874. if ($line2['fk_warehouse'] > 0) {
  875. $result = $tmpwarehouse->fetch($line2['fk_warehouse']);
  876. if ($result > 0) {
  877. print $tmpwarehouse->getNomUrl(1);
  878. }
  879. }
  880. print '</td>';
  881. // Stock
  882. if (isModEnabled('stock')) {
  883. print '<td></td>';
  884. }
  885. // Lot Batch
  886. if (isModEnabled('productbatch')) {
  887. print '<td>';
  888. if ($line2['batch'] != '') {
  889. $tmpbatch->fetch(0, $line2['fk_product'], $line2['batch']);
  890. print $tmpbatch->getNomUrl(1);
  891. }
  892. print '</td>';
  893. }
  894. // Split
  895. print '<td></td>';
  896. // Split All
  897. print '<td></td>';
  898. // Action delete line
  899. if ($permissiontodelete) {
  900. $href = $_SERVER["PHP_SELF"].'?id='.((int) $object->id).'&action=deleteline&token='.newToken().'&lineid='.((int) $line2['rowid']).'&fk_movement='.((int) $line2['fk_stock_movement']);
  901. print '<td class="center">';
  902. print '<a class="reposition" href="'.$href.'">';
  903. print img_picto($langs->trans('TooltipDeleteAndRevertStockMovement'), 'delete');
  904. print '</a>';
  905. print '</td>';
  906. }
  907. print '</tr>';
  908. }
  909. if (in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  910. $i = 1;
  911. print '<!-- Enter line to consume -->'."\n";
  912. $maxQty = 1;
  913. print '<tr data-max-qty="'.$maxQty.'" name="batch_'.$line->id.'_'.$i.'">';
  914. // Ref
  915. print '<td><span class="opacitymedium">'.$langs->trans("ToConsume").'</span></td>';
  916. $preselected = (GETPOSTISSET('qty-'.$line->id.'-'.$i) ? GETPOST('qty-'.$line->id.'-'.$i) : max(0, $line->qty - $alreadyconsumed));
  917. if ($action == 'consumeorproduce' && !GETPOSTISSET('qty-'.$line->id.'-'.$i)) {
  918. $preselected = 0;
  919. }
  920. $disable = '';
  921. if (!empty($conf->global->MRP_NEVER_CONSUME_MORE_THAN_EXPECTED) && ($line->qty - $alreadyconsumed) <= 0) {
  922. $disable = 'disabled';
  923. }
  924. // input hidden with fk_product of line
  925. print '<input type="hidden" name="product-'.$line->id.'-'.$i.'" value="'.$line->fk_product.'">';
  926. // Qty
  927. print '<td class="right"><input type="text" class="width50 right" id="qtytoconsume-'.$line->id.'-'.$i.'" name="qty-'.$line->id.'-'.$i.'" value="'.$preselected.'" '.$disable.'></td>';
  928. // Cost
  929. if ($permissiontoupdatecost && !empty($conf->global->MRP_SHOW_COST_FOR_CONSUMPTION)) {
  930. print '<td></td>';
  931. }
  932. // Already consumed
  933. print '<td></td>';
  934. // Warehouse
  935. print '<td>';
  936. if ($tmpproduct->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
  937. if (empty($line->disable_stock_change)) {
  938. $preselected = (GETPOSTISSET('idwarehouse-'.$line->id.'-'.$i) ? GETPOST('idwarehouse-'.$line->id.'-'.$i) : ($tmpproduct->fk_default_warehouse > 0 ? $tmpproduct->fk_default_warehouse : 'ifone'));
  939. print $formproduct->selectWarehouses($preselected, 'idwarehouse-'.$line->id.'-'.$i, '', 1, 0, $line->fk_product, '', 1, 0, null, 'maxwidth200 csswarehouse_'.$line->id.'_'.$i);
  940. } else {
  941. print '<span class="opacitymedium">'.$langs->trans("DisableStockChange").'</span>';
  942. }
  943. } else {
  944. print '<span class="opacitymedium">'.$langs->trans("NoStockChangeOnServices").'</span>';
  945. }
  946. print '</td>';
  947. // Stock
  948. if (isModEnabled('stock')) {
  949. print '<td></td>';
  950. }
  951. // Lot / Batch
  952. if (isModEnabled('productbatch')) {
  953. print '<td class="nowraponall">';
  954. if ($tmpproduct->status_batch) {
  955. $preselected = (GETPOSTISSET('batch-'.$line->id.'-'.$i) ? GETPOST('batch-'.$line->id.'-'.$i) : '');
  956. print '<input type="text" class="width75" name="batch-'.$line->id.'-'.$i.'" value="'.$preselected.'" list="batch-'.$line->id.'-'.$i.'">';
  957. print $formproduct->selectLotDataList('batch-'.$line->id.'-'.$i, 0, $line->fk_product, '', '');
  958. }
  959. print '</td>';
  960. }
  961. // Split
  962. $type = 'batch';
  963. print '<td align="right" class="split">';
  964. print ' '.img_picto($langs->trans('AddStockLocationLine'), 'split.png', 'class="splitbutton" onClick="addDispatchLine('.((int) $line->id).', \''.dol_escape_js($type).'\', \'qtymissingconsume\')"');
  965. print '</td>';
  966. // Split All
  967. print '<td align="right" class="splitall">';
  968. if (($action == 'consumeorproduce' || $action == 'consumeandproduceall') && $tmpproduct->status_batch == 2) {
  969. print img_picto($langs->trans('SplitAllQuantity'), 'split.png', 'class="splitbutton splitallbutton field-error-icon" data-max-qty="1" onClick="addDispatchLine('.$line->id.', \'batch\', \'allmissingconsume\')"');
  970. }
  971. print '</td>';
  972. // Action delete line
  973. if ($permissiontodelete) {
  974. print '<td></td>';
  975. }
  976. print '</tr>';
  977. }
  978. }
  979. }
  980. }
  981. print '</table>';
  982. print '</div>';
  983. // default warehouse processing
  984. print '<script type="text/javascript">
  985. $(document).ready(function () {
  986. $("select[name=fk_default_warehouse]").change(function() {
  987. var fk_default_warehouse = $("option:selected", this).val();
  988. $("select[name^=idwarehouse-]").val(fk_default_warehouse).change();
  989. });
  990. });
  991. </script>';
  992. if (in_array($action, array('consumeorproduce', 'consumeandproduceall')) &&
  993. !empty($conf->global->STOCK_CONSUMPTION_FROM_MANUFACTURING_WAREHOUSE)) {
  994. print '<script>$(document).ready(function () {
  995. $("#fk_default_warehouse").change();
  996. });</script>';
  997. }
  998. // Lines to produce
  999. print '</div>';
  1000. print '<div class="fichehalfright">';
  1001. print '<div class="clearboth"></div>';
  1002. $nblinetoproduce = 0;
  1003. foreach ($object->lines as $line) {
  1004. if ($line->role == 'toproduce') {
  1005. $nblinetoproduce++;
  1006. }
  1007. }
  1008. $newcardbutton = '';
  1009. $url = $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=addproduceline&token='.newToken();
  1010. $permissiontoaddaproductline = $object->status != $object::STATUS_PRODUCED && $object->status != $object::STATUS_CANCELED;
  1011. $parameters = array('morecss'=>'reposition');
  1012. if ($action != 'consumeorproduce' && $action != 'consumeandproduceall') {
  1013. if ($nblinetoproduce == 0 || $object->mrptype == 1) {
  1014. $newcardbutton = dolGetButtonTitle($langs->trans('AddNewProduceLines'), '', 'fa fa-plus-circle size15x', $url, '', $permissiontoaddaproductline, $parameters);
  1015. }
  1016. }
  1017. print load_fiche_titre($langs->trans('Production'), $newcardbutton, '', 0, '', '');
  1018. print '<div class="div-table-responsive-no-min">';
  1019. print '<table id="tablelinestoproduce" class="noborder noshadow nobottom centpercent">';
  1020. print '<tr class="liste_titre">';
  1021. // Product
  1022. print '<td>'.$langs->trans("Product").'</td>';
  1023. // Qty
  1024. print '<td class="right">'.$langs->trans("Qty").'</td>';
  1025. // Cost price
  1026. if ($permissiontoupdatecost) {
  1027. if (empty($bomcostupdated)) {
  1028. print '<td class="right">'.$form->textwithpicto($langs->trans("UnitCost"), $langs->trans("AmountUsedToUpdateWAP")).'</td>';
  1029. } else {
  1030. print '<td class="right">'.$form->textwithpicto($langs->trans("ManufacturingPrice"), $langs->trans("AmountUsedToUpdateWAP")).'</td>';
  1031. }
  1032. }
  1033. // Already produced
  1034. print '<td class="right">'.$langs->trans("QtyAlreadyProduced").'</td>';
  1035. // Warehouse
  1036. print '<td>';
  1037. if ($collapse || in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  1038. print $langs->trans("Warehouse");
  1039. }
  1040. print '</td>';
  1041. // Lot
  1042. if (isModEnabled('productbatch')) {
  1043. print '<td>';
  1044. if ($collapse || in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  1045. print $langs->trans("Batch");
  1046. }
  1047. print '</td>';
  1048. // Split
  1049. print '<td></td>';
  1050. // Split All
  1051. print '<td></td>';
  1052. }
  1053. // Action delete
  1054. if ($permissiontodelete) {
  1055. print '<td></td>';
  1056. }
  1057. print '</tr>';
  1058. if ($action == 'addproduceline') {
  1059. print '<!-- Add line to produce -->'."\n";
  1060. print '<tr class="liste_titre">';
  1061. // Product
  1062. print '<td>';
  1063. print $form->select_produits('', 'productidtoadd', '', 0, 0, -1, 2, '', 1, array(), 0, '1', 0, 'maxwidth300');
  1064. print '</td>';
  1065. // Qty
  1066. print '<td class="right"><input type="text" name="qtytoadd" value="1" class="width50 right"></td>';
  1067. // Cost price
  1068. if ($permissiontoupdatecost) {
  1069. print '<td></td>';
  1070. }
  1071. // Action (cost price + already produced)
  1072. print '<td colspan="2">';
  1073. print '<input type="submit" class="button buttongen button-add" name="addproducelinebutton" value="'.$langs->trans("Add").'">';
  1074. print '<input type="submit" class="button buttongen button-cancel" name="canceladdproducelinebutton" value="'.$langs->trans("Cancel").'">';
  1075. print '</td>';
  1076. // Lot - serial
  1077. if (isModEnabled('productbatch')) {
  1078. print '<td></td>';
  1079. // Split
  1080. print '<td></td>';
  1081. // Split All
  1082. print '<td></td>';
  1083. }
  1084. // Action delete
  1085. if ($permissiontodelete) {
  1086. print '<td></td>';
  1087. }
  1088. print '</tr>';
  1089. }
  1090. if (!empty($object->lines)) {
  1091. $nblinetoproduce = 0;
  1092. foreach ($object->lines as $line) {
  1093. if ($line->role == 'toproduce') {
  1094. $nblinetoproduce++;
  1095. }
  1096. }
  1097. $nblinetoproducecursor = 0;
  1098. foreach ($object->lines as $line) {
  1099. if ($line->role == 'toproduce') {
  1100. $i = 1;
  1101. $nblinetoproducecursor++;
  1102. $tmpproduct = new Product($db);
  1103. $tmpproduct->fetch($line->fk_product);
  1104. $arrayoflines = $object->fetchLinesLinked('produced', $line->id);
  1105. $alreadyproduced = 0;
  1106. foreach ($arrayoflines as $line2) {
  1107. $alreadyproduced += $line2['qty'];
  1108. }
  1109. $suffix = '_'.$line->id;
  1110. print '<!-- Line to dispatch '.$suffix.' -->'."\n";
  1111. // hidden fields for js function
  1112. print '<input id="qty_ordered'.$suffix.'" type="hidden" value="'.$line->qty.'">';
  1113. print '<input id="qty_dispatched'.$suffix.'" type="hidden" value="'.$alreadyproduced.'">';
  1114. print '<tr>';
  1115. // Product
  1116. print '<td>'.$tmpproduct->getNomUrl(1);
  1117. print '<br><span class="opacitymedium small">'.$tmpproduct->label.'</span>';
  1118. print '</td>';
  1119. // Qty
  1120. print '<td class="right">'.$line->qty.'</td>';
  1121. // Cost price
  1122. if ($permissiontoupdatecost) {
  1123. // Defined $manufacturingcost
  1124. $manufacturingcost = 0;
  1125. $manufacturingcostsrc = '';
  1126. if ($object->mrptype == 0) { // If MO is a "Manufacture" type (and not "Disassemble")
  1127. $manufacturingcost = $bomcostupdated;
  1128. $manufacturingcostsrc = $langs->trans("CalculatedFromProductsToConsume");
  1129. if (empty($manufacturingcost)) {
  1130. $manufacturingcost = $bomcost;
  1131. $manufacturingcostsrc = $langs->trans("ValueFromBom");
  1132. }
  1133. if (empty($manufacturingcost)) {
  1134. $manufacturingcost = price2num($tmpproduct->cost_price, 'MU');
  1135. $manufacturingcostsrc = $langs->trans("CostPrice");
  1136. }
  1137. if (empty($manufacturingcost)) {
  1138. $manufacturingcost = price2num($tmpproduct->pmp, 'MU');
  1139. $manufacturingcostsrc = $langs->trans("PMPValue");
  1140. }
  1141. }
  1142. print '<td class="right nowraponall" title="'.dol_escape_htmltag($manufacturingcostsrc).'">';
  1143. if ($manufacturingcost) {
  1144. print price($manufacturingcost);
  1145. }
  1146. print '</td>';
  1147. }
  1148. // Already produced
  1149. print '<td class="right nowraponall">';
  1150. if ($alreadyproduced) {
  1151. print '<script>';
  1152. print 'jQuery(document).ready(function() {
  1153. jQuery("#expandtoproduce'.$line->id.'").click(function() {
  1154. console.log("Expand mrp_production line '.$line->id.'");
  1155. jQuery(".expanddetailtoproduce'.$line->id.'").toggle();';
  1156. if ($nblinetoproduce == $nblinetoproducecursor) {
  1157. print 'if (jQuery("#tablelinestoproduce").hasClass("nobottom")) { jQuery("#tablelinestoproduce").removeClass("nobottom"); } else { jQuery("#tablelinestoproduce").addClass("nobottom"); }';
  1158. }
  1159. print '
  1160. });
  1161. });';
  1162. print '</script>';
  1163. if (empty($conf->use_javascript_ajax)) {
  1164. print '<a href="'.$_SERVER["PHP_SELF"].'?collapse='.$collapse.','.$line->id.'">';
  1165. }
  1166. print img_picto($langs->trans("ShowDetails"), "chevron-down", 'id="expandtoproduce'.$line->id.'"');
  1167. if (empty($conf->use_javascript_ajax)) {
  1168. print '</a>';
  1169. }
  1170. }
  1171. print ' '.$alreadyproduced;
  1172. print '</td>';
  1173. // Warehouse
  1174. print '<td>';
  1175. print '</td>';
  1176. // Lot
  1177. if (isModEnabled('productbatch')) {
  1178. print '<td></td>';
  1179. // Split
  1180. print '<td></td>';
  1181. // Split All
  1182. print '<td></td>';
  1183. }
  1184. // Delete
  1185. if ($permissiontodelete) {
  1186. if ($line->origin_type == 'free') {
  1187. $href = $_SERVER["PHP_SELF"];
  1188. $href .= '?id='.$object->id;
  1189. $href .= '&action=deleteline';
  1190. $href .= '&lineid='.$line->id;
  1191. print '<td class="center">';
  1192. print '<a class="reposition" href="'.$href.'">';
  1193. print img_picto($langs->trans('TooltipDeleteAndRevertStockMovement'), "delete");
  1194. print '</a>';
  1195. print '</td>';
  1196. } else {
  1197. print '<td></td>';
  1198. }
  1199. }
  1200. print '</tr>';
  1201. // Show detailed of already consumed with js code to collapse
  1202. foreach ($arrayoflines as $line2) {
  1203. print '<tr class="expanddetailtoproduce'.$line->id.' hideobject opacitylow">';
  1204. // Product
  1205. print '<td>';
  1206. $tmpstockmovement->id = $line2['fk_stock_movement'];
  1207. print '<a href="'.DOL_URL_ROOT.'/product/stock/movement_list.php?search_ref='.$tmpstockmovement->id.'">'.img_picto($langs->trans("StockMovement"), 'movement', 'class="paddingright"').'</a>';
  1208. print dol_print_date($line2['date'], 'dayhour', 'tzuserrel');
  1209. print '</td>';
  1210. // Qty
  1211. print '<td></td>';
  1212. // Cost price
  1213. if ($permissiontoupdatecost) {
  1214. print '<td></td>';
  1215. }
  1216. // Already produced
  1217. print '<td class="right">'.$line2['qty'].'</td>';
  1218. // Warehouse
  1219. print '<td class="tdoverflowmax150">';
  1220. if ($line2['fk_warehouse'] > 0) {
  1221. $result = $tmpwarehouse->fetch($line2['fk_warehouse']);
  1222. if ($result > 0) {
  1223. print $tmpwarehouse->getNomUrl(1);
  1224. }
  1225. }
  1226. print '</td>';
  1227. // Lot
  1228. if (isModEnabled('productbatch')) {
  1229. print '<td>';
  1230. if ($line2['batch'] != '') {
  1231. $tmpbatch->fetch(0, $line2['fk_product'], $line2['batch']);
  1232. print $tmpbatch->getNomUrl(1);
  1233. }
  1234. print '</td>';
  1235. // Split
  1236. print '<td></td>';
  1237. // Split All
  1238. print '<td></td>';
  1239. }
  1240. // Action delete
  1241. if ($permissiontodelete) {
  1242. print '<td></td>';
  1243. }
  1244. print '</tr>';
  1245. }
  1246. if (in_array($action, array('consumeorproduce', 'consumeandproduceall'))) {
  1247. print '<!-- Enter line to produce -->'."\n";
  1248. $maxQty = 1;
  1249. print '<tr data-max-qty="'.$maxQty.'" name="batch_'.$line->id.'_'.$i.'">';
  1250. // Product
  1251. print '<td><span class="opacitymedium">'.$langs->trans("ToProduce").'</span></td>';
  1252. $preselected = (GETPOSTISSET('qtytoproduce-'.$line->id.'-'.$i) ? GETPOST('qtytoproduce-'.$line->id.'-'.$i) : max(0, $line->qty - $alreadyproduced));
  1253. if ($action == 'consumeorproduce' && !GETPOSTISSET('qtytoproduce-'.$line->id.'-'.$i)) {
  1254. $preselected = 0;
  1255. }
  1256. // Qty
  1257. print '<td class="right"><input type="text" class="width50 right" id="qtytoproduce-'.$line->id.'-'.$i.'" name="qtytoproduce-'.$line->id.'-'.$i.'" value="'.$preselected.'"></td>';
  1258. // Cost
  1259. if ($permissiontoupdatecost) {
  1260. // Defined $manufacturingcost
  1261. $manufacturingcost = 0;
  1262. $manufacturingcostsrc = '';
  1263. if ($object->mrptype == 0) { // If MO is a "Manufacture" type (and not "Disassemble")
  1264. $manufacturingcost = $bomcostupdated;
  1265. $manufacturingcostsrc = $langs->trans("CalculatedFromProductsToConsume");
  1266. if (empty($manufacturingcost)) {
  1267. $manufacturingcost = $bomcost;
  1268. $manufacturingcostsrc = $langs->trans("ValueFromBom");
  1269. }
  1270. if (empty($manufacturingcost)) {
  1271. $manufacturingcost = price2num($tmpproduct->cost_price, 'MU');
  1272. $manufacturingcostsrc = $langs->trans("CostPrice");
  1273. }
  1274. if (empty($manufacturingcost)) {
  1275. $manufacturingcost = price2num($tmpproduct->pmp, 'MU');
  1276. $manufacturingcostsrc = $langs->trans("PMPValue");
  1277. }
  1278. }
  1279. if ($tmpproduct->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
  1280. $preselected = (GETPOSTISSET('pricetoproduce-'.$line->id.'-'.$i) ? GETPOST('pricetoproduce-'.$line->id.'-'.$i) : ($manufacturingcost ? price($manufacturingcost) : ''));
  1281. print '<td class="right"><input type="text" class="width75 right" name="pricetoproduce-'.$line->id.'-'.$i.'" value="'.$preselected.'"></td>';
  1282. } else {
  1283. print '<td><input type="hidden" class="width50 right" name="pricetoproduce-'.$line->id.'-'.$i.'" value="'.($manufacturingcost ? $manufacturingcost : '').'"></td>';
  1284. }
  1285. }
  1286. // Already produced
  1287. print '<td></td>';
  1288. // Warehouse
  1289. print '<td>';
  1290. if ($tmpproduct->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
  1291. $preselected = (GETPOSTISSET('idwarehousetoproduce-'.$line->id.'-'.$i) ? GETPOST('idwarehousetoproduce-'.$line->id.'-'.$i) : ($object->fk_warehouse > 0 ? $object->fk_warehouse : 'ifone'));
  1292. print $formproduct->selectWarehouses($preselected, 'idwarehousetoproduce-'.$line->id.'-'.$i, '', 1, 0, $line->fk_product, '', 1, 0, null, 'maxwidth200 csswarehouse_'.$line->id.'_'.$i);
  1293. } else {
  1294. print '<span class="opacitymedium">'.$langs->trans("NoStockChangeOnServices").'</span>';
  1295. }
  1296. print '</td>';
  1297. // Lot
  1298. if (isModEnabled('productbatch')) {
  1299. print '<td>';
  1300. if ($tmpproduct->status_batch) {
  1301. $preselected = (GETPOSTISSET('batchtoproduce-'.$line->id.'-'.$i) ? GETPOST('batchtoproduce-'.$line->id.'-'.$i) : '');
  1302. print '<input type="text" class="width75" name="batchtoproduce-'.$line->id.'-'.$i.'" value="'.$preselected.'">';
  1303. }
  1304. print '</td>';
  1305. // Batch number in same column than the stock movement picto
  1306. if ($tmpproduct->status_batch) {
  1307. $type = 'batch';
  1308. print '<td align="right" class="split">';
  1309. print img_picto($langs->trans('AddStockLocationLine'), 'split.png', 'class="splitbutton" onClick="addDispatchLine('.$line->id.', \''.$type.'\', \'qtymissing\')"');
  1310. print '</td>';
  1311. print '<td align="right" class="splitall">';
  1312. if (($action == 'consumeorproduce' || $action == 'consumeandproduceall') && $tmpproduct->status_batch == 2) print img_picto($langs->trans('SplitAllQuantity'), 'split.png', 'class="splitbutton splitallbutton field-error-icon" onClick="addDispatchLine('.$line->id.', \'batch\', \'alltoproduce\')"'); //
  1313. print '</td>';
  1314. } else {
  1315. print '<td></td>';
  1316. print '<td></td>';
  1317. }
  1318. }
  1319. // Action delete
  1320. print '<td></td>';
  1321. print '</tr>';
  1322. }
  1323. }
  1324. }
  1325. }
  1326. print '</table>';
  1327. print '</div>';
  1328. print '</div>';
  1329. print '</div>';
  1330. }
  1331. if (in_array($action, array('consumeorproduce', 'consumeandproduceall', 'addconsumeline'))) {
  1332. print "</form>\n";
  1333. }
  1334. ?>
  1335. <script type="text/javascript" language="javascript">
  1336. $(document).ready(function() {
  1337. //Consumption : When a warehouse is selected, only the lot/serial numbers that are available in it are offered
  1338. updateselectbatchbywarehouse();
  1339. //Consumption : When a lot/serial number is selected and it is only available in one warehouse, the warehouse is automatically selected
  1340. updateselectwarehousebybatch();
  1341. });
  1342. function updateselectbatchbywarehouse() {
  1343. $(document).on('change', "select[name*='idwarehouse']", function () {
  1344. console.log("We change warehouse so we update the list of possible batch number");
  1345. var selectwarehouse = $(this);
  1346. var selectbatch_name = selectwarehouse.attr('name').replace('idwarehouse', 'batch');
  1347. var selectbatch = $("datalist[id*='" + selectbatch_name + "']");
  1348. var selectedbatch = selectbatch.val();
  1349. var product_element_name = selectwarehouse.attr('name').replace('idwarehouse', 'product');
  1350. $.ajax({
  1351. type: "POST",
  1352. url: "<?php echo DOL_URL_ROOT . '/mrp/ajax/interface.php'; ?>",
  1353. data: {
  1354. action: "updateselectbatchbywarehouse",
  1355. permissiontoproduce: <?php echo $permissiontoproduce ?>,
  1356. warehouse_id: $(this).val(),
  1357. token: '<?php echo currentToken(); ?>',
  1358. product_id: $("input[name='" + product_element_name + "']").val()
  1359. }
  1360. }).done(function (data) {
  1361. selectbatch.empty();
  1362. if (typeof data == "object") {
  1363. console.log("data is already type object, no need to parse it");
  1364. } else {
  1365. console.log("data is type "+(typeof data));
  1366. data = JSON.parse(data);
  1367. }
  1368. selectbatch.append($('<option>', {
  1369. value: '',
  1370. }));
  1371. $.each(data, function (key, value) {
  1372. if(selectwarehouse.val() == -1) {
  1373. var label = " (<?php echo $langs->trans('Stock total') ?> : " + value + ")";
  1374. } else {
  1375. var label = " (<?php echo $langs->trans('Stock') ?> : " + value + ")";
  1376. }
  1377. if(key === selectedbatch) {
  1378. var option ='<option value="'+key+'" selected>'+ label +'</option>';
  1379. } else {
  1380. var option ='<option value="'+key+'">'+ label +'</option>';
  1381. }
  1382. selectbatch.append(option);
  1383. });
  1384. });
  1385. });
  1386. }
  1387. function updateselectwarehousebybatch() {
  1388. $(document).on('change', 'input[name*=batch]', function(){
  1389. console.log("We change batch so we update the list of possible warehouses");
  1390. var selectbatch = $(this);
  1391. var selectwarehouse_name = selectbatch.attr('name').replace('batch', 'idwarehouse');
  1392. var selectwarehouse = $("select[name*='" + selectwarehouse_name + "']");
  1393. var selectedwarehouse = selectwarehouse.val();
  1394. if(selectedwarehouse != -1){
  1395. return;
  1396. }
  1397. var product_element_name = selectbatch.attr('name').replace('batch', 'product');
  1398. $.ajax({
  1399. type: "POST",
  1400. url: "<?php echo DOL_URL_ROOT . '/mrp/ajax/interface.php'; ?>",
  1401. data: {
  1402. action: "updateselectwarehousebybatch",
  1403. permissiontoproduce: <?php echo $permissiontoproduce ?>,
  1404. batch: $(this).val(),
  1405. token: '<?php echo currentToken(); ?>',
  1406. product_id: $("input[name='" + product_element_name + "']").val()
  1407. }
  1408. }).done(function (data) {
  1409. if (typeof data == "object") {
  1410. console.log("data is already type object, no need to parse it");
  1411. } else {
  1412. console.log("data is type "+(typeof data));
  1413. data = JSON.parse(data);
  1414. }
  1415. if(data != 0){
  1416. selectwarehouse.val(data).change();
  1417. }
  1418. });
  1419. });
  1420. }
  1421. </script>
  1422. <?php
  1423. }
  1424. // End of page
  1425. llxFooter();
  1426. $db->close();