fournisseur.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /* Copyright (C) 2004-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2006 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  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 htdocs/fourn/class/fournisseur.class.php
  22. * \ingroup fournisseur,societe
  23. * \brief File of class to manage suppliers
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  28. /**
  29. * Class to manage suppliers
  30. */
  31. class Fournisseur extends Societe
  32. {
  33. public $next_prev_filter = "te.fournisseur = 1"; // Used to add a filter in Form::showrefnav method
  34. /**
  35. * Constructor
  36. *
  37. * @param DoliDB $db Database handler
  38. */
  39. public function __construct($db)
  40. {
  41. $this->db = $db;
  42. $this->client = 0;
  43. $this->fournisseur = 1;
  44. }
  45. /**
  46. * Return nb of orders
  47. *
  48. * @return int Nb of orders
  49. */
  50. public function getNbOfOrders()
  51. {
  52. $sql = "SELECT rowid";
  53. $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as cf";
  54. $sql .= " WHERE cf.fk_soc = ".((int) $this->id);
  55. $resql = $this->db->query($sql);
  56. if ($resql) {
  57. $num = $this->db->num_rows($resql);
  58. if ($num == 1) {
  59. $row = $this->db->fetch_row($resql);
  60. $this->single_open_commande = $row[0];
  61. }
  62. }
  63. return $num;
  64. }
  65. /**
  66. * Returns number of ref prices (not number of products).
  67. *
  68. * @return int Nb of ref prices, or <0 if error
  69. */
  70. public function nbOfProductRefs()
  71. {
  72. global $conf;
  73. $sql = "SELECT count(pfp.rowid) as nb";
  74. $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
  75. $sql .= " WHERE pfp.entity = ".$conf->entity;
  76. $sql .= " AND pfp.fk_soc = ".((int) $this->id);
  77. $resql = $this->db->query($sql);
  78. if ($resql) {
  79. $obj = $this->db->fetch_object($resql);
  80. return $obj->nb;
  81. } else {
  82. return -1;
  83. }
  84. }
  85. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  86. /**
  87. * Load statistics indicators
  88. *
  89. * @return int <0 if KO, >0 if OK
  90. */
  91. public function load_state_board()
  92. {
  93. // phpcs:enable
  94. global $conf, $user, $hookmanager;
  95. $this->nb = array();
  96. $clause = "WHERE";
  97. $sql = "SELECT count(s.rowid) as nb";
  98. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  99. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  100. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
  101. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  102. $clause = "AND";
  103. }
  104. $sql .= " ".$clause." s.fournisseur = 1";
  105. $sql .= " AND s.entity IN (".getEntity('societe').")";
  106. // Add where from hooks
  107. if (is_object($hookmanager)) {
  108. $parameters = array();
  109. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
  110. $sql .= $hookmanager->resPrint;
  111. }
  112. $resql = $this->db->query($sql);
  113. if ($resql) {
  114. while ($obj = $this->db->fetch_object($resql)) {
  115. $this->nb["suppliers"] = $obj->nb;
  116. }
  117. $this->db->free($resql);
  118. return 1;
  119. } else {
  120. dol_print_error($this->db);
  121. $this->error = $this->db->error();
  122. return -1;
  123. }
  124. }
  125. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  126. /**
  127. * Create a supplier category
  128. *
  129. * @param User $user User asking creation
  130. * @param string $name Category name
  131. * @return int <0 if KO, 0 if OK
  132. */
  133. public function CreateCategory($user, $name)
  134. {
  135. // phpcs:enable
  136. $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie (label,visible,type)";
  137. $sql .= " VALUES ";
  138. $sql .= " ('".$this->db->escape($name)."',1,1)";
  139. dol_syslog("Fournisseur::CreateCategory", LOG_DEBUG);
  140. $resql = $this->db->query($sql);
  141. if ($resql) {
  142. dol_syslog("Fournisseur::CreateCategory : Success");
  143. return 0;
  144. } else {
  145. $this->error = $this->db->lasterror();
  146. dol_syslog("Fournisseur::CreateCategory : Failed (".$this->error.")");
  147. return -1;
  148. }
  149. }
  150. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  151. /**
  152. * Return the suppliers list
  153. *
  154. * @return array Array of suppliers
  155. */
  156. public function ListArray()
  157. {
  158. // phpcs:enable
  159. global $conf;
  160. global $user;
  161. $arr = array();
  162. $sql = "SELECT s.rowid, s.nom as name";
  163. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  164. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  165. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  166. }
  167. $sql .= " WHERE s.fournisseur = 1";
  168. $sql .= " AND s.entity IN (".getEntity('societe').")";
  169. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  170. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  171. }
  172. $resql = $this->db->query($sql);
  173. if ($resql) {
  174. while ($obj = $this->db->fetch_object($resql)) {
  175. $arr[$obj->rowid] = $obj->name;
  176. }
  177. } else {
  178. dol_print_error($this->db);
  179. $this->error = $this->db->lasterror();
  180. }
  181. return $arr;
  182. }
  183. /**
  184. * Function used to replace a thirdparty id with another one.
  185. *
  186. * @param DoliDB $db Database handler
  187. * @param int $origin_id Old third-party id
  188. * @param int $dest_id New third-party id
  189. * @return bool
  190. */
  191. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  192. {
  193. $tables = array(
  194. 'facture_fourn'
  195. );
  196. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  197. }
  198. }