lotcheck.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /* Copyright (C) 2001-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file invoiceimport/lotcheck.php
  22. * \ingroup invoiceimport
  23. * \brief Home page of invoiceimport top menu
  24. */
  25. // Load Dolibarr environment
  26. $res = 0;
  27. // Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
  28. if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
  29. $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
  30. }
  31. // Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
  32. $tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME']; $tmp2 = realpath(__FILE__); $i = strlen($tmp) - 1; $j = strlen($tmp2) - 1;
  33. while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
  34. $i--; $j--;
  35. }
  36. if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
  37. $res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
  38. }
  39. if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
  40. $res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
  41. }
  42. // Try main.inc.php using relative path
  43. if (!$res && file_exists("../main.inc.php")) {
  44. $res = @include "../main.inc.php";
  45. }
  46. if (!$res && file_exists("../../main.inc.php")) {
  47. $res = @include "../../main.inc.php";
  48. }
  49. if (!$res && file_exists("../../../main.inc.php")) {
  50. $res = @include "../../../main.inc.php";
  51. }
  52. if (!$res) {
  53. die("Include of main fails");
  54. }
  55. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  56. // Load translation files required by the page
  57. $langs->loadLangs(array("invoiceimport@invoiceimport"));
  58. $action = GETPOST('action', 'aZ09');
  59. // Security check
  60. // if (! $user->rights->invoiceimport->myobject->read) {
  61. // accessforbidden();
  62. // }
  63. $socid = GETPOST('socid', 'int');
  64. if (isset($user->socid) && $user->socid > 0) {
  65. $action = '';
  66. $socid = $user->socid;
  67. }
  68. $max = 5;
  69. $now = dol_now();
  70. /*
  71. * Actions
  72. */
  73. if ($_REQUEST['action'] == 'getProducts') {
  74. $productsArray = getProductsList();
  75. if (is_array($productsArray)) {
  76. foreach ($productsArray as $product) {
  77. if ($product['lots'] == false) {
  78. $lots = '<span style="color: red;">(!)</span>';
  79. }
  80. else {
  81. $lotsQty = count(getProductsLOTS($product['data']->rowid));
  82. $lots = '<span style="color: green;">'.$lotsQty.' LOT in stock</span>';
  83. }
  84. print '<tr><td>'.$lots.'</td><td><a href="#" onclick="getProductLots(\''.$product['data']->rowid.'\')">'.$product['data']->label.'</a></td></tr>';
  85. }
  86. }
  87. else {
  88. print '<tr><td colspan="2">Products list is empty...</td></tr>';
  89. }
  90. die();
  91. }
  92. if ($_REQUEST['action'] == 'addlot') {
  93. $res = addNewLot($_REQUEST);
  94. print $res;
  95. die();
  96. }
  97. if ($_REQUEST['action'] == 'getLots') {
  98. if (isValidProduct($_REQUEST['productID'])) {
  99. $lots = getProductsLOTS($_REQUEST['productID']);
  100. if (is_array($lots)) {
  101. foreach ($lots as $lot) {
  102. print '<tr><td>'.$lot['batch']->batch.'</td><td>'.$lot['batch']->eatby.'</td><td>'.$lot['batch']->qty.' pcs.</td><td>'.$lot['entrepot']->ref.'</td></tr>';
  103. }
  104. }
  105. }
  106. else {
  107. print '<tr><td colspan="4">Invalid product ID...</td></tr>';
  108. }
  109. print '<tr>';
  110. print '<td colspan="4"><strong>Add new LOT</strong></td>';
  111. print '</tr>';
  112. if (is_array($lots)) {
  113. print '<tr>';
  114. print '<td><input type="text" id="code" placeholder="Serial number" /></td>';
  115. print '<td><input type="datetime-local" id="eatdate" placeholder="Income date" /></td>';
  116. print '<td><input type="number" id="qty" value="1" placeholder="Qty" /></td>';
  117. //print '<td><select name="etrepot"><option value="">Select location</option></select></td>';
  118. print '<td><input type="hidden" id="productID" value="'.$lots[0]['stock']->fk_product.'"><input type="hidden" id="stock" value="'.$lots[0]['stock']->rowid.'"><button type="button" name="addLot" onclick="addLOT()">Add</button></td>';
  119. print '</tr>';
  120. //print '<tr>';
  121. //print '<td colspan="4"><button type="button" name="addLot">Save</button></td>';
  122. //print '</tr>';
  123. }
  124. else {
  125. $entrepots = getEntrepots();
  126. print '<tr>';
  127. print '<td><input type="text" id="code" placeholder="Serial number" /></td>';
  128. print '<td><input type="datetime-local" id="eatdate" placeholder="Income date" /></td>';
  129. print '<td><input type="number" id="qty" value="1" placeholder="Qty" /></td>';
  130. print '<td><select id="etrepot"><option value="">Select location</option>'.$entrepots.'</select></td>';
  131. print '</tr>';
  132. print '<tr>';
  133. print '<td colspan="4"><input type="hidden" id="stock" value=""><input type="hidden" id="productID" value="'.$_REQUEST['productID'].'"><button type="button" name="addLot" onclick="addLOT()">Add</button></td>';
  134. print '</tr>';
  135. }
  136. die();
  137. }
  138. /*
  139. * View
  140. */
  141. $form = new Form($db);
  142. $formfile = new FormFile($db);
  143. llxHeader("", $langs->trans("LOTCheckerArea"));
  144. print '<script type="text/javascript" src="/custom/invoiceimport/js/lotcheck.js"></script>';
  145. print load_fiche_titre($langs->trans("LOTCheckerArea"), '', 'invoiceimport.png@invoiceimport');
  146. print '<div class="fichecenter"><div class="fichethirdleft">';
  147. print '<div class="nowrap">';
  148. print '<div class="div-table-responsive">';
  149. print '<table class="tagtable nobottomiftotal liste"><thead><tr><th>LOT</th><th>Product name</th></tr></thead><tbody id="prodlist"></tbody></table>';
  150. print '</div>';
  151. print '</div>';
  152. print '</div><div class="fichetwothirdright">';
  153. print '<div class="div-table-responsive">';
  154. print '<table class="tagtable nobottomiftotal liste"><thead><tr><th>Code</th><th>Date of income</th><th>Qty</th><th>Location</th></tr></thead><tbody id="lotlist"></tbody></table>';
  155. print '</div>';
  156. $NBMAX = $conf->global->MAIN_SIZE_SHORTLIST_LIMIT;
  157. $max = $conf->global->MAIN_SIZE_SHORTLIST_LIMIT;
  158. /* BEGIN MODULEBUILDER LASTMODIFIED MYOBJECT
  159. // Last modified myobject
  160. if (! empty($conf->invoiceimport->enabled) && $user->rights->invoiceimport->read)
  161. {
  162. $sql = "SELECT s.rowid, s.ref, s.label, s.date_creation, s.tms";
  163. $sql.= " FROM ".MAIN_DB_PREFIX."invoiceimport_myobject as s";
  164. //if (! $user->rights->societe->client->voir && ! $socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  165. $sql.= " WHERE s.entity IN (".getEntity($myobjectstatic->element).")";
  166. //if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  167. //if ($socid) $sql.= " AND s.rowid = $socid";
  168. $sql .= " ORDER BY s.tms DESC";
  169. $sql .= $db->plimit($max, 0);
  170. $resql = $db->query($sql);
  171. if ($resql)
  172. {
  173. $num = $db->num_rows($resql);
  174. $i = 0;
  175. print '<table class="noborder centpercent">';
  176. print '<tr class="liste_titre">';
  177. print '<th colspan="2">';
  178. print $langs->trans("BoxTitleLatestModifiedMyObjects", $max);
  179. print '</th>';
  180. print '<th class="right">'.$langs->trans("DateModificationShort").'</th>';
  181. print '</tr>';
  182. if ($num)
  183. {
  184. while ($i < $num)
  185. {
  186. $objp = $db->fetch_object($resql);
  187. $myobjectstatic->id=$objp->rowid;
  188. $myobjectstatic->ref=$objp->ref;
  189. $myobjectstatic->label=$objp->label;
  190. $myobjectstatic->status = $objp->status;
  191. print '<tr class="oddeven">';
  192. print '<td class="nowrap">'.$myobjectstatic->getNomUrl(1).'</td>';
  193. print '<td class="right nowrap">';
  194. print "</td>";
  195. print '<td class="right nowrap">'.dol_print_date($db->jdate($objp->tms), 'day')."</td>";
  196. print '</tr>';
  197. $i++;
  198. }
  199. $db->free($resql);
  200. } else {
  201. print '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  202. }
  203. print "</table><br>";
  204. }
  205. }
  206. */
  207. print '</div></div>';
  208. function hasProductLOTS($productID) {
  209. global $db;
  210. $resultset = $db->query("SELECT * FROM ".MAIN_DB_PREFIX."product_stock WHERE fk_product = ".$productID.";");
  211. if ($db->num_rows($resultset) > 0) {
  212. $stock = $db->fetch_object($resultset);
  213. $batchResults = $db->query("SELECT * FROM ".MAIN_DB_PREFIX."product_batch WHERE fk_product_stock='".$stock->rowid."';");
  214. if ($db->num_rows($batchResults) > 0) {
  215. return true;
  216. }
  217. else {
  218. return false;
  219. }
  220. }
  221. else {
  222. return false;
  223. }
  224. }
  225. function getProductsList() {
  226. global $db;
  227. $productList = array();
  228. $resultset = $db->query("SELECT * FROM ".MAIN_DB_PREFIX."product;");
  229. if ($db->num_rows($resultset) > 0) {
  230. while ($product = $db->fetch_object($resultset)) {
  231. $productList[] = array('data' => $product, 'lots' => hasProductLOTS($product->rowid));
  232. }
  233. return $productList;
  234. }
  235. else {
  236. return false;
  237. }
  238. }
  239. function isValidProduct($productID) {
  240. global $db;
  241. if (is_numeric($productID)) {
  242. $resultset = $db->query("SELECT * FROM ".MAIN_DB_PREFIX."product WHERE rowid='".$productID."';");
  243. if ($db->num_rows($resultset) > 0) {
  244. return true;
  245. }
  246. else {
  247. return false;
  248. }
  249. }
  250. else {
  251. return false;
  252. }
  253. }
  254. function getProductsLOTS($productID) {
  255. global $db;
  256. $lotList = array();
  257. $resultset = $db->query("SELECT * FROM ".MAIN_DB_PREFIX."product_stock WHERE fk_product = ".$productID.";");
  258. if ($db->num_rows($resultset) > 0) {
  259. $stock = $db->fetch_object($resultset);
  260. $entrepotResult = $db->query("SELECT * FROM ".MAIN_DB_PREFIX."entrepot WHERE rowid = ".$stock->fk_entrepot.";");
  261. $entrepot = $db->fetch_object($entrepotResult);
  262. $batchResults = $db->query("SELECT * FROM ".MAIN_DB_PREFIX."product_batch WHERE fk_product_stock='".$stock->rowid."';");
  263. if ($db->num_rows($batchResults) > 0) {
  264. while ($row = $db->fetch_object($batchResults)) {
  265. $lotList[] = array('stock' => $stock, 'batch' => $row, 'entrepot' => $entrepot);
  266. }
  267. return $lotList;
  268. }
  269. else {
  270. return false;
  271. }
  272. }
  273. else {
  274. return false;
  275. }
  276. }
  277. function addNewLot($lotData) {
  278. global $db;
  279. if ($lotData['fk_product_stock'] == '') {
  280. $resStock = $db->query("INSERT INTO ".MAIN_DB_PREFIX."product_stock (fk_product, fk_entrepot, reel) VALUES ('".$lotData['productID']."', '".$lotData['entrepot']."', '1')");
  281. $lotData['fk_product_stock'] = $db->last_insert_id(MAIN_DB_PREFIX."product_stock");
  282. }
  283. $resultset = $db->query("INSERT INTO ".MAIN_DB_PREFIX."product_batch (fk_product_stock, eatby, batch, qty) VALUES
  284. (
  285. '".$lotData['fk_product_stock']."',
  286. '".$lotData['eatby']."',
  287. '".$lotData['batch']."',
  288. '".$lotData['qty']."'
  289. );");
  290. return true;
  291. }
  292. function getEntrepots() {
  293. global $db;
  294. $options = '';
  295. $resultset = $db->query("SELECT * FROM ".MAIN_DB_PREFIX."entrepot;");
  296. if ($db->num_rows($resultset) > 0) {
  297. while ($row = $db->fetch_object($resultset)) {
  298. $options.= '<option value="'.$row->rowid.'">'.$row->ref.'</option>';
  299. }
  300. }
  301. return $options;
  302. }
  303. // End of page
  304. llxFooter();
  305. $db->close();