box_services_contracts.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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->rights->service->lire && $user->rights->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->rights->service->lire && $user->rights->contrat->lire)
  68. {
  69. $contractstatic = new Contrat($this->db);
  70. $contractlinestatic = new ContratLigne($this->db);
  71. $thirdpartytmp = new Societe($this->db);
  72. $productstatic = new Product($this->db);
  73. $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,";
  74. $sql .= " c.rowid, c.ref, c.statut as contract_status, c.ref_customer, c.ref_supplier,";
  75. $sql .= " cd.rowid as cdid, cd.label, cd.description, cd.tms as datem, cd.statut, cd.product_type as type,";
  76. $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as plabel, p.fk_product_type as ptype, p.entity, p.tobuy, p.tosell";
  77. $sql .= " FROM (".MAIN_DB_PREFIX."societe as s";
  78. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."contrat as c ON s.rowid = c.fk_soc";
  79. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."contratdet as cd ON c.rowid = cd.fk_contrat";
  80. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON cd.fk_product = p.rowid";
  81. if (!$user->rights->societe->client->voir && !$user->socid) $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
  82. $sql .= ")";
  83. $sql .= " WHERE c.entity = ".$conf->entity;
  84. if ($user->socid) $sql .= " AND s.rowid = ".$user->socid;
  85. $sql .= $this->db->order("c.tms", "DESC");
  86. $sql .= $this->db->plimit($max, 0);
  87. $result = $this->db->query($sql);
  88. if ($result)
  89. {
  90. $num = $this->db->num_rows($result);
  91. $now = dol_now();
  92. $i = 0;
  93. while ($i < $num)
  94. {
  95. $objp = $this->db->fetch_object($result);
  96. $datem = $this->db->jdate($objp->datem);
  97. $contractlinestatic->id = $objp->cdid;
  98. $contractlinestatic->fk_contrat = $objp->rowid;
  99. $contractlinestatic->label = $objp->label;
  100. $contractlinestatic->description = $objp->description;
  101. $contractlinestatic->type = $objp->type;
  102. $contractlinestatic->product_id = $objp->product_id;
  103. $contractlinestatic->product_ref = $objp->product_ref;
  104. $contractstatic->statut = $objp->contract_status;
  105. $contractstatic->id = $objp->rowid;
  106. $contractstatic->ref = $objp->ref;
  107. $contractstatic->ref_customer = $objp->ref_customer;
  108. $contractstatic->ref_supplier = $objp->ref_supplier;
  109. $thirdpartytmp->name = $objp->name;
  110. $thirdpartytmp->id = $objp->socid;
  111. $thirdpartytmp->email = $objp->email;
  112. $thirdpartytmp->client = $objp->client;
  113. $thirdpartytmp->fournisseur = $objp->fournisseur;
  114. $thirdpartytmp->code_client = $objp->code_client;
  115. $thirdpartytmp->code_fournisseur = $objp->code_fournisseur;
  116. $thirdpartytmp->code_compta = $objp->code_compta;
  117. $thirdpartytmp->code_compta_fournisseur = $objp->code_compta_fournisseur;
  118. // Multilangs
  119. if (!empty($conf->global->MAIN_MULTILANGS) && $objp->product_id > 0) // if option multilang is on
  120. {
  121. $sqld = "SELECT label";
  122. $sqld .= " FROM ".MAIN_DB_PREFIX."product_lang";
  123. $sqld .= " WHERE fk_product=".$objp->product_id;
  124. $sqld .= " AND lang='".$langs->getDefaultLang()."'";
  125. $sqld .= " LIMIT 1";
  126. $resultd = $this->db->query($sqld);
  127. if ($resultd)
  128. {
  129. $objtp = $this->db->fetch_object($resultd);
  130. if ($objtp->label != '') $contractlinestatic->label = $objtp->label;
  131. }
  132. }
  133. // Label
  134. if ($objp->product_id > 0)
  135. {
  136. $productstatic->id = $objp->product_id;
  137. $productstatic->type = $objp->ptype;
  138. $productstatic->ref = $objp->product_ref;
  139. $productstatic->entity = $objp->pentity;
  140. $productstatic->label = $objp->plabel;
  141. $productstatic->status = $objp->tosell;
  142. $productstatic->status_buy = $objp->tobuy;
  143. $text = $productstatic->getNomUrl(1, '', 20);
  144. if ($objp->plabel)
  145. {
  146. $text .= ' - ';
  147. //$productstatic->ref=$objp->label;
  148. //$text .= $productstatic->getNomUrl(0,'',16);
  149. $text .= $objp->plabel;
  150. }
  151. $description = $objp->description;
  152. // Add description in form
  153. if (!empty($conf->global->PRODUIT_DESC_IN_FORM))
  154. {
  155. //$text .= (! empty($objp->description) && $objp->description!=$objp->plabel)?'<br>'.dol_htmlentitiesbr($objp->description):'';
  156. $description = ''; // Already added into main visible desc
  157. }
  158. $s = $form->textwithtooltip($text, $description, 3, '', '', $cursorline, 0, (!empty($line->fk_parent_line) ?img_picto('', 'rightarrow') : ''));
  159. }
  160. else
  161. {
  162. $s = img_object($langs->trans("ShowProductOrService"), ($objp->product_type ? 'service' : 'product')).' '.dol_htmlentitiesbr($objp->description);
  163. }
  164. $this->info_box_contents[$i][] = array(
  165. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  166. 'text' => $s,
  167. 'asis' => 1
  168. );
  169. $this->info_box_contents[$i][] = array(
  170. 'td' => 'class="nowraponall"',
  171. 'text' => $contractstatic->getNomUrl(1),
  172. 'asis' => 1
  173. );
  174. $this->info_box_contents[$i][] = array(
  175. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  176. 'text' => $thirdpartytmp->getNomUrl(1),
  177. 'asis' => 1
  178. );
  179. $this->info_box_contents[$i][] = array(
  180. 'td' => '',
  181. 'text' => dol_print_date($datem, 'day'),
  182. );
  183. $this->info_box_contents[$i][] = array(
  184. 'td' => 'class="right" width="18"',
  185. 'text' => $contractlinestatic->LibStatut($objp->statut, 3)
  186. );
  187. $i++;
  188. }
  189. if ($num == 0) $this->info_box_contents[$i][0] = array(
  190. 'td' => 'class="center opacitymedium"',
  191. 'text'=>$langs->trans("NoContractedProducts")
  192. );
  193. $this->db->free($result);
  194. }
  195. else
  196. {
  197. $this->info_box_contents[0][0] = array(
  198. 'td' => '',
  199. 'maxlength' => 500,
  200. 'text' => ($this->db->error().' sql='.$sql),
  201. );
  202. }
  203. }
  204. else {
  205. $this->info_box_contents[0][0] = array(
  206. 'td' => 'class="nohover opacitymedium left"',
  207. 'text' => $langs->trans("ReadPermissionNotAllowed")
  208. );
  209. }
  210. }
  211. /**
  212. * Method to show box
  213. *
  214. * @param array $head Array with properties of box title
  215. * @param array $contents Array with properties of box lines
  216. * @param int $nooutput No print, only return string
  217. * @return string
  218. */
  219. public function showBox($head = null, $contents = null, $nooutput = 0)
  220. {
  221. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  222. }
  223. }