box_commandes.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 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 = !($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->rights->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."societe as s";
  86. $sql .= ", ".MAIN_DB_PREFIX."commande as c";
  87. if (!$user->rights->societe->client->voir && !$user->socid) {
  88. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  89. }
  90. $sql .= " WHERE c.fk_soc = s.rowid";
  91. $sql .= " AND c.entity IN (".getEntity('commande').")";
  92. if (!empty($conf->global->ORDER_BOX_LAST_ORDERS_VALIDATED_ONLY)) {
  93. $sql .= " AND c.fk_statut = 1";
  94. }
  95. if (!$user->rights->societe->client->voir && !$user->socid) {
  96. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  97. }
  98. if ($user->socid) {
  99. $sql .= " AND s.rowid = ".((int) $user->socid);
  100. }
  101. if (!empty($conf->global->MAIN_LASTBOX_ON_OBJECT_DATE)) {
  102. $sql .= " ORDER BY c.date_commande DESC, c.ref DESC ";
  103. } else {
  104. $sql .= " ORDER BY c.tms DESC, c.ref DESC ";
  105. }
  106. $sql .= $this->db->plimit($max, 0);
  107. $result = $this->db->query($sql);
  108. if ($result) {
  109. $num = $this->db->num_rows($result);
  110. $line = 0;
  111. while ($line < $num) {
  112. $objp = $this->db->fetch_object($result);
  113. $date = $this->db->jdate($objp->date_commande);
  114. $datem = $this->db->jdate($objp->tms);
  115. $commandestatic->id = $objp->rowid;
  116. $commandestatic->ref = $objp->ref;
  117. $commandestatic->ref_client = $objp->ref_client;
  118. $commandestatic->total_ht = $objp->total_ht;
  119. $commandestatic->total_tva = $objp->total_tva;
  120. $commandestatic->total_ttc = $objp->total_ttc;
  121. $societestatic->id = $objp->socid;
  122. $societestatic->name = $objp->name;
  123. //$societestatic->name_alias = $objp->name_alias;
  124. $societestatic->code_client = $objp->code_client;
  125. $societestatic->code_compta = $objp->code_compta;
  126. $societestatic->client = $objp->client;
  127. $societestatic->logo = $objp->logo;
  128. $societestatic->email = $objp->email;
  129. $societestatic->entity = $objp->entity;
  130. $this->info_box_contents[$line][] = array(
  131. 'td' => 'class="nowraponall"',
  132. 'text' => $commandestatic->getNomUrl(1),
  133. 'asis' => 1,
  134. );
  135. $this->info_box_contents[$line][] = array(
  136. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  137. 'text' => $societestatic->getNomUrl(1),
  138. 'asis' => 1,
  139. );
  140. $this->info_box_contents[$line][] = array(
  141. 'td' => 'class="nowraponall right"',
  142. 'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
  143. );
  144. if (!empty($conf->global->ORDER_BOX_LAST_ORDERS_SHOW_VALIDATE_USER)) {
  145. if ($objp->fk_user_valid > 0) {
  146. $userstatic->fetch($objp->fk_user_valid);
  147. }
  148. $this->info_box_contents[$line][] = array(
  149. 'td' => 'class="right"',
  150. 'text' => (($objp->fk_user_valid > 0) ? $userstatic->getNomUrl(1) : ''),
  151. 'asis' => 1,
  152. );
  153. }
  154. $this->info_box_contents[$line][] = array(
  155. 'td' => 'class="right"',
  156. 'text' => dol_print_date($date, 'day', 'tzuserrel'),
  157. );
  158. $this->info_box_contents[$line][] = array(
  159. 'td' => 'class="right" width="18"',
  160. 'text' => $commandestatic->LibStatut($objp->fk_statut, $objp->facture, 3),
  161. );
  162. $line++;
  163. }
  164. if ($num == 0) {
  165. $this->info_box_contents[$line][0] = array(
  166. 'td' => 'class="center opacitymedium"',
  167. 'text'=>$langs->trans("NoRecordedOrders")
  168. );
  169. }
  170. $this->db->free($result);
  171. } else {
  172. $this->info_box_contents[0][0] = array(
  173. 'td' => '',
  174. 'maxlength'=>500,
  175. 'text' => ($this->db->error().' sql='.$sql),
  176. );
  177. }
  178. } else {
  179. $this->info_box_contents[0][0] = array(
  180. 'td' => 'class="nohover opacitymedium left"',
  181. 'text' => $langs->trans("ReadPermissionNotAllowed")
  182. );
  183. }
  184. }
  185. /**
  186. * Method to show box
  187. *
  188. * @param array $head Array with properties of box title
  189. * @param array $contents Array with properties of box lines
  190. * @param int $nooutput No print, only return string
  191. * @return string
  192. */
  193. public function showBox($head = null, $contents = null, $nooutput = 0)
  194. {
  195. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  196. }
  197. }