box_services_expired.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/boxes/box_services_expired.php
  19. * \ingroup contracts
  20. * \brief Module to show the box of last expired services
  21. */
  22. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  23. /**
  24. * Class to manage the box to show expired services
  25. */
  26. class box_services_expired extends ModeleBoxes
  27. {
  28. public $boxcode = "expiredservices"; // id of box
  29. public $boximg = "object_contract";
  30. public $boxlabel = "BoxOldestExpiredServices";
  31. public $depends = array("contrat"); // conf->propal->enabled
  32. /**
  33. * @var DoliDB Database handler.
  34. */
  35. public $db;
  36. public $param;
  37. public $info_box_head = array();
  38. public $info_box_contents = array();
  39. /**
  40. * Constructor
  41. *
  42. * @param DoliDB $db Database handler
  43. * @param string $param More parameters
  44. */
  45. public function __construct($db, $param)
  46. {
  47. global $user;
  48. $this->db = $db;
  49. $this->hidden = empty($user->rights->contrat->lire);
  50. }
  51. /**
  52. * Load data for box to show them later
  53. *
  54. * @param int $max Maximum number of records to load
  55. * @return void
  56. */
  57. public function loadBox($max = 5)
  58. {
  59. global $user, $langs, $conf;
  60. $this->max = $max;
  61. include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  62. $now = dol_now();
  63. $this->info_box_head = array('text' => $langs->trans("BoxLastExpiredServices", $max));
  64. if ($user->rights->contrat->lire) {
  65. // Select contracts with at least one expired service
  66. $sql = "SELECT ";
  67. $sql .= " c.rowid, c.ref, c.statut as fk_statut, c.date_contrat, c.ref_customer, c.ref_supplier,";
  68. $sql .= " s.nom as name, s.rowid as socid, s.email, s.client, s.fournisseur, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur,";
  69. $sql .= " MIN(cd.date_fin_validite) as date_line, COUNT(cd.rowid) as nb_services";
  70. $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c, ".MAIN_DB_PREFIX."societe s, ".MAIN_DB_PREFIX."contratdet as cd";
  71. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  72. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  73. }
  74. $sql .= " WHERE cd.statut = 4 AND cd.date_fin_validite <= '".$this->db->idate($now)."'";
  75. $sql .= " AND c.entity = ".$conf->entity;
  76. $sql .= " AND c.fk_soc=s.rowid AND cd.fk_contrat=c.rowid AND c.statut > 0";
  77. if ($user->socid) {
  78. $sql .= ' AND c.fk_soc = '.((int) $user->socid);
  79. }
  80. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  81. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  82. }
  83. $sql .= " GROUP BY c.rowid, c.ref, c.statut, c.date_contrat, c.ref_customer, c.ref_supplier, s.nom, s.rowid";
  84. $sql .= ", s.email, s.client, s.fournisseur, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur";
  85. $sql .= " ORDER BY date_line ASC";
  86. $sql .= $this->db->plimit($max, 0);
  87. $resql = $this->db->query($sql);
  88. if ($resql) {
  89. $num = $this->db->num_rows($resql);
  90. $i = 0;
  91. $thirdpartytmp = new Societe($this->db);
  92. $contract = new Contrat($this->db);
  93. while ($i < $num) {
  94. $late = '';
  95. $objp = $this->db->fetch_object($resql);
  96. $thirdpartytmp->name = $objp->name;
  97. $thirdpartytmp->id = $objp->socid;
  98. $thirdpartytmp->email = $objp->email;
  99. $thirdpartytmp->client = $objp->client;
  100. $thirdpartytmp->fournisseur = $objp->fournisseur;
  101. $thirdpartytmp->code_client = $objp->code_client;
  102. $thirdpartytmp->code_fournisseur = $objp->code_fournisseur;
  103. $thirdpartytmp->code_compta = $objp->code_compta;
  104. $thirdpartytmp->code_compta_fournisseur = $objp->code_compta_fournisseur;
  105. $contract->id = $objp->rowid;
  106. $contract->ref = $objp->ref;
  107. $contract->statut = $objp->fk_statut;
  108. $contract->ref_customer = $objp->ref_customer;
  109. $contract->ref_supplier = $objp->ref_supplier;
  110. $dateline = $this->db->jdate($objp->date_line);
  111. if (($dateline + $conf->contrat->services->expires->warning_delay) < $now) {
  112. $late = img_warning($langs->trans("Late"));
  113. }
  114. $this->info_box_contents[$i][] = array(
  115. 'td' => 'class="nowraponall"',
  116. 'text' => $contract->getNomUrl(1),
  117. 'asis' => 1
  118. );
  119. $this->info_box_contents[$i][] = array(
  120. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone left"',
  121. 'text' => $thirdpartytmp->getNomUrl(1, 'customer'),
  122. 'asis' => 1
  123. );
  124. $this->info_box_contents[$i][] = array(
  125. 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateEndPlanned").': '.dol_print_date($dateline, 'dayhour', 'tzuserrel')).'"',
  126. 'text' => dol_print_date($dateline, 'day', 'tzuserrel'),
  127. 'text2'=> $late,
  128. );
  129. $this->info_box_contents[$i][] = array(
  130. 'td' => 'class="right"',
  131. 'text' => $objp->nb_services,
  132. );
  133. $i++;
  134. }
  135. if ($num == 0) {
  136. $langs->load("contracts");
  137. $this->info_box_contents[$i][] = array(
  138. 'td' => 'class="nohover opacitymedium center"',
  139. 'text' => $langs->trans("NoExpiredServices"),
  140. );
  141. }
  142. $this->db->free($resql);
  143. } else {
  144. $this->info_box_contents[0][] = array(
  145. 'td' => '',
  146. 'maxlength'=>500,
  147. 'text' => ($this->db->error().' sql='.$sql),
  148. );
  149. }
  150. } else {
  151. $this->info_box_contents[0][0] = array(
  152. 'td' => 'class="nohover opacitymedium left"',
  153. 'text' => $langs->trans("ReadPermissionNotAllowed")
  154. );
  155. }
  156. }
  157. /**
  158. * Method to show box
  159. *
  160. * @param array $head Array with properties of box title
  161. * @param array $contents Array with properties of box lines
  162. * @param int $nooutput No print, only return string
  163. * @return string
  164. */
  165. public function showBox($head = null, $contents = null, $nooutput = 0)
  166. {
  167. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  168. }
  169. }