box_services_contracts.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2017 Nicolas Zabouri <info@inovea-conseil.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/boxes/box_services_contracts.php
  22. * \ingroup produits,services
  23. * \brief Widget of sells products
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  26. /**
  27. * Class to manage the box to show last contracted products/services lines
  28. */
  29. class box_services_contracts extends ModeleBoxes
  30. {
  31. public $boxcode = "lastproductsincontract";
  32. public $boximg = "object_product";
  33. public $boxlabel = "BoxLastProductsInContract";
  34. public $depends = array("service", "contrat");
  35. /**
  36. * @var DoliDB Database handler.
  37. */
  38. public $db;
  39. public $param;
  40. public $info_box_head = array();
  41. public $info_box_contents = array();
  42. /**
  43. * Constructor
  44. *
  45. * @param DoliDB $db Database handler
  46. * @param string $param More parameters
  47. */
  48. public function __construct($db, $param)
  49. {
  50. global $user;
  51. $this->db = $db;
  52. $this->hidden = !($user->hasRight('service', 'lire') && $user->hasRight('contrat', 'lire'));
  53. }
  54. /**
  55. * Load data into info_box_contents array to show array later.
  56. *
  57. * @param int $max Maximum number of records to load
  58. * @return void
  59. */
  60. public function loadBox($max = 5)
  61. {
  62. global $user, $langs, $conf;
  63. $this->max = $max;
  64. include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  65. $form = new Form($this->db);
  66. $this->info_box_head = array('text' => $langs->trans("BoxLastProductsInContract", $max));
  67. if ($user->hasRight('service', 'lire') && $user->hasRight('contrat', 'lire')) {
  68. $contractstatic = new Contrat($this->db);
  69. $contractlinestatic = new ContratLigne($this->db);
  70. $thirdpartytmp = new Societe($this->db);
  71. $productstatic = new Product($this->db);
  72. $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,";
  73. $sql .= " c.rowid, c.ref, c.statut as contract_status, c.ref_customer, c.ref_supplier,";
  74. $sql .= " cd.rowid as cdid, cd.label, cd.description, cd.tms as datem, cd.statut as contractline_status, cd.product_type as type, cd.date_fin_validite as date_line,";
  75. $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as product_type, p.entity as product_entity, p.tobuy, p.tosell";
  76. $sql .= " FROM (".MAIN_DB_PREFIX."societe as s";
  77. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."contrat as c ON s.rowid = c.fk_soc";
  78. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."contratdet as cd ON c.rowid = cd.fk_contrat";
  79. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON cd.fk_product = p.rowid";
  80. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  81. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  82. }
  83. $sql .= ")";
  84. $sql .= " WHERE c.entity = ".$conf->entity;
  85. if ($user->socid) {
  86. $sql .= " AND s.rowid = ".((int) $user->socid);
  87. }
  88. $sql .= $this->db->order("c.tms", "DESC");
  89. $sql .= $this->db->plimit($max, 0);
  90. $result = $this->db->query($sql);
  91. if ($result) {
  92. $num = $this->db->num_rows($result);
  93. $now = dol_now();
  94. $i = 0;
  95. while ($i < $num) {
  96. $late = '';
  97. $objp = $this->db->fetch_object($result);
  98. $datem = $this->db->jdate($objp->datem);
  99. $contractlinestatic->id = $objp->cdid;
  100. $contractlinestatic->fk_contrat = $objp->rowid;
  101. $contractlinestatic->label = $objp->label;
  102. $contractlinestatic->description = $objp->description;
  103. $contractlinestatic->type = $objp->type;
  104. $contractlinestatic->fk_product = $objp->product_id;
  105. $contractlinestatic->product_ref = $objp->product_ref;
  106. $contractlinestatic->product_type = $objp->product_type;
  107. $contractlinestatic->statut = $objp->contractline_status;
  108. $contractstatic->id = $objp->rowid;
  109. $contractstatic->ref = $objp->ref;
  110. $contractstatic->ref_customer = $objp->ref_customer;
  111. $contractstatic->ref_supplier = $objp->ref_supplier;
  112. $contractstatic->statut = $objp->contract_status;
  113. $thirdpartytmp->name = $objp->name;
  114. $thirdpartytmp->id = $objp->socid;
  115. $thirdpartytmp->email = $objp->email;
  116. $thirdpartytmp->client = $objp->client;
  117. $thirdpartytmp->fournisseur = $objp->fournisseur;
  118. $thirdpartytmp->code_client = $objp->code_client;
  119. $thirdpartytmp->code_fournisseur = $objp->code_fournisseur;
  120. $thirdpartytmp->code_compta = $objp->code_compta;
  121. $thirdpartytmp->code_compta_fournisseur = $objp->code_compta_fournisseur;
  122. $dateline = $this->db->jdate($objp->date_line);
  123. if ($contractstatic->statut == Contrat::STATUS_VALIDATED && $objp->contractline_status == ContratLigne::STATUS_OPEN && !empty($dateline) && ($dateline + $conf->contrat->services->expires->warning_delay) < $now) {
  124. $late = img_warning($langs->trans("Late"));
  125. }
  126. // Label
  127. if ($objp->product_id > 0) {
  128. $productstatic->id = $objp->product_id;
  129. $productstatic->type = $objp->product_type;
  130. $productstatic->ref = $objp->product_ref;
  131. $productstatic->entity = $objp->product_entity;
  132. $productstatic->label = $objp->product_label;
  133. $productstatic->status = $objp->tosell;
  134. $productstatic->status_buy = $objp->tobuy;
  135. $text = $productstatic->getNomUrl(1, '', 20);
  136. if ($objp->product_label) {
  137. $text .= ' - ';
  138. //$productstatic->ref=$objp->label;
  139. //$text .= $productstatic->getNomUrl(0,'',16);
  140. $text .= $objp->product_label;
  141. }
  142. $description = $objp->description;
  143. $s = $form->textwithtooltip($text, $description, 3, '', '', '', 0, '');
  144. } else {
  145. $s = img_object($langs->trans("ShowProductOrService"), ($objp->product_type ? 'service' : 'product')).' '.dol_htmlentitiesbr($objp->description);
  146. }
  147. $this->info_box_contents[$i][] = array(
  148. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  149. 'text' => $s,
  150. 'asis' => 1
  151. );
  152. $this->info_box_contents[$i][] = array(
  153. 'td' => 'class="nowraponall"',
  154. 'text' => $contractstatic->getNomUrl(1),
  155. 'asis' => 1
  156. );
  157. $this->info_box_contents[$i][] = array(
  158. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  159. 'text' => $thirdpartytmp->getNomUrl(1),
  160. 'asis' => 1
  161. );
  162. $this->info_box_contents[$i][] = array(
  163. 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateModification").': '.dol_print_date($datem, 'dayhour', 'tzuserrel')).'"',
  164. 'text' => dol_print_date($datem, 'day', 'tzuserrel'),
  165. 'text2'=> $late,
  166. );
  167. $this->info_box_contents[$i][] = array(
  168. 'td' => 'class="right" width="18"',
  169. 'text' => $contractlinestatic->getLibStatut(3)
  170. );
  171. $i++;
  172. }
  173. if ($num == 0) {
  174. $this->info_box_contents[$i][0] = array(
  175. 'td' => 'class="center opacitymedium"',
  176. 'text'=>$langs->trans("NoContractedProducts")
  177. );
  178. }
  179. $this->db->free($result);
  180. } else {
  181. $this->info_box_contents[0][0] = array(
  182. 'td' => '',
  183. 'maxlength' => 500,
  184. 'text' => ($this->db->error().' sql='.$sql),
  185. );
  186. }
  187. } else {
  188. $this->info_box_contents[0][0] = array(
  189. 'td' => 'class="nohover opacitymedium left"',
  190. 'text' => $langs->trans("ReadPermissionNotAllowed")
  191. );
  192. }
  193. }
  194. /**
  195. * Method to show box
  196. *
  197. * @param array $head Array with properties of box title
  198. * @param array $contents Array with properties of box lines
  199. * @param int $nooutput No print, only return string
  200. * @return string
  201. */
  202. public function showBox($head = null, $contents = null, $nooutput = 0)
  203. {
  204. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  205. }
  206. }