box_commandes.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
  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_commandes.php
  22. * \ingroup commande
  23. * \brief Widget for latest sale orders
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  26. /**
  27. * Class to manage the box to show last customer orders
  28. */
  29. class box_commandes extends ModeleBoxes
  30. {
  31. public $boxcode = "lastcustomerorders";
  32. public $boximg = "object_order";
  33. public $boxlabel = "BoxLastCustomerOrders";
  34. public $depends = array("commande");
  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 = empty($user->rights->commande->lire);
  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. public function loadBox($max = 5)
  61. {
  62. global $user, $langs, $conf;
  63. $langs->load('orders');
  64. $this->max = $max;
  65. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  66. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  67. $commandestatic = new Commande($this->db);
  68. $societestatic = new Societe($this->db);
  69. $userstatic = new User($this->db);
  70. $this->info_box_head = array('text' => $langs->trans("BoxTitleLast".(!empty($conf->global->MAIN_LASTBOX_ON_OBJECT_DATE) ? "" : "Modified")."CustomerOrders", $max));
  71. if ($user->hasRight('commande', 'lire')) {
  72. $sql = "SELECT s.rowid as socid, s.nom as name, s.name_alias";
  73. $sql .= ", s.code_client, s.code_compta, s.client";
  74. $sql .= ", s.logo, s.email, s.entity";
  75. $sql .= ", c.ref, c.tms";
  76. $sql .= ", c.rowid";
  77. $sql .= ", c.date_commande";
  78. $sql .= ", c.ref_client";
  79. $sql .= ", c.fk_statut";
  80. $sql .= ", c.fk_user_valid";
  81. $sql .= ", c.facture";
  82. $sql .= ", c.total_ht";
  83. $sql .= ", c.total_tva";
  84. $sql .= ", c.total_ttc";
  85. $sql .= " FROM ".MAIN_DB_PREFIX."commande as c, ".MAIN_DB_PREFIX."societe as s";
  86. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  87. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  88. }
  89. $sql .= " WHERE c.fk_soc = s.rowid";
  90. $sql .= " AND c.entity IN (".getEntity('commande').")";
  91. if (!empty($conf->global->ORDER_BOX_LAST_ORDERS_VALIDATED_ONLY)) {
  92. $sql .= " AND c.fk_statut = 1";
  93. }
  94. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  95. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  96. }
  97. if ($user->socid) {
  98. $sql .= " AND s.rowid = ".((int) $user->socid);
  99. }
  100. if (!empty($conf->global->MAIN_LASTBOX_ON_OBJECT_DATE)) {
  101. $sql .= " ORDER BY c.date_commande DESC, c.ref DESC ";
  102. } else {
  103. $sql .= " ORDER BY c.tms DESC, c.ref DESC ";
  104. }
  105. $sql .= $this->db->plimit($max, 0);
  106. $result = $this->db->query($sql);
  107. if ($result) {
  108. $num = $this->db->num_rows($result);
  109. $line = 0;
  110. while ($line < $num) {
  111. $objp = $this->db->fetch_object($result);
  112. $date = $this->db->jdate($objp->date_commande);
  113. $datem = $this->db->jdate($objp->tms);
  114. $commandestatic->id = $objp->rowid;
  115. $commandestatic->ref = $objp->ref;
  116. $commandestatic->ref_client = $objp->ref_client;
  117. $commandestatic->total_ht = $objp->total_ht;
  118. $commandestatic->total_tva = $objp->total_tva;
  119. $commandestatic->total_ttc = $objp->total_ttc;
  120. $commandestatic->date = $date;
  121. $commandestatic->date_modification = $datem;
  122. $societestatic->id = $objp->socid;
  123. $societestatic->name = $objp->name;
  124. //$societestatic->name_alias = $objp->name_alias;
  125. $societestatic->code_client = $objp->code_client;
  126. $societestatic->code_compta = $objp->code_compta;
  127. $societestatic->client = $objp->client;
  128. $societestatic->logo = $objp->logo;
  129. $societestatic->email = $objp->email;
  130. $societestatic->entity = $objp->entity;
  131. $this->info_box_contents[$line][] = array(
  132. 'td' => 'class="nowraponall"',
  133. 'text' => $commandestatic->getNomUrl(1),
  134. 'asis' => 1,
  135. );
  136. $this->info_box_contents[$line][] = array(
  137. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  138. 'text' => $societestatic->getNomUrl(1),
  139. 'asis' => 1,
  140. );
  141. $this->info_box_contents[$line][] = array(
  142. 'td' => 'class="nowraponall right amount"',
  143. 'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
  144. );
  145. if (!empty($conf->global->ORDER_BOX_LAST_ORDERS_SHOW_VALIDATE_USER)) {
  146. if ($objp->fk_user_valid > 0) {
  147. $userstatic->fetch($objp->fk_user_valid);
  148. }
  149. $this->info_box_contents[$line][] = array(
  150. 'td' => 'class="right"',
  151. 'text' => (($objp->fk_user_valid > 0) ? $userstatic->getNomUrl(1) : ''),
  152. 'asis' => 1,
  153. );
  154. }
  155. $this->info_box_contents[$line][] = array(
  156. 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateModification").': '.dol_print_date($datem, 'dayhour', 'tzuserrel')).'"',
  157. 'text' => dol_print_date($datem, 'day', 'tzuserrel'),
  158. );
  159. $this->info_box_contents[$line][] = array(
  160. 'td' => 'class="right" width="18"',
  161. 'text' => $commandestatic->LibStatut($objp->fk_statut, $objp->facture, 3),
  162. );
  163. $line++;
  164. }
  165. if ($num == 0) {
  166. $this->info_box_contents[$line][0] = array(
  167. 'td' => 'class="center opacitymedium"',
  168. 'text'=>$langs->trans("NoRecordedOrders")
  169. );
  170. }
  171. $this->db->free($result);
  172. } else {
  173. $this->info_box_contents[0][0] = array(
  174. 'td' => '',
  175. 'maxlength'=>500,
  176. 'text' => ($this->db->error().' sql='.$sql),
  177. );
  178. }
  179. } else {
  180. $this->info_box_contents[0][0] = array(
  181. 'td' => 'class="nohover opacitymedium left"',
  182. 'text' => $langs->trans("ReadPermissionNotAllowed")
  183. );
  184. }
  185. }
  186. /**
  187. * Method to show box
  188. *
  189. * @param array $head Array with properties of box title
  190. * @param array $contents Array with properties of box lines
  191. * @param int $nooutput No print, only return string
  192. * @return string
  193. */
  194. public function showBox($head = null, $contents = null, $nooutput = 0)
  195. {
  196. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  197. }
  198. }