mo_production.php 54 KB

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