dispatch.php 51 KB

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