popuprop.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2020 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  7. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/product/popuprop.php
  24. * \ingroup propal, commande, facture, produit
  25. * \brief List of products or services by popularity
  26. */
  27. require '../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('commande', 'propal', 'bills', 'other', 'products'));
  31. $backtopage = GETPOST('backtopage', 'alpha');
  32. $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
  33. $type = GETPOST("type", "int");
  34. $mode = GETPOST('mode', 'alpha') ? GETPOST('mode', 'alpha') : '';
  35. // Security check
  36. if (!empty($user->socid)) {
  37. $socid = $user->socid;
  38. }
  39. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  40. $sortfield = GETPOST('sortfield', 'aZ09comma');
  41. $sortorder = GETPOST('sortorder', 'aZ09comma');
  42. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  43. if (empty($page) || $page == -1) {
  44. $page = 0;
  45. } // If $page is not defined, or '' or -1
  46. if (!$sortfield) {
  47. $sortfield = "c";
  48. }
  49. if (!$sortorder) {
  50. $sortorder = "DESC";
  51. }
  52. $offset = $limit * $page;
  53. $pageprev = $page - 1;
  54. $pagenext = $page + 1;
  55. restrictedArea($user, 'produit|service', 0, 'product&product', '', '');
  56. /*
  57. * View
  58. */
  59. $form = new Form($db);
  60. $helpurl = '';
  61. if ($type == '0') {
  62. $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  63. } elseif ($type == '1') {
  64. $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  65. } else {
  66. $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  67. }
  68. $title = $langs->trans("Statistics");
  69. llxHeader('', $title, $helpurl);
  70. print load_fiche_titre($title, '', 'product');
  71. $param = '';
  72. $title = $langs->trans("ListProductServiceByPopularity");
  73. if ((string) $type == '1') {
  74. $title = $langs->trans("ListServiceByPopularity");
  75. }
  76. if ((string) $type == '0') {
  77. $title = $langs->trans("ListProductByPopularity");
  78. }
  79. if ($type != '') {
  80. $param .= '&type='.urlencode($type);
  81. }
  82. if ($mode != '') {
  83. $param .= '&mode='.urlencode($mode);
  84. }
  85. $h = 0;
  86. $head = array();
  87. $head[$h][0] = DOL_URL_ROOT.'/product/stats/card.php?id=all';
  88. $head[$h][1] = $langs->trans("Chart");
  89. $head[$h][2] = 'chart';
  90. $h++;
  91. $head[$h][0] = DOL_URL_ROOT.'/product/popuprop.php';
  92. $head[$h][1] = $langs->trans("ProductsPerPopularity");
  93. $head[$h][2] = 'popularity';
  94. $h++;
  95. print dol_get_fiche_head($head, 'popularity', '', -1);
  96. // Array of liens to show
  97. $infoprod = array();
  98. // Add lines for object
  99. $sql = "SELECT p.rowid, p.label, p.ref, p.fk_product_type as type, SUM(pd.qty) as c";
  100. $textforqty = 'Qty';
  101. if ($mode == 'facture') {
  102. $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as pd";
  103. } elseif ($mode == 'commande') {
  104. $textforqty = 'NbOfQtyInOrders';
  105. $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as pd";
  106. } elseif ($mode == 'propal') {
  107. $textforqty = 'NbOfQtyInProposals';
  108. $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pd";
  109. }
  110. $sql .= ", ".MAIN_DB_PREFIX."product as p";
  111. $sql .= ' WHERE p.entity IN ('.getEntity('product').')';
  112. $sql .= " AND p.rowid = pd.fk_product";
  113. if ($type !== '') {
  114. $sql .= " AND fk_product_type = ".((int) $type);
  115. }
  116. $sql .= " GROUP BY p.rowid, p.label, p.ref, p.fk_product_type";
  117. $num = 0;
  118. $totalnboflines = 0;
  119. if (!empty($mode) && $mode != '-1') {
  120. $result = $db->query($sql);
  121. if ($result) {
  122. $totalnboflines = $db->num_rows($result);
  123. }
  124. $sql .= $db->order($sortfield, $sortorder);
  125. $sql .= $db->plimit($limit + 1, $offset);
  126. $resql = $db->query($sql);
  127. if ($resql) {
  128. $num = $db->num_rows($resql);
  129. $i = 0;
  130. while ($i < $num) {
  131. $objp = $db->fetch_object($resql);
  132. $infoprod[$objp->rowid] = array('type'=>$objp->type, 'ref'=>$objp->ref, 'label'=>$objp->label);
  133. $infoprod[$objp->rowid]['nbline'] = $objp->c;
  134. $i++;
  135. }
  136. $db->free($resql);
  137. } else {
  138. dol_print_error($db);
  139. }
  140. }
  141. //var_dump($infoprod);
  142. $arrayofmode = array(
  143. 'propal' => 'Proposals',
  144. 'commande' => 'Orders',
  145. 'facture' => 'Facture'
  146. );
  147. $title .= ' '.$form->selectarray('mode', $arrayofmode, $mode, 1);
  148. $title .= ' <input type="submit" class="button small" name="refresh" value="'.$langs->trans("Refresh").'">';
  149. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  150. print '<input type="hidden" name="token" value="'.newToken().'">';
  151. print '<input type="hidden" name="mode" value="'.$mode.'">';
  152. print '<input type="hidden" name="type" value="'.$type.'">';
  153. print '<input type="hidden" name="action" value="add">';
  154. if ($backtopage) {
  155. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  156. }
  157. if ($backtopageforcancel) {
  158. print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
  159. }
  160. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, "", $num, $totalnboflines, '', 0, '', '', -1, 0, 0, 1);
  161. print '<table class="noborder centpercent">';
  162. print '<tr class="liste_titre">';
  163. print_liste_field_titre('Ref', $_SERVER["PHP_SELF"], 'p.ref', '', $param, '', $sortfield, $sortorder);
  164. print_liste_field_titre('Type', $_SERVER["PHP_SELF"], 'p.fk_product_type', '', $param, '', $sortfield, $sortorder);
  165. print_liste_field_titre('Label', $_SERVER["PHP_SELF"], 'p.label', '', $param, '', $sortfield, $sortorder);
  166. print_liste_field_titre($textforqty, $_SERVER["PHP_SELF"], 'c', '', $param, '', $sortfield, $sortorder, 'right ');
  167. print "</tr>\n";
  168. if ($mode && $mode != '-1') {
  169. foreach ($infoprod as $prodid => $vals) {
  170. // Multilangs
  171. if (!empty($conf->global->MAIN_MULTILANGS)) { // si l'option est active
  172. $sql = "SELECT label";
  173. $sql .= " FROM ".MAIN_DB_PREFIX."product_lang";
  174. $sql .= " WHERE fk_product = ".((int) $prodid);
  175. $sql .= " AND lang = '".$db->escape($langs->getDefaultLang())."'";
  176. $sql .= " LIMIT 1";
  177. $resultp = $db->query($sql);
  178. if ($resultp) {
  179. $objtp = $db->fetch_object($resultp);
  180. if (!empty($objtp->label)) {
  181. $vals['label'] = $objtp->label;
  182. }
  183. }
  184. }
  185. print "<tr>";
  186. print '<td><a href="'.DOL_URL_ROOT.'/product/stats/card.php?id='.$prodid.'">';
  187. if ($vals['type'] == 1) {
  188. print img_object($langs->trans("ShowService"), "service");
  189. } else {
  190. print img_object($langs->trans("ShowProduct"), "product");
  191. }
  192. print " ";
  193. print $vals['ref'].'</a></td>';
  194. print '<td>';
  195. if ($vals['type'] == 1) {
  196. print $langs->trans("Service");
  197. } else {
  198. print $langs->trans("Product");
  199. }
  200. print '</td>';
  201. print '<td>'.$vals['label'].'</td>';
  202. print '<td class="right">'.$vals['nbline'].'</td>';
  203. print "</tr>\n";
  204. $i++;
  205. }
  206. } else {
  207. print '<tr><td colspan="4"><span class="opacitymedium">'.$langs->trans("SelectTheTypeOfObjectToAnalyze").'</span></td></tr>';
  208. }
  209. print "</table>";
  210. print '</form>';
  211. print dol_get_fiche_end();
  212. // End of page
  213. llxFooter();
  214. $db->close();