dolibarr 25.0.0-alpha
commande.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-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
6 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2026 Vincent MAURY TimGroup <vmaury@timgroup.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30// Load Dolibarr environment
31require '../../main.inc.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
34require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
35require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
36require_once DOL_DOCUMENT_ROOT.'/core/class/html.formorder.class.php';
37
46// Load translation files required by the page
47$langs->loadLangs(array('orders', 'products', 'companies'));
48
49$id = GETPOSTINT('id');
50$ref = GETPOST('ref', 'alpha');
51
52// Security check
53$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
54$fieldtype = (!empty($ref) ? 'ref' : 'rowid');
55$socid = '';
56if (!empty($user->socid)) {
57 $socid = $user->socid;
58}
59
60// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
61$hookmanager->initHooks(array('productstatsorder'));
62
63// Load variable for pagination
64$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
65$sortfield = GETPOST('sortfield', 'aZ09comma');
66$sortorder = GETPOST('sortorder', 'aZ09comma');
67$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
68if (empty($page) || $page == -1) {
69 $page = 0;
70} // If $page is not defined, or '' or -1
71$offset = $limit * $page;
72$pageprev = $page - 1;
73$pagenext = $page + 1;
74if (!$sortorder) {
75 $sortorder = "DESC";
76}
77if (!$sortfield) {
78 $sortfield = "c.date_commande";
79}
80$search_month = GETPOSTINT('search_month');
81$search_year = GETPOSTINT('search_year');
82if (GETPOSTISARRAY('search_status')) {
83 $search_status = implode(',', GETPOST('search_status', 'array:intcomma'));
84} else {
85 $search_status = (GETPOST('search_status', 'intcomma') != '' ? GETPOST('search_status', 'intcomma') : GETPOST('statut', 'intcomma'));
86}
87$search_company = GETPOSTINT('search_company');
88if (empty($search_type)) $search_type = -1;
89
90if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
91 $search_month = '';
92 $search_year = '';
93 $search_status = '';
94 $search_company = '';
95}
96
97$result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
98
99
100/*
101 * View
102 */
103
104$orderstatic = new Commande($db);
105$societestatic = new Societe($db);
106
107$form = new Form($db);
108$formother = new FormOther($db);
109$formorder = new FormOrder($db);
110
111if ($id > 0 || !empty($ref)) {
112 $product = new Product($db);
113 $result = $product->fetch($id, $ref);
114
115 $object = $product;
116
117 $parameters = array('id' => $id);
118 $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
119 if ($reshook < 0) {
120 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
121 }
122
123 llxHeader("", "", $langs->trans("CardProduct".$product->type), '', 0, 0, '', '', 'mod-product page-stats_commande');
124
125 if ($result > 0) {
126 $head = product_prepare_head($product);
127 $titre = $langs->trans("CardProduct".$product->type);
128 $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
129 print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
130
131 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
132 print $hookmanager->resPrint;
133 if ($reshook < 0) {
134 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
135 }
136
137 $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
138
139 $shownav = 1;
140 if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
141 $shownav = 0;
142 }
143
144 dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
145
146 print '<div class="fichecenter">';
147
148 print '<div class="clearboth"></div>';
149 print '<table class="noborder tableforfield centpercent">';
150
151 $nboflines = show_stats_for_company($product, $socid);
152
153 print "</table>";
154
155 print '</div>';
156 print '<div class="clearboth"></div>';
157
158 print dol_get_fiche_end();
159
160
161 if ($user->hasRight('commande', 'lire')) {
162 $sql = "SELECT DISTINCT s.nom as name, s.rowid as socid, s.code_client, c.rowid, d.total_ht as total_ht, c.ref,";
163 $sql .= " c.ref_client,";
164 $sql .= " c.date_commande, c.fk_statut as statut, c.facture, c.rowid as commandeid, d.rowid, d.qty,";
165 $sql .= " c.date_livraison as delivery_date";
166 if (!$user->hasRight('societe', 'client', 'voir')) {
167 $sql .= ", sc.fk_soc, sc.fk_user ";
168 }
169 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
170 $sql .= ", ".MAIN_DB_PREFIX."commande as c";
171 $sql .= ", ".MAIN_DB_PREFIX."commandedet as d";
172 if (!$user->hasRight('societe', 'client', 'voir')) {
173 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
174 }
175 $sql .= " WHERE c.fk_soc = s.rowid";
176 $sql .= " AND c.entity IN (".getEntity('commande').")";
177 $sql .= " AND d.fk_commande = c.rowid";
178 $sql .= " AND d.fk_product = ".((int) $product->id);
179 if (!empty($search_month)) {
180 $sql .= " AND MONTH(c.date_commande) = ".((int) $search_month);
181 }
182 if (!empty($search_year)) {
183 $sql .= " AND YEAR(c.date_commande) = ".((int) $search_year);
184 }
185 if (!$user->hasRight('societe', 'client', 'voir')) {
186 $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
187 }
188 if ($socid) {
189 $sql .= " AND c.fk_soc = ".((int) $socid);
190 } elseif ($search_company > 0) {
191 $sql .= " AND c.fk_soc = ".((int) $search_company);
192 }
193
194 if ($search_status != '') {
195 $sql .= " AND c.fk_statut IN (".$db->sanitize($search_status).")";
196 }
197
198 // Add where from hooks
199 $parameters = array('socid' => $socid, 'type_element' => 'order');
200 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object); // Note that $action and $object may have been modified by hook
201 $sql .= $hookmanager->resPrint;
202 $sql .= $db->order($sortfield, $sortorder);
203
204 //Calcul total qty and amount for global if full scan list
205 $total_ht = 0;
206 $total_qty = 0;
207
208 // Count total nb of records
209 $totalofrecords = '';
210 if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
211 $result = $db->query($sql);
212 $totalofrecords = $db->num_rows($result);
213 }
214
215 $sql .= $db->plimit($limit + 1, $offset);
216
217 $result = $db->query($sql);
218 if ($result) {
219 $num = $db->num_rows($result);
220
221 $option = '&id='.$product->id;
222
223 if ($limit > 0 && $limit != $conf->liste_limit) {
224 $option .= '&limit='.((int) $limit);
225 }
226 if (!empty($search_month)) {
227 $option .= '&search_month='.urlencode((string) ($search_month));
228 }
229 if (!empty($search_year)) {
230 $option .= '&search_year='.urlencode((string) ($search_year));
231 }
232
233 if ($search_status != '') {
234 $option .= '&search_status='.urlencode($search_status);
235 }
236
237 print '<span id="anchorundermenu" class="anchorundermenu"></span>';
238 print '<form method="post" action="'.$_SERVER ['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
239 print '<input type="hidden" name="token" value="'.newToken().'">';
240 print '<input type="hidden" name="page_y" value="">';
241 if (!empty($sortfield)) {
242 print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
243 }
244 if (!empty($sortorder)) {
245 print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
246 }
247
248 // @phan-suppress-next-line PhanPluginSuspiciousParamOrder
249 print_barre_liste($langs->trans("CustomersOrders"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
250
251 if (!empty($page)) {
252 $option .= '&page='.urlencode((string) ($page));
253 }
254
255 print '<div class="liste_titre liste_titre_bydiv centpercent">';
256 print '<div class="divsearchfield">';
257 print $langs->trans('Period').' ('.$langs->trans("OrderDate").') - ';
258 print $langs->trans('Month').':<input class="flat" type="text" size="4" name="search_month" value="'.($search_month > 0 ? $search_month : '').'"> ';
259 print $langs->trans('Year').':'.$formother->selectyear(($search_year ? (string) $search_year : '-1'), 'search_year', 1, 20, 5);
260 print $langs->trans('Status');
261 $formorder->selectOrderStatus($search_status, 1, 'search_status');
262 if (empty($socid)) print $langs->trans('Company').' : '.$form->select_company($search_company, 'search_company', '(s.client:in:1,2,3)', 1);
263
264 print '<div style="vertical-align: middle; display: inline-block">';
265 print '<input type="image" class="liste_titre reposition" name="button_search" src="'.img_picto($langs->trans("Search"), 'search.png', '', 0, 1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
266 print '<input type="image" class="liste_titre reposition" name="button_removefilter" src="'.img_picto($langs->trans("Search"), 'searchclear.png', '', 0, 1).'" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
267 print '</div>';
268 print '</div>';
269 print '</div>';
270
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"], "c.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("OrderDate", $_SERVER["PHP_SELF"], "c.date_commande", "", $option, '', $sortfield, $sortorder, 'center ');
279 print_liste_field_titre('DateDeliveryPlanned', $_SERVER['PHP_SELF'], 'c.date_livraison', '', $option, '', $sortfield, $sortorder, 'center ');
280 print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $option, '', $sortfield, $sortorder, 'center ');
281 print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], "c.total_ht", "", $option, '', $sortfield, $sortorder, 'right ');
282 print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "c.fk_statut", "", $option, '', $sortfield, $sortorder, 'right ');
283 print "</tr>\n";
284
285 if ($num > 0) {
286 while ($i < min($num, $limit)) {
287 $objp = $db->fetch_object($result);
288
289 $total_ht += $objp->total_ht;
290 $total_qty += $objp->qty;
291
292 $orderstatic->id = $objp->commandeid;
293 $orderstatic->ref = $objp->ref;
294 $orderstatic->ref_client = $objp->ref_client;
295 $societestatic->fetch($objp->socid);
296
297 print '<tr class="oddeven">';
298 print '<td>';
299 print $orderstatic->getNomUrl(1);
300 print "</td>\n";
301 print '<td>'.$societestatic->getNomUrl(1).'</td>';
302 print "<td>".$objp->code_client."</td>\n";
303 print '<td class="center">';
304 print dol_print_date($db->jdate($objp->date_commande), 'dayhour')."</td>";
305 // delivery planned date
306 print '<td class="center">';
307 print dol_print_date($db->jdate($objp->delivery_date), 'dayhour');
308 print '</td>';
309 print '<td class="center">'.$objp->qty."</td>\n";
310 print '<td align="right">'.price($objp->total_ht)."</td>\n";
311 print '<td align="right">'.$orderstatic->LibStatut($objp->statut, $objp->facture, 5).'</td>';
312 print "</tr>\n";
313 $i++;
314 }
315 }
316 print '<tr class="liste_total">';
317 if ($num < $limit && empty($offset)) {
318 print '<td>'.$langs->trans("Total").'</td>';
319 } else {
320 print '<td>'.$form->textwithpicto($langs->trans("Total"), $langs->trans("Totalforthispage")).'</td>';
321 }
322 print '<td colspan="3"></td>';
323 // delivery planned date
324 print '<td></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
341// End of page
342llxFooter();
343$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 customers orders.
Class to manage generation of HTML components Only common components must be here.
Class to manage HTML output components for orders Before adding component here, check they are not in...
Class to help generate other html components Only common components are here.
Class to manage products or services.
const TYPE_SERVICE
Service.
Class to manage third parties objects (customers, suppliers, prospects...)
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
GETPOSTISARRAY($paramname, $method=0)
Return true if the parameter $paramname is submit from a POST OR GET as an array.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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).
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
print_liste_field_titre($name, $file="", $field="", $begin="", $param="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
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.
Definition html.lib.php:540
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:738
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:181
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, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.