box_contracts.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /* Copyright (C) 2010 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
  4. * Copyright (C) 2016-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  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/core/boxes/box_contracts.php
  21. * \ingroup contracts
  22. * \brief Module de generation de l'affichage de la box contracts
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  25. /**
  26. * Class to manage the box to show last contracts
  27. */
  28. class box_contracts extends ModeleBoxes
  29. {
  30. public $boxcode = "lastcontracts";
  31. public $boximg = "object_contract";
  32. public $boxlabel = "BoxLastContracts";
  33. public $depends = array("contrat"); // conf->contrat->enabled
  34. /**
  35. * @var DoliDB Database handler.
  36. */
  37. public $db;
  38. public $param;
  39. public $info_box_head = array();
  40. public $info_box_contents = array();
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. * @param string $param More parameters
  46. */
  47. public function __construct($db, $param)
  48. {
  49. global $user;
  50. $this->db = $db;
  51. $this->hidden = !($user->hasRight('contrat', 'lire'));
  52. }
  53. /**
  54. * Load data for box to show them later
  55. *
  56. * @param int $max Maximum number of records to load
  57. * @return void
  58. */
  59. public function loadBox($max = 5)
  60. {
  61. global $user, $langs, $conf;
  62. $this->max = $max;
  63. include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  64. $this->info_box_head = array('text' => $langs->trans("BoxTitleLastContracts", $max));
  65. if ($user->hasRight('contrat', 'lire')) {
  66. $contractstatic = new Contrat($this->db);
  67. $thirdpartytmp = new Societe($this->db);
  68. $sql = "SELECT 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 .= " c.rowid, c.ref, c.statut as fk_statut, c.date_contrat, c.datec, c.tms as date_modification, c.fin_validite, c.date_cloture,";
  70. $sql .= " c.ref_customer, c.ref_supplier";
  71. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
  72. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  73. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  74. }
  75. $sql .= " WHERE c.fk_soc = s.rowid";
  76. $sql .= " AND c.entity = ".$conf->entity;
  77. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  78. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  79. }
  80. if ($user->socid) {
  81. $sql .= " AND s.rowid = ".((int) $user->socid);
  82. }
  83. if (!empty($conf->global->MAIN_LASTBOX_ON_OBJECT_DATE)) {
  84. $sql .= " ORDER BY c.date_contrat DESC, c.ref DESC ";
  85. } else {
  86. $sql .= " ORDER BY c.tms DESC, c.ref DESC ";
  87. }
  88. $sql .= $this->db->plimit($max, 0);
  89. $resql = $this->db->query($sql);
  90. if ($resql) {
  91. $num = $this->db->num_rows($resql);
  92. $now = dol_now();
  93. $line = 0;
  94. $langs->load("contracts");
  95. while ($line < $num) {
  96. $objp = $this->db->fetch_object($resql);
  97. $datec = $this->db->jdate($objp->datec);
  98. $datem = $this->db->jdate($objp->date_modification);
  99. $dateterm = $this->db->jdate($objp->fin_validite);
  100. $dateclose = $this->db->jdate($objp->date_cloture);
  101. $late = '';
  102. $contractstatic->statut = $objp->fk_statut;
  103. $contractstatic->id = $objp->rowid;
  104. $contractstatic->ref = $objp->ref;
  105. $contractstatic->ref_customer = $objp->ref_customer;
  106. $contractstatic->ref_supplier = $objp->ref_supplier;
  107. $result = $contractstatic->fetch_lines();
  108. $thirdpartytmp->name = $objp->name;
  109. $thirdpartytmp->id = $objp->socid;
  110. $thirdpartytmp->email = $objp->email;
  111. $thirdpartytmp->client = $objp->client;
  112. $thirdpartytmp->fournisseur = $objp->fournisseur;
  113. $thirdpartytmp->code_client = $objp->code_client;
  114. $thirdpartytmp->code_fournisseur = $objp->code_fournisseur;
  115. $thirdpartytmp->code_compta = $objp->code_compta;
  116. $thirdpartytmp->code_compta_fournisseur = $objp->code_compta_fournisseur;
  117. // fin_validite is no more on contract but on services
  118. // if ($objp->fk_statut == 1 && $dateterm < ($now - $conf->contrat->cloture->warning_delay)) { $late = img_warning($langs->trans("Late")); }
  119. $this->info_box_contents[$line][] = array(
  120. 'td' => 'class="nowraponall"',
  121. 'text' => $contractstatic->getNomUrl(1),
  122. 'text2'=> $late,
  123. 'asis'=>1
  124. );
  125. $this->info_box_contents[$line][] = array(
  126. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  127. 'text' => $thirdpartytmp->getNomUrl(1),
  128. 'asis'=>1
  129. );
  130. $this->info_box_contents[$line][] = array(
  131. 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateModification").': '.dol_print_date($datem, 'dayhour', 'tzuserrel')).'"',
  132. 'text' => dol_print_date($datem, 'day', 'tzuserrel'),
  133. );
  134. $this->info_box_contents[$line][] = array(
  135. 'td' => 'class="nowraponall right"',
  136. 'text' => $contractstatic->getLibStatut(7),
  137. 'asis'=>1,
  138. );
  139. $line++;
  140. }
  141. if ($num == 0) {
  142. $this->info_box_contents[$line][0] = array(
  143. 'td' => 'class="center opacitymedium"',
  144. 'text'=>$langs->trans("NoRecordedContracts"),
  145. );
  146. }
  147. $this->db->free($resql);
  148. } else {
  149. $this->info_box_contents[0][0] = array(
  150. 'td' => '',
  151. 'maxlength'=>500,
  152. 'text' => ($this->db->error().' sql='.$sql),
  153. );
  154. }
  155. } else {
  156. $this->info_box_contents[0][0] = array(
  157. 'td' => 'class="nohover opacitymedium left"',
  158. 'text' => $langs->trans("ReadPermissionNotAllowed")
  159. );
  160. }
  161. }
  162. /**
  163. * Method to show box
  164. *
  165. * @param array $head Array with properties of box title
  166. * @param array $contents Array with properties of box lines
  167. * @param int $nooutput No print, only return string
  168. * @return string
  169. */
  170. public function showBox($head = null, $contents = null, $nooutput = 0)
  171. {
  172. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  173. }
  174. }