box_activity.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /* Copyright (C) 2012 Charles-François BENKE <charles.fr@benke.fr>
  3. * Copyright (C) 2005-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2014-2021 Frederic France <frederic.france@netlogic.fr>
  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_activity.php
  21. * \ingroup societes
  22. * \brief Module to show box of bills, orders & propal of the current year
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  25. /**
  26. * Class to manage the box of customer activity (invoice, order, proposal)
  27. */
  28. class box_activity extends ModeleBoxes
  29. {
  30. public $boxcode = "activity";
  31. public $boximg = "object_bill";
  32. public $boxlabel = 'BoxGlobalActivity';
  33. public $depends = array("facture");
  34. /**
  35. * @var DoliDB Database handler.
  36. */
  37. public $db;
  38. public $param;
  39. public $enabled = 1;
  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 $conf, $user;
  51. $this->db = $db;
  52. // FIXME: Pb into some status
  53. $this->enabled = ($conf->global->MAIN_FEATURES_LEVEL); // Not enabled by default due to bugs (see previous comments)
  54. $this->hidden = !(
  55. (isModEnabled('facture') && $user->hasRight('facture', 'read'))
  56. || (isModEnabled('commande') && $user->hasRight('commande', 'read'))
  57. || (isModEnabled('propal') && $user->hasRight('propal', 'read'))
  58. );
  59. }
  60. /**
  61. * Charge les donnees en memoire pour affichage ulterieur
  62. *
  63. * @param int $max Maximum number of records to load
  64. * @return void
  65. */
  66. public function loadBox($max = 5)
  67. {
  68. global $conf, $user, $langs;
  69. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  70. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  71. $totalnb = 0;
  72. $line = 0;
  73. $now = dol_now();
  74. $nbofperiod = 3;
  75. // Force use of cache for this box as it has very bad performances
  76. $savMAIN_ACTIVATE_FILECACHE = getDolGlobalInt('MAIN_ACTIVATE_FILECACHE');
  77. $conf->global->MAIN_ACTIVATE_FILECACHE = 1;
  78. if (getDolGlobalString('MAIN_BOX_ACTIVITY_DURATION')) {
  79. $nbofperiod = $conf->global->MAIN_BOX_ACTIVITY_DURATION;
  80. }
  81. $textHead = $langs->trans("Activity").' - '.$langs->trans("LastXMonthRolling", $nbofperiod);
  82. $this->info_box_head = array(
  83. 'text' => $textHead,
  84. 'limit'=> dol_strlen($textHead),
  85. );
  86. // compute the year limit to show
  87. $tmpdate = dol_time_plus_duree(dol_now(), -1 * $nbofperiod, "m");
  88. // list the summary of the propals
  89. if (isModEnabled("propal") && $user->hasRight("propal", "lire")) {
  90. include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  91. $propalstatic = new Propal($this->db);
  92. $data = array();
  93. $sql = "SELECT p.fk_statut, SUM(p.total_ttc) as Mnttot, COUNT(*) as nb";
  94. $sql .= " FROM (".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p";
  95. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  96. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  97. }
  98. $sql .= ")";
  99. $sql .= " WHERE p.entity IN (".getEntity('propal').")";
  100. $sql .= " AND p.fk_soc = s.rowid";
  101. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  102. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  103. }
  104. if ($user->socid) {
  105. $sql .= " AND s.rowid = ".((int) $user->socid);
  106. }
  107. $sql .= " AND p.datep >= '".$this->db->idate($tmpdate)."'";
  108. $sql .= " AND p.date_cloture IS NULL"; // just unclosed
  109. $sql .= " GROUP BY p.fk_statut";
  110. $sql .= " ORDER BY p.fk_statut DESC";
  111. $result = $this->db->query($sql);
  112. if ($result) {
  113. $num = $this->db->num_rows($result);
  114. $j = 0;
  115. while ($j < $num) {
  116. $data[$j] = $this->db->fetch_object($result);
  117. $j++;
  118. }
  119. $this->db->free($result);
  120. } else {
  121. dol_print_error($this->db);
  122. }
  123. if (!empty($data)) {
  124. $j = 0;
  125. while ($j < count($data)) {
  126. $this->info_box_contents[$line][0] = array(
  127. 'td' => 'class="left" width="16"',
  128. 'url' => DOL_URL_ROOT."/comm/propal/list.php?mainmenu=commercial&leftmenu=propals&search_status=".((int) $data[$j]->fk_statut),
  129. 'tooltip' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
  130. 'logo' => 'object_propal'
  131. );
  132. $this->info_box_contents[$line][1] = array(
  133. 'td' => '',
  134. 'text' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
  135. );
  136. $this->info_box_contents[$line][2] = array(
  137. 'td' => 'class="right"',
  138. 'text' => $data[$j]->nb,
  139. 'tooltip' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
  140. 'url' => DOL_URL_ROOT."/comm/propal/list.php?mainmenu=commercial&leftmenu=propals&search_status=".((int) $data[$j]->fk_statut),
  141. );
  142. $totalnb += $data[$j]->nb;
  143. $this->info_box_contents[$line][3] = array(
  144. 'td' => 'class="nowraponall right amount"',
  145. 'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
  146. );
  147. $this->info_box_contents[$line][4] = array(
  148. 'td' => 'class="right" width="18"',
  149. 'text' => $propalstatic->LibStatut($data[$j]->fk_statut, 3),
  150. );
  151. $line++;
  152. $j++;
  153. }
  154. if (count($data) == 0) {
  155. $this->info_box_contents[$line][0] = array(
  156. 'td' => 'class="center"',
  157. 'text'=>'<span class="opacitymedium">'.$langs->trans("NoRecordedProposals").'</span>',
  158. );
  159. $line++;
  160. }
  161. }
  162. }
  163. // list the summary of the orders
  164. if (isModEnabled('commande') && $user->hasRight("commande", "lire")) {
  165. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  166. $commandestatic = new Commande($this->db);
  167. $langs->load("orders");
  168. $data = array();
  169. $sql = "SELECT c.fk_statut, sum(c.total_ttc) as Mnttot, count(*) as nb";
  170. $sql .= " FROM (".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."commande as c";
  171. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  172. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  173. }
  174. $sql .= ")";
  175. $sql .= " WHERE c.entity IN (".getEntity('commande').")";
  176. $sql .= " AND c.fk_soc = s.rowid";
  177. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  178. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  179. }
  180. if ($user->socid) {
  181. $sql .= " AND s.rowid = ".((int) $user->socid);
  182. }
  183. $sql .= " AND c.date_commande >= '".$this->db->idate($tmpdate)."'";
  184. $sql .= " GROUP BY c.fk_statut";
  185. $sql .= " ORDER BY c.fk_statut DESC";
  186. $result = $this->db->query($sql);
  187. if ($result) {
  188. $num = $this->db->num_rows($result);
  189. $j = 0;
  190. while ($j < $num) {
  191. $data[$j] = $this->db->fetch_object($result);
  192. $j++;
  193. }
  194. $this->db->free($result);
  195. } else {
  196. dol_print_error($this->db);
  197. }
  198. if (!empty($data)) {
  199. $j = 0;
  200. while ($j < count($data)) {
  201. $this->info_box_contents[$line][0] = array(
  202. 'td' => 'class="left" width="16"',
  203. 'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
  204. 'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
  205. 'logo' => 'object_order',
  206. );
  207. $this->info_box_contents[$line][1] = array(
  208. 'td' => '',
  209. 'text' =>$langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
  210. );
  211. $this->info_box_contents[$line][2] = array(
  212. 'td' => 'class="right"',
  213. 'text' => $data[$j]->nb,
  214. 'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
  215. 'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
  216. );
  217. $totalnb += $data[$j]->nb;
  218. $this->info_box_contents[$line][3] = array(
  219. 'td' => 'class="nowraponall right amount"',
  220. 'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
  221. );
  222. $this->info_box_contents[$line][4] = array(
  223. 'td' => 'class="right" width="18"',
  224. 'text' => $commandestatic->LibStatut($data[$j]->fk_statut, 0, 3),
  225. );
  226. $line++;
  227. $j++;
  228. }
  229. if (count($data) == 0) {
  230. $this->info_box_contents[$line][0] = array(
  231. 'td' => 'class="center"',
  232. 'text'=>$langs->trans("NoRecordedOrders"),
  233. );
  234. $line++;
  235. }
  236. }
  237. }
  238. // list the summary of the bills
  239. if (isModEnabled('facture') && $user->hasRight("facture", "lire")) {
  240. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  241. $facturestatic = new Facture($this->db);
  242. // part 1
  243. $data = array();
  244. $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
  245. $sql .= " FROM (".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
  246. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  247. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  248. }
  249. $sql .= ")";
  250. $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
  251. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  252. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  253. }
  254. if ($user->socid) {
  255. $sql .= " AND s.rowid = ".((int) $user->socid);
  256. }
  257. $sql .= " AND f.fk_soc = s.rowid";
  258. $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=1";
  259. $sql .= " GROUP BY f.fk_statut";
  260. $sql .= " ORDER BY f.fk_statut DESC";
  261. $result = $this->db->query($sql);
  262. if ($result) {
  263. $num = $this->db->num_rows($result);
  264. $j = 0;
  265. while ($j < $num) {
  266. $data[$j] = $this->db->fetch_object($result);
  267. $j++;
  268. }
  269. $this->db->free($result);
  270. } else {
  271. dol_print_error($this->db);
  272. }
  273. if (!empty($data)) {
  274. $j = 0;
  275. while ($j < count($data)) {
  276. $billurl = "search_status=2&paye=1";
  277. $this->info_box_contents[$line][0] = array(
  278. 'td' => 'class="left" width="16"',
  279. 'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
  280. 'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&mainmenu=accountancy&leftmenu=customers_bills",
  281. 'logo' => 'bill',
  282. );
  283. $this->info_box_contents[$line][1] = array(
  284. 'td' => '',
  285. 'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
  286. );
  287. $this->info_box_contents[$line][2] = array(
  288. 'td' => 'class="right"',
  289. 'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
  290. 'text' => $data[$j]->nb,
  291. 'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&mainmenu=accountancy&leftmenu=customers_bills",
  292. );
  293. $this->info_box_contents[$line][3] = array(
  294. 'td' => 'class="nowraponall right amount"',
  295. 'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency)
  296. );
  297. // We add only for the current year
  298. $totalnb += $data[$j]->nb;
  299. $this->info_box_contents[$line][4] = array(
  300. 'td' => 'class="right" width="18"',
  301. 'text' => $facturestatic->LibStatut(1, $data[$j]->fk_statut, 3),
  302. );
  303. $line++;
  304. $j++;
  305. }
  306. if (count($data) == 0) {
  307. $this->info_box_contents[$line][0] = array(
  308. 'td' => 'class="center"',
  309. 'text'=>$langs->trans("NoRecordedInvoices"),
  310. );
  311. $line++;
  312. }
  313. }
  314. // part 2
  315. $data = array();
  316. $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
  317. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
  318. $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
  319. $sql .= " AND f.fk_soc = s.rowid";
  320. $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=0";
  321. $sql .= " GROUP BY f.fk_statut";
  322. $sql .= " ORDER BY f.fk_statut DESC";
  323. $result = $this->db->query($sql);
  324. if ($result) {
  325. $num = $this->db->num_rows($result);
  326. $j = 0;
  327. while ($j < $num) {
  328. $data[$j] = $this->db->fetch_object($result);
  329. $j++;
  330. }
  331. $this->db->free($result);
  332. } else {
  333. dol_print_error($this->db);
  334. }
  335. if (!empty($data)) {
  336. $alreadypaid = -1;
  337. $j = 0;
  338. while ($j < count($data)) {
  339. $billurl = "search_status=".$data[$j]->fk_statut."&paye=0";
  340. $this->info_box_contents[$line][0] = array(
  341. 'td' => 'class="left" width="16"',
  342. 'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
  343. 'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&mainmenu=accountancy&leftmenu=customers_bills",
  344. 'logo' => 'bill',
  345. );
  346. $this->info_box_contents[$line][1] = array(
  347. 'td' => '',
  348. 'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
  349. );
  350. $this->info_box_contents[$line][2] = array(
  351. 'td' => 'class="right"',
  352. 'text' => $data[$j]->nb,
  353. 'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
  354. 'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
  355. );
  356. $totalnb += $data[$j]->nb;
  357. $this->info_box_contents[$line][3] = array(
  358. 'td' => 'class="nowraponall right amount"',
  359. 'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
  360. );
  361. $this->info_box_contents[$line][4] = array(
  362. 'td' => 'class="right" width="18"',
  363. 'text' => $facturestatic->LibStatut(0, $data[$j]->fk_statut, 3, $alreadypaid),
  364. );
  365. $line++;
  366. $j++;
  367. }
  368. if (count($data) == 0) {
  369. $this->info_box_contents[$line][0] = array(
  370. 'td' => 'class="center"',
  371. 'text'=>$langs->trans("NoRecordedUnpaidInvoices"),
  372. );
  373. $line++;
  374. }
  375. }
  376. }
  377. // Add the sum in the bottom of the boxes
  378. $this->info_box_contents[$line][0] = array('tr' => 'class="liste_total_wrap"');
  379. $this->info_box_contents[$line][1] = array('td' => 'class="liste_total left" ', 'text' => $langs->trans("Total")."&nbsp;".$textHead);
  380. $this->info_box_contents[$line][2] = array('td' => 'class="liste_total right" ', 'text' => $totalnb);
  381. $this->info_box_contents[$line][3] = array('td' => 'class="liste_total right" ', 'text' => '');
  382. $this->info_box_contents[$line][4] = array('td' => 'class="liste_total right" ', 'text' => "");
  383. $conf->global->MAIN_ACTIVATE_FILECACHE = $savMAIN_ACTIVATE_FILECACHE;
  384. }
  385. /**
  386. * Method to show box
  387. *
  388. * @param array $head Array with properties of box title
  389. * @param array $contents Array with properties of box lines
  390. * @param int $nooutput No print, only return string
  391. * @return string
  392. */
  393. public function showBox($head = null, $contents = null, $nooutput = 0)
  394. {
  395. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  396. }
  397. }