list.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /* Copyright (C) 2001-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2007 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  7. * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.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/fourn/product/list.php
  24. * \ingroup product
  25. * \brief Page to list supplier products and services
  26. */
  27. // Load Dolibarr environment
  28. require '../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT .'/product/class/product.class.php';
  30. require_once DOL_DOCUMENT_ROOT .'/societe/class/societe.class.php';
  31. require_once DOL_DOCUMENT_ROOT .'/fourn/class/fournisseur.class.php';
  32. // Load translation files required by the page
  33. $langs->loadLangs(array('products', 'suppliers'));
  34. // Get Parameters
  35. $sref = GETPOST('sref', 'alphanohtml');
  36. $sRefSupplier = GETPOST('srefsupplier');
  37. $snom = GETPOST('snom', 'alphanohtml');
  38. $type = GETPOST('type', 'alphanohtml');
  39. $optioncss = GETPOST('optioncss', 'alpha');
  40. // Load variable for pagination
  41. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  42. $sortfield = GETPOST('sortfield', 'aZ09comma');
  43. $sortorder = GETPOST('sortorder', 'aZ09comma');
  44. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  45. if (empty($page) || $page == -1) {
  46. $page = 0;
  47. } // If $page is not defined, or '' or -1
  48. $offset = $limit * $page;
  49. $pageprev = $page - 1;
  50. $pagenext = $page + 1;
  51. if (!$sortfield) {
  52. $sortfield = "p.ref"; // Set here default search field
  53. }
  54. if (!$sortorder) {
  55. $sortorder = "ASC";
  56. }
  57. $fourn_id = GETPOST('fourn_id', 'intcomma');
  58. if ($user->socid) {
  59. $fourn_id = $user->socid;
  60. }
  61. $catid = GETPOST('catid', 'intcomma');
  62. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
  63. $hookmanager->initHooks(array('supplierpricelist'));
  64. $extrafields = new ExtraFields($db);
  65. if (empty($user->rights->produit->lire) && empty($user->rights->service->lire)) {
  66. accessforbidden();
  67. }
  68. // Permissions
  69. $permissiontoadd = ($user->hasRight('product', 'read') || $user->hasRight('service', 'read'));
  70. /*
  71. * Actions
  72. */
  73. if (GETPOST('cancel', 'alpha')) {
  74. $action = 'list'; $massaction = '';
  75. }
  76. if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
  77. $massaction = '';
  78. }
  79. $parameters = array();
  80. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  81. if ($reshook < 0) {
  82. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  83. }
  84. if (empty($reshook)) {
  85. // Selection of new fields
  86. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  87. // Purge search criteria
  88. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
  89. $sref = '';
  90. $sRefSupplier = '';
  91. $snom = '';
  92. $search_field1 = '';
  93. $search_field2 = '';
  94. $search_date_creation = '';
  95. $search_date_update = '';
  96. $toselect = array();
  97. $search_array_options = array();
  98. }
  99. }
  100. /*
  101. * View
  102. */
  103. $form = new Form($db);
  104. $productstatic = new Product($db);
  105. $companystatic = new Societe($db);
  106. $title = $langs->trans('Supplier')." - ".$langs->trans('ProductsAndServices');
  107. if ($fourn_id) {
  108. $supplier = new Fournisseur($db);
  109. $supplier->fetch($fourn_id);
  110. }
  111. $arrayofmassactions = array(
  112. 'generate_doc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("ReGeneratePDF"),
  113. 'builddoc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("PDFMerge"),
  114. 'presend'=>img_picto('', 'email', 'class="pictofixedwidth"').$langs->trans("SendByMail"),
  115. );
  116. if ($user->rights->mymodule->supprimer) {
  117. $arrayofmassactions['predelete'] = img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete");
  118. }
  119. if (in_array($massaction, array('presend', 'predelete'))) {
  120. $arrayofmassactions = array();
  121. }
  122. $massactionbutton = $form->selectMassAction('', $arrayofmassactions);
  123. $sql = "SELECT p.rowid, p.label, p.ref, p.fk_product_type, p.entity, p.tosell, p.tobuy, p.barcode, p.fk_barcode_type,";
  124. $sql .= " ppf.fk_soc, ppf.ref_fourn, ppf.price as price, ppf.quantity as qty, ppf.unitprice,";
  125. $sql .= " s.rowid as socid, s.nom as name";
  126. // Add fields to SELECT from hooks
  127. $parameters = array();
  128. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object, $action);
  129. if ($reshook < 0) {
  130. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  131. }
  132. $sql .= $hookmanager->resPrint;
  133. $sqlfields = $sql; // $sql fields to remove for count total
  134. $sql .= " FROM ".MAIN_DB_PREFIX."product as p";
  135. if ($catid) {
  136. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_product as cp ON cp.fk_product = p.rowid";
  137. }
  138. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as ppf ON p.rowid = ppf.fk_product AND p.entity = ppf.entity";
  139. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON ppf.fk_soc = s.rowid AND s.entity IN (".getEntity('societe').")";
  140. $sql .= " WHERE p.entity IN (".getEntity('product').")";
  141. if ($sRefSupplier) {
  142. $sql .= natural_search('ppf.ref_fourn', $sRefSupplier);
  143. }
  144. if (GETPOST('type')) {
  145. $sql .= " AND p.fk_product_type = ".GETPOST('type', 'int');
  146. }
  147. if ($sref) {
  148. $sql .= natural_search('p.ref', $sref);
  149. }
  150. if ($snom) {
  151. $sql .= natural_search('p.label', $snom);
  152. }
  153. if ($catid) {
  154. $sql .= " AND cp.fk_categorie = ".((int) $catid);
  155. }
  156. if ($fourn_id > 0) {
  157. $sql .= " AND ppf.fk_soc = ".((int) $fourn_id);
  158. }
  159. // Add WHERE filters from hooks
  160. $parameters = array();
  161. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters);
  162. if ($reshook < 0) {
  163. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  164. }
  165. $sql .= $hookmanager->resPrint;
  166. // Count total nb of records
  167. $nbtotalofrecords = '';
  168. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  169. /* The fast and low memory method to get and count full list converts the sql into a sql count */
  170. $sqlforcount = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT COUNT(*) as nbtotalofrecords', $sql);
  171. $sqlforcount = preg_replace('/GROUP BY .*$/', '', $sqlforcount);
  172. $resql = $db->query($sqlforcount);
  173. if ($resql) {
  174. $objforcount = $db->fetch_object($resql);
  175. $nbtotalofrecords = $objforcount->nbtotalofrecords;
  176. } else {
  177. dol_print_error($db);
  178. }
  179. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  180. $page = 0;
  181. $offset = 0;
  182. }
  183. $db->free($resql);
  184. }
  185. // Complete request and execute it with limit
  186. $sql .= $db->order($sortfield, $sortorder);
  187. if ($limit) {
  188. $sql .= $db->plimit($limit + 1, $offset);
  189. }
  190. dol_syslog("fourn/product/list.php:", LOG_DEBUG);
  191. $resql = $db->query($sql);
  192. if ($resql) {
  193. $num = $db->num_rows($resql);
  194. $i = 0;
  195. if ($num == 1 && (GETPOST("mode") == 'search')) {
  196. $objp = $db->fetch_object($resql);
  197. header("Location: ".DOL_URL_ROOT."/product/card.php?id=".$objp->rowid);
  198. exit;
  199. }
  200. if (!empty($supplier->id)) {
  201. $texte = $langs->trans("ListOfSupplierProductForSupplier", $supplier->name);
  202. } else {
  203. $texte = $langs->trans("List");
  204. }
  205. llxHeader("", "", $texte);
  206. $param = "&sref=".$sref."&snom=".$snom."&fourn_id=".$fourn_id.(isset($type) ? "&amp;type=".$type : "").(empty($sRefSupplier) ? "" : "&amp;srefsupplier=".$sRefSupplier);
  207. if ($optioncss != '') {
  208. $param .= '&optioncss='.$optioncss;
  209. }
  210. $newcardbutton = '';
  211. $newcardbutton .= dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/product/list.php?action=create&backtopage='.urlencode($_SERVER['PHP_SELF']), '', $permissiontoadd);
  212. print_barre_liste($texte, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'generic', 0, $newcardbutton);
  213. if (!empty($catid)) {
  214. print "<div id='ways'>";
  215. $c = new Categorie($db);
  216. $ways = $c->print_all_ways(' &gt; ', 'fourn/product/list.php');
  217. print " &gt; ".$ways[0]."<br>\n";
  218. print "</div><br>";
  219. }
  220. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  221. if ($optioncss != '') {
  222. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  223. }
  224. print '<input type="hidden" name="token" value="'.newToken().'">';
  225. if ($fourn_id > 0) {
  226. print '<input type="hidden" name="fourn_id" value="'.$fourn_id.'">';
  227. }
  228. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  229. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  230. print '<input type="hidden" name="page" value="'.$page.'">';
  231. print '<input type="hidden" name="type" value="'.$type.'">';
  232. $topicmail = "Information";
  233. $modelmail = "product";
  234. $objecttmp = new Product($db);
  235. $trackid = 'prod'.$object->id;
  236. include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
  237. print '<div class="div-table-responsive-no-min">';
  238. print '<table class="liste centpercent">';
  239. // Fields title search
  240. print '<tr class="liste_titre">';
  241. print '<td class="liste_titre">';
  242. print '<input class="flat maxwidth100" type="text" name="sref" value="'.$sref.'">';
  243. print '</td>';
  244. print '<td class="liste_titre">';
  245. print '<input class="flat maxwidth100" type="text" name="srefsupplier" value="'.$sRefSupplier.'">';
  246. print '</td>';
  247. print '<td class="liste_titre">';
  248. print '<input class="flat maxwidth100" type="text" name="snom" value="'.$snom.'">';
  249. print '</td>';
  250. print '<td></td>';
  251. print '<td></td>';
  252. print '<td></td>';
  253. print '<td></td>';
  254. // add filters from hooks
  255. $parameters = array();
  256. $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters, $object, $action);
  257. if ($reshook < 0) {
  258. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  259. }
  260. print $hookmanager->resPrint;
  261. print '<td class="liste_titre maxwidthsearch">';
  262. $searchpicto = $form->showFilterButtons();
  263. print $searchpicto;
  264. print '</td>';
  265. print '</tr>';
  266. // Line for title
  267. print '<tr class="liste_titre">';
  268. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "p.ref", $param, "", "", $sortfield, $sortorder);
  269. print_liste_field_titre("RefSupplierShort", $_SERVER["PHP_SELF"], "ppf.ref_fourn", $param, "", "", $sortfield, $sortorder);
  270. print_liste_field_titre("Label", $_SERVER["PHP_SELF"], "p.label", $param, "", "", $sortfield, $sortorder);
  271. print_liste_field_titre("Supplier", $_SERVER["PHP_SELF"], "ppf.fk_soc", $param, "", "", $sortfield, $sortorder);
  272. print_liste_field_titre("BuyingPrice", $_SERVER["PHP_SELF"], "ppf.price", $param, "", '', $sortfield, $sortorder, 'right ');
  273. print_liste_field_titre("QtyMin", $_SERVER["PHP_SELF"], "ppf.quantity", $param, "", '', $sortfield, $sortorder, 'right ');
  274. print_liste_field_titre("UnitPrice", $_SERVER["PHP_SELF"], "ppf.unitprice", $param, "", '', $sortfield, $sortorder, 'right ');
  275. // add header cells from hooks
  276. $parameters = array();
  277. $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object, $action);
  278. if ($reshook < 0) {
  279. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  280. }
  281. print $hookmanager->resPrint;
  282. print_liste_field_titre('', $_SERVER["PHP_SELF"]);
  283. print "</tr>\n";
  284. while ($i < min($num, $limit)) {
  285. $objp = $db->fetch_object($resql);
  286. $productstatic->id = $objp->rowid;
  287. $productstatic->ref = $objp->ref;
  288. $productstatic->type = $objp->fk_product_type;
  289. $productstatic->entity = $objp->entity;
  290. $productstatic->status = $objp->tosell;
  291. $productstatic->status_buy = $objp->tobuy;
  292. $productstatic->barcode = $objp->barcode;
  293. $productstatic->barcode_type = $objp->fk_barcode_type;
  294. print '<tr class="oddeven">';
  295. print '<td>';
  296. print $productstatic->getNomUrl(1, 'supplier');
  297. print '</td>';
  298. print '<td>'.$objp->ref_fourn.'</td>';
  299. print '<td>'.$objp->label.'</td>'."\n";
  300. $companystatic->name = $objp->name;
  301. $companystatic->id = $objp->socid;
  302. print '<td>';
  303. if ($companystatic->id > 0) {
  304. print $companystatic->getNomUrl(1, 'supplier');
  305. }
  306. print '</td>';
  307. print '<td class="right">'.(isset($objp->price) ? price($objp->price) : '').'</td>';
  308. print '<td class="right">'.$objp->qty.'</td>';
  309. print '<td class="right">'.(isset($objp->unitprice) ? price($objp->unitprice) : '').'</td>';
  310. // add additional columns from hooks
  311. $parameters = array();
  312. $reshook = $hookmanager->executeHooks('printFieldListValue', $parameters, $objp, $action);
  313. if ($reshook < 0) {
  314. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  315. }
  316. print $hookmanager->resPrint;
  317. print '<td class="right"></td>';
  318. print "</tr>\n";
  319. $i++;
  320. }
  321. $db->free($resql);
  322. // If no record found
  323. if ($num == 0) {
  324. $colspan = 8;
  325. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
  326. }
  327. print "</table></div>";
  328. print '</form>';
  329. } else {
  330. dol_print_error($db);
  331. }
  332. // End of page
  333. llxFooter();
  334. $db->close();