dolibarr 24.0.0-beta
contrat.php
Go to the documentation of this file.
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 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
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
28// Load Dolibarr environment
29require '../../main.inc.php';
38require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
39require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
40require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
41
42// Load translation files required by the page
43$langs->loadLangs(array('contracts', 'products', 'companies'));
44
45$id = GETPOSTINT('id');
46$ref = GETPOST('ref', 'alpha');
47
48$search_month = GETPOSTINT('search_month');
49$search_year = GETPOSTINT('search_year');
50
51// Security check
52$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
53$fieldtype = (!empty($ref) ? 'ref' : 'rowid');
54if ($user->socid) {
55 $socid = $user->socid;
56}
57
58// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
59$hookmanager->initHooks(array('productstatscontract'));
60
61// Load variable for pagination
62$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
63$sortfield = GETPOST('sortfield', 'aZ09comma');
64$sortorder = GETPOST('sortorder', 'aZ09comma');
65$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
66if (empty($page) || $page == -1) {
67 $page = 0;
68} // If $page is not defined, or '' or -1
69$offset = $limit * $page;
70$pageprev = $page - 1;
71$pagenext = $page + 1;
72if (!$sortorder) {
73 $sortorder = "DESC";
74}
75if (!$sortfield) {
76 $sortfield = "c.date_contrat";
77}
78
79$socid = 0;
80
81$result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
82
83
84/*
85 * View
86 */
87
88$staticcontrat = new Contrat($db);
89$staticcontratligne = new ContratLigne($db);
90
91$form = new Form($db);
92
93if ($id > 0 || !empty($ref)) {
94 $product = new Product($db);
95 $result = $product->fetch($id, $ref);
96
97 $object = $product;
98
99 $parameters = array('id' => $id);
100 $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
101 if ($reshook < 0) {
102 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
103 }
104
105 llxHeader("", "", $langs->trans("CardProduct".$product->type), '', 0, 0, '', '', 'mod-product page-stats_contrat');
106
107 if ($result > 0) {
108 $head = product_prepare_head($product);
109 $titre = $langs->trans("CardProduct".$product->type);
110 $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
111 print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
112
113 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
114 print $hookmanager->resPrint;
115 if ($reshook < 0) {
116 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
117 }
118
119 $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
120
121 $shownav = 1;
122 if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
123 $shownav = 0;
124 }
125
126 dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
127
128 print '<div class="fichecenter">';
129
130 print '<div class="clearboth"></div>';
131 print '<table class="noborder tableforfield centpercent">';
132
133 $nboflines = show_stats_for_company($product, $socid);
134
135 print "</table>";
136
137 print '</div>';
138 print '<div class="clearboth"></div>';
139
140 print dol_get_fiche_end();
141
142
143 $now = dol_now();
144
145 $sql = "SELECT";
146 $sql .= " sum(".$db->ifsql("cd.statut=0", '1', '0').') as nb_initial,';
147 $sql .= " sum(".$db->ifsql("cd.statut=4 AND cd.date_fin_validite > '".$db->idate($now)."'", '1', '0').") as nb_running,";
148 $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,';
149 $sql .= " sum(".$db->ifsql("cd.statut=5", '1', '0').') as nb_closed,';
150 $sql .= " c.rowid as rowid, c.ref, c.ref_customer, c.ref_supplier, c.date_contrat, c.statut as statut,";
151 $sql .= " s.nom as name, s.rowid as socid, s.code_client";
152 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
153 if (!$user->hasRight('societe', 'client', 'voir')) {
154 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
155 }
156 $sql .= ", ".MAIN_DB_PREFIX."contrat as c";
157 $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd";
158 $sql .= " WHERE c.rowid = cd.fk_contrat";
159 $sql .= " AND c.fk_soc = s.rowid";
160 $sql .= " AND c.entity IN (".getEntity('contract').")";
161 $sql .= " AND cd.fk_product = ".((int) $product->id);
162 if (!$user->hasRight('societe', 'client', 'voir')) {
163 $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
164 }
165 if ($socid) {
166 $sql .= " AND s.rowid = ".((int) $socid);
167 }
168 $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";
169 $sql .= $db->order($sortfield, $sortorder);
170
171 //Calcul total qty and amount for global if full scan list
172 $total_ht = 0;
173 $total_qty = 0;
174
175 // Count total nb of records
176 $totalofrecords = '';
177 if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
178 $result = $db->query($sql);
179 $totalofrecords = $db->num_rows($result);
180 }
181
182 $sql .= $db->plimit($limit + 1, $offset);
183
184 $result = $db->query($sql);
185 if ($result) {
186 $num = $db->num_rows($result);
187
188 $option = '&id='.$product->id;
189
190 if ($limit > 0 && $limit != $conf->liste_limit) {
191 $option .= '&limit='.((int) $limit);
192 }
193 if (!empty($search_month)) {
194 $option .= '&search_month='.urlencode((string) $search_month);
195 }
196 if (!empty($search_year)) {
197 $option .= '&search_year='.urlencode((string) $search_year);
198 }
199
200 print '<span id="anchorundermenu" class="anchorundermenu"></span>';
201 print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
202 print '<input type="hidden" name="token" value="'.newToken().'">';
203 print '<input type="hidden" name="page_y" value="">';
204 if (!empty($sortfield)) {
205 print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
206 }
207 if (!empty($sortorder)) {
208 print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
209 }
210
211 // @phan-suppress-next-line PhanPluginSuspiciousParamOrder
212 print_barre_liste($langs->trans("Contracts"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
213
214 if (!empty($page)) {
215 $option .= '&page='.urlencode((string) ($page));
216 }
217
218 $i = 0;
219 print '<div class="div-table-responsive">';
220 print '<table class="tagtable liste listwithfilterbefore" width="100%">';
221
222 print '<tr class="liste_titre">';
223 print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "c.rowid", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
224 print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
225 print_liste_field_titre("CustomerCode", $_SERVER["PHP_SELF"], "s.code_client", "", "&amp;id=".$product->id, '', $sortfield, $sortorder);
226 print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "c.date_contrat", "", "&amp;id=".$product->id, 'align="center"', $sortfield, $sortorder);
227 //print_liste_field_titre("AmountHT"),$_SERVER["PHP_SELF"],"c.amount","","&amp;id=".$product->id,'align="right"',$sortfield,$sortorder);
228 print_liste_field_titre($staticcontratligne->LibStatut($staticcontratligne::STATUS_INITIAL, 3, -1, 'class="nochangebackground"'), $_SERVER["PHP_SELF"], "", '', '', 'align="center" width="16"', $sortfield, $sortorder, 'maxwidthsearch ');
229 print_liste_field_titre($staticcontratligne->LibStatut($staticcontratligne::STATUS_OPEN, 3, -1, 'class="nochangebackground"'), $_SERVER["PHP_SELF"], "", '', '', 'align="center" width="16"', $sortfield, $sortorder, 'maxwidthsearch ');
230 print_liste_field_titre($staticcontratligne->LibStatut($staticcontratligne::STATUS_CLOSED, 3, -1, 'class="nochangebackground"'), $_SERVER["PHP_SELF"], "", '', '', 'align="center" width="16"', $sortfield, $sortorder, 'maxwidthsearch ');
231 print "</tr>\n";
232
233 $contracttmp = new Contrat($db);
234
235 if ($num > 0) {
236 while ($i < min($num, $limit)) {
237 $objp = $db->fetch_object($result);
238
239 $contracttmp->id = $objp->rowid;
240 $contracttmp->ref = $objp->ref;
241 $contracttmp->ref_customer = $objp->ref_customer;
242 $contracttmp->ref_supplier = $objp->ref_supplier;
243
244 print '<tr class="oddeven">';
245 print '<td>';
246 print $contracttmp->getNomUrl(1);
247 print "</td>\n";
248 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>';
249 print "<td>".$objp->code_client."</td>\n";
250 print "<td align=\"center\">";
251 print dol_print_date($db->jdate($objp->date_contrat), 'dayhour')."</td>";
252 //print "<td align=\"right\">".price($objp->total_ht)."</td>\n";
253 //print '<td align="right">';
254 print '<td class="center">'.($objp->nb_initial > 0 ? $objp->nb_initial : '').'</td>';
255 print '<td class="center">'.($objp->nb_running + $objp->nb_late > 0 ? $objp->nb_running + $objp->nb_late : '').'</td>';
256 print '<td class="center">'.($objp->nb_closed > 0 ? $objp->nb_closed : '').'</td>';
257 //$contratstatic->LibStatut($objp->statut,5).'</td>';
258 print "</tr>\n";
259 $i++;
260 }
261 } else {
262 print '<tr><td colspan="7"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
263 }
264
265 print '</table>';
266 print '</div>';
267 print '</form>';
268 } else {
270 }
271 $db->free($result);
272 }
273} else {
275}
276
277// End of page
278llxFooter();
279$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage lines of contracts.
Class to manage generation of HTML components Only common components must be here.
Class to manage products or services.
const TYPE_SERVICE
Service.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
print_liste_field_titre($name, $file="", $field="", $begin="", $param="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
print_barre_liste($title, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $selectlimitsuffix=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
product_prepare_head($object)
Prepare array with list of tabs.
show_stats_for_company($product, $socid)
Show stats for a product.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.