facture_fournisseur.php 12 KB

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