replenishment.lib.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/product/stock/lib/replenishment.lib.php
  20. * \ingroup produit
  21. * \brief Contains functions used in replenish.php and replenishorders.php
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  24. /**
  25. * Check if there is still some dispatching of stock to do.
  26. *
  27. * @param int $order_id Id of order to check
  28. * @return boolean True = There is some dispatching to do, False = All dispatching is done (may be we receive more) or is not required
  29. */
  30. function dolDispatchToDo($order_id)
  31. {
  32. global $db;
  33. $dispatched = array();
  34. $ordered = array();
  35. // Count nb of quantity dispatched per product
  36. $sql = 'SELECT fk_product, SUM(qty) FROM '.MAIN_DB_PREFIX.'commande_fournisseur_dispatch';
  37. $sql .= ' WHERE fk_commande = '.((int) $order_id);
  38. $sql .= ' GROUP BY fk_product';
  39. $sql .= ' ORDER by fk_product';
  40. $resql = $db->query($sql);
  41. if ($resql && $db->num_rows($resql)) {
  42. while ($obj = $db->fetch_object($resql)) {
  43. $dispatched[$obj->fk_product] = $obj;
  44. }
  45. }
  46. // Count nb of quantity to dispatch per product
  47. $sql = 'SELECT fk_product, SUM(qty) FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet';
  48. $sql .= ' WHERE fk_commande = '.((int) $order_id);
  49. $sql .= ' AND fk_product > 0';
  50. if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
  51. $sql .= ' AND product_type = 0';
  52. }
  53. $sql .= ' GROUP BY fk_product';
  54. $sql .= ' ORDER by fk_product';
  55. $resql = $db->query($sql);
  56. if ($resql && $db->num_rows($resql)) {
  57. while ($obj = $db->fetch_object($resql)) {
  58. $ordered[$obj->fk_product] = $obj;
  59. }
  60. }
  61. $todispatch = 0;
  62. foreach ($ordered as $key => $val) {
  63. if ($ordered[$key] > $dispatched[$key]) {
  64. $todispatch++;
  65. }
  66. }
  67. return ($todispatch ? true : false);
  68. //return true;
  69. }
  70. /**
  71. * dispatchedOrders
  72. *
  73. * @return string Array of id of orders wit all dispathing already done or not required
  74. */
  75. function dispatchedOrders()
  76. {
  77. global $db;
  78. $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'commande_fournisseur';
  79. $resql = $db->query($sql);
  80. $resarray = array();
  81. if ($resql && $db->num_rows($resql) > 0) {
  82. while ($obj = $db->fetch_object($resql)) {
  83. if (!dolDispatchToDo($obj->rowid)) {
  84. $resarray[] = $obj->rowid;
  85. }
  86. }
  87. }
  88. if (count($resarray)) {
  89. $res = '('.implode(',', $resarray).')';
  90. } else {
  91. //hack to make sure ordered SQL request won't syntax error
  92. $res = '(0)';
  93. }
  94. return $res;
  95. }
  96. /**
  97. * ordered
  98. *
  99. * @param int $product_id Product id
  100. * @return string|null
  101. */
  102. function ordered($product_id)
  103. {
  104. global $db, $langs, $conf;
  105. $sql = 'SELECT DISTINCT cfd.fk_product, SUM(cfd.qty) as qty FROM';
  106. $sql .= ' '.MAIN_DB_PREFIX.'commande_fournisseurdet as cfd ';
  107. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'commande_fournisseur as cf';
  108. $sql .= ' ON cfd.fk_commande = cf.rowid WHERE';
  109. if ($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) {
  110. $sql .= ' cf.fk_statut < 3';
  111. } elseif ($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) {
  112. $sql .= ' cf.fk_statut < 6 AND cf.rowid NOT IN '.dispatchedOrders();
  113. } else {
  114. $sql .= ' cf.fk_statut < 5';
  115. }
  116. $sql .= ' AND cfd.fk_product = '.((int) $product_id);
  117. $sql .= ' GROUP BY cfd.fk_product';
  118. $resql = $db->query($sql);
  119. if ($resql) {
  120. $exists = $db->num_rows($resql);
  121. if ($exists) {
  122. $obj = $db->fetch_array($resql);
  123. return $obj['qty']; //. ' ' . img_picto('','tick');
  124. } else {
  125. return null; //img_picto('', 'stcomm-1');
  126. }
  127. } else {
  128. $error = $db->lasterror();
  129. dol_print_error($db);
  130. return $langs->trans('error');
  131. }
  132. }
  133. /**
  134. * getProducts
  135. *
  136. * @param int $order_id Order id
  137. * @return array|integer[]
  138. */
  139. function getProducts($order_id)
  140. {
  141. global $db;
  142. $order = new CommandeFournisseur($db);
  143. $f = $order->fetch($order_id);
  144. $products = array();
  145. if ($f) {
  146. foreach ($order->lines as $line) {
  147. if (!in_array($line->fk_product, $products)) {
  148. $products[] = $line->fk_product;
  149. }
  150. }
  151. }
  152. return $products;
  153. }