box_goodcustomers.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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@capnetworks.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 <http://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. var $boxcode="goodcustomers";
  33. var $boximg="object_company";
  34. var $boxlabel="BoxGoodCustomers";
  35. var $depends = array("societe");
  36. var $db;
  37. var $enabled = 1;
  38. var $info_box_head = array();
  39. var $info_box_contents = array();
  40. /**
  41. * Constructor
  42. *
  43. * @param DoliDB $db Database handler
  44. * @param string $param More parameters
  45. */
  46. function __construct($db,$param='')
  47. {
  48. global $conf, $user;
  49. $this->db = $db;
  50. // disable box for such cases
  51. if (! empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) $this->enabled=0; // disabled by this option
  52. if (empty($conf->global->MAIN_BOX_ENABLE_BEST_CUSTOMERS)) $this->enabled=0; // not enabled by default. Very slow on large database
  53. }
  54. /**
  55. * Load data for box to show them later
  56. *
  57. * @param int $max Maximum number of records to load
  58. * @return void
  59. */
  60. function loadBox($max=5)
  61. {
  62. global $user, $langs, $db, $conf;
  63. $langs->load("boxes");
  64. $this->max=$max;
  65. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  66. $thirdpartystatic=new Societe($db);
  67. $this->info_box_head = array('text' => $langs->trans("BoxTitleGoodCustomers",$max));
  68. if ($user->rights->societe->lire)
  69. {
  70. $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,";
  71. $sql.= " count(*) as nbfact, sum(". $db->ifsql('f.paye=1','1','0').") as nbfactpaye";
  72. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture as f";
  73. $sql.= ' WHERE s.entity IN ('.getEntity('societe', 1).')';
  74. $sql.= ' AND s.rowid = f.fk_soc';
  75. $sql.= " GROUP BY s.rowid, s.nom, s.logo, s.code_client, s.code_fournisseur, s.client, s.fournisseur, s.tms, s.status";
  76. $sql.= $db->order("nbfact","DESC");
  77. $sql.= $db->plimit($max,0);
  78. dol_syslog(get_class($this)."::loadBox", LOG_DEBUG);
  79. $result = $db->query($sql);
  80. if ($result)
  81. {
  82. $num = $db->num_rows($result);
  83. $line = 0;
  84. while ($line < $num)
  85. {
  86. $objp = $db->fetch_object($result);
  87. $datem=$db->jdate($objp->tms);
  88. $thirdpartystatic->id = $objp->rowid;
  89. $thirdpartystatic->name = $objp->name;
  90. $thirdpartystatic->code_client = $objp->code_client;
  91. $thirdpartystatic->code_fournisseur = $objp->code_fournisseur;
  92. $thirdpartystatic->client = $objp->client;
  93. $thirdpartystatic->fournisseur = $objp->fournisseur;
  94. $thirdpartystatic->logo = $objp->logo;
  95. $nbfact = $objp->nbfact;
  96. $nbimpaye = $objp->nbfact - $objp->nbfactpaye;
  97. $this->info_box_contents[$line][] = array(
  98. 'td' => 'align="left"',
  99. 'text' => $thirdpartystatic->getNomUrl(1),
  100. 'asis' => 1,
  101. );
  102. $this->info_box_contents[$line][] = array(
  103. 'td' => 'align="right"',
  104. 'text' => dol_print_date($datem, "day")
  105. );
  106. $this->info_box_contents[$line][] = array(
  107. 'td' => 'align="right"',
  108. 'text' => $nbfact.( $nbimpaye != 0 ? ' ('.$nbimpaye.')':'')
  109. );
  110. $this->info_box_contents[$line][] = array(
  111. 'td' => 'align="right" width="18"',
  112. 'text' => $thirdpartystatic->LibStatut($objp->status,3)
  113. );
  114. $line++;
  115. }
  116. if ($num==0) $this->info_box_contents[$line][0] = array('td' => 'align="center"','text'=>$langs->trans("NoRecordedCustomers"));
  117. $db->free($result);
  118. }
  119. else {
  120. $this->info_box_contents[0][0] = array( 'td' => 'align="left"',
  121. 'maxlength'=>500,
  122. 'text' => ($db->error().' sql='.$sql));
  123. }
  124. }
  125. else {
  126. $this->info_box_contents[0][0] = array('align' => 'left',
  127. 'text' => $langs->trans("ReadPermissionNotAllowed"));
  128. }
  129. }
  130. /**
  131. * Method to show box
  132. *
  133. * @param array $head Array with properties of box title
  134. * @param array $contents Array with properties of box lines
  135. * @param int $nooutput No print, only return string
  136. * @return void
  137. */
  138. function showBox($head = null, $contents = null, $nooutput=0)
  139. {
  140. parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  141. }
  142. }