dispatch.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. <?php
  2. /* Copyright (C) 2004-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2010-2021 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2014 Cedric Gross <c.gross@kreiz-it.fr>
  8. * Copyright (C) 2016 Florian Henry <florian.henry@atm-consulting.fr>
  9. * Copyright (C) 2017-2022 Ferran Marcet <fmarcet@2byte.es>
  10. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  11. * Copyright (C) 2019-2020 Christophe Battarel <christophe@altairis.fr>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  25. */
  26. /**
  27. * \file htdocs/fourn/commande/dispatch.php
  28. * \ingroup commande
  29. * \brief Page to dispatch receiving
  30. */
  31. require '../../main.inc.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_order/modules_commandefournisseur.php';
  33. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/lib/fourn.lib.php';
  35. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.dispatch.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  39. if (!empty($conf->project->enabled)) {
  40. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  41. }
  42. // Load translation files required by the page
  43. $langs->loadLangs(array("bills", "orders", "sendings", "companies", "deliveries", "products", "stocks", "receptions"));
  44. if (!empty($conf->productbatch->enabled)) {
  45. $langs->load('productbatch');
  46. }
  47. // Security check
  48. $id = GETPOST("id", 'int');
  49. $ref = GETPOST('ref');
  50. $lineid = GETPOST('lineid', 'int');
  51. $action = GETPOST('action', 'aZ09');
  52. $fk_default_warehouse = GETPOST('fk_default_warehouse', 'int');
  53. $cancel = GETPOST('cancel', 'alpha');
  54. $confirm = GETPOST('confirm', 'alpha');
  55. if ($user->socid) {
  56. $socid = $user->socid;
  57. }
  58. $hookmanager->initHooks(array('ordersupplierdispatch'));
  59. // Recuperation de l'id de projet
  60. $projectid = 0;
  61. if ($_GET["projectid"]) {
  62. $projectid = GETPOST("projectid", 'int');
  63. }
  64. $object = new CommandeFournisseur($db);
  65. if ($id > 0 || !empty($ref)) {
  66. $result = $object->fetch($id, $ref);
  67. if ($result < 0) {
  68. setEventMessages($object->error, $object->errors, 'errors');
  69. }
  70. $result = $object->fetch_thirdparty();
  71. if ($result < 0) {
  72. setEventMessages($object->error, $object->errors, 'errors');
  73. }
  74. }
  75. if (empty($conf->reception->enabled)) {
  76. $permissiontoreceive = $user->rights->fournisseur->commande->receptionner;
  77. $permissiontocontrol = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->fournisseur->commande->receptionner)) || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->fournisseur->commande_advance->check)));
  78. } else {
  79. $permissiontoreceive = $user->rights->reception->creer;
  80. $permissiontocontrol = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->reception->creer)) || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->reception->reception_advance->validate)));
  81. }
  82. // $id is id of a purchase order.
  83. $result = restrictedArea($user, 'fournisseur', $id, 'commande_fournisseur', 'commande');
  84. if (empty($conf->stock->enabled)) {
  85. accessforbidden();
  86. }
  87. /*
  88. * Actions
  89. */
  90. $parameters = array();
  91. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  92. if ($reshook < 0) {
  93. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  94. }
  95. if ($action == 'checkdispatchline' && $permissiontocontrol) {
  96. $error = 0;
  97. $supplierorderdispatch = new CommandeFournisseurDispatch($db);
  98. $db->begin();
  99. $result = $supplierorderdispatch->fetch($lineid);
  100. if (!$result) {
  101. $error++;
  102. setEventMessages($supplierorderdispatch->error, $supplierorderdispatch->errors, 'errors');
  103. $action = '';
  104. }
  105. if (!$error) {
  106. $result = $supplierorderdispatch->setStatut(1);
  107. if ($result < 0) {
  108. setEventMessages($supplierorderdispatch->error, $supplierorderdispatch->errors, 'errors');
  109. $error++;
  110. $action = '';
  111. }
  112. }
  113. if (!$error) {
  114. $result = $object->calcAndSetStatusDispatch($user);
  115. if ($result < 0) {
  116. setEventMessages($object->error, $object->errors, 'errors');
  117. $error++;
  118. $action = '';
  119. }
  120. }
  121. if (!$error) {
  122. $db->commit();
  123. } else {
  124. $db->rollback();
  125. }
  126. }
  127. if ($action == 'uncheckdispatchline' && $permissiontocontrol) {
  128. $error = 0;
  129. $supplierorderdispatch = new CommandeFournisseurDispatch($db);
  130. $db->begin();
  131. $result = $supplierorderdispatch->fetch($lineid);
  132. if (!$result) {
  133. $error++;
  134. setEventMessages($supplierorderdispatch->error, $supplierorderdispatch->errors, 'errors');
  135. $action = '';
  136. }
  137. if (!$error) {
  138. $result = $supplierorderdispatch->setStatut(0);
  139. if ($result < 0) {
  140. setEventMessages($supplierorderdispatch->error, $supplierorderdispatch->errors, 'errors');
  141. $error++;
  142. $action = '';
  143. }
  144. }
  145. if (!$error) {
  146. $result = $object->calcAndSetStatusDispatch($user);
  147. if ($result < 0) {
  148. setEventMessages($object->error, $object->errors, 'errors');
  149. $error++;
  150. $action = '';
  151. }
  152. }
  153. if (!$error) {
  154. $db->commit();
  155. } else {
  156. $db->rollback();
  157. }
  158. }
  159. if ($action == 'denydispatchline' && $permissiontocontrol) {
  160. $error = 0;
  161. $supplierorderdispatch = new CommandeFournisseurDispatch($db);
  162. $db->begin();
  163. $result = $supplierorderdispatch->fetch($lineid);
  164. if (!$result) {
  165. $error++;
  166. setEventMessages($supplierorderdispatch->error, $supplierorderdispatch->errors, 'errors');
  167. $action = '';
  168. }
  169. if (!$error) {
  170. $result = $supplierorderdispatch->setStatut(2);
  171. if ($result < 0) {
  172. setEventMessages($supplierorderdispatch->error, $supplierorderdispatch->errors, 'errors');
  173. $error++;
  174. $action = '';
  175. }
  176. }
  177. if (!$error) {
  178. $result = $object->calcAndSetStatusDispatch($user);
  179. if ($result < 0) {
  180. setEventMessages($object->error, $object->errors, 'errors');
  181. $error++;
  182. $action = '';
  183. }
  184. }
  185. if (!$error) {
  186. $db->commit();
  187. } else {
  188. $db->rollback();
  189. }
  190. }
  191. if ($action == 'dispatch' && $permissiontoreceive) {
  192. $error = 0;
  193. $notrigger = 0;
  194. $db->begin();
  195. $pos = 0;
  196. foreach ($_POST as $key => $value) {
  197. // without batch module enabled
  198. $reg = array();
  199. if (preg_match('/^product_([0-9]+)_([0-9]+)$/i', $key, $reg)) {
  200. $pos++;
  201. // $numline=$reg[2] + 1; // line of product
  202. $numline = $pos;
  203. $prod = "product_".$reg[1].'_'.$reg[2];
  204. $qty = "qty_".$reg[1].'_'.$reg[2];
  205. $ent = "entrepot_".$reg[1].'_'.$reg[2];
  206. if (empty(GETPOST($ent))) {
  207. $ent = $fk_default_warehouse;
  208. }
  209. $pu = "pu_".$reg[1].'_'.$reg[2]; // This is unit price including discount
  210. $fk_commandefourndet = "fk_commandefourndet_".$reg[1].'_'.$reg[2];
  211. if (!empty($conf->global->SUPPLIER_ORDER_CAN_UPDATE_BUYINGPRICE_DURING_RECEIPT)) {
  212. if (empty($conf->multicurrency->enabled) && empty($conf->dynamicprices->enabled)) {
  213. $dto = GETPOST("dto_".$reg[1].'_'.$reg[2], 'int');
  214. if (!empty($dto)) {
  215. $unit_price = price2num(GETPOST("pu_".$reg[1]) * (100 - $dto) / 100, 'MU');
  216. }
  217. $saveprice = "saveprice_".$reg[1].'_'.$reg[2];
  218. }
  219. }
  220. // We ask to move a qty
  221. if (GETPOST($qty) != 0) {
  222. if (!(GETPOST($ent, 'int') > 0)) {
  223. dol_syslog('No dispatch for line '.$key.' as no warehouse was chosen.');
  224. $text = $langs->transnoentities('Warehouse').', '.$langs->transnoentities('Line').' '.($numline);
  225. setEventMessages($langs->trans('ErrorFieldRequired', $text), null, 'errors');
  226. $error++;
  227. }
  228. if (!$error) {
  229. $result = $object->dispatchProduct($user, GETPOST($prod, 'int'), GETPOST($qty), GETPOST($ent, 'int'), GETPOST($pu), GETPOST('comment'), '', '', '', GETPOST($fk_commandefourndet, 'int'), $notrigger);
  230. if ($result < 0) {
  231. setEventMessages($object->error, $object->errors, 'errors');
  232. $error++;
  233. }
  234. if (!$error && !empty($conf->global->SUPPLIER_ORDER_CAN_UPDATE_BUYINGPRICE_DURING_RECEIPT)) {
  235. if (empty($conf->multicurrency->enabled) && empty($conf->dynamicprices->enabled)) {
  236. $dto = price2num(GETPOST("dto_".$reg[1].'_'.$reg[2], 'int'), '');
  237. if (empty($dto)) {
  238. $dto = 0;
  239. }
  240. //update supplier price
  241. if (GETPOSTISSET($saveprice)) {
  242. // TODO Use class
  243. $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
  244. $sql .= " SET unitprice='".price2num(GETPOST($pu), 'MU')."'";
  245. $sql .= ", price=".price2num(GETPOST($pu), 'MU')."*quantity";
  246. $sql .= ", remise_percent = ".((float) $dto);
  247. $sql .= " WHERE fk_soc=".((int) $object->socid);
  248. $sql .= " AND fk_product=".((int) GETPOST($prod, 'int'));
  249. $resql = $db->query($sql);
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. // with batch module enabled
  257. if (preg_match('/^product_batch_([0-9]+)_([0-9]+)$/i', $key, $reg)) {
  258. $pos++;
  259. // eat-by date dispatch
  260. // $numline=$reg[2] + 1; // line of product
  261. $numline = $pos;
  262. $prod = 'product_batch_'.$reg[1].'_'.$reg[2];
  263. $qty = 'qty_'.$reg[1].'_'.$reg[2];
  264. $ent = 'entrepot_'.$reg[1].'_'.$reg[2];
  265. $pu = 'pu_'.$reg[1].'_'.$reg[2];
  266. $fk_commandefourndet = 'fk_commandefourndet_'.$reg[1].'_'.$reg[2];
  267. $lot = 'lot_number_'.$reg[1].'_'.$reg[2];
  268. $dDLUO = dol_mktime(12, 0, 0, GETPOST('dluo_'.$reg[1].'_'.$reg[2].'month', 'int'), GETPOST('dluo_'.$reg[1].'_'.$reg[2].'day', 'int'), GETPOST('dluo_'.$reg[1].'_'.$reg[2].'year', 'int'));
  269. $dDLC = dol_mktime(12, 0, 0, GETPOST('dlc_'.$reg[1].'_'.$reg[2].'month', 'int'), GETPOST('dlc_'.$reg[1].'_'.$reg[2].'day', 'int'), GETPOST('dlc_'.$reg[1].'_'.$reg[2].'year', 'int'));
  270. $fk_commandefourndet = 'fk_commandefourndet_'.$reg[1].'_'.$reg[2];
  271. if (!empty($conf->global->SUPPLIER_ORDER_CAN_UPDATE_BUYINGPRICE_DURING_RECEIPT)) {
  272. if (empty($conf->multicurrency->enabled) && empty($conf->dynamicprices->enabled)) {
  273. $dto = GETPOST("dto_".$reg[1].'_'.$reg[2], 'int');
  274. if (!empty($dto)) {
  275. $unit_price = price2num(GETPOST("pu_".$reg[1]) * (100 - $dto) / 100, 'MU');
  276. }
  277. $saveprice = "saveprice_".$reg[1].'_'.$reg[2];
  278. }
  279. }
  280. // We ask to move a qty
  281. if (GETPOST($qty) > 0) {
  282. if (!(GETPOST($ent, 'int') > 0)) {
  283. dol_syslog('No dispatch for line '.$key.' as no warehouse was chosen.');
  284. $text = $langs->transnoentities('Warehouse').', '.$langs->transnoentities('Line').' '.($numline).'-'.($reg[1] + 1);
  285. setEventMessages($langs->trans('ErrorFieldRequired', $text), null, 'errors');
  286. $error++;
  287. }
  288. if (!(GETPOST($lot, 'alpha') || $dDLUO || $dDLC)) {
  289. dol_syslog('No dispatch for line '.$key.' as serial/eat-by/sellby date are not set');
  290. $text = $langs->transnoentities('atleast1batchfield').', '.$langs->transnoentities('Line').' '.($numline).'-'.($reg[1] + 1);
  291. setEventMessages($langs->trans('ErrorFieldRequired', $text), null, 'errors');
  292. $error++;
  293. }
  294. if (!$error) {
  295. $result = $object->dispatchProduct($user, GETPOST($prod, 'int'), GETPOST($qty), GETPOST($ent, 'int'), GETPOST($pu), GETPOST('comment'), $dDLUO, $dDLC, GETPOST($lot, 'alpha'), GETPOST($fk_commandefourndet, 'int'), $notrigger);
  296. if ($result < 0) {
  297. setEventMessages($object->error, $object->errors, 'errors');
  298. $error++;
  299. }
  300. if (!$error && !empty($conf->global->SUPPLIER_ORDER_CAN_UPDATE_BUYINGPRICE_DURING_RECEIPT)) {
  301. if (empty($conf->multicurrency->enabled) && empty($conf->dynamicprices->enabled)) {
  302. $dto = GETPOST("dto_".$reg[1].'_'.$reg[2], 'int');
  303. //update supplier price
  304. if (GETPOSTISSET($saveprice)) {
  305. // TODO Use class
  306. $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
  307. $sql .= " SET unitprice = ".price2num(GETPOST($pu), 'MU', 2);
  308. $sql .= ", price = ".price2num(GETPOST($pu), 'MU', 2)." * quantity";
  309. $sql .= ", remise_percent = ".price2num((empty($dto) ? 0 : $dto), 3, 2)."'";
  310. $sql .= " WHERE fk_soc = ".((int) $object->socid);
  311. $sql .= " AND fk_product=".((int) GETPOST($prod, 'int'));
  312. $resql = $db->query($sql);
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. }
  320. if (!$error) {
  321. $result = $object->calcAndSetStatusDispatch($user, GETPOST('closeopenorder') ? 1 : 0, GETPOST('comment'));
  322. if ($result < 0) {
  323. setEventMessages($object->error, $object->errors, 'errors');
  324. $error++;
  325. }
  326. }
  327. if (!$error) {
  328. global $conf, $langs, $user;
  329. // Call trigger
  330. $result = $object->call_trigger('ORDER_SUPPLIER_DISPATCH', $user);
  331. // End call triggers
  332. if ($result < 0) {
  333. setEventMessages($object->error, $object->errors, 'errors');
  334. $error++;
  335. }
  336. }
  337. if ($result >= 0 && !$error) {
  338. $db->commit();
  339. header("Location: dispatch.php?id=".$id);
  340. exit();
  341. } else {
  342. $db->rollback();
  343. }
  344. }
  345. // Remove a dispatched line
  346. if ($action == 'confirm_deleteline' && $confirm == 'yes' && $permissiontoreceive) {
  347. $db->begin();
  348. $supplierorderdispatch = new CommandeFournisseurDispatch($db);
  349. $result = $supplierorderdispatch->fetch($lineid);
  350. if ($result > 0) {
  351. $qty = $supplierorderdispatch->qty;
  352. $entrepot = $supplierorderdispatch->fk_entrepot;
  353. $product = $supplierorderdispatch->fk_product;
  354. $price = price2num(GETPOST('price', 'alpha'), 'MU');
  355. $comment = $supplierorderdispatch->comment;
  356. $eatby = $supplierorderdispatch->eatby;
  357. $sellby = $supplierorderdispatch->sellby;
  358. $batch = $supplierorderdispatch->batch;
  359. $result = $supplierorderdispatch->delete($user);
  360. }
  361. if ($result < 0) {
  362. $errors = $object->errors;
  363. $error++;
  364. } else {
  365. // If module stock is enabled and the stock increase is done on purchase order dispatching
  366. if ($entrepot > 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) && empty($supplierorderdispatch->fk_reception)) {
  367. $mouv = new MouvementStock($db);
  368. if ($product > 0) {
  369. $mouv->origin = &$object;
  370. $mouv->setOrigin($object->element, $object->id);
  371. $result = $mouv->livraison($user, $product, $entrepot, $qty, $price, $comment, '', $eatby, $sellby, $batch);
  372. if ($result < 0) {
  373. $errors = $mouv->errors;
  374. $error++;
  375. }
  376. }
  377. }
  378. }
  379. if ($error > 0) {
  380. $db->rollback();
  381. setEventMessages($error, $errors, 'errors');
  382. } else {
  383. $db->commit();
  384. }
  385. }
  386. // Update a dispatched line
  387. if ($action == 'updateline' && $permissiontoreceive) {
  388. $db->begin();
  389. $error = 0;
  390. $supplierorderdispatch = new CommandeFournisseurDispatch($db);
  391. $result = $supplierorderdispatch->fetch($lineid);
  392. if ($result > 0) {
  393. $qty = $supplierorderdispatch->qty;
  394. $entrepot = $supplierorderdispatch->fk_entrepot;
  395. $product = $supplierorderdispatch->fk_product;
  396. $price = price2num(GETPOST('price'), '', 2);
  397. $comment = $supplierorderdispatch->comment;
  398. $eatby = $supplierorderdispatch->eatby;
  399. $sellby = $supplierorderdispatch->sellby;
  400. $batch = $supplierorderdispatch->batch;
  401. $supplierorderdispatch->qty = price2num(GETPOST('qty', 'alpha'), 'MS', 2);
  402. $supplierorderdispatch->fk_entrepot = GETPOST('fk_entrepot');
  403. $result = $supplierorderdispatch->update($user);
  404. }
  405. if ($result < 0) {
  406. $error++;
  407. $errors = $supplierorderdispatch->errors;
  408. } else {
  409. // If module stock is enabled and the stock increase is done on purchase order dispatching
  410. if ($entrepot > 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
  411. $mouv = new MouvementStock($db);
  412. if ($product > 0) {
  413. $mouv->origin = &$object;
  414. $mouv->setOrigin($object->element, $object->id);
  415. $result = $mouv->livraison($user, $product, $entrepot, $qty, $price, $comment, '', $eatby, $sellby, $batch);
  416. if ($result < 0) {
  417. $errors = $mouv->errors;
  418. $error++;
  419. } else {
  420. $mouv->origin = &$object;
  421. $result = $mouv->reception($user, $product, $supplierorderdispatch->fk_entrepot, $supplierorderdispatch->qty, $price, $comment, $eatby, $sellby, $batch);
  422. if ($result < 0) {
  423. $errors = $mouv->errors;
  424. $error++;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. if ($error > 0) {
  431. $db->rollback();
  432. setEventMessages($error, $errors, 'errors');
  433. } else {
  434. $db->commit();
  435. }
  436. }
  437. /*
  438. * View
  439. */
  440. $now = dol_now();
  441. $form = new Form($db);
  442. $formproduct = new FormProduct($db);
  443. $warehouse_static = new Entrepot($db);
  444. $supplierorderdispatch = new CommandeFournisseurDispatch($db);
  445. $help_url = 'EN:Module_Suppliers_Orders|FR:CommandeFournisseur|ES:Módulo_Pedidos_a_proveedores';
  446. $morejs = array('/fourn/js/lib_dispatch.js.php');
  447. llxHeader('', $langs->trans("OrderDispatch"), $help_url, '', 0, 0, $morejs);
  448. if ($id > 0 || !empty($ref)) {
  449. $soc = new Societe($db);
  450. $soc->fetch($object->socid);
  451. $author = new User($db);
  452. $author->fetch($object->user_author_id);
  453. $head = ordersupplier_prepare_head($object);
  454. $title = $langs->trans("SupplierOrder");
  455. print dol_get_fiche_head($head, 'dispatch', $title, -1, 'order');
  456. $formconfirm = '';
  457. // Confirmation to delete line
  458. if ($action == 'ask_deleteline') {
  459. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_deleteline', '', 0, 1);
  460. }
  461. // Call Hook formConfirm
  462. $parameters = array('lineid' => $lineid);
  463. // Note that $action and $object may be modified by hook
  464. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action);
  465. if (empty($reshook)) {
  466. $formconfirm .= $hookmanager->resPrint;
  467. } elseif ($reshook > 0) {
  468. $formconfirm = $hookmanager->resPrint;
  469. }
  470. // Print form confirm
  471. print $formconfirm;
  472. // Supplier order card
  473. $linkback = '<a href="'.DOL_URL_ROOT.'/fourn/commande/list.php'.(!empty($socid) ? '?socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  474. $morehtmlref = '<div class="refidno">';
  475. // Ref supplier
  476. $morehtmlref .= $form->editfieldkey("RefSupplier", 'ref_supplier', $object->ref_supplier, $object, 0, 'string', '', 0, 1);
  477. $morehtmlref .= $form->editfieldval("RefSupplier", 'ref_supplier', $object->ref_supplier, $object, 0, 'string', '', null, null, '', 1);
  478. // Thirdparty
  479. $morehtmlref .= '<br>'.$langs->trans('ThirdParty').' : '.$object->thirdparty->getNomUrl(1);
  480. // Project
  481. if (!empty($conf->project->enabled)) {
  482. $langs->load("projects");
  483. $morehtmlref .= '<br>'.$langs->trans('Project').' ';
  484. if ($user->rights->fournisseur->commande->creer || $user->rights->supplier_order->creer) {
  485. if ($action != 'classify') {
  486. //$morehtmlref.='<a class="editfielda" href="' . $_SERVER['PHP_SELF'] . '?action=classify&token='.newToken().'&id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
  487. $morehtmlref .= ' : ';
  488. }
  489. if ($action == 'classify') {
  490. //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
  491. $morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
  492. $morehtmlref .= '<input type="hidden" name="action" value="classin">';
  493. $morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
  494. $morehtmlref .= $formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
  495. $morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  496. $morehtmlref .= '</form>';
  497. } else {
  498. $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
  499. }
  500. } else {
  501. if (!empty($object->fk_project)) {
  502. $proj = new Project($db);
  503. $proj->fetch($object->fk_project);
  504. $morehtmlref .= ' : '.$proj->getNomUrl(1);
  505. if ($proj->title) {
  506. $morehtmlref .= ' - '.$proj->title;
  507. }
  508. } else {
  509. $morehtmlref .= '';
  510. }
  511. }
  512. }
  513. $morehtmlref .= '</div>';
  514. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  515. print '<div class="fichecenter">';
  516. print '<div class="underbanner clearboth"></div>';
  517. print '<table class="border tableforfield" width="100%">';
  518. // Date
  519. if ($object->methode_commande_id > 0) {
  520. print '<tr><td class="titlefield">'.$langs->trans("Date").'</td><td>';
  521. if ($object->date_commande) {
  522. print dol_print_date($object->date_commande, "dayhour")."\n";
  523. }
  524. print "</td></tr>";
  525. if ($object->methode_commande) {
  526. print '<tr><td>'.$langs->trans("Method").'</td><td>'.$object->getInputMethod().'</td></tr>';
  527. }
  528. }
  529. // Author
  530. print '<tr><td class="titlefield">'.$langs->trans("AuthorRequest").'</td>';
  531. print '<td>'.$author->getNomUrl(1, '', 0, 0, 0).'</td>';
  532. print '</tr>';
  533. $parameters = array();
  534. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  535. print "</table>";
  536. print '</div>';
  537. // if ($mesg) print $mesg;
  538. print '<br>';
  539. /*$disabled = 1;
  540. if (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
  541. $disabled = 0;
  542. }*/
  543. $disabled = 0; // This is used to disable or not the bulk selection of target warehouse. No reason to have it disabled so forced to 0.
  544. // Line of orders
  545. if ($object->statut <= CommandeFournisseur::STATUS_ACCEPTED || $object->statut >= CommandeFournisseur::STATUS_CANCELED) {
  546. print '<br><span class="opacitymedium">'.$langs->trans("OrderStatusNotReadyToDispatch").'</span>';
  547. }
  548. if ($object->statut == CommandeFournisseur::STATUS_ORDERSENT
  549. || $object->statut == CommandeFournisseur::STATUS_RECEIVED_PARTIALLY
  550. || $object->statut == CommandeFournisseur::STATUS_RECEIVED_COMPLETELY) {
  551. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  552. $formproduct = new FormProduct($db);
  553. $formproduct->loadWarehouses();
  554. $entrepot = new Entrepot($db);
  555. $listwarehouses = $entrepot->list_array(1);
  556. if (empty($conf->reception->enabled)) {
  557. print '<form method="POST" action="dispatch.php?id='.$object->id.'">';
  558. } else {
  559. print '<form method="post" action="'.dol_buildpath('/reception/card.php', 1).'?originid='.$object->id.'&origin=supplierorder">';
  560. }
  561. print '<input type="hidden" name="token" value="'.newToken().'">';
  562. if (empty($conf->reception->enabled)) {
  563. print '<input type="hidden" name="action" value="dispatch">';
  564. } else {
  565. print '<input type="hidden" name="action" value="create">';
  566. }
  567. print '<div class="div-table-responsive-no-min">';
  568. print '<table class="noborder centpercent">';
  569. // Set $products_dispatched with qty dispatched for each product id
  570. $products_dispatched = array();
  571. $sql = "SELECT l.rowid, cfd.fk_product, sum(cfd.qty) as qty";
  572. $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as cfd";
  573. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."commande_fournisseurdet as l on l.rowid = cfd.fk_commandefourndet";
  574. $sql .= " WHERE cfd.fk_commande = ".((int) $object->id);
  575. $sql .= " GROUP BY l.rowid, cfd.fk_product";
  576. $resql = $db->query($sql);
  577. if ($resql) {
  578. $num = $db->num_rows($resql);
  579. $i = 0;
  580. if ($num) {
  581. while ($i < $num) {
  582. $objd = $db->fetch_object($resql);
  583. $products_dispatched[$objd->rowid] = price2num($objd->qty, 5);
  584. $i++;
  585. }
  586. }
  587. $db->free($resql);
  588. }
  589. //$sql = "SELECT l.rowid, l.fk_product, l.subprice, l.remise_percent, l.ref AS sref, SUM(l.qty) as qty,";
  590. $sql = "SELECT l.rowid, l.fk_product, l.subprice, l.remise_percent, l.ref AS sref, l.qty as qty,";
  591. $sql .= " p.ref, p.label, p.tobatch, p.fk_default_warehouse";
  592. // Enable hooks to alter the SQL query (SELECT)
  593. $parameters = array();
  594. $reshook = $hookmanager->executeHooks(
  595. 'printFieldListSelect',
  596. $parameters,
  597. $object,
  598. $action
  599. );
  600. if ($reshook < 0) {
  601. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  602. }
  603. $sql .= $hookmanager->resPrint;
  604. $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as l";
  605. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON l.fk_product=p.rowid";
  606. $sql .= " WHERE l.fk_commande = ".((int) $object->id);
  607. if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
  608. $sql .= " AND l.product_type = 0";
  609. }
  610. // Enable hooks to alter the SQL query (WHERE)
  611. $parameters = array();
  612. $reshook = $hookmanager->executeHooks(
  613. 'printFieldListWhere',
  614. $parameters,
  615. $object,
  616. $action
  617. );
  618. if ($reshook < 0) {
  619. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  620. }
  621. $sql .= $hookmanager->resPrint;
  622. //$sql .= " GROUP BY p.ref, p.label, p.tobatch, p.fk_default_warehouse, l.rowid, l.fk_product, l.subprice, l.remise_percent, l.ref"; // Calculation of amount dispatched is done per fk_product so we must group by fk_product
  623. // Added by MMI Mathieu Moulin iProspective
  624. // Sort commandedet lines by id when receive
  625. if (!empty($conf->global->ORDER_DISPATCH_BY_COMMANDEDET_ID))
  626. $sql .= " ORDER BY l.rowid";
  627. else
  628. $sql .= " ORDER BY l.rang, p.ref, p.label";
  629. $resql = $db->query($sql);
  630. if ($resql) {
  631. $num = $db->num_rows($resql);
  632. $i = 0;
  633. if ($num) {
  634. print '<tr class="liste_titre">';
  635. print '<td>'.$langs->trans("Description").'</td>';
  636. if (!empty($conf->productbatch->enabled)) {
  637. print '<td class="dispatch_batch_number_title">'.$langs->trans("batch_number").'</td>';
  638. if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  639. print '<td class="dispatch_dlc_title">'.$langs->trans("SellByDate").'</td>';
  640. }
  641. if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  642. print '<td class="dispatch_dluo_title">'.$langs->trans("EatByDate").'</td>';
  643. }
  644. } else {
  645. print '<td></td>';
  646. print '<td></td>';
  647. print '<td></td>';
  648. }
  649. print '<td class="right">'.$langs->trans("SupplierRef").'</td>';
  650. print '<td class="right">'.$langs->trans("QtyOrdered").'</td>';
  651. print '<td class="right">'.$langs->trans("QtyDispatchedShort").'</td>';
  652. print ' <td class="right">'.$langs->trans("QtyToDispatchShort");
  653. print '<br><a href="#" id="autoreset">'.$langs->trans("Reset").'</a></td>';
  654. print '<td width="32"></td>';
  655. if (!empty($conf->global->SUPPLIER_ORDER_CAN_UPDATE_BUYINGPRICE_DURING_RECEIPT)) {
  656. if (empty($conf->multicurrency->enabled) && empty($conf->dynamicprices->enabled)) {
  657. print '<td class="right">'.$langs->trans("Price").'</td>';
  658. print '<td class="right">'.$langs->trans("ReductionShort").' (%)</td>';
  659. print '<td class="right">'.$langs->trans("UpdatePrice").'</td>';
  660. }
  661. }
  662. print '<td align="right">'.$langs->trans("Warehouse");
  663. // Select warehouse to force it everywhere
  664. if (count($listwarehouses) > 1) {
  665. print '<br><span class="opacitymedium">'.$langs->trans("ForceTo").'</span> '.$form->selectarray('fk_default_warehouse', $listwarehouses, $fk_default_warehouse, 1, 0, 0, '', 0, 0, $disabled, '', 'minwidth100 maxwidth300', 1);
  666. } elseif (count($listwarehouses) == 1) {
  667. print '<br><span class="opacitymedium">'.$langs->trans("ForceTo").'</span> '.$form->selectarray('fk_default_warehouse', $listwarehouses, $fk_default_warehouse, 0, 0, 0, '', 0, 0, $disabled, '', 'minwidth100 maxwidth300', 1);
  668. }
  669. print '</td>';
  670. // Enable hooks to append additional columns
  671. $parameters = array();
  672. $reshook = $hookmanager->executeHooks(
  673. 'printFieldListTitle',
  674. $parameters,
  675. $object,
  676. $action
  677. );
  678. if ($reshook < 0) {
  679. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  680. }
  681. print $hookmanager->resPrint;
  682. print "</tr>\n";
  683. }
  684. $nbfreeproduct = 0; // Nb of lins of free products/services
  685. $nbproduct = 0; // Nb of predefined product lines to dispatch (already done or not) if SUPPLIER_ORDER_DISABLE_STOCK_DISPATCH_WHEN_TOTAL_REACHED is off (default)
  686. // or nb of line that remain to dispatch if SUPPLIER_ORDER_DISABLE_STOCK_DISPATCH_WHEN_TOTAL_REACHED is on.
  687. $conf->cache['product'] = array();
  688. while ($i < $num) {
  689. $objp = $db->fetch_object($resql);
  690. // On n'affiche pas les produits libres
  691. if (!$objp->fk_product > 0) {
  692. $nbfreeproduct++;
  693. } else {
  694. $remaintodispatch = price2num($objp->qty - ((float) $products_dispatched[$objp->rowid]), 5); // Calculation of dispatched
  695. if ($remaintodispatch < 0 && empty($conf->global->SUPPLIER_ORDER_ALLOW_NEGATIVE_QTY_FOR_SUPPLIER_ORDER_RETURN)) {
  696. $remaintodispatch = 0;
  697. }
  698. if ($remaintodispatch || empty($conf->global->SUPPLIER_ORDER_DISABLE_STOCK_DISPATCH_WHEN_TOTAL_REACHED)) {
  699. $nbproduct++;
  700. // To show detail cref and description value, we must make calculation by cref
  701. // print ($objp->cref?' ('.$objp->cref.')':'');
  702. // if ($objp->description) print '<br>'.nl2br($objp->description);
  703. $suffix = '_0_'.$i;
  704. print "\n";
  705. print '<!-- Line to dispatch '.$suffix.' -->'."\n";
  706. // hidden fields for js function
  707. print '<input id="qty_ordered'.$suffix.'" type="hidden" value="'.$objp->qty.'">';
  708. print '<input id="qty_dispatched'.$suffix.'" type="hidden" value="'.(float) $products_dispatched[$objp->rowid].'">';
  709. print '<tr class="oddeven">';
  710. if (empty($conf->cache['product'][$objp->fk_product])) {
  711. $tmpproduct = new Product($db);
  712. $tmpproduct->fetch($objp->fk_product);
  713. $conf->cache['product'][$objp->fk_product] = $tmpproduct;
  714. } else {
  715. $tmpproduct = $conf->cache['product'][$objp->fk_product];
  716. }
  717. $linktoprod = $tmpproduct->getNomUrl(1);
  718. $linktoprod .= ' - '.$objp->label."\n";
  719. if (!empty($conf->productbatch->enabled)) {
  720. if ($objp->tobatch) {
  721. // Product
  722. print '<td>';
  723. print $linktoprod;
  724. print "</td>";
  725. print '<td class="dispatch_batch_number"></td>';
  726. if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  727. print '<td class="dispatch_dlc"></td>';
  728. }
  729. if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  730. print '<td class="dispatch_dluo"></td>';
  731. }
  732. } else {
  733. // Product
  734. print '<td>';
  735. print $linktoprod;
  736. print "</td>";
  737. print '<td class="dispatch_batch_number">';
  738. print $langs->trans("ProductDoesNotUseBatchSerial");
  739. print '</td>';
  740. if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  741. print '<td class="dispatch_dlc"></td>';
  742. }
  743. if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  744. print '<td class="dispatch_dluo"></td>';
  745. }
  746. }
  747. } else {
  748. print '<td colspan="4">';
  749. print $linktoprod;
  750. print "</td>";
  751. }
  752. // Define unit price for PMP calculation
  753. $up_ht_disc = $objp->subprice;
  754. if (!empty($objp->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) {
  755. $up_ht_disc = price2num($up_ht_disc * (100 - $objp->remise_percent) / 100, 'MU');
  756. }
  757. // Supplier ref
  758. print '<td class="right">'.$objp->sref.'</td>';
  759. // Qty ordered
  760. print '<td class="right">'.$objp->qty.'</td>';
  761. // Already dispatched
  762. print '<td class="right">'.$products_dispatched[$objp->rowid].'</td>';
  763. if (!empty($conf->productbatch->enabled) && $objp->tobatch > 0) {
  764. $type = 'batch';
  765. print '<td class="right">';
  766. print '</td>'; // Qty to dispatch
  767. print '<td>';
  768. //print img_picto($langs->trans('AddDispatchBatchLine'), 'split.png', 'onClick="addDispatchLine(' . $i . ',\'' . $type . '\')"');
  769. print '</td>'; // Dispatch column
  770. print '<td></td>'; // Warehouse column
  771. // Enable hooks to append additional columns
  772. $parameters = array(
  773. 'is_information_row' => true, // allows hook to distinguish between the
  774. // rows with information and the rows with
  775. // dispatch form input
  776. 'objp' => $objp
  777. );
  778. $reshook = $hookmanager->executeHooks(
  779. 'printFieldListValue',
  780. $parameters,
  781. $object,
  782. $action
  783. );
  784. if ($reshook < 0) {
  785. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  786. }
  787. print $hookmanager->resPrint;
  788. print '</tr>';
  789. print '<tr class="oddeven" name="'.$type.$suffix.'">';
  790. print '<td>';
  791. print '<input name="fk_commandefourndet'.$suffix.'" type="hidden" value="'.$objp->rowid.'">';
  792. print '<input name="product_batch'.$suffix.'" type="hidden" value="'.$objp->fk_product.'">';
  793. print '<!-- This is a up (may include discount or not depending on STOCK_EXCLUDE_DISCOUNT_FOR_PMP. will be used for PMP calculation) -->';
  794. if (!empty($conf->global->SUPPLIER_ORDER_EDIT_BUYINGPRICE_DURING_RECEIPT)) { // Not tested !
  795. print $langs->trans("BuyingPrice").': <input class="maxwidth75" name="pu'.$suffix.'" type="text" value="'.price2num($up_ht_disc, 'MU').'">';
  796. } else {
  797. print '<input class="maxwidth75" name="pu'.$suffix.'" type="hidden" value="'.price2num($up_ht_disc, 'MU').'">';
  798. }
  799. print '</td>';
  800. print '<td>';
  801. print '<input type="text" class="inputlotnumber quatrevingtquinzepercent" id="lot_number'.$suffix.'" name="lot_number'.$suffix.'" value="'.GETPOST('lot_number'.$suffix).'">';
  802. print '</td>';
  803. if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  804. print '<td class="nowraponall">';
  805. $dlcdatesuffix = dol_mktime(0, 0, 0, GETPOST('dlc'.$suffix.'month'), GETPOST('dlc'.$suffix.'day'), GETPOST('dlc'.$suffix.'year'));
  806. print $form->selectDate($dlcdatesuffix, 'dlc'.$suffix, '', '', 1, '');
  807. print '</td>';
  808. }
  809. if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  810. print '<td class="nowraponall">';
  811. $dluodatesuffix = dol_mktime(0, 0, 0, GETPOST('dluo'.$suffix.'month'), GETPOST('dluo'.$suffix.'day'), GETPOST('dluo'.$suffix.'year'));
  812. print $form->selectDate($dluodatesuffix, 'dluo'.$suffix, '', '', 1, '');
  813. print '</td>';
  814. }
  815. print '<td colspan="3">&nbsp;</td>'; // Supplier ref + Qty ordered + qty already dispatched
  816. } else {
  817. $type = 'dispatch';
  818. $colspan = 7;
  819. $colspan = (!empty($conf->global->PRODUCT_DISABLE_SELLBY)) ? --$colspan : $colspan;
  820. $colspan = (!empty($conf->global->PRODUCT_DISABLE_EATBY)) ? --$colspan : $colspan;
  821. print '<td class="right">';
  822. print '</td>'; // Qty to dispatch
  823. print '<td>';
  824. //print img_picto($langs->trans('AddStockLocationLine'), 'split.png', 'onClick="addDispatchLine(' . $i . ',\'' . $type . '\')"');
  825. print '</td>'; // Dispatch column
  826. print '<td></td>'; // Warehouse column
  827. // Enable hooks to append additional columns
  828. $parameters = array(
  829. 'is_information_row' => true, // allows hook to distinguish between the
  830. // rows with information and the rows with
  831. // dispatch form input
  832. 'objp' => $objp
  833. );
  834. $reshook = $hookmanager->executeHooks(
  835. 'printFieldListValue',
  836. $parameters,
  837. $object,
  838. $action
  839. );
  840. if ($reshook < 0) {
  841. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  842. }
  843. print $hookmanager->resPrint;
  844. print '</tr>';
  845. print '<tr class="oddeven" name="'.$type.$suffix.'">';
  846. print '<td colspan="'.$colspan.'">';
  847. print '<input name="fk_commandefourndet'.$suffix.'" type="hidden" value="'.$objp->rowid.'">';
  848. print '<input name="product'.$suffix.'" type="hidden" value="'.$objp->fk_product.'">';
  849. print '<!-- This is a up (may include discount or not depending on STOCK_EXCLUDE_DISCOUNT_FOR_PMP. will be used for PMP calculation) -->';
  850. if (!empty($conf->global->SUPPLIER_ORDER_EDIT_BUYINGPRICE_DURING_RECEIPT)) { // Not tested !
  851. print $langs->trans("BuyingPrice").': <input class="maxwidth75" name="pu'.$suffix.'" type="text" value="'.price2num($up_ht_disc, 'MU').'">';
  852. } else {
  853. print '<input class="maxwidth75" name="pu'.$suffix.'" type="hidden" value="'.price2num($up_ht_disc, 'MU').'">';
  854. }
  855. print '</td>';
  856. }
  857. // Qty to dispatch
  858. print '<td class="right">';
  859. print '<input id="qty'.$suffix.'" name="qty'.$suffix.'" type="text" class="width50 right" value="'.(GETPOSTISSET('qty'.$suffix) ? GETPOST('qty'.$suffix, 'int') : (empty($conf->global->SUPPLIER_ORDER_DISPATCH_FORCE_QTY_INPUT_TO_ZERO) ? $remaintodispatch : 0)).'">';
  860. print '</td>';
  861. print '<td>';
  862. if (!empty($conf->productbatch->enabled) && $objp->tobatch > 0) {
  863. $type = 'batch';
  864. print img_picto($langs->trans('AddStockLocationLine'), 'split.png', 'class="splitbutton" onClick="addDispatchLine('.$i.', \''.$type.'\')"');
  865. } else {
  866. $type = 'dispatch';
  867. print img_picto($langs->trans('AddStockLocationLine'), 'split.png', 'class="splitbutton" onClick="addDispatchLine('.$i.', \''.$type.'\')"');
  868. }
  869. print '</td>';
  870. if (!empty($conf->global->SUPPLIER_ORDER_CAN_UPDATE_BUYINGPRICE_DURING_RECEIPT)) {
  871. if (empty($conf->multicurrency->enabled) && empty($conf->dynamicprices->enabled)) {
  872. // Price
  873. print '<td class="right">';
  874. print '<input id="pu'.$suffix.'" name="pu'.$suffix.'" type="text" size="8" value="'.price((GETPOST('pu'.$suffix) != '' ? price2num(GETPOST('pu'.$suffix)) : $up_ht_disc)).'">';
  875. print '</td>';
  876. // Discount
  877. print '<td class="right">';
  878. print '<input id="dto'.$suffix.'" name="dto'.$suffix.'" type="text" size="8" value="'.(GETPOST('dto'.$suffix) != '' ? GETPOST('dto'.$suffix) : '').'">';
  879. print '</td>';
  880. // Save price
  881. print '<td class="center">';
  882. print '<input class="flat checkformerge" type="checkbox" name="saveprice'.$suffix.'" value="'.(GETPOST('saveprice'.$suffix) != '' ? GETPOST('saveprice'.$suffix) : '').'">';
  883. print '</td>';
  884. }
  885. }
  886. // Warehouse
  887. print '<td class="right">';
  888. if (count($listwarehouses) > 1) {
  889. print $formproduct->selectWarehouses(GETPOST("entrepot".$suffix) ?GETPOST("entrepot".$suffix) : ($objp->fk_default_warehouse ? $objp->fk_default_warehouse : ''), "entrepot".$suffix, '', 1, 0, $objp->fk_product, '', 1, 0, null, 'csswarehouse'.$suffix);
  890. } elseif (count($listwarehouses) == 1) {
  891. print $formproduct->selectWarehouses(GETPOST("entrepot".$suffix) ?GETPOST("entrepot".$suffix) : ($objp->fk_default_warehouse ? $objp->fk_default_warehouse : ''), "entrepot".$suffix, '', 0, 0, $objp->fk_product, '', 1, 0, null, 'csswarehouse'.$suffix);
  892. } else {
  893. $langs->load("errors");
  894. print $langs->trans("ErrorNoWarehouseDefined");
  895. }
  896. print "</td>\n";
  897. // Enable hooks to append additional columns
  898. $parameters = array(
  899. 'is_information_row' => false // this is a dispatch form row
  900. );
  901. $reshook = $hookmanager->executeHooks(
  902. 'printFieldListValue',
  903. $parameters,
  904. $object,
  905. $action
  906. );
  907. if ($reshook < 0) {
  908. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  909. }
  910. print $hookmanager->resPrint;
  911. print "</tr>\n";
  912. }
  913. }
  914. $i++;
  915. }
  916. $db->free($resql);
  917. } else {
  918. dol_print_error($db);
  919. }
  920. print "</table>\n";
  921. print '</div>';
  922. if ($nbproduct) {
  923. $checkboxlabel = $langs->trans("CloseReceivedSupplierOrdersAutomatically", $langs->transnoentitiesnoconv('StatusOrderReceivedAll'));
  924. print '<div class="center">';
  925. $parameters = array();
  926. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
  927. // modified by hook
  928. if (empty($reshook)) {
  929. if (empty($conf->reception->enabled)) {
  930. print $langs->trans("Comment").' : ';
  931. print '<input type="text" class="minwidth400" maxlength="128" name="comment" value="';
  932. print GETPOSTISSET("comment") ? GETPOST("comment") : $langs->trans("DispatchSupplierOrder", $object->ref);
  933. // print ' / '.$object->ref_supplier; // Not yet available
  934. print '" class="flat"><br>';
  935. print '<input type="checkbox" checked="checked" name="closeopenorder"> '.$checkboxlabel;
  936. }
  937. $dispatchBt = empty($conf->reception->enabled) ? $langs->trans("Receive") : $langs->trans("CreateReception");
  938. print '<br>';
  939. print '<input type="submit" class="button" name="dispatch" value="'.dol_escape_htmltag($dispatchBt).'"';
  940. $disabled = 0;
  941. if (!$permissiontoreceive) {
  942. $disabled = 1;
  943. }
  944. if (count($listwarehouses) <= 0) {
  945. $disabled = 1;
  946. }
  947. if ($disabled) {
  948. print ' disabled';
  949. }
  950. print '>';
  951. }
  952. print '</div>';
  953. }
  954. // Message if nothing to dispatch
  955. if (!$nbproduct) {
  956. print "<br>\n";
  957. if (empty($conf->global->SUPPLIER_ORDER_DISABLE_STOCK_DISPATCH_WHEN_TOTAL_REACHED)) {
  958. print '<div class="opacitymedium">'.$langs->trans("NoPredefinedProductToDispatch").'</div>'; // No predefined line at all
  959. } else {
  960. print '<div class="opacitymedium">'.$langs->trans("NoMorePredefinedProductToDispatch").'</div>'; // No predefined line that remain to be dispatched.
  961. }
  962. }
  963. print '</form>';
  964. }
  965. print dol_get_fiche_end();
  966. // traitement entrepot par défaut
  967. print '<script type="text/javascript">
  968. $(document).ready(function () {
  969. $("select[name=fk_default_warehouse]").change(function() {
  970. var fk_default_warehouse = $("option:selected", this).val();
  971. $("select[name^=entrepot_]").val(fk_default_warehouse).change();
  972. });
  973. jQuery("#autoreset").click(function() {';
  974. $i = 0;
  975. while ($i < $nbproduct) {
  976. print ' jQuery("#qty_0_'.$i.'").val("");';
  977. $i++;
  978. }
  979. print '
  980. });
  981. });
  982. </script>';
  983. // List of lines already dispatched
  984. $sql = "SELECT p.rowid as pid, p.ref, p.label,";
  985. $sql .= " e.rowid as warehouse_id, e.ref as entrepot,";
  986. $sql .= " cfd.rowid as dispatchlineid, cfd.fk_product, cfd.qty, cfd.eatby, cfd.sellby, cfd.batch, cfd.comment, cfd.status, cfd.datec";
  987. $sql .= " ,cd.rowid, cd.subprice";
  988. if ($conf->reception->enabled) {
  989. $sql .= " ,cfd.fk_reception, r.date_delivery";
  990. }
  991. $sql .= " FROM ".MAIN_DB_PREFIX."product as p,";
  992. $sql .= " ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as cfd";
  993. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."commande_fournisseurdet as cd ON cd.rowid = cfd.fk_commandefourndet";
  994. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."entrepot as e ON cfd.fk_entrepot = e.rowid";
  995. if ($conf->reception->enabled) {
  996. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."reception as r ON cfd.fk_reception = r.rowid";
  997. }
  998. $sql .= " WHERE cfd.fk_commande = ".((int) $object->id);
  999. $sql .= " AND cfd.fk_product = p.rowid";
  1000. $sql .= " ORDER BY cfd.rowid ASC";
  1001. $resql = $db->query($sql);
  1002. if ($resql) {
  1003. $num = $db->num_rows($resql);
  1004. $i = 0;
  1005. if ($num > 0) {
  1006. print "<br>\n";
  1007. print load_fiche_titre($langs->trans("ReceivingForSameOrder"));
  1008. print '<div class="div-table-responsive">';
  1009. print '<table id="dispatch_received_products" class="noborder centpercent">';
  1010. print '<tr class="liste_titre">';
  1011. // Reception ref
  1012. if ($conf->reception->enabled) {
  1013. print '<td>'.$langs->trans("Reception").'</td>';
  1014. }
  1015. // Product
  1016. print '<td>'.$langs->trans("Product").'</td>';
  1017. print '<td>'.$langs->trans("DateCreation").'</td>';
  1018. print '<td>'.$langs->trans("DateDeliveryPlanned").'</td>';
  1019. if (!empty($conf->productbatch->enabled)) {
  1020. print '<td class="dispatch_batch_number_title">'.$langs->trans("batch_number").'</td>';
  1021. if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  1022. print '<td class="dispatch_dlc_title">'.$langs->trans("SellByDate").'</td>';
  1023. }
  1024. if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  1025. print '<td class="dispatch_dluo_title">'.$langs->trans("EatByDate").'</td>';
  1026. }
  1027. }
  1028. print '<td class="right">'.$langs->trans("QtyDispatched").'</td>';
  1029. print '<td>'.$langs->trans("Warehouse").'</td>';
  1030. print '<td>'.$langs->trans("Comment").'</td>';
  1031. // Status
  1032. if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS) && empty($reception->rowid)) {
  1033. print '<td class="center" colspan="2">'.$langs->trans("Status").'</td>';
  1034. } elseif (!empty($conf->reception->enabled)) {
  1035. print '<td class="center"></td>';
  1036. }
  1037. print '<td class="center" colspan="2"></td>';
  1038. print "</tr>\n";
  1039. while ($i < $num) {
  1040. $objp = $db->fetch_object($resql);
  1041. if ($action == 'editline' && $lineid == $objp->dispatchlineid) {
  1042. print '<form name="editdispatchedlines" id="editdispatchedlines" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'#line_'.GETPOST('lineid', 'int').'" method="POST">
  1043. <input type="hidden" name="token" value="'.newToken().'">
  1044. <input type="hidden" name="action" value="updateline">
  1045. <input type="hidden" name="mode" value="">
  1046. <input type="hidden" name="lineid" value="'.$objp->dispatchlineid.'">';
  1047. }
  1048. print '<tr class="oddeven" id="line_'.$objp->dispatchlineid.'" >';
  1049. // Reception ref
  1050. if (!empty($conf->reception->enabled)) {
  1051. print '<td>';
  1052. if (!empty($objp->fk_reception)) {
  1053. $reception = new Reception($db);
  1054. $reception->fetch($objp->fk_reception);
  1055. print $reception->getNomUrl(1);
  1056. }
  1057. print "</td>";
  1058. }
  1059. // Product
  1060. print '<td>';
  1061. if (empty($conf->cache['product'][$objp->fk_product])) {
  1062. $tmpproduct = new Product($db);
  1063. $tmpproduct->fetch($objp->fk_product);
  1064. $conf->cache['product'][$objp->fk_product] = $tmpproduct;
  1065. } else {
  1066. $tmpproduct = $conf->cache['product'][$objp->fk_product];
  1067. }
  1068. print $tmpproduct->getNomUrl(1);
  1069. print ' - '.$objp->label;
  1070. print "</td>\n";
  1071. print '<td>'.dol_print_date($db->jdate($objp->datec), 'day').'</td>';
  1072. print '<td>'.dol_print_date($db->jdate($objp->date_delivery), 'day').'</td>';
  1073. if (!empty($conf->productbatch->enabled)) {
  1074. if ($objp->batch) {
  1075. include_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
  1076. $lot = new Productlot($db);
  1077. $lot->fetch(0, $objp->pid, $objp->batch);
  1078. print '<td class="dispatch_batch_number">'.$lot->getNomUrl(1).'</td>';
  1079. if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  1080. print '<td class="dispatch_dlc">'.dol_print_date($lot->sellby, 'day').'</td>';
  1081. }
  1082. if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  1083. print '<td class="dispatch_dluo">'.dol_print_date($lot->eatby, 'day').'</td>';
  1084. }
  1085. } else {
  1086. print '<td class="dispatch_batch_number"></td>';
  1087. if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  1088. print '<td class="dispatch_dlc"></td>';
  1089. }
  1090. if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  1091. print '<td class="dispatch_dluo"></td>';
  1092. }
  1093. }
  1094. }
  1095. // Qty
  1096. print '<td class="right">';
  1097. if ($action == 'editline' && $lineid == $objp->dispatchlineid) {
  1098. print '<input style="width: 50px;" type="text" min="1" name="qty" value="'.$objp->qty.'" />';
  1099. } else {
  1100. print $objp->qty;
  1101. }
  1102. print '<input type="hidden" name="price" value="'.$objp->subprice.'" />';
  1103. print '</td>';
  1104. // Warehouse
  1105. print '<td>';
  1106. if ($action == 'editline' && $lineid == $objp->dispatchlineid) {
  1107. if (count($listwarehouses) > 1) {
  1108. print $formproduct->selectWarehouses(GETPOST("fk_entrepot") ?GETPOST("fk_entrepot") : ($objp->warehouse_id ? $objp->warehouse_id : ''), "fk_entrepot", '', 1, 0, $objp->fk_product, '', 1, 1, null, 'csswarehouse');
  1109. } elseif (count($listwarehouses) == 1) {
  1110. print $formproduct->selectWarehouses(GETPOST("fk_entrepot") ?GETPOST("fk_entrepot") : ($objp->warehouse_id ? $objp->warehouse_id : ''), "fk_entrepot", '', 0, 0, $objp->fk_product, '', 1, 1, null, 'csswarehouse');
  1111. } else {
  1112. $langs->load("errors");
  1113. print $langs->trans("ErrorNoWarehouseDefined");
  1114. }
  1115. } else {
  1116. $warehouse_static->id = $objp->warehouse_id;
  1117. $warehouse_static->label = $objp->entrepot;
  1118. print $warehouse_static->getNomUrl(1);
  1119. }
  1120. print '</td>';
  1121. // Comment
  1122. print '<td class="tdoverflowmax300" style="white-space: pre;">'.$objp->comment.'</td>';
  1123. // Status
  1124. if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS) && empty($reception->rowid)) {
  1125. print '<td class="right">';
  1126. $supplierorderdispatch->status = (empty($objp->status) ? 0 : $objp->status);
  1127. // print $supplierorderdispatch->status;
  1128. print $supplierorderdispatch->getLibStatut(5);
  1129. print '</td>';
  1130. // Add button to check/uncheck disaptching
  1131. print '<td class="center">';
  1132. if (!$permissiontocontrol) {
  1133. if (empty($objp->status)) {
  1134. print '<a class="button buttonRefused" href="#">'.$langs->trans("Approve").'</a>';
  1135. print '<a class="button buttonRefused" href="#">'.$langs->trans("Deny").'</a>';
  1136. } else {
  1137. print '<a class="button buttonRefused" href="#">'.$langs->trans("Disapprove").'</a>';
  1138. print '<a class="button buttonRefused" href="#">'.$langs->trans("Deny").'</a>';
  1139. }
  1140. } else {
  1141. $disabled = '';
  1142. if ($object->statut == 5) {
  1143. $disabled = 1;
  1144. }
  1145. if (empty($objp->status)) {
  1146. print '<a class="button'.($disabled ? ' buttonRefused' : '').'" href="'.$_SERVER["PHP_SELF"]."?id=".$id."&action=checkdispatchline&lineid=".$objp->dispatchlineid.'">'.$langs->trans("Approve").'</a>';
  1147. print '<a class="button'.($disabled ? ' buttonRefused' : '').'" href="'.$_SERVER["PHP_SELF"]."?id=".$id."&action=denydispatchline&lineid=".$objp->dispatchlineid.'">'.$langs->trans("Deny").'</a>';
  1148. }
  1149. if ($objp->status == 1) {
  1150. print '<a class="button'.($disabled ? ' buttonRefused' : '').'" href="'.$_SERVER["PHP_SELF"]."?id=".$id."&action=uncheckdispatchline&lineid=".$objp->dispatchlineid.'">'.$langs->trans("Reinit").'</a>';
  1151. print '<a class="button'.($disabled ? ' buttonRefused' : '').'" href="'.$_SERVER["PHP_SELF"]."?id=".$id."&action=denydispatchline&lineid=".$objp->dispatchlineid.'">'.$langs->trans("Deny").'</a>';
  1152. }
  1153. if ($objp->status == 2) {
  1154. print '<a class="button'.($disabled ? ' buttonRefused' : '').'" href="'.$_SERVER["PHP_SELF"]."?id=".$id."&action=uncheckdispatchline&lineid=".$objp->dispatchlineid.'">'.$langs->trans("Reinit").'</a>';
  1155. print '<a class="button'.($disabled ? ' buttonRefused' : '').'" href="'.$_SERVER["PHP_SELF"]."?id=".$id."&action=checkdispatchline&lineid=".$objp->dispatchlineid.'">'.$langs->trans("Approve").'</a>';
  1156. }
  1157. }
  1158. print '</td>';
  1159. } elseif (!empty($conf->reception->enabled)) {
  1160. print '<td class="right">';
  1161. if (!empty($reception->id)) {
  1162. print $reception->getLibStatut(5);
  1163. }
  1164. print '</td>';
  1165. }
  1166. if ($action != 'editline' || $lineid != $objp->dispatchlineid) {
  1167. if (empty($reception->id) || ($reception->statut == Reception::STATUS_DRAFT)) { // only allow edit on draft reception
  1168. print '<td class="linecoledit center">';
  1169. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=editline&token='.newToken().'&lineid='.$objp->dispatchlineid.'#line_'.$objp->dispatchlineid.'">';
  1170. print img_edit();
  1171. print '</a>';
  1172. print '</td>';
  1173. print '<td class="linecoldelete center">';
  1174. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=ask_deleteline&token='.newToken().'&lineid='.$objp->dispatchlineid.'#dispatch_received_products">';
  1175. print img_delete();
  1176. print '</a>';
  1177. print '</td>';
  1178. } else {
  1179. print '<td></td><td></td>';
  1180. }
  1181. } else {
  1182. print '<td class="center valignmiddle">';
  1183. print '<input type="submit" class="button button-save" id="savelinebutton" name="save" value="'.$langs->trans("Save").'" />';
  1184. print '</td>';
  1185. print '<td class="center valignmiddle">';
  1186. print '<input type="submit" class="button button-cancel" id="cancellinebutton" name="cancel" value="'.$langs->trans("Cancel").'" />';
  1187. print '</td>';
  1188. }
  1189. print "</tr>\n";
  1190. if ($action == 'editline' && $lineid == $objp->dispatchlineid) {
  1191. print '</form>';
  1192. }
  1193. $i++;
  1194. }
  1195. $db->free($resql);
  1196. print "</table>\n";
  1197. print '</div>';
  1198. }
  1199. } else {
  1200. dol_print_error($db);
  1201. }
  1202. }
  1203. // End of page
  1204. llxFooter();
  1205. $db->close();