mo_production.php 54 KB

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