client.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2020 Open-Dsi <support@open-dsi.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/societe/class/client.class.php
  21. * \ingroup societe
  22. * \brief File for class of customers
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  25. /**
  26. * Class to manage customers or prospects
  27. */
  28. class Client extends Societe
  29. {
  30. public $next_prev_filter = "te.client in (1,2,3)"; // Used to add a filter in Form::showrefnav method
  31. public $cacheprospectstatus = array();
  32. /**
  33. * Constructor
  34. *
  35. * @param DoliDB $db Database handler
  36. */
  37. public function __construct($db)
  38. {
  39. $this->db = $db;
  40. $this->client = 3;
  41. $this->fournisseur = 0;
  42. }
  43. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  44. /**
  45. * Load indicators into this->nb for board
  46. *
  47. * @return int <0 if KO, >0 if OK
  48. */
  49. public function load_state_board()
  50. {
  51. // phpcs:enable
  52. global $user, $hookmanager;
  53. $this->nb = array("prospects" => 0, "customers" => 0);
  54. $clause = "WHERE";
  55. $sql = "SELECT count(s.rowid) as nb, s.client";
  56. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  57. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  58. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
  59. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  60. $clause = "AND";
  61. }
  62. $sql .= " ".$clause." s.client IN (1,2,3)";
  63. $sql .= ' AND s.entity IN ('.getEntity($this->element).')';
  64. // Add where from hooks
  65. if (is_object($hookmanager)) {
  66. $parameters = array();
  67. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
  68. $sql .= $hookmanager->resPrint;
  69. }
  70. $sql .= " GROUP BY s.client";
  71. $resql = $this->db->query($sql);
  72. if ($resql) {
  73. while ($obj = $this->db->fetch_object($resql)) {
  74. if ($obj->client == 1 || $obj->client == 3) {
  75. $this->nb["customers"] += $obj->nb;
  76. }
  77. if ($obj->client == 2 || $obj->client == 3) {
  78. $this->nb["prospects"] += $obj->nb;
  79. }
  80. }
  81. $this->db->free($resql);
  82. return 1;
  83. } else {
  84. dol_print_error($this->db);
  85. $this->error = $this->db->lasterror();
  86. return -1;
  87. }
  88. }
  89. /**
  90. * Load array of prospect status
  91. *
  92. * @param int $active 1=Active only, 0=Not active only, -1=All
  93. * @return int <0 if KO, >0 if OK
  94. */
  95. public function loadCacheOfProspStatus($active = 1)
  96. {
  97. global $langs;
  98. $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcomm";
  99. if ($active >= 0) {
  100. $sql .= " WHERE active = ".((int) $active);
  101. }
  102. $resql = $this->db->query($sql);
  103. $num = $this->db->num_rows($resql);
  104. $i = 0;
  105. while ($i < $num) {
  106. $obj = $this->db->fetch_object($resql);
  107. $this->cacheprospectstatus[$obj->id] = array('id'=>$obj->id, 'code'=>$obj->code, 'label'=>($langs->trans("ST_".strtoupper($obj->code)) == "ST_".strtoupper($obj->code)) ? $obj->label : $langs->trans("ST_".strtoupper($obj->code)), 'picto'=>$obj->picto);
  108. $i++;
  109. }
  110. return 1;
  111. }
  112. }