contrat.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/product/stats/contrat.php
  21. * \ingroup product service contrat
  22. * \brief Page des stats des contrats pour un produit
  23. */
  24. // Load Dolibarr environment
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('contracts', 'products', 'companies'));
  31. $id = GETPOST('id', 'int');
  32. $ref = GETPOST('ref', 'alpha');
  33. // Security check
  34. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  35. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  36. if ($user->socid) {
  37. $socid = $user->socid;
  38. }
  39. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  40. $hookmanager->initHooks(array('productstatscontract'));
  41. // Load variable for pagination
  42. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  43. $sortfield = GETPOST('sortfield', 'aZ09comma');
  44. $sortorder = GETPOST('sortorder', 'aZ09comma');
  45. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  46. if (empty($page) || $page == -1) {
  47. $page = 0;
  48. } // If $page is not defined, or '' or -1
  49. $offset = $limit * $page;
  50. $pageprev = $page - 1;
  51. $pagenext = $page + 1;
  52. if (!$sortorder) {
  53. $sortorder = "DESC";
  54. }
  55. if (!$sortfield) {
  56. $sortfield = "c.date_contrat";
  57. }
  58. $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  59. /*
  60. * View
  61. */
  62. $staticcontrat = new Contrat($db);
  63. $staticcontratligne = new ContratLigne($db);
  64. $form = new Form($db);
  65. if ($id > 0 || !empty($ref)) {
  66. $product = new Product($db);
  67. $result = $product->fetch($id, $ref);
  68. $object = $product;
  69. $parameters = array('id'=>$id);
  70. $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
  71. if ($reshook < 0) {
  72. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  73. }
  74. llxHeader("", "", $langs->trans("CardProduct".$product->type));
  75. if ($result > 0) {
  76. $head = product_prepare_head($product);
  77. $titre = $langs->trans("CardProduct".$product->type);
  78. $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
  79. print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
  80. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
  81. print $hookmanager->resPrint;
  82. if ($reshook < 0) {
  83. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  84. }
  85. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  86. $shownav = 1;
  87. if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
  88. $shownav = 0;
  89. }
  90. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  91. print '<div class="fichecenter">';
  92. print '<div class="underbanner clearboth"></div>';
  93. print '<table class="border tableforfield" width="100%">';
  94. $nboflines = show_stats_for_company($product, $socid);
  95. print "</table>";
  96. print '</div>';
  97. print '<div class="clearboth"></div>';
  98. print dol_get_fiche_end();
  99. $now = dol_now();
  100. $sql = "SELECT";
  101. $sql .= " sum(".$db->ifsql("cd.statut=0", 1, 0).') as nb_initial,';
  102. $sql .= " sum(".$db->ifsql("cd.statut=4 AND cd.date_fin_validite > '".$db->idate($now)."'", 1, 0).") as nb_running,";
  103. $sql .= " sum(".$db->ifsql("cd.statut=4 AND (cd.date_fin_validite IS NULL OR cd.date_fin_validite <= '".$db->idate($now)."')", 1, 0).') as nb_late,';
  104. $sql .= " sum(".$db->ifsql("cd.statut=5", 1, 0).') as nb_closed,';
  105. $sql .= " c.rowid as rowid, c.ref, c.ref_customer, c.ref_supplier, c.date_contrat, c.statut as statut,";
  106. $sql .= " s.nom as name, s.rowid as socid, s.code_client";
  107. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  108. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  109. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  110. }
  111. $sql .= ", ".MAIN_DB_PREFIX."contrat as c";
  112. $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd";
  113. $sql .= " WHERE c.rowid = cd.fk_contrat";
  114. $sql .= " AND c.fk_soc = s.rowid";
  115. $sql .= " AND c.entity IN (".getEntity('contract').")";
  116. $sql .= " AND cd.fk_product = ".((int) $product->id);
  117. if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
  118. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  119. }
  120. if ($socid) {
  121. $sql .= " AND s.rowid = ".((int) $socid);
  122. }
  123. $sql .= " GROUP BY c.rowid, c.ref, c.ref_customer, c.ref_supplier, c.date_contrat, c.statut, s.nom, s.rowid, s.code_client";
  124. $sql .= $db->order($sortfield, $sortorder);
  125. //Calcul total qty and amount for global if full scan list
  126. $total_ht = 0;
  127. $total_qty = 0;
  128. // Count total nb of records
  129. $totalofrecords = '';
  130. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  131. $result = $db->query($sql);
  132. $totalofrecords = $db->num_rows($result);
  133. }
  134. $sql .= $db->plimit($limit + 1, $offset);
  135. $result = $db->query($sql);
  136. if ($result) {
  137. $num = $db->num_rows($result);
  138. $option = '&id='.$product->id;
  139. if ($limit > 0 && $limit != $conf->liste_limit) {
  140. $option .= '&limit='.((int) $limit);
  141. }
  142. if (!empty($search_month)) {
  143. $option .= '&search_month='.urlencode($search_month);
  144. }
  145. if (!empty($search_year)) {
  146. $option .= '&search_year='.urlencode($search_year);
  147. }
  148. print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
  149. print '<input type="hidden" name="token" value="'.newToken().'">';
  150. if (!empty($sortfield)) {
  151. print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
  152. }
  153. if (!empty($sortorder)) {
  154. print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
  155. }
  156. print_barre_liste($langs->trans("Contrats"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
  157. if (!empty($page)) {
  158. $option .= '&page='.urlencode($page);
  159. }
  160. $i = 0;
  161. print '<div class="div-table-responsive">';
  162. print '<table class="tagtable liste listwithfilterbefore" width="100%">';
  163. print '<tr class="liste_titre">';
  164. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "c.rowid", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
  165. print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
  166. print_liste_field_titre("CustomerCode", $_SERVER["PHP_SELF"], "s.code_client", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
  167. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "c.date_contrat", "", "&amp;id=".$product->id, 'align="center"', $sortfield, $sortorder);
  168. //print_liste_field_titre("AmountHT"),$_SERVER["PHP_SELF"],"c.amount","","&amp;id=".$product->id,'align="right"',$sortfield,$sortorder);
  169. print_liste_field_titre($staticcontratligne->LibStatut($staticcontratligne::STATUS_INITIAL, 3, -1, 'class="nochangebackground"'), $_SERVER["PHP_SELF"], "", '', '', 'align="center" width="16"', $sortfield, $sortorder, 'maxwidthsearch ');
  170. print_liste_field_titre($staticcontratligne->LibStatut($staticcontratligne::STATUS_OPEN, 3, -1, 'class="nochangebackground"'), $_SERVER["PHP_SELF"], "", '', '', 'align="center" width="16"', $sortfield, $sortorder, 'maxwidthsearch ');
  171. print_liste_field_titre($staticcontratligne->LibStatut($staticcontratligne::STATUS_CLOSED, 3, -1, 'class="nochangebackground"'), $_SERVER["PHP_SELF"], "", '', '', 'align="center" width="16"', $sortfield, $sortorder, 'maxwidthsearch ');
  172. print "</tr>\n";
  173. $contracttmp = new Contrat($db);
  174. if ($num > 0) {
  175. while ($i < min($num, $limit)) {
  176. $objp = $db->fetch_object($result);
  177. $contracttmp->id = $objp->rowid;
  178. $contracttmp->ref = $objp->ref;
  179. $contracttmp->ref_customer = $objp->ref_customer;
  180. $contracttmp->ref_supplier = $objp->ref_supplier;
  181. print '<tr class="oddeven">';
  182. print '<td>';
  183. print $contracttmp->getNomUrl(1);
  184. print "</td>\n";
  185. print '<td><a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$objp->socid.'">'.img_object($langs->trans("ShowCompany"), "company").' '.dol_trunc($objp->name, 44).'</a></td>';
  186. print "<td>".$objp->code_client."</td>\n";
  187. print "<td align=\"center\">";
  188. print dol_print_date($db->jdate($objp->date_contrat), 'dayhour')."</td>";
  189. //print "<td align=\"right\">".price($objp->total_ht)."</td>\n";
  190. //print '<td align="right">';
  191. print '<td class="center">'.($objp->nb_initial > 0 ? $objp->nb_initial : '').'</td>';
  192. print '<td class="center">'.($objp->nb_running + $objp->nb_late > 0 ? $objp->nb_running + $objp->nb_late : '').'</td>';
  193. print '<td class="center">'.($objp->nb_closed > 0 ? $objp->nb_closed : '').'</td>';
  194. //$contratstatic->LibStatut($objp->statut,5).'</td>';
  195. print "</tr>\n";
  196. $i++;
  197. }
  198. }
  199. print '</table>';
  200. print '</div>';
  201. print '</form>';
  202. } else {
  203. dol_print_error($db);
  204. }
  205. $db->free($result);
  206. }
  207. } else {
  208. dol_print_error();
  209. }
  210. // End of page
  211. llxFooter();
  212. $db->close();