searchfrombarcode.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * \file /htdocs/product/inventory/ajax/searchfrombarcode.php
  18. * \brief File to make Ajax action on product and stock
  19. */
  20. if (!defined('NOTOKENRENEWAL')) {
  21. define('NOTOKENRENEWAL', 1); // Disables token renewal
  22. }
  23. if (!defined('NOREQUIREMENU')) {
  24. define('NOREQUIREMENU', '1');
  25. }
  26. if (!defined('NOREQUIREHTML')) {
  27. define('NOREQUIREHTML', '1');
  28. }
  29. if (!defined('NOREQUIREAJAX')) {
  30. define('NOREQUIREAJAX', '1');
  31. }
  32. if (!defined('NOREQUIRESOC')) {
  33. define('NOREQUIRESOC', '1');
  34. }
  35. require '../../../main.inc.php';
  36. require_once DOL_DOCUMENT_ROOT."/product/stock/class/entrepot.class.php";
  37. $warehouse = new Entrepot($db);
  38. $action = GETPOST("action", "alpha");
  39. $barcode = GETPOST("barcode", "aZ09");
  40. $product = GETPOST("product");
  41. $response = "";
  42. $fk_entrepot = GETPOST("fk_entrepot", "int");
  43. $fk_inventory = GETPOST("fk_inventory", "int");
  44. $fk_product = GETPOST("fk_product", "int");
  45. $reelqty = GETPOST("reelqty", "int");
  46. $batch = GETPOST("batch", "int");
  47. $mode = GETPOST("mode", "aZ");
  48. $warehousefound = 0;
  49. $warehouseid = 0;
  50. $objectreturn = array();
  51. /*
  52. * View
  53. */
  54. top_httphead('application/json');
  55. if ($action == "existbarcode" && !empty($barcode)) {
  56. if (!empty($mode) && $mode == "lotserial") {
  57. $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode, ps.reel, pb.batch";
  58. $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
  59. $sql .= " JOIN ".MAIN_DB_PREFIX."product_stock as ps ON pb.fk_product_stock = ps.rowid JOIN ".MAIN_DB_PREFIX."product as p ON ps.fk_product = p.rowid";
  60. $sql .= " WHERE pb.batch = '".$db->escape($barcode)."'";
  61. } else {
  62. $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode,ps.reel";
  63. $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps JOIN ".MAIN_DB_PREFIX."product as p ON ps.fk_product = p.rowid";
  64. $sql .= " WHERE p.barcode = '".$db->escape($barcode)."'";
  65. }
  66. if (!empty($fk_entrepot)) {
  67. $sql .= " AND ps.fk_entrepot = '".$db->escape($fk_entrepot)."'";
  68. }
  69. if (!empty($fk_product)) {
  70. $sql .= " AND ps.fk_product = '".$db->escape($fk_product)."'";
  71. }
  72. $result = $db->query($sql);
  73. if ($result) {
  74. $nbline = $db->num_rows($result);
  75. for ($i=0; $i < $nbline; $i++) {
  76. $object = $db->fetch_object($result);
  77. if (($mode == "barcode" && $barcode == $object->barcode) || ($mode == "lotserial" && $barcode == $object->batch)) {
  78. $warehouse->fetch(0, $product["Warehouse"]);
  79. if (!empty($object->fk_entrepot) && $warehouse->id == $object->fk_entrepot) {
  80. $warehousefound++;
  81. $warehouseid = $object->fk_entrepot;
  82. $fk_product = $object->fk_product;
  83. $reelqty = $object->reel;
  84. $objectreturn = array('fk_warehouse'=>$warehouseid,'fk_product'=>$fk_product,'reelqty'=>$reelqty);
  85. }
  86. }
  87. }
  88. if ($warehousefound < 1) {
  89. $response = array('status'=>'error','errorcode'=>'NotFound','message'=>'No warehouse found for barcode'.$barcode);
  90. } elseif ($warehousefound > 1) {
  91. $response = array('status'=>'error','errorcode'=>'TooManyWarehouse','message'=>'Too many warehouse found');
  92. } else {
  93. $response = array('status'=>'success','message'=>'Warehouse found','object'=>$objectreturn);
  94. }
  95. } else {
  96. $response = array('status'=>'error','errorcode'=>'NotFound','message'=>"No results found for barcode");
  97. }
  98. } else {
  99. $response = array('status'=>'error','errorcode'=>'ActionError','message'=>"Error on action");
  100. }
  101. if ($action == "addnewlineproduct") {
  102. require_once DOL_DOCUMENT_ROOT."/product/inventory/class/inventory.class.php";
  103. $inventoryline = new InventoryLine($db);
  104. if (!empty($fk_inventory)) {
  105. $inventoryline->fk_inventory = $fk_inventory;
  106. $inventoryline->fk_warehouse = $fk_entrepot;
  107. $inventoryline->fk_product = $fk_product;
  108. $inventoryline->qty_stock = $reelqty;
  109. if (!empty($batch)) {
  110. $inventoryline->batch = $batch;
  111. }
  112. $inventoryline->datec = dol_now();
  113. $result = $inventoryline->create($user);
  114. if ($result > 0) {
  115. $response = array('status'=>'success','message'=>'Success on creating line','id_line'=>$result);
  116. } else {
  117. $response = array('status'=>'error','errorcode'=>'ErrorCreation','message'=>"Error on line creation");
  118. }
  119. } else {
  120. $response = array('status'=>'error','errorcode'=>'NoIdForInventory','message'=>"No id for inventory");
  121. }
  122. }
  123. $response = json_encode($response);
  124. echo $response;