box_goodcustomers.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
  6. * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/core/boxes/box_goodcustomers.php
  23. * \ingroup societes
  24. * \brief Module to generated widget of best customers (the most invoiced)
  25. */
  26. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  27. /**
  28. * Class to manage the box to show last thirdparties
  29. */
  30. class box_goodcustomers extends ModeleBoxes
  31. {
  32. public $boxcode = "goodcustomers";
  33. public $boximg = "object_company";
  34. public $boxlabel = "BoxGoodCustomers";
  35. public $depends = array("societe");
  36. /**
  37. * @var DoliDB Database handler.
  38. */
  39. public $db;
  40. public $enabled = 1;
  41. public $info_box_head = array();
  42. public $info_box_contents = array();
  43. /**
  44. * Constructor
  45. *
  46. * @param DoliDB $db Database handler
  47. * @param string $param More parameters
  48. */
  49. public function __construct($db, $param = '')
  50. {
  51. global $conf, $user;
  52. $this->db = $db;
  53. // disable box for such cases
  54. if (!empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
  55. $this->enabled = 0; // disabled by this option
  56. }
  57. if (empty($conf->global->MAIN_BOX_ENABLE_BEST_CUSTOMERS)) {
  58. $this->enabled = 0; // not enabled by default. Very slow on large database
  59. }
  60. $this->hidden = !($user->rights->societe->lire);
  61. }
  62. /**
  63. * Load data for box to show them later
  64. *
  65. * @param int $max Maximum number of records to load
  66. * @return void
  67. */
  68. public function loadBox($max = 5)
  69. {
  70. global $user, $langs, $conf;
  71. $langs->load("boxes");
  72. $this->max = $max;
  73. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  74. $thirdpartystatic = new Societe($this->db);
  75. $this->info_box_head = array('text' => $langs->trans("BoxTitleGoodCustomers", $max));
  76. if ($user->rights->societe->lire) {
  77. $sql = "SELECT s.rowid, s.nom as name, s.logo, s.code_client, s.code_fournisseur, s.client, s.fournisseur, s.tms as datem, s.status as status,";
  78. $sql .= " count(*) as nbfact, sum(".$this->db->ifsql('f.paye=1', '1', '0').") as nbfactpaye";
  79. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture as f";
  80. $sql .= ' WHERE s.entity IN ('.getEntity('societe').')';
  81. $sql .= ' AND s.rowid = f.fk_soc';
  82. $sql .= " GROUP BY s.rowid, s.nom, s.logo, s.code_client, s.code_fournisseur, s.client, s.fournisseur, s.tms, s.status";
  83. $sql .= $this->db->order("nbfact", "DESC");
  84. $sql .= $this->db->plimit($max, 0);
  85. dol_syslog(get_class($this)."::loadBox", LOG_DEBUG);
  86. $result = $this->db->query($sql);
  87. if ($result) {
  88. $num = $this->db->num_rows($result);
  89. $line = 0;
  90. while ($line < $num) {
  91. $objp = $this->db->fetch_object($result);
  92. $datem = $this->db->jdate($objp->tms);
  93. $thirdpartystatic->id = $objp->rowid;
  94. $thirdpartystatic->name = $objp->name;
  95. $thirdpartystatic->code_client = $objp->code_client;
  96. $thirdpartystatic->code_fournisseur = $objp->code_fournisseur;
  97. $thirdpartystatic->client = $objp->client;
  98. $thirdpartystatic->fournisseur = $objp->fournisseur;
  99. $thirdpartystatic->logo = $objp->logo;
  100. $nbfact = $objp->nbfact;
  101. $nbimpaye = $objp->nbfact - $objp->nbfactpaye;
  102. $this->info_box_contents[$line][] = array(
  103. 'td' => 'class="tdoverflowmax150"',
  104. 'text' => $thirdpartystatic->getNomUrl(1),
  105. 'asis' => 1,
  106. );
  107. $this->info_box_contents[$line][] = array(
  108. 'td' => 'class="center nowraponall"',
  109. 'text' => dol_print_date($datem, "day", 'tzuserrel')
  110. );
  111. $this->info_box_contents[$line][] = array(
  112. 'td' => 'class="right"',
  113. 'text' => $nbfact.($nbimpaye != 0 ? ' ('.$nbimpaye.')' : '')
  114. );
  115. $this->info_box_contents[$line][] = array(
  116. 'td' => 'class="right" width="18"',
  117. 'text' => $thirdpartystatic->LibStatut($objp->status, 3)
  118. );
  119. $line++;
  120. }
  121. if ($num == 0) {
  122. $this->info_box_contents[$line][0] = array(
  123. 'td' => 'class="center opacitymedium"',
  124. 'text'=>$langs->trans("NoRecordedCustomers")
  125. );
  126. }
  127. $this->db->free($result);
  128. } else {
  129. $this->info_box_contents[0][0] = array(
  130. 'td' => '',
  131. 'maxlength'=>500,
  132. 'text' => ($this->db->error().' sql='.$sql),
  133. );
  134. }
  135. } else {
  136. $this->info_box_contents[0][0] = array(
  137. 'td' => 'class="nohover opacitymedium left"',
  138. 'text' => $langs->trans("ReadPermissionNotAllowed")
  139. );
  140. }
  141. }
  142. /**
  143. * Method to show box
  144. *
  145. * @param array $head Array with properties of box title
  146. * @param array $contents Array with properties of box lines
  147. * @param int $nooutput No print, only return string
  148. * @return string
  149. */
  150. public function showBox($head = null, $contents = null, $nooutput = 0)
  151. {
  152. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  153. }
  154. }