popuprop.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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("ProductsPerPopularity");
  95. $head[$h][2] = 'popularity';
  96. $h++;
  97. print dol_get_fiche_head($head, 'popularity', '', -1);
  98. // Array of liens to show
  99. $infoprod = array();
  100. // Add lines for object
  101. $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";
  102. $textforqty = 'Qty';
  103. if ($mode == 'facture') {
  104. $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as pd";
  105. } elseif ($mode == 'commande') {
  106. $textforqty = 'NbOfQtyInOrders';
  107. $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as pd";
  108. } elseif ($mode == 'propal') {
  109. $textforqty = 'NbOfQtyInProposals';
  110. $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pd";
  111. }
  112. $sql .= ", ".MAIN_DB_PREFIX."product as p";
  113. $sql .= ' WHERE p.entity IN ('.getEntity('product').')';
  114. $sql .= " AND p.rowid = pd.fk_product";
  115. if ($type !== '') {
  116. $sql .= " AND fk_product_type = ".((int) $type);
  117. }
  118. $sql .= " GROUP BY p.rowid, p.label, p.ref, p.fk_product_type, p.tobuy, p.tosell, p.tobatch, p.barcode";
  119. $num = 0;
  120. $totalnboflines = 0;
  121. if (!empty($mode) && $mode != '-1') {
  122. $result = $db->query($sql);
  123. if ($result) {
  124. $totalnboflines = $db->num_rows($result);
  125. }
  126. $sql .= $db->order($sortfield, $sortorder);
  127. $sql .= $db->plimit($limit + 1, $offset);
  128. $resql = $db->query($sql);
  129. if ($resql) {
  130. $num = $db->num_rows($resql);
  131. $i = 0;
  132. while ($i < $num) {
  133. $objp = $db->fetch_object($resql);
  134. $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);
  135. $infoprod[$objp->rowid]['nbline'] = $objp->c;
  136. $i++;
  137. }
  138. $db->free($resql);
  139. } else {
  140. dol_print_error($db);
  141. }
  142. }
  143. //var_dump($infoprod);
  144. $arrayofmode = array(
  145. 'propal' => 'Proposals',
  146. 'commande' => 'Orders',
  147. 'facture' => 'Facture'
  148. );
  149. $title .= ' '.$form->selectarray('mode', $arrayofmode, $mode, 1, 0, 0, '', 1);
  150. $title .= ' <input type="submit" class="button small" name="refresh" value="'.$langs->trans("Refresh").'">';
  151. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  152. print '<input type="hidden" name="token" value="'.newToken().'">';
  153. print '<input type="hidden" name="mode" value="'.$mode.'">';
  154. print '<input type="hidden" name="type" value="'.$type.'">';
  155. print '<input type="hidden" name="action" value="add">';
  156. if ($backtopage) {
  157. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  158. }
  159. if ($backtopageforcancel) {
  160. print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
  161. }
  162. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, "", $num, $totalnboflines, '', 0, '', '', -1, 0, 0, 1);
  163. print '<table class="noborder centpercent">';
  164. print '<tr class="liste_titre">';
  165. print_liste_field_titre('Ref', $_SERVER["PHP_SELF"], 'p.ref', '', $param, '', $sortfield, $sortorder);
  166. print_liste_field_titre('Type', $_SERVER["PHP_SELF"], 'p.fk_product_type', '', $param, '', $sortfield, $sortorder);
  167. print_liste_field_titre('Label', $_SERVER["PHP_SELF"], 'p.label', '', $param, '', $sortfield, $sortorder);
  168. print_liste_field_titre($textforqty, $_SERVER["PHP_SELF"], 'c', '', $param, '', $sortfield, $sortorder, 'right ');
  169. print "</tr>\n";
  170. if ($mode && $mode != '-1') {
  171. foreach ($infoprod as $prodid => $vals) {
  172. // Multilangs
  173. if (getDolGlobalInt('MAIN_MULTILANGS')) { // si l'option est active
  174. $sql = "SELECT label";
  175. $sql .= " FROM ".MAIN_DB_PREFIX."product_lang";
  176. $sql .= " WHERE fk_product = ".((int) $prodid);
  177. $sql .= " AND lang = '".$db->escape($langs->getDefaultLang())."'";
  178. $sql .= " LIMIT 1";
  179. $resultp = $db->query($sql);
  180. if ($resultp) {
  181. $objtp = $db->fetch_object($resultp);
  182. if (!empty($objtp->label)) {
  183. $vals['label'] = $objtp->label;
  184. }
  185. }
  186. }
  187. $tmpproduct->id = $prodid;
  188. $tmpproduct->ref = $vals['ref'];
  189. $tmpproduct->label = $vals['label'];
  190. $tmpproduct->type = $vals['type'];
  191. $tmpproduct->status = $vals['tosell'];
  192. $tmpproduct->status_buy = $vals['tobuy'];
  193. $tmpproduct->status_batch = $vals['tobatch'];
  194. $tmpproduct->barcode = $vals['barcode'];
  195. print "<tr>";
  196. print '<td>';
  197. print $tmpproduct->getNomUrl(1);
  198. print '</td>';
  199. print '<td>';
  200. $s = '';
  201. if ($vals['type'] == 1) {
  202. $s .= img_picto($langs->trans("Service"), 'service', 'class="paddingleftonly paddingrightonly colorgrey"');
  203. } else {
  204. $s .= img_picto($langs->trans("Product"), 'product', 'class="paddingleftonly paddingrightonly colorgrey"');
  205. }
  206. print $s;
  207. print '</td>';
  208. print '<td>'.dol_escape_htmltag($vals['label']).'</td>';
  209. print '<td class="right">'.$vals['nbline'].'</td>';
  210. print "</tr>\n";
  211. $i++;
  212. }
  213. } else {
  214. print '<tr><td colspan="4"><span class="opacitymedium">'.$langs->trans("SelectTheTypeOfObjectToAnalyze").'</span></td></tr>';
  215. }
  216. print "</table>";
  217. print '</form>';
  218. print dol_get_fiche_end();
  219. // End of page
  220. llxFooter();
  221. $db->close();