commande_fournisseur.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/product/stats/commande_fournisseur.php
  22. * \ingroup product service commande
  23. * \brief Page des stats des commandes fournisseurs pour un produit
  24. */
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  30. // Load translation files required by the page
  31. $langs->loadLangs(array('orders', 'products', 'companies'));
  32. $id = GETPOST('id', 'int');
  33. $ref = GETPOST('ref', 'alpha');
  34. // Security check
  35. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  36. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  37. $socid = '';
  38. if (!empty($user->socid)) {
  39. $socid = $user->socid;
  40. }
  41. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  42. $hookmanager->initHooks(array('productstatssupplierorder'));
  43. $mesg = '';
  44. // Load variable for pagination
  45. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  46. $sortfield = GETPOST('sortfield', 'aZ09comma');
  47. $sortorder = GETPOST('sortorder', 'aZ09comma');
  48. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  49. if (empty($page) || $page == -1) {
  50. $page = 0;
  51. } // If $page is not defined, or '' or -1
  52. $offset = $limit * $page;
  53. $pageprev = $page - 1;
  54. $pagenext = $page + 1;
  55. if (!$sortorder) {
  56. $sortorder = "DESC";
  57. }
  58. if (!$sortfield) {
  59. $sortfield = "c.date_commande";
  60. }
  61. $search_month = GETPOST('search_month', 'int');
  62. $search_year = GETPOST('search_year', 'int');
  63. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
  64. $search_month = '';
  65. $search_year = '';
  66. }
  67. $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  68. /*
  69. * View
  70. */
  71. $supplierorderstatic = new CommandeFournisseur($db);
  72. $societestatic = new Societe($db);
  73. $form = new Form($db);
  74. $formother = new FormOther($db);
  75. if ($id > 0 || !empty($ref)) {
  76. $product = new Product($db);
  77. $result = $product->fetch($id, $ref);
  78. $object = $product;
  79. $parameters = array('id' => $id);
  80. $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $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. llxHeader("", "", $langs->trans("CardProduct".$product->type));
  85. if ($result > 0) {
  86. $head = product_prepare_head($product);
  87. $titre = $langs->trans("CardProduct".$product->type);
  88. $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
  89. print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
  90. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
  91. print $hookmanager->resPrint;
  92. if ($reshook < 0) {
  93. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  94. }
  95. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  96. $shownav = 1;
  97. if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
  98. $shownav = 0;
  99. }
  100. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  101. print '<div class="fichecenter">';
  102. print '<div class="underbanner clearboth"></div>';
  103. print '<table class="border tableforfield" width="100%">';
  104. $nboflines = show_stats_for_company($product, $socid);
  105. print "</table>";
  106. print '</div>';
  107. print '<div style="clear:both"></div>';
  108. print dol_get_fiche_end();
  109. if ($user->rights->fournisseur->commande->lire) {
  110. $sql = "SELECT DISTINCT s.nom as name, s.rowid as socid, s.code_client,";
  111. $sql .= " c.rowid, d.total_ht as total_ht, c.ref,";
  112. $sql .= " c.date_commande, c.fk_statut as statut, c.rowid as commandeid, d.rowid, d.qty";
  113. $sql .= ", c.date_livraison";
  114. if (empty($user->rights->societe->client->voir) && !$socid) {
  115. $sql .= ", sc.fk_soc, sc.fk_user ";
  116. }
  117. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  118. $sql .= ", ".MAIN_DB_PREFIX."commande_fournisseur as c";
  119. $sql .= ", ".MAIN_DB_PREFIX."commande_fournisseurdet as d";
  120. if (empty($user->rights->societe->client->voir) && !$socid) {
  121. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  122. }
  123. $sql .= " WHERE c.fk_soc = s.rowid";
  124. $sql .= " AND c.entity IN (".getEntity('supplier_order').")";
  125. $sql .= " AND d.fk_commande = c.rowid";
  126. $sql .= " AND d.fk_product = ".((int) $product->id);
  127. if (!empty($search_month)) {
  128. $sql .= ' AND MONTH(c.date_commande) IN ('.$db->sanitize($search_month).')';
  129. }
  130. if (!empty($search_year)) {
  131. $sql .= ' AND YEAR(c.date_commande) IN ('.$db->sanitize($search_year).')';
  132. }
  133. if (empty($user->rights->societe->client->voir) && !$socid) {
  134. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  135. }
  136. if ($socid) {
  137. $sql .= " AND c.fk_soc = ".((int) $socid);
  138. }
  139. $sql .= $db->order($sortfield, $sortorder);
  140. // Calcul total qty and amount for global if full scan list
  141. $total_ht = 0;
  142. $total_qty = 0;
  143. // Count total nb of records
  144. $totalofrecords = '';
  145. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  146. $result = $db->query($sql);
  147. $totalofrecords = $db->num_rows($result);
  148. }
  149. $sql .= $db->plimit($limit + 1, $offset);
  150. $result = $db->query($sql);
  151. if ($result) {
  152. $num = $db->num_rows($result);
  153. $option = '&id='.$product->id;
  154. if ($limit > 0 && $limit != $conf->liste_limit) {
  155. $option .= '&limit='.urlencode($limit);
  156. }
  157. if (!empty($search_month)) {
  158. $option .= '&search_month='.urlencode($search_month);
  159. }
  160. if (!empty($search_year)) {
  161. $option .= '&search_year='.urlencode($search_year);
  162. }
  163. print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
  164. print '<input type="hidden" name="token" value="'.newToken().'">';
  165. if (!empty($sortfield)) {
  166. print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
  167. }
  168. if (!empty($sortorder)) {
  169. print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
  170. }
  171. print_barre_liste($langs->trans("SuppliersOrders"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
  172. if (!empty($page)) {
  173. $option .= '&page='.urlencode($page);
  174. }
  175. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  176. print '<div class="divsearchfield">';
  177. print $langs->trans('Period').' ('.$langs->trans("OrderDate").') - ';
  178. print $langs->trans('Month').':<input class="flat" type="text" size="4" name="search_month" value="'.$search_month.'"> ';
  179. print $langs->trans('Year').':'.$formother->selectyear($search_year ? $search_year : - 1, 'search_year', 1, 20, 5);
  180. print '<div style="vertical-align: middle; display: inline-block">';
  181. print '<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans("Search"), 'search.png', '', '', 1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  182. print '<input type="image" class="liste_titre" name="button_removefilter" src="'.img_picto($langs->trans("Search"), 'searchclear.png', '', '', 1).'" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
  183. print '</div>';
  184. print '</div>';
  185. print '</div>';
  186. $i = 0;
  187. print '<div class="div-table-responsive">';
  188. print '<table class="tagtable liste listwithfilterbefore centpercent">';
  189. print '<tr class="liste_titre">';
  190. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "c.rowid", "", $option, '', $sortfield, $sortorder);
  191. print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", $option, '', $sortfield, $sortorder);
  192. print_liste_field_titre("SupplierCode", $_SERVER["PHP_SELF"], "s.code_client", "", $option, '', $sortfield, $sortorder);
  193. print_liste_field_titre("OrderDate", $_SERVER["PHP_SELF"], "c.date_commande", "", $option, 'align="center"', $sortfield, $sortorder);
  194. print_liste_field_titre('DateDeliveryPlanned', $_SERVER['PHP_SELF'], 'c.date_livraison', '', $option, 'align="center"', $sortfield, $sortorder);
  195. print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $option, 'align="center"', $sortfield, $sortorder);
  196. print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], "c.total_ht", "", $option, 'align="right"', $sortfield, $sortorder);
  197. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "c.fk_statut", "", $option, 'align="right"', $sortfield, $sortorder);
  198. print "</tr>\n";
  199. if ($num > 0) {
  200. while ($i < min($num, $limit)) {
  201. $objp = $db->fetch_object($result);
  202. $total_ht += $objp->total_ht;
  203. $total_qty += $objp->qty;
  204. $supplierorderstatic->id = $objp->commandeid;
  205. $supplierorderstatic->ref = $objp->ref;
  206. $supplierorderstatic->statut = $objp->statut;
  207. $societestatic->fetch($objp->socid);
  208. print '<tr class="oddeven">';
  209. print '<td>';
  210. print $supplierorderstatic->getNomUrl(1);
  211. print "</td>\n";
  212. print '<td>'.$societestatic->getNomUrl(1).'</td>';
  213. print "<td>".$objp->code_client."</td>\n";
  214. print '<td class="center">';
  215. print dol_print_date($db->jdate($objp->date_commande), 'dayhour')."</td>";
  216. // delivery planned date
  217. print '<td class="center">';
  218. print dol_print_date($db->jdate($objp->date_livraison), 'dayhour');
  219. print '</td>';
  220. print '<td class="center">'.$objp->qty."</td>\n";
  221. print '<td align="right">'.price($objp->total_ht)."</td>\n";
  222. print '<td align="right">'.$supplierorderstatic->getLibStatut(4).'</td>';
  223. print "</tr>\n";
  224. $i++;
  225. }
  226. }
  227. print '<tr class="liste_total">';
  228. if ($num < $limit) {
  229. print '<td class="left">'.$langs->trans("Total").'</td>';
  230. } else {
  231. print '<td class="left">'.$langs->trans("Totalforthispage").'</td>';
  232. }
  233. print '<td colspan="3"></td>';
  234. // delivery planned date
  235. print '<td></td>';
  236. print '<td class="center">'.$total_qty.'</td>';
  237. print '<td align="right">'.price($total_ht).'</td>';
  238. print '<td></td>';
  239. print "</table>";
  240. print '</div>';
  241. print '</form>';
  242. } else {
  243. dol_print_error($db);
  244. }
  245. $db->free($result);
  246. }
  247. }
  248. } else {
  249. dol_print_error();
  250. }
  251. // End of page
  252. llxFooter();
  253. $db->close();