propal.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /* Copyright (C) 2004-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/propal.php
  22. * \ingroup product service propal
  23. * \brief Page des stats des propals pour un produit
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array('products', 'companies'));
  33. $id = GETPOST('id', 'int');
  34. $ref = GETPOST('ref', 'alpha');
  35. // Security check
  36. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  37. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  38. $socid = '';
  39. if (!empty($user->socid)) {
  40. $socid = $user->socid;
  41. }
  42. $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  43. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  44. $hookmanager->initHooks(array('productstatspropal'));
  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 = "p.datep";
  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. $propalstatic = new Propal($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('propal', 'lire')) {
  111. $sql = "SELECT DISTINCT s.nom as name, s.rowid as socid, p.rowid as propalid, p.ref, d.total_ht as amount,";
  112. $sql .= " p.ref_client,";
  113. $sql .= "p.datep, p.fk_statut as statut, d.rowid, d.qty";
  114. if (!$user->hasRight('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."propal as p";
  119. $sql .= ", ".MAIN_DB_PREFIX."propaldet as d";
  120. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  121. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  122. }
  123. $sql .= " WHERE p.fk_soc = s.rowid";
  124. $sql .= " AND p.entity IN (".getEntity('propal').")";
  125. $sql .= " AND d.fk_propal = p.rowid";
  126. $sql .= " AND d.fk_product = ".((int) $product->id);
  127. if (!empty($search_month)) {
  128. $sql .= ' AND MONTH(p.datep) IN ('.$db->sanitize($search_month).')';
  129. }
  130. if (!empty($search_year)) {
  131. $sql .= ' AND YEAR(p.datep) IN ('.$db->sanitize($search_year).')';
  132. }
  133. if (!$user->hasRight('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 p.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 (!getDolGlobalInt('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='.((int) $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("Proposals"), $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("DatePropal").') - ';
  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" width="100%">';
  189. print '<tr class="liste_titre">';
  190. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "p.rowid", "", $option, '', $sortfield, $sortorder);
  191. print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", $option, '', $sortfield, $sortorder);
  192. print_liste_field_titre("DatePropal", $_SERVER["PHP_SELF"], "p.datep", "", $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"], "p.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->amount;
  201. $total_qty += $objp->qty;
  202. $propalstatic->id = $objp->propalid;
  203. $propalstatic->ref = $objp->ref;
  204. $propalstatic->ref_client = $objp->ref_client;
  205. $societestatic->fetch($objp->socid);
  206. print '<tr class="oddeven">';
  207. print '<td>';
  208. print $propalstatic->getNomUrl(1);
  209. print "</td>\n";
  210. print '<td>'.$societestatic->getNomUrl(1).'</td>';
  211. print '<td class="center">';
  212. print dol_print_date($db->jdate($objp->datep), 'dayhour')."</td>";
  213. print "<td align=\"center\">".$objp->qty."</td>\n";
  214. print '<td align="right">'.price($objp->amount).'</td>'."\n";
  215. print '<td align="right">'.$propalstatic->LibStatut($objp->statut, 5).'</td>';
  216. print "</tr>\n";
  217. $i++;
  218. }
  219. }
  220. print '<tr class="liste_total">';
  221. if ($num < $limit) {
  222. print '<td class="left">'.$langs->trans("Total").'</td>';
  223. } else {
  224. print '<td class="left">'.$langs->trans("Totalforthispage").'</td>';
  225. }
  226. print '<td colspan="2"></td>';
  227. print '<td class="center">'.$total_qty.'</td>';
  228. print '<td align="right">'.price($total_ht).'</td>';
  229. print '<td></td>';
  230. print "</table>";
  231. print '</div>';
  232. print '</form>';
  233. } else {
  234. dol_print_error($db);
  235. }
  236. $db->free($result);
  237. }
  238. }
  239. } else {
  240. dol_print_error();
  241. }
  242. // End of page
  243. llxFooter();
  244. $db->close();