dispatch.php 52 KB

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