facture.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2014 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.php
  23. * \ingroup product service facture
  24. * \brief Page of 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.'/compta/facture/class/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', '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('productstatsinvoice'));
  45. $showmessage = GETPOST('showmessage');
  46. // Load variable for pagination
  47. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  48. $sortfield = GETPOST('sortfield', 'aZ09comma');
  49. $sortorder = GETPOST('sortorder', 'aZ09comma');
  50. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  51. if (empty($page) || $page == -1) {
  52. $page = 0;
  53. } // If $page is not defined, or '' or -1
  54. $offset = $limit * $page;
  55. $pageprev = $page - 1;
  56. $pagenext = $page + 1;
  57. if (!$sortorder) {
  58. $sortorder = "DESC";
  59. }
  60. if (!$sortfield) {
  61. $sortfield = "f.datef";
  62. }
  63. $search_month = GETPOST('search_month', 'int');
  64. $search_year = GETPOST('search_year', 'int');
  65. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
  66. $search_month = '';
  67. $search_year = '';
  68. }
  69. $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  70. /*
  71. * View
  72. */
  73. $invoicestatic = new Facture($db);
  74. $societestatic = new Societe($db);
  75. $form = new Form($db);
  76. $formother = new FormOther($db);
  77. if ($id > 0 || !empty($ref)) {
  78. $product = new Product($db);
  79. $result = $product->fetch($id, $ref);
  80. $object = $product;
  81. $parameters = array('id'=>$id);
  82. $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
  83. if ($reshook < 0) {
  84. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  85. }
  86. $title = $langs->trans('ProductServiceCard');
  87. $helpurl = '';
  88. $shortlabel = dol_trunc($object->label, 16);
  89. if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
  90. $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Referers');
  91. $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  92. }
  93. if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
  94. $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Referers');
  95. $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  96. }
  97. llxHeader('', $title, $helpurl);
  98. if ($result > 0) {
  99. $head = product_prepare_head($product);
  100. $titre = $langs->trans("CardProduct".$product->type);
  101. $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
  102. print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
  103. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
  104. print $hookmanager->resPrint;
  105. if ($reshook < 0) {
  106. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  107. }
  108. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  109. $shownav = 1;
  110. if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
  111. $shownav = 0;
  112. }
  113. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  114. print '<div class="fichecenter">';
  115. print '<div class="underbanner clearboth"></div>';
  116. print '<table class="border tableforfield" width="100%">';
  117. $nboflines = show_stats_for_company($product, $socid);
  118. print "</table>";
  119. print '</div>';
  120. print '<div class="clearboth"></div>';
  121. print dol_get_fiche_end();
  122. if ($showmessage && $nboflines > 1) {
  123. print '<span class="opacitymedium">'.$langs->trans("ClinkOnALinkOfColumn", $langs->transnoentitiesnoconv("Referers")).'</span>';
  124. } elseif ($user->hasRight('facture', 'lire')) {
  125. $sql = "SELECT DISTINCT s.nom as name, s.rowid as socid, s.code_client,";
  126. $sql .= " f.ref, f.datef, f.paye, f.type, f.fk_statut as statut, f.rowid as facid,";
  127. $sql .= " d.rowid, d.total_ht as total_ht, d.qty"; // We must keep the d.rowid here to not loose record because of the distinct used to ignore duplicate line when link on societe_commerciaux is used
  128. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  129. $sql .= ", sc.fk_soc, sc.fk_user ";
  130. }
  131. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  132. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  133. $sql .= ", ".MAIN_DB_PREFIX."facturedet as d";
  134. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  135. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  136. }
  137. $sql .= " WHERE f.fk_soc = s.rowid";
  138. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  139. $sql .= " AND d.fk_facture = f.rowid";
  140. $sql .= " AND d.fk_product = ".((int) $product->id);
  141. if (!empty($search_month)) {
  142. $sql .= ' AND MONTH(f.datef) IN ('.$db->sanitize($search_month).')';
  143. }
  144. if (!empty($search_year)) {
  145. $sql .= ' AND YEAR(f.datef) IN ('.$db->sanitize($search_year).')';
  146. }
  147. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  148. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  149. }
  150. if ($socid) {
  151. $sql .= " AND f.fk_soc = ".((int) $socid);
  152. }
  153. $sql .= $db->order($sortfield, $sortorder);
  154. // Calcul total qty and amount for global if full scan list
  155. $total_ht = 0;
  156. $total_qty = 0;
  157. // Count total nb of records
  158. $totalofrecords = '';
  159. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  160. $result = $db->query($sql);
  161. $totalofrecords = $db->num_rows($result);
  162. }
  163. $sql .= $db->plimit($limit + 1, $offset);
  164. $result = $db->query($sql);
  165. if ($result) {
  166. $num = $db->num_rows($result);
  167. $option .= '&id='.$product->id;
  168. if ($limit > 0 && $limit != $conf->liste_limit) {
  169. $option .= '&limit='.((int) $limit);
  170. }
  171. if (!empty($search_month)) {
  172. $option .= '&search_month='.urlencode($search_month);
  173. }
  174. if (!empty($search_year)) {
  175. $option .= '&search_year='.urlencode($search_year);
  176. }
  177. print '<form method="post" action="'.$_SERVER ['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
  178. print '<input type="hidden" name="token" value="'.newToken().'">';
  179. if (!empty($sortfield)) {
  180. print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
  181. }
  182. if (!empty($sortorder)) {
  183. print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
  184. }
  185. print_barre_liste($langs->trans("CustomersInvoices"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
  186. if (!empty($page)) {
  187. $option .= '&page='.urlencode($page);
  188. }
  189. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  190. print '<div class="divsearchfield">';
  191. print $langs->trans('Period').' ('.$langs->trans("DateInvoice").') - ';
  192. print $langs->trans('Month').':<input class="flat" type="text" size="4" name="search_month" value="'.$search_month.'"> ';
  193. print $langs->trans('Year').':'.$formother->selectyear($search_year ? $search_year : - 1, 'search_year', 1, 20, 5);
  194. print '<div style="vertical-align: middle; display: inline-block">';
  195. 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")).'">';
  196. 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")).'">';
  197. print '</div>';
  198. print '</div>';
  199. print '</div>';
  200. $i = 0;
  201. print '<div class="div-table-responsive">';
  202. print '<table class="tagtable liste listwithfilterbefore" width="100%">';
  203. print '<tr class="liste_titre">';
  204. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "s.rowid", "", $option, '', $sortfield, $sortorder);
  205. print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", $option, '', $sortfield, $sortorder);
  206. print_liste_field_titre("CustomerCode", $_SERVER["PHP_SELF"], "s.code_client", "", $option, '', $sortfield, $sortorder);
  207. print_liste_field_titre("DateInvoice", $_SERVER["PHP_SELF"], "f.datef", "", $option, 'align="center"', $sortfield, $sortorder);
  208. print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $option, 'align="center"', $sortfield, $sortorder);
  209. print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], "d.total_ht", "", $option, 'align="right"', $sortfield, $sortorder);
  210. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "f.paye,f.fk_statut", "", $option, 'align="right"', $sortfield, $sortorder);
  211. print "</tr>\n";
  212. if ($num > 0) {
  213. while ($i < min($num, $limit)) {
  214. $objp = $db->fetch_object($result);
  215. if ($objp->type == Facture::TYPE_CREDIT_NOTE) {
  216. $objp->qty = -($objp->qty);
  217. }
  218. $total_ht += $objp->total_ht;
  219. $total_qty += $objp->qty;
  220. $invoicestatic->id = $objp->facid;
  221. $invoicestatic->ref = $objp->ref;
  222. $societestatic->fetch($objp->socid);
  223. $paiement = $invoicestatic->getSommePaiement();
  224. print '<tr class="oddeven">';
  225. print '<td>';
  226. print $invoicestatic->getNomUrl(1);
  227. print "</td>\n";
  228. print '<td>'.$societestatic->getNomUrl(1).'</td>';
  229. print "<td>".$objp->code_client."</td>\n";
  230. print '<td class="center">';
  231. print dol_print_date($db->jdate($objp->datef), 'dayhour')."</td>";
  232. print '<td class="center">'.$objp->qty."</td>\n";
  233. print '<td align="right">'.price($objp->total_ht)."</td>\n";
  234. print '<td align="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5, $paiement, $objp->type).'</td>';
  235. print "</tr>\n";
  236. $i++;
  237. }
  238. }
  239. print '<tr class="liste_total">';
  240. if ($num < $limit) {
  241. print '<td class="left">'.$langs->trans("Total").'</td>';
  242. } else {
  243. print '<td class="left">'.$langs->trans("Totalforthispage").'</td>';
  244. }
  245. print '<td colspan="3"></td>';
  246. print '<td class="center">'.$total_qty.'</td>';
  247. print '<td align="right">'.price($total_ht).'</td>';
  248. print '<td></td>';
  249. print "</table>";
  250. print '</div>';
  251. print '</form>';
  252. } else {
  253. dol_print_error($db);
  254. }
  255. $db->free($result);
  256. }
  257. }
  258. } else {
  259. dol_print_error();
  260. }
  261. // End of page
  262. llxFooter();
  263. $db->close();