box_activity.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 = !((isModEnabled('facture') && $user->rights->facture->lire)
  55. || (isModEnabled('commande') && $user->rights->commande->lire)
  56. || (isModEnabled('propal') && $user->rights->propale->lire)
  57. );
  58. }
  59. /**
  60. * Charge les donnees en memoire pour affichage ulterieur
  61. *
  62. * @param int $max Maximum number of records to load
  63. * @return void
  64. */
  65. public function loadBox($max = 5)
  66. {
  67. global $conf, $user, $langs;
  68. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  69. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  70. $totalnb = 0;
  71. $line = 0;
  72. $cachetime = 3600;
  73. $fileid = '-e'.$conf->entity.'-u'.$user->id.'-s'.$user->socid.'-r'.($user->rights->societe->client->voir ? '1' : '0').'.cache';
  74. $now = dol_now();
  75. $nbofperiod = 3;
  76. if (!empty($conf->global->MAIN_BOX_ACTIVITY_DURATION)) {
  77. $nbofperiod = $conf->global->MAIN_BOX_ACTIVITY_DURATION;
  78. }
  79. $textHead = $langs->trans("Activity").' - '.$langs->trans("LastXMonthRolling", $nbofperiod);
  80. $this->info_box_head = array(
  81. 'text' => $textHead,
  82. 'limit'=> dol_strlen($textHead),
  83. );
  84. // compute the year limit to show
  85. $tmpdate = dol_time_plus_duree(dol_now(), -1 * $nbofperiod, "m");
  86. // list the summary of the propals
  87. if (!empty($conf->propal->enabled) && $user->rights->propale->lire) {
  88. include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  89. $propalstatic = new Propal($this->db);
  90. $cachedir = DOL_DATA_ROOT.'/propale/temp';
  91. $filename = '/boxactivity-propal'.$fileid;
  92. $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
  93. $data = array();
  94. if ($refresh) {
  95. $sql = "SELECT p.fk_statut, SUM(p.total_ttc) as Mnttot, COUNT(*) as nb";
  96. $sql .= " FROM (".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p";
  97. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  98. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  99. }
  100. $sql .= ")";
  101. $sql .= " WHERE p.entity IN (".getEntity('propal').")";
  102. $sql .= " AND p.fk_soc = s.rowid";
  103. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  104. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  105. }
  106. if ($user->socid) {
  107. $sql .= " AND s.rowid = ".((int) $user->socid);
  108. }
  109. $sql .= " AND p.datep >= '".$this->db->idate($tmpdate)."'";
  110. $sql .= " AND p.date_cloture IS NULL"; // just unclosed
  111. $sql .= " GROUP BY p.fk_statut";
  112. $sql .= " ORDER BY p.fk_statut DESC";
  113. $result = $this->db->query($sql);
  114. if ($result) {
  115. $num = $this->db->num_rows($result);
  116. $j = 0;
  117. while ($j < $num) {
  118. $data[$j] = $this->db->fetch_object($result);
  119. $j++;
  120. }
  121. if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
  122. dol_filecache($cachedir, $filename, $data);
  123. }
  124. $this->db->free($result);
  125. } else {
  126. dol_print_error($this->db);
  127. }
  128. } else {
  129. $data = dol_readcachefile($cachedir, $filename);
  130. }
  131. if (!empty($data)) {
  132. $j = 0;
  133. while ($j < count($data)) {
  134. $this->info_box_contents[$line][0] = array(
  135. 'td' => 'class="left" width="16"',
  136. 'url' => DOL_URL_ROOT."/comm/propal/list.php?mainmenu=commercial&amp;leftmenu=propals&amp;search_status=".$data[$j]->fk_statut,
  137. 'tooltip' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
  138. 'logo' => 'object_propal'
  139. );
  140. $this->info_box_contents[$line][1] = array(
  141. 'td' => '',
  142. 'text' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
  143. );
  144. $this->info_box_contents[$line][2] = array(
  145. 'td' => 'class="right"',
  146. 'text' => $data[$j]->nb,
  147. 'tooltip' => $langs->trans("Proposals")."&nbsp;".$propalstatic->LibStatut($data[$j]->fk_statut, 0),
  148. 'url' => DOL_URL_ROOT."/comm/propal/list.php?mainmenu=commercial&amp;leftmenu=propals&amp;search_status=".$data[$j]->fk_statut,
  149. );
  150. $totalnb += $data[$j]->nb;
  151. $this->info_box_contents[$line][3] = array(
  152. 'td' => 'class="nowraponall right amount"',
  153. 'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
  154. );
  155. $this->info_box_contents[$line][4] = array(
  156. 'td' => 'class="right" width="18"',
  157. 'text' => $propalstatic->LibStatut($data[$j]->fk_statut, 3),
  158. );
  159. $line++;
  160. $j++;
  161. }
  162. }
  163. }
  164. // list the summary of the orders
  165. if (!empty($conf->commande->enabled) && $user->rights->commande->lire) {
  166. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  167. $commandestatic = new Commande($this->db);
  168. $langs->load("orders");
  169. $cachedir = DOL_DATA_ROOT.'/commande/temp';
  170. $filename = '/boxactivity-order'.$fileid;
  171. $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
  172. $data = array();
  173. if ($refresh) {
  174. $sql = "SELECT c.fk_statut, sum(c.total_ttc) as Mnttot, count(*) as nb";
  175. $sql .= " FROM (".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."commande as c";
  176. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  177. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  178. }
  179. $sql .= ")";
  180. $sql .= " WHERE c.entity IN (".getEntity('commande').")";
  181. $sql .= " AND c.fk_soc = s.rowid";
  182. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  183. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  184. }
  185. if ($user->socid) {
  186. $sql .= " AND s.rowid = ".((int) $user->socid);
  187. }
  188. $sql .= " AND c.date_commande >= '".$this->db->idate($tmpdate)."'";
  189. $sql .= " GROUP BY c.fk_statut";
  190. $sql .= " ORDER BY c.fk_statut DESC";
  191. $result = $this->db->query($sql);
  192. if ($result) {
  193. $num = $this->db->num_rows($result);
  194. $j = 0;
  195. while ($j < $num) {
  196. $data[$j] = $this->db->fetch_object($result);
  197. $j++;
  198. }
  199. if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
  200. dol_filecache($cachedir, $filename, $data);
  201. }
  202. $this->db->free($result);
  203. } else {
  204. dol_print_error($this->db);
  205. }
  206. } else {
  207. $data = dol_readcachefile($cachedir, $filename);
  208. }
  209. if (!empty($data)) {
  210. $j = 0;
  211. while ($j < count($data)) {
  212. $this->info_box_contents[$line][0] = array(
  213. 'td' => 'class="left" width="16"',
  214. 'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
  215. 'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
  216. 'logo' => 'object_order',
  217. );
  218. $this->info_box_contents[$line][1] = array(
  219. 'td' => '',
  220. 'text' =>$langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
  221. );
  222. $this->info_box_contents[$line][2] = array(
  223. 'td' => 'class="right"',
  224. 'text' => $data[$j]->nb,
  225. 'tooltip' => $langs->trans("Orders")."&nbsp;".$commandestatic->LibStatut($data[$j]->fk_statut, 0, 0),
  226. 'url' => DOL_URL_ROOT."/commande/list.php?mainmenu=commercial&amp;leftmenu=orders&amp;search_status=".$data[$j]->fk_statut,
  227. );
  228. $totalnb += $data[$j]->nb;
  229. $this->info_box_contents[$line][3] = array(
  230. 'td' => 'class="nowraponall right amount"',
  231. 'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
  232. );
  233. $this->info_box_contents[$line][4] = array(
  234. 'td' => 'class="right" width="18"',
  235. 'text' => $commandestatic->LibStatut($data[$j]->fk_statut, 0, 3),
  236. );
  237. $line++;
  238. $j++;
  239. }
  240. }
  241. }
  242. // list the summary of the bills
  243. if (isModEnabled('facture') && $user->rights->facture->lire) {
  244. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  245. $facturestatic = new Facture($this->db);
  246. // part 1
  247. $cachedir = DOL_DATA_ROOT.'/facture/temp';
  248. $filename = '/boxactivity-invoice'.$fileid;
  249. $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
  250. $data = array();
  251. if ($refresh) {
  252. $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
  253. $sql .= " FROM (".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
  254. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  255. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  256. }
  257. $sql .= ")";
  258. $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
  259. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  260. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  261. }
  262. if ($user->socid) {
  263. $sql .= " AND s.rowid = ".((int) $user->socid);
  264. }
  265. $sql .= " AND f.fk_soc = s.rowid";
  266. $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=1";
  267. $sql .= " GROUP BY f.fk_statut";
  268. $sql .= " ORDER BY f.fk_statut DESC";
  269. $result = $this->db->query($sql);
  270. if ($result) {
  271. $num = $this->db->num_rows($result);
  272. $j = 0;
  273. while ($j < $num) {
  274. $data[$j] = $this->db->fetch_object($result);
  275. $j++;
  276. }
  277. if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
  278. dol_filecache($cachedir, $filename, $data);
  279. }
  280. $this->db->free($result);
  281. } else {
  282. dol_print_error($this->db);
  283. }
  284. } else {
  285. $data = dol_readcachefile($cachedir, $filename);
  286. }
  287. if (!empty($data)) {
  288. $j = 0;
  289. while ($j < count($data)) {
  290. $billurl = "search_status=2&amp;paye=1";
  291. $this->info_box_contents[$line][0] = array(
  292. 'td' => 'class="left" width="16"',
  293. 'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
  294. 'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
  295. 'logo' => 'bill',
  296. );
  297. $this->info_box_contents[$line][1] = array(
  298. 'td' => '',
  299. 'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
  300. );
  301. $this->info_box_contents[$line][2] = array(
  302. 'td' => 'class="right"',
  303. 'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(1, $data[$j]->fk_statut, 0),
  304. 'text' => $data[$j]->nb,
  305. 'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
  306. );
  307. $this->info_box_contents[$line][3] = array(
  308. 'td' => 'class="nowraponall right amount"',
  309. 'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency)
  310. );
  311. // We add only for the current year
  312. $totalnb += $data[$j]->nb;
  313. $this->info_box_contents[$line][4] = array(
  314. 'td' => 'class="right" width="18"',
  315. 'text' => $facturestatic->LibStatut(1, $data[$j]->fk_statut, 3),
  316. );
  317. $line++;
  318. $j++;
  319. }
  320. if (count($data) == 0) {
  321. $this->info_box_contents[$line][0] = array(
  322. 'td' => 'class="center"',
  323. 'text'=>$langs->trans("NoRecordedInvoices"),
  324. );
  325. }
  326. }
  327. // part 2
  328. $cachedir = DOL_DATA_ROOT.'/facture/temp';
  329. $filename = '/boxactivity-invoice2'.$fileid;
  330. $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
  331. $data = array();
  332. if ($refresh) {
  333. $sql = "SELECT f.fk_statut, SUM(f.total_ttc) as Mnttot, COUNT(*) as nb";
  334. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
  335. $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
  336. $sql .= " AND f.fk_soc = s.rowid";
  337. $sql .= " AND f.datef >= '".$this->db->idate($tmpdate)."' AND f.paye=0";
  338. $sql .= " GROUP BY f.fk_statut";
  339. $sql .= " ORDER BY f.fk_statut DESC";
  340. $result = $this->db->query($sql);
  341. if ($result) {
  342. $num = $this->db->num_rows($result);
  343. $j = 0;
  344. while ($j < $num) {
  345. $data[$j] = $this->db->fetch_object($result);
  346. $j++;
  347. }
  348. if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
  349. dol_filecache($cachedir, $filename, $data);
  350. }
  351. $this->db->free($result);
  352. } else {
  353. dol_print_error($this->db);
  354. }
  355. } else {
  356. $data = dol_readcachefile($cachedir, $filename);
  357. }
  358. if (!empty($data)) {
  359. $alreadypaid = -1;
  360. $j = 0;
  361. while ($j < count($data)) {
  362. $billurl = "search_status=".$data[$j]->fk_statut."&amp;paye=0";
  363. $this->info_box_contents[$line][0] = array(
  364. 'td' => 'class="left" width="16"',
  365. 'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
  366. 'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
  367. 'logo' => 'bill',
  368. );
  369. $this->info_box_contents[$line][1] = array(
  370. 'td' => '',
  371. 'text' => $langs->trans("Bills")."&nbsp;".$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
  372. );
  373. $this->info_box_contents[$line][2] = array(
  374. 'td' => 'class="right"',
  375. 'text' => $data[$j]->nb,
  376. 'tooltip' => $langs->trans('Bills').'&nbsp;'.$facturestatic->LibStatut(0, $data[$j]->fk_statut, 0),
  377. 'url' => DOL_URL_ROOT."/compta/facture/list.php?".$billurl."&amp;mainmenu=accountancy&amp;leftmenu=customers_bills",
  378. );
  379. $totalnb += $data[$j]->nb;
  380. $this->info_box_contents[$line][3] = array(
  381. 'td' => 'class="nowraponall right amount"',
  382. 'text' => price($data[$j]->Mnttot, 1, $langs, 0, 0, -1, $conf->currency),
  383. );
  384. $this->info_box_contents[$line][4] = array(
  385. 'td' => 'class="right" width="18"',
  386. 'text' => $facturestatic->LibStatut(0, $data[$j]->fk_statut, 3, $alreadypaid),
  387. );
  388. $line++;
  389. $j++;
  390. }
  391. if (count($data) == 0) {
  392. $this->info_box_contents[$line][0] = array(
  393. 'td' => 'class="center"',
  394. 'text'=>$langs->trans("NoRecordedInvoices"),
  395. );
  396. }
  397. }
  398. }
  399. // Add the sum in the bottom of the boxes
  400. $this->info_box_contents[$line][0] = array('tr' => 'class="liste_total_wrap"');
  401. $this->info_box_contents[$line][1] = array('td' => 'class="liste_total left" ', 'text' => $langs->trans("Total")."&nbsp;".$textHead);
  402. $this->info_box_contents[$line][2] = array('td' => 'class="liste_total right" ', 'text' => $totalnb);
  403. $this->info_box_contents[$line][3] = array('td' => 'class="liste_total right" ', 'text' => '');
  404. $this->info_box_contents[$line][4] = array('td' => 'class="liste_total right" ', 'text' => "");
  405. }
  406. /**
  407. * Method to show box
  408. *
  409. * @param array $head Array with properties of box title
  410. * @param array $contents Array with properties of box lines
  411. * @param int $nooutput No print, only return string
  412. * @return string
  413. */
  414. public function showBox($head = null, $contents = null, $nooutput = 0)
  415. {
  416. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  417. }
  418. }