popuprop.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. // Load Dolibarr environment
  28. require '../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  30. // Load translation files required by the page
  31. $langs->loadLangs(array('commande', 'propal', 'bills', 'other', 'products'));
  32. $backtopage = GETPOST('backtopage', 'alpha');
  33. $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
  34. $type = GETPOST("type", "int");
  35. $mode = GETPOST('mode', 'alpha') ? GETPOST('mode', 'alpha') : '';
  36. // Security check
  37. if (!empty($user->socid)) {
  38. $socid = $user->socid;
  39. }
  40. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  41. $sortfield = GETPOST('sortfield', 'aZ09comma');
  42. $sortorder = GETPOST('sortorder', 'aZ09comma');
  43. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  44. if (empty($page) || $page == -1) {
  45. $page = 0;
  46. } // If $page is not defined, or '' or -1
  47. if (!$sortfield) {
  48. $sortfield = "c";
  49. }
  50. if (!$sortorder) {
  51. $sortorder = "DESC";
  52. }
  53. $offset = $limit * $page;
  54. $pageprev = $page - 1;
  55. $pagenext = $page + 1;
  56. restrictedArea($user, 'produit|service', 0, 'product&product', '', '');
  57. /*
  58. * View
  59. */
  60. $form = new Form($db);
  61. $tmpproduct = new Product($db);
  62. $helpurl = '';
  63. if ($type == '0') {
  64. $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  65. } elseif ($type == '1') {
  66. $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  67. } else {
  68. $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  69. }
  70. $title = $langs->trans("Statistics");
  71. llxHeader('', $title, $helpurl);
  72. print load_fiche_titre($title, '', 'product');
  73. $param = '';
  74. $title = $langs->trans("ListProductServiceByPopularity");
  75. if ((string) $type == '1') {
  76. $title = $langs->trans("ListServiceByPopularity");
  77. }
  78. if ((string) $type == '0') {
  79. $title = $langs->trans("ListProductByPopularity");
  80. }
  81. if ($type != '') {
  82. $param .= '&type='.urlencode($type);
  83. }
  84. if ($mode != '') {
  85. $param .= '&mode='.urlencode($mode);
  86. }
  87. $h = 0;
  88. $head = array();
  89. $head[$h][0] = DOL_URL_ROOT.'/product/stats/card.php?id=all';
  90. $head[$h][1] = $langs->trans("Chart");
  91. $head[$h][2] = 'chart';
  92. $h++;
  93. $head[$h][0] = DOL_URL_ROOT.'/product/popuprop.php';
  94. $head[$h][1] = $langs->trans("ProductsServicesPerPopularity");
  95. if ((string) $type == '0') {
  96. $head[$h][1] = $langs->trans("ProductsPerPopularity");
  97. }
  98. if ((string) $type == '1') {
  99. $head[$h][1] = $langs->trans("ServicesPerPopularity");
  100. }
  101. $head[$h][2] = 'popularity';
  102. $h++;
  103. print dol_get_fiche_head($head, 'popularity', '', -1);
  104. // Array of liens to show
  105. $infoprod = array();
  106. // Add lines for object
  107. $sql = "SELECT p.rowid, p.label, p.ref, p.fk_product_type as type, p.tobuy, p.tosell, p.tobatch, p.barcode, SUM(pd.qty) as c";
  108. $textforqty = 'Qty';
  109. if ($mode == 'facture') {
  110. $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as pd";
  111. } elseif ($mode == 'commande') {
  112. $textforqty = 'NbOfQtyInOrders';
  113. $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as pd";
  114. } elseif ($mode == 'propal') {
  115. $textforqty = 'NbOfQtyInProposals';
  116. $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pd";
  117. }
  118. $sql .= ", ".MAIN_DB_PREFIX."product as p";
  119. $sql .= ' WHERE p.entity IN ('.getEntity('product').')';
  120. $sql .= " AND p.rowid = pd.fk_product";
  121. if ($type !== '') {
  122. $sql .= " AND fk_product_type = ".((int) $type);
  123. }
  124. $sql .= " GROUP BY p.rowid, p.label, p.ref, p.fk_product_type, p.tobuy, p.tosell, p.tobatch, p.barcode";
  125. $num = 0;
  126. $totalnboflines = 0;
  127. if (!empty($mode) && $mode != '-1') {
  128. $result = $db->query($sql);
  129. if ($result) {
  130. $totalnboflines = $db->num_rows($result);
  131. }
  132. $sql .= $db->order($sortfield, $sortorder);
  133. $sql .= $db->plimit($limit + 1, $offset);
  134. $resql = $db->query($sql);
  135. if ($resql) {
  136. $num = $db->num_rows($resql);
  137. $i = 0;
  138. while ($i < $num) {
  139. $objp = $db->fetch_object($resql);
  140. $infoprod[$objp->rowid] = array('type'=>$objp->type, 'ref'=>$objp->ref, 'label'=>$objp->label, 'tobuy'=>$objp->tobuy, 'tosell'=>$objp->tobuy, 'tobatch'=>$objp->tobatch, 'barcode'=>$objp->barcode);
  141. $infoprod[$objp->rowid]['nbline'] = $objp->c;
  142. $i++;
  143. }
  144. $db->free($resql);
  145. } else {
  146. dol_print_error($db);
  147. }
  148. }
  149. //var_dump($infoprod);
  150. $arrayofmode = array(
  151. 'propal' => 'Proposals',
  152. 'commande' => 'Orders',
  153. 'facture' => 'Facture'
  154. );
  155. $title .= ' '.$form->selectarray('mode', $arrayofmode, $mode, 1, 0, 0, '', 1);
  156. $title .= ' <input type="submit" class="button small" name="refresh" value="'.$langs->trans("Refresh").'">';
  157. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  158. print '<input type="hidden" name="token" value="'.newToken().'">';
  159. print '<input type="hidden" name="mode" value="'.$mode.'">';
  160. print '<input type="hidden" name="type" value="'.$type.'">';
  161. print '<input type="hidden" name="action" value="add">';
  162. if ($backtopage) {
  163. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  164. }
  165. if ($backtopageforcancel) {
  166. print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
  167. }
  168. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, "", $num, $totalnboflines, '', 0, '', '', -1, 0, 0, 1);
  169. print '<table class="noborder centpercent">';
  170. print '<tr class="liste_titre">';
  171. print_liste_field_titre('Ref', $_SERVER["PHP_SELF"], 'p.ref', '', $param, '', $sortfield, $sortorder);
  172. print_liste_field_titre('Type', $_SERVER["PHP_SELF"], 'p.fk_product_type', '', $param, '', $sortfield, $sortorder);
  173. print_liste_field_titre('Label', $_SERVER["PHP_SELF"], 'p.label', '', $param, '', $sortfield, $sortorder);
  174. print_liste_field_titre($textforqty, $_SERVER["PHP_SELF"], 'c', '', $param, '', $sortfield, $sortorder, 'right ');
  175. print "</tr>\n";
  176. if ($mode && $mode != '-1') {
  177. foreach ($infoprod as $prodid => $vals) {
  178. // Multilangs
  179. if (getDolGlobalInt('MAIN_MULTILANGS')) { // si l'option est active
  180. $sql = "SELECT label";
  181. $sql .= " FROM ".MAIN_DB_PREFIX."product_lang";
  182. $sql .= " WHERE fk_product = ".((int) $prodid);
  183. $sql .= " AND lang = '".$db->escape($langs->getDefaultLang())."'";
  184. $sql .= " LIMIT 1";
  185. $resultp = $db->query($sql);
  186. if ($resultp) {
  187. $objtp = $db->fetch_object($resultp);
  188. if (!empty($objtp->label)) {
  189. $vals['label'] = $objtp->label;
  190. }
  191. }
  192. }
  193. $tmpproduct->id = $prodid;
  194. $tmpproduct->ref = $vals['ref'];
  195. $tmpproduct->label = $vals['label'];
  196. $tmpproduct->type = $vals['type'];
  197. $tmpproduct->status = $vals['tosell'];
  198. $tmpproduct->status_buy = $vals['tobuy'];
  199. $tmpproduct->status_batch = $vals['tobatch'];
  200. $tmpproduct->barcode = $vals['barcode'];
  201. print "<tr>";
  202. print '<td>';
  203. print $tmpproduct->getNomUrl(1);
  204. print '</td>';
  205. print '<td>';
  206. $s = '';
  207. if ($vals['type'] == 1) {
  208. $s .= img_picto($langs->trans("Service"), 'service', 'class="paddingleftonly paddingrightonly colorgrey"');
  209. } else {
  210. $s .= img_picto($langs->trans("Product"), 'product', 'class="paddingleftonly paddingrightonly colorgrey"');
  211. }
  212. print $s;
  213. print '</td>';
  214. print '<td>'.dol_escape_htmltag($vals['label']).'</td>';
  215. print '<td class="right">'.$vals['nbline'].'</td>';
  216. print "</tr>\n";
  217. $i++;
  218. }
  219. } else {
  220. print '<tr><td colspan="4"><span class="opacitymedium">'.$langs->trans("SelectTheTypeOfObjectToAnalyze").'</span></td></tr>';
  221. }
  222. print "</table>";
  223. print '</form>';
  224. print dol_get_fiche_end();
  225. // End of page
  226. llxFooter();
  227. $db->close();