facture.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. $extrafields = new ExtraFields($db);
  46. // Fetch optionals attributes and labels
  47. $extrafields->fetch_name_optionals_label('facture');
  48. $search_array_options = $extrafields->getOptionalsFromPost('facture', '', 'search_');
  49. $showmessage = GETPOST('showmessage');
  50. // Load variable for pagination
  51. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  52. $sortfield = GETPOST('sortfield', 'aZ09comma');
  53. $sortorder = GETPOST('sortorder', 'aZ09comma');
  54. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  55. if (empty($page) || $page == -1) {
  56. $page = 0;
  57. } // If $page is not defined, or '' or -1
  58. $offset = $limit * $page;
  59. $pageprev = $page - 1;
  60. $pagenext = $page + 1;
  61. if (!$sortorder) {
  62. $sortorder = "DESC";
  63. }
  64. if (!$sortfield) {
  65. $sortfield = "f.datef";
  66. }
  67. $search_date_startday = GETPOSTINT('search_date_startday');
  68. if (!empty($search_date_startday)) {
  69. $option .= '&search_date_startday='.$search_date_startday;
  70. }
  71. $search_date_startmonth = GETPOSTINT('search_date_startmonth');
  72. if (!empty($search_date_startmonth)) {
  73. $option .= '&search_date_startmonth='.$search_date_startmonth;
  74. }
  75. $search_date_startyear = GETPOSTINT('search_date_startyear');
  76. if (!empty($search_date_startyear)) {
  77. $option .= '&search_date_startyear='.$search_date_startyear;
  78. }
  79. $search_date_endday = GETPOSTINT('search_date_endday');
  80. if (!empty($search_date_endday)) {
  81. $option .= '&search_date_endday='.$search_date_endday;
  82. }
  83. $search_date_endmonth = GETPOSTINT('search_date_endmonth');
  84. if (!empty($search_date_endmonth)) {
  85. $option .= '&search_date_endmonth='.$search_date_endmonth;
  86. }
  87. $search_date_endyear = GETPOSTINT('search_date_endyear');
  88. if (!empty($search_date_endyear)) {
  89. $option .= '&search_date_endyear='.$search_date_endyear;
  90. }
  91. $search_date_start = dol_mktime(0, 0, 0, $search_date_startmonth, $search_date_startday, $search_date_startyear); // Use tzserver
  92. $search_date_end = dol_mktime(23, 59, 59, $search_date_endmonth, $search_date_endday, $search_date_endyear);
  93. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
  94. $search_date_startday = '';
  95. $search_date_startmonth = '';
  96. $search_date_startyear = '';
  97. $search_date_endday = '';
  98. $search_date_endmonth = '';
  99. $search_date_endyear = '';
  100. $search_date_start = '';
  101. $search_date_end = '';
  102. }
  103. $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  104. /*
  105. * View
  106. */
  107. $invoicestatic = new Facture($db);
  108. $societestatic = new Societe($db);
  109. $form = new Form($db);
  110. $formother = new FormOther($db);
  111. if ($id > 0 || !empty($ref)) {
  112. $product = new Product($db);
  113. $result = $product->fetch($id, $ref);
  114. $object = $product;
  115. $parameters = array('id'=>$id);
  116. $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
  117. if ($reshook < 0) {
  118. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  119. }
  120. $title = $langs->trans('ProductServiceCard');
  121. $helpurl = '';
  122. $shortlabel = dol_trunc($object->label, 16);
  123. if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
  124. $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Referers');
  125. $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  126. }
  127. if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
  128. $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Referers');
  129. $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  130. }
  131. llxHeader('', $title, $helpurl);
  132. if ($result > 0) {
  133. $head = product_prepare_head($product);
  134. $titre = $langs->trans("CardProduct".$product->type);
  135. $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
  136. print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
  137. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
  138. print $hookmanager->resPrint;
  139. if ($reshook < 0) {
  140. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  141. }
  142. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  143. $shownav = 1;
  144. if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
  145. $shownav = 0;
  146. }
  147. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  148. print '<div class="fichecenter">';
  149. print '<div class="underbanner clearboth"></div>';
  150. print '<table class="border tableforfield" width="100%">';
  151. $nboflines = show_stats_for_company($product, $socid);
  152. print "</table>";
  153. print '</div>';
  154. print '<div class="clearboth"></div>';
  155. print dol_get_fiche_end();
  156. if ($showmessage && $nboflines > 1) {
  157. print '<span class="opacitymedium">'.$langs->trans("ClinkOnALinkOfColumn", $langs->transnoentitiesnoconv("Referers")).'</span>';
  158. } elseif ($user->hasRight('facture', 'lire')) {
  159. $sql = "SELECT DISTINCT s.nom as name, s.rowid as socid, s.code_client,";
  160. $sql .= " f.ref, f.datef, f.paye, f.type, f.fk_statut as statut, f.rowid as facid,";
  161. $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
  162. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  163. $sql .= ", sc.fk_soc, sc.fk_user ";
  164. }
  165. // Add fields from extrafields
  166. if (!empty($extrafields->attributes['facture']['label'])) {
  167. foreach ($extrafields->attributes['facture']['label'] as $key => $val) {
  168. $sql .= ($extrafields->attributes['facture']['type'][$key] != 'separate' ? ", ef.".$key." as options_".$key : '');
  169. }
  170. }
  171. // Add fields from hooks
  172. $parameters = array();
  173. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  174. $sql .= $hookmanager->resPrint;
  175. $sql = preg_replace('/,\s*$/', '', $sql);
  176. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  177. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  178. if (isset($extrafields->attributes['facture']['label']) && is_array($extrafields->attributes['facture']['label']) && count($extrafields->attributes['facture']['label'])) {
  179. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.'facture'."_extrafields as ef on (f.rowid = ef.fk_object)";
  180. }
  181. $sql .= ", ".MAIN_DB_PREFIX."facturedet as d";
  182. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  183. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  184. }
  185. // Add table from hooks
  186. $parameters = array();
  187. $reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters, $object); // Note that $action and $object may have been modified by hook
  188. $sql .= $hookmanager->resPrint;
  189. $sql .= " WHERE f.fk_soc = s.rowid";
  190. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  191. $sql .= " AND d.fk_facture = f.rowid";
  192. $sql .= " AND d.fk_product = ".((int) $product->id);
  193. if ($search_date_start) {
  194. $sql .= " AND f.datef >= '".$db->idate($search_date_start)."'";
  195. }
  196. if ($search_date_end) {
  197. $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
  198. }
  199. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  200. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  201. }
  202. if ($socid) {
  203. $sql .= " AND f.fk_soc = ".((int) $socid);
  204. }
  205. // Add where from extra fields
  206. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
  207. // Add where from hooks
  208. $parameters = array();
  209. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  210. $sql .= $hookmanager->resPrint;
  211. // Add HAVING from hooks
  212. $parameters = array();
  213. $reshook = $hookmanager->executeHooks('printFieldListHaving', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  214. $sql .= empty($hookmanager->resPrint) ? "" : " HAVING 1=1 ".$hookmanager->resPrint;
  215. $sql .= $db->order($sortfield, $sortorder);
  216. // Calcul total qty and amount for global if full scan list
  217. $total_ht = 0;
  218. $total_qty = 0;
  219. // Count total nb of records
  220. $totalofrecords = '';
  221. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  222. $result = $db->query($sql);
  223. $totalofrecords = $db->num_rows($result);
  224. }
  225. $sql .= $db->plimit($limit + 1, $offset);
  226. $result = $db->query($sql);
  227. if ($result) {
  228. $num = $db->num_rows($result);
  229. $option .= '&id='.$product->id;
  230. if ($limit > 0 && $limit != $conf->liste_limit) {
  231. $option .= '&limit='.((int) $limit);
  232. }
  233. if (!empty($search_month)) {
  234. $option .= '&search_month='.urlencode($search_month);
  235. }
  236. if (!empty($search_year)) {
  237. $option .= '&search_year='.urlencode($search_year);
  238. }
  239. // Add $param from extra fields
  240. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php';
  241. // Add $param from hooks
  242. $parameters = array();
  243. $reshook = $hookmanager->executeHooks('printFieldListSearchParam', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  244. $option .= $hookmanager->resPrint;
  245. print '<form method="post" action="'.$_SERVER ['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
  246. print '<input type="hidden" name="token" value="'.newToken().'">';
  247. if (!empty($sortfield)) {
  248. print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
  249. }
  250. if (!empty($sortorder)) {
  251. print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
  252. }
  253. print_barre_liste($langs->trans("CustomersInvoices"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
  254. if (!empty($page)) {
  255. $option .= '&page='.urlencode($page);
  256. }
  257. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  258. print '<div class="divsearchfield">';
  259. print $langs->trans('Period').' ('.$langs->trans("DateInvoice").') - ';
  260. print $form->selectDate($search_date_start ? $search_date_start : -1, 'search_date_start', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('From'));
  261. print $form->selectDate($search_date_end ? $search_date_end : -1, 'search_date_end', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('to'));
  262. $parameters = array();
  263. $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  264. print $hookmanager->resPrint;
  265. print '<div style="vertical-align: middle; display: inline-block">';
  266. 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")).'">';
  267. 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")).'">';
  268. print '</div>';
  269. print '</div>';
  270. print '</div>';
  271. $i = 0;
  272. print '<div class="div-table-responsive">';
  273. print '<table class="tagtable liste listwithfilterbefore" width="100%">';
  274. print '<tr class="liste_titre">';
  275. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "s.rowid", "", $option, '', $sortfield, $sortorder);
  276. print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", $option, '', $sortfield, $sortorder);
  277. print_liste_field_titre("CustomerCode", $_SERVER["PHP_SELF"], "s.code_client", "", $option, '', $sortfield, $sortorder);
  278. print_liste_field_titre("DateInvoice", $_SERVER["PHP_SELF"], "f.datef", "", $option, 'align="center"', $sortfield, $sortorder);
  279. print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $option, 'align="center"', $sortfield, $sortorder);
  280. print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], "d.total_ht", "", $option, 'align="right"', $sortfield, $sortorder);
  281. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "f.paye,f.fk_statut", "", $option, 'align="right"', $sortfield, $sortorder);
  282. // Hook fields
  283. $parameters = array('param'=>$option, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
  284. $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  285. print $hookmanager->resPrint;
  286. print "</tr>\n";
  287. if ($num > 0) {
  288. while ($i < min($num, $limit)) {
  289. $objp = $db->fetch_object($result);
  290. if ($objp->type == Facture::TYPE_CREDIT_NOTE) {
  291. $objp->qty = -($objp->qty);
  292. }
  293. $total_ht += $objp->total_ht;
  294. $total_qty += $objp->qty;
  295. $invoicestatic->id = $objp->facid;
  296. $invoicestatic->ref = $objp->ref;
  297. $societestatic->fetch($objp->socid);
  298. $paiement = $invoicestatic->getSommePaiement();
  299. print '<tr class="oddeven">';
  300. print '<td>';
  301. print $invoicestatic->getNomUrl(1);
  302. print "</td>\n";
  303. print '<td>'.$societestatic->getNomUrl(1).'</td>';
  304. print "<td>".$objp->code_client."</td>\n";
  305. print '<td class="center">';
  306. print dol_print_date($db->jdate($objp->datef), 'dayhour')."</td>";
  307. print '<td class="center">'.$objp->qty."</td>\n";
  308. print '<td align="right">'.price($objp->total_ht)."</td>\n";
  309. print '<td align="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5, $paiement, $objp->type).'</td>';
  310. // Fields from hook
  311. $parameters = array();
  312. $reshook = $hookmanager->executeHooks('printFieldListValue', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  313. print $hookmanager->resPrint;
  314. print "</tr>\n";
  315. $i++;
  316. }
  317. }
  318. print '<tr class="liste_total">';
  319. if ($num < $limit) {
  320. print '<td class="left">'.$langs->trans("Total").'</td>';
  321. } else {
  322. print '<td class="left">'.$langs->trans("Totalforthispage").'</td>';
  323. }
  324. print '<td colspan="3"></td>';
  325. print '<td class="center">'.$total_qty.'</td>';
  326. print '<td align="right">'.price($total_ht).'</td>';
  327. print '<td></td>';
  328. print "</table>";
  329. print '</div>';
  330. print '</form>';
  331. } else {
  332. dol_print_error($db);
  333. }
  334. $db->free($result);
  335. }
  336. }
  337. } else {
  338. dol_print_error();
  339. }
  340. // End of page
  341. llxFooter();
  342. $db->close();