list.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 (!$user->hasRight("produit", "lire") && !$user->hasRight("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';
  75. $massaction = '';
  76. }
  77. if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
  78. $massaction = '';
  79. }
  80. $parameters = array();
  81. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  82. if ($reshook < 0) {
  83. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  84. }
  85. if (empty($reshook)) {
  86. // Selection of new fields
  87. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  88. // Purge search criteria
  89. 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
  90. $sref = '';
  91. $sRefSupplier = '';
  92. $snom = '';
  93. $search_field1 = '';
  94. $search_field2 = '';
  95. $search_date_creation = '';
  96. $search_date_update = '';
  97. $toselect = array();
  98. $search_array_options = array();
  99. }
  100. }
  101. /*
  102. * View
  103. */
  104. $form = new Form($db);
  105. $productstatic = new Product($db);
  106. $companystatic = new Societe($db);
  107. $title = $langs->trans('Supplier')." - ".$langs->trans('ProductsAndServices');
  108. if ($fourn_id) {
  109. $supplier = new Fournisseur($db);
  110. $supplier->fetch($fourn_id);
  111. }
  112. $arrayofmassactions = array(
  113. 'generate_doc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("ReGeneratePDF"),
  114. 'builddoc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("PDFMerge"),
  115. 'presend'=>img_picto('', 'email', 'class="pictofixedwidth"').$langs->trans("SendByMail"),
  116. );
  117. if ($user->hasRight('mymodule', 'supprimer')) {
  118. $arrayofmassactions['predelete'] = img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete");
  119. }
  120. if (in_array($massaction, array('presend', 'predelete'))) {
  121. $arrayofmassactions = array();
  122. }
  123. $massactionbutton = $form->selectMassAction('', $arrayofmassactions);
  124. $sql = "SELECT p.rowid, p.label, p.ref, p.fk_product_type, p.entity, p.tosell, p.tobuy, p.barcode, p.fk_barcode_type,";
  125. $sql .= " ppf.fk_soc, ppf.ref_fourn, ppf.price as price, ppf.quantity as qty, ppf.unitprice,";
  126. $sql .= " s.rowid as socid, s.nom as name";
  127. // Add fields to SELECT from hooks
  128. $parameters = array();
  129. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object, $action);
  130. if ($reshook < 0) {
  131. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  132. }
  133. $sql .= $hookmanager->resPrint;
  134. $sqlfields = $sql; // $sql fields to remove for count total
  135. $sql .= " FROM ".MAIN_DB_PREFIX."product as p";
  136. if ($catid) {
  137. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_product as cp ON cp.fk_product = p.rowid";
  138. }
  139. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as ppf ON p.rowid = ppf.fk_product AND p.entity = ppf.entity";
  140. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON ppf.fk_soc = s.rowid AND s.entity IN (".getEntity('societe').")";
  141. $sql .= " WHERE p.entity IN (".getEntity('product').")";
  142. if ($sRefSupplier) {
  143. $sql .= natural_search('ppf.ref_fourn', $sRefSupplier);
  144. }
  145. if (GETPOST('type')) {
  146. $sql .= " AND p.fk_product_type = ".GETPOST('type', 'int');
  147. }
  148. if ($sref) {
  149. $sql .= natural_search('p.ref', $sref);
  150. }
  151. if ($snom) {
  152. $sql .= natural_search('p.label', $snom);
  153. }
  154. if ($catid) {
  155. $sql .= " AND cp.fk_categorie = ".((int) $catid);
  156. }
  157. if ($fourn_id > 0) {
  158. $sql .= " AND ppf.fk_soc = ".((int) $fourn_id);
  159. }
  160. // Add WHERE filters from hooks
  161. $parameters = array();
  162. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters);
  163. if ($reshook < 0) {
  164. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  165. }
  166. $sql .= $hookmanager->resPrint;
  167. // Count total nb of records
  168. $nbtotalofrecords = '';
  169. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  170. /* The fast and low memory method to get and count full list converts the sql into a sql count */
  171. $sqlforcount = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT COUNT(*) as nbtotalofrecords', $sql);
  172. $sqlforcount = preg_replace('/GROUP BY .*$/', '', $sqlforcount);
  173. $resql = $db->query($sqlforcount);
  174. if ($resql) {
  175. $objforcount = $db->fetch_object($resql);
  176. $nbtotalofrecords = $objforcount->nbtotalofrecords;
  177. } else {
  178. dol_print_error($db);
  179. }
  180. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  181. $page = 0;
  182. $offset = 0;
  183. }
  184. $db->free($resql);
  185. }
  186. // Complete request and execute it with limit
  187. $sql .= $db->order($sortfield, $sortorder);
  188. if ($limit) {
  189. $sql .= $db->plimit($limit + 1, $offset);
  190. }
  191. dol_syslog("fourn/product/list.php:", LOG_DEBUG);
  192. $resql = $db->query($sql);
  193. if ($resql) {
  194. $num = $db->num_rows($resql);
  195. $i = 0;
  196. if ($num == 1 && (GETPOST("mode") == 'search')) {
  197. $objp = $db->fetch_object($resql);
  198. header("Location: ".DOL_URL_ROOT."/product/card.php?id=".$objp->rowid);
  199. exit;
  200. }
  201. if (!empty($supplier->id)) {
  202. $texte = $langs->trans("ListOfSupplierProductForSupplier", $supplier->name);
  203. } else {
  204. $texte = $langs->trans("List");
  205. }
  206. llxHeader("", "", $texte);
  207. $param = "&sref=".$sref."&snom=".$snom."&fourn_id=".$fourn_id.(isset($type) ? "&amp;type=".$type : "").(empty($sRefSupplier) ? "" : "&amp;srefsupplier=".$sRefSupplier);
  208. if ($optioncss != '') {
  209. $param .= '&optioncss='.$optioncss;
  210. }
  211. $newcardbutton = '';
  212. $newcardbutton .= dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/product/list.php?action=create&backtopage='.urlencode($_SERVER['PHP_SELF']), '', $permissiontoadd);
  213. print_barre_liste($texte, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'generic', 0, $newcardbutton);
  214. if (!empty($catid)) {
  215. print "<div id='ways'>";
  216. $c = new Categorie($db);
  217. $ways = $c->print_all_ways(' &gt; ', 'fourn/product/list.php');
  218. print " &gt; ".$ways[0]."<br>\n";
  219. print "</div><br>";
  220. }
  221. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  222. if ($optioncss != '') {
  223. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  224. }
  225. print '<input type="hidden" name="token" value="'.newToken().'">';
  226. if ($fourn_id > 0) {
  227. print '<input type="hidden" name="fourn_id" value="'.$fourn_id.'">';
  228. }
  229. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  230. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  231. print '<input type="hidden" name="page" value="'.$page.'">';
  232. print '<input type="hidden" name="type" value="'.$type.'">';
  233. $topicmail = "Information";
  234. $modelmail = "product";
  235. $objecttmp = new Product($db);
  236. $trackid = 'prod'.$object->id;
  237. include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
  238. print '<div class="div-table-responsive-no-min">';
  239. print '<table class="liste centpercent">';
  240. // Fields title search
  241. print '<tr class="liste_titre">';
  242. print '<td class="liste_titre">';
  243. print '<input class="flat maxwidth100" type="text" name="sref" value="'.$sref.'">';
  244. print '</td>';
  245. print '<td class="liste_titre">';
  246. print '<input class="flat maxwidth100" type="text" name="srefsupplier" value="'.$sRefSupplier.'">';
  247. print '</td>';
  248. print '<td class="liste_titre">';
  249. print '<input class="flat maxwidth100" type="text" name="snom" value="'.$snom.'">';
  250. print '</td>';
  251. print '<td></td>';
  252. print '<td></td>';
  253. print '<td></td>';
  254. print '<td></td>';
  255. // add filters from hooks
  256. $parameters = array();
  257. $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters, $object, $action);
  258. if ($reshook < 0) {
  259. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  260. }
  261. print $hookmanager->resPrint;
  262. print '<td class="liste_titre maxwidthsearch">';
  263. $searchpicto = $form->showFilterButtons();
  264. print $searchpicto;
  265. print '</td>';
  266. print '</tr>';
  267. // Line for title
  268. print '<tr class="liste_titre">';
  269. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "p.ref", $param, "", "", $sortfield, $sortorder);
  270. print_liste_field_titre("RefSupplierShort", $_SERVER["PHP_SELF"], "ppf.ref_fourn", $param, "", "", $sortfield, $sortorder);
  271. print_liste_field_titre("Label", $_SERVER["PHP_SELF"], "p.label", $param, "", "", $sortfield, $sortorder);
  272. print_liste_field_titre("Supplier", $_SERVER["PHP_SELF"], "ppf.fk_soc", $param, "", "", $sortfield, $sortorder);
  273. print_liste_field_titre("BuyingPrice", $_SERVER["PHP_SELF"], "ppf.price", $param, "", '', $sortfield, $sortorder, 'right ');
  274. print_liste_field_titre("QtyMin", $_SERVER["PHP_SELF"], "ppf.quantity", $param, "", '', $sortfield, $sortorder, 'right ');
  275. print_liste_field_titre("UnitPrice", $_SERVER["PHP_SELF"], "ppf.unitprice", $param, "", '', $sortfield, $sortorder, 'right ');
  276. // add header cells from hooks
  277. $parameters = array();
  278. $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object, $action);
  279. if ($reshook < 0) {
  280. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  281. }
  282. print $hookmanager->resPrint;
  283. print_liste_field_titre('', $_SERVER["PHP_SELF"]);
  284. print "</tr>\n";
  285. while ($i < min($num, $limit)) {
  286. $objp = $db->fetch_object($resql);
  287. $productstatic->id = $objp->rowid;
  288. $productstatic->ref = $objp->ref;
  289. $productstatic->type = $objp->fk_product_type;
  290. $productstatic->entity = $objp->entity;
  291. $productstatic->status = $objp->tosell;
  292. $productstatic->status_buy = $objp->tobuy;
  293. $productstatic->barcode = $objp->barcode;
  294. $productstatic->barcode_type = $objp->fk_barcode_type;
  295. print '<tr class="oddeven">';
  296. print '<td>';
  297. print $productstatic->getNomUrl(1, 'supplier');
  298. print '</td>';
  299. print '<td>'.$objp->ref_fourn.'</td>';
  300. print '<td>'.$objp->label.'</td>'."\n";
  301. $companystatic->name = $objp->name;
  302. $companystatic->id = $objp->socid;
  303. print '<td>';
  304. if ($companystatic->id > 0) {
  305. print $companystatic->getNomUrl(1, 'supplier');
  306. }
  307. print '</td>';
  308. print '<td class="right">'.(isset($objp->price) ? price($objp->price) : '').'</td>';
  309. print '<td class="right">'.$objp->qty.'</td>';
  310. print '<td class="right">'.(isset($objp->unitprice) ? price($objp->unitprice) : '').'</td>';
  311. // add additional columns from hooks
  312. $parameters = array();
  313. $reshook = $hookmanager->executeHooks('printFieldListValue', $parameters, $objp, $action);
  314. if ($reshook < 0) {
  315. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  316. }
  317. print $hookmanager->resPrint;
  318. print '<td class="right"></td>';
  319. print "</tr>\n";
  320. $i++;
  321. }
  322. $db->free($resql);
  323. // If no record found
  324. if ($num == 0) {
  325. $colspan = 8;
  326. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
  327. }
  328. print "</table></div>";
  329. print '</form>';
  330. } else {
  331. dol_print_error($db);
  332. }
  333. // End of page
  334. llxFooter();
  335. $db->close();