1<?php
2/* Copyright (C) 2015       ATM Consulting          <support@atm-consulting.fr>
3 * Copyright (C) 2019-2020  Open-DSI                <support@open-dsi.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 *  \file       htdocs/intracommreport/list.php
21 *  \ingroup    Intracomm Report
22 *  \brief      Page to list intracomm report
23 */
24
25require '../main.inc.php';
26require_once DOL_DOCUMENT_ROOT.'/intracommreport/class/intracommreport.class.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
29
30// Load translation files required by the page
31$langs->loadLangs(array('intracommreport'));
32
33$action = GETPOST('action', 'alpha');
34$massaction = GETPOST('massaction', 'alpha');
35$show_files = GETPOST('show_files', 'int');
36$confirm = GETPOST('confirm', 'alpha');
37$toselect = GETPOST('toselect', 'array');
38
39$sall = trim((GETPOST('search_all', 'alphanohtml') != '') ?GETPOST('search_all', 'alphanohtml') : GETPOST('sall', 'alphanohtml'));
40$search_ref = GETPOST("search_ref", 'alpha');
41$search_type = GETPOST("search_type", 'int');
42$optioncss = GETPOST('optioncss', 'alpha');
43$type = GETPOST("type", "int");
44
45$diroutputmassaction = $conf->product->dir_output.'/temp/massgeneration/'.$user->id;
46
47$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
48$sortfield = GETPOST("sortfield", 'alpha');
49$sortorder = GETPOST("sortorder", 'alpha');
50$page = (GETPOST("page", 'int') ?GETPOST("page", 'int') : 0);
51if (empty($page) || $page == -1) {
52	$page = 0;
53}     // If $page is not defined, or '' or -1
54$offset = $limit * $page;
55$pageprev = $page - 1;
56$pagenext = $page + 1;
57if (!$sortfield) {
58	$sortfield = "i.ref";
59}
60if (!$sortorder) {
61	$sortorder = "ASC";
62}
63
64// Initialize context for list
65$contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'intracommreportlist';
66if ((string) $type == '1') {
67	$contextpage = 'DESlist'; if ($search_type == '') {
68		$search_type = '1';
69	}
70}
71if ((string) $type == '0') {
72	$contextpage = 'DEBlist'; if ($search_type == '') {
73		$search_type = '0';
74	}
75}
76
77// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
78$object = new IntracommReport($db);
79$hookmanager->initHooks(array('intracommreportlist'));
80$extrafields = new ExtraFields($db);
81$form = new Form($db);
82
83/*
84// fetch optionals attributes and labels
85$extralabels = $extrafields->fetch_name_optionals_label('intracommreport');
86$search_array_options=$extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
87*/
88
89if (empty($action)) {
90	$action = 'list';
91}
92
93// Get object canvas (By default, this is not defined, so standard usage of dolibarr)
94$canvas = GETPOST("canvas");
95$objcanvas = null;
96if (!empty($canvas)) {
97	require_once DOL_DOCUMENT_ROOT.'/core/class/canvas.class.php';
98	$objcanvas = new Canvas($db, $action);
99	$objcanvas->getCanvas('product', 'list', $canvas);
100}
101
102// Security check
103/*
104if ($search_type=='0') $result=restrictedArea($user, 'produit', '', '', '', '', '', $objcanvas);
105elseif ($search_type=='1') $result=restrictedArea($user, 'service', '', '', '', '', '', $objcanvas);
106else $result=restrictedArea($user, 'produit|service', '', '', '', '', '', $objcanvas);
107*/
108
109// List of fields to search into when doing a "search in all"
110$fieldstosearchall = array(
111	'i.ref'=>"Ref",
112	'pfi.ref_fourn'=>"RefSupplier",
113	'i.label'=>"ProductLabel",
114	'i.description'=>"Description",
115	"i.note"=>"Note",
116);
117
118$isInEEC = isInEEC($mysoc);
119
120// Definition of fields for lists
121$arrayfields = array(
122	'i.ref' => array('label'=>$langs->trans("Ref"), 'checked'=>1),
123	'i.label' => array('label'=>$langs->trans("Label"), 'checked'=>1),
124	'i.fk_product_type'=>array('label'=>$langs->trans("Type"), 'checked'=>0, 'enabled'=>(!empty($conf->produit->enabled) && !empty($conf->service->enabled))),
125);
126/*
127// Extra fields
128if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label']))
129{
130	foreach($extrafields->attributes[$object->table_element]['label'] as $key => $val)
131	{
132		if (! empty($extrafields->attributes[$object->table_element]['list'][$key]))
133			$arrayfields["ef.".$key]=array('label'=>$extrafields->attributes[$object->table_element]['label'][$key], 'checked'=>(($extrafields->attributes[$object->table_element]['list'][$key]<0)?0:1), 'position'=>$extrafields->attributes[$object->table_element]['pos'][$key], 'enabled'=>(abs((int) $extrafields->attributes[$object->table_element]['list'][$key])!=3 && $extrafields->attributes[$object->table_element]['perms'][$key]));
134	}
135}
136*/
137$object->fields = dol_sort_array($object->fields, 'position');
138$arrayfields = dol_sort_array($arrayfields, 'position');
139
140// Security check
141if ($search_type == '0') {
142	$result = restrictedArea($user, 'produit', '', '', '', '', '', 0);
143} elseif ($search_type == '1') {
144	$result = restrictedArea($user, 'service', '', '', '', '', '', 0);
145} else {
146	$result = restrictedArea($user, 'produit|service', '', '', '', '', '', 0);
147}
148
149
150/*
151 * Actions
152 */
153
154if (GETPOST('cancel', 'alpha')) {
155	$action = 'list'; $massaction = '';
156}
157if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
158	$massaction = '';
159}
160
161$parameters = array();
162$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
163if ($reshook < 0) {
164	setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
165}
166
167if (empty($reshook)) {
168	// Selection of new fields
169	include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
170
171	// Purge search criteria
172	if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
173		$sall = "";
174		$search_ref = "";
175		$search_label = "";
176		//$search_type='';						// There is 2 types of list: a list of product and a list of services. No list with both. So when we clear search criteria, we must keep the filter on type.
177
178		$show_childproducts = '';
179		$search_array_options = array();
180	}
181
182	// Mass actions
183	$objectclass = 'Product';
184	if ((string) $search_type == '1') {
185		$objectlabel = 'Services';
186	}
187	if ((string) $search_type == '0') {
188		$objectlabel = 'Products';
189	}
190
191	$permtoread = $user->rights->produit->lire;
192	$permtodelete = $user->rights->produit->supprimer;
193	$uploaddir = $conf->product->dir_output;
194	include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
195}
196
197
198/*
199 * View
200 */
201
202$formother = new FormOther($db);
203
204$title = $langs->trans('IntracommReportList'.$type);
205
206$sql = 'SELECT DISTINCT i.rowid, i.type_declaration, i.type_export, i.periods, i.mode, i.entity';
207/*
208// Add fields from extrafields
209if (! empty($extrafields->attributes[$object->table_element]['label'])) {
210	foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) $sql.=($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? ", ef.".$key.' as options_'.$key : '');
211}
212*/
213// Add fields from hooks
214$parameters = array();
215$reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook
216$sql .= $hookmanager->resPrint;
217
218$sql .= ' FROM '.MAIN_DB_PREFIX.'intracommreport as i';
219
220// if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."intracommreport_extrafields as ef on (i.rowid = ef.fk_object)";
221
222$sql .= ' WHERE i.entity IN ('.getEntity('intracommreport').')';
223
224if ($sall) {
225	$sql .= natural_search(array_keys($fieldstosearchall), $sall);
226}
227// if the type is not 1, we show all products (type = 0,2,3)
228if (dol_strlen($search_type) && $search_type != '-1') {
229	if ($search_type == 1) {
230		$sql .= " AND i.type = 1";
231	} else {
232		$sql .= " AND i.type = 0";
233	}
234}
235
236/*
237if ($search_ref)     $sql .= natural_search('i.ref', $search_ref);
238if ($search_label)   $sql .= natural_search('i.label', $search_label);
239if ($search_barcode) $sql .= natural_search('i.barcode', $search_barcode);
240if (isset($search_tosell) && dol_strlen($search_tosell) > 0  && $search_tosell!=-1) $sql.= " AND i.tosell = ".((int) $search_tosell);
241if (isset($search_tobuy) && dol_strlen($search_tobuy) > 0  && $search_tobuy!=-1)   $sql.= " AND i.tobuy = ".((int) $search_tobuy);
242if (dol_strlen($canvas) > 0)                    $sql.= " AND i.canvas = '".$db->escape($canvas)."'";
243*/
244
245/*
246// Add where from extra fields
247include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
248*/
249// Add where from hooks
250$parameters = array();
251$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
252$sql .= $hookmanager->resPrint;
253
254$sql .= " GROUP BY i.rowid";
255
256/*
257// Add fields from extrafields
258if (! empty($extrafields->attributes[$object->table_element]['label'])) {
259	foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) $sql.=($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? ", ef.".$key : '');
260}
261*/
262
263// Add fields from hooks
264$parameters = array();
265$reshook = $hookmanager->executeHooks('printFieldSelect', $parameters); // Note that $action and $object may have been modified by hook
266$sql .= $hookmanager->resPrint;
267
268$sql .= $db->order($sortfield, $sortorder);
269
270$nbtotalofrecords = '';
271if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
272	$result = $db->query($sql);
273	$nbtotalofrecords = $db->num_rows($result);
274	if (($page * $limit) > $nbtotalofrecords) {	// if total resultset is smaller then paging size (filtering), goto and load page 0
275		$page = 0;
276		$offset = 0;
277	}
278}
279
280$sql .= $db->plimit($limit + 1, $offset);
281
282$resql = $db->query($sql);
283
284if ($resql) {
285	$num = $db->num_rows($resql);
286
287	$arrayofselected = is_array($toselect) ? $toselect : array();
288
289	$helpurl = 'EN:Module_IntracommReport|FR:Module_ProDouane';
290	llxHeader('', $title, $helpurl, '');
291
292	// Displays product removal confirmation
293	if (GETPOST('delreport')) {
294		setEventMessages($langs->trans("IntracommReportDeleted", GETPOST('delreport')), null, 'mesgs');
295	}
296
297	$param = '';
298	if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
299		$param .= '&contextpage='.urlencode($contextpage);
300	}
301	if ($limit > 0 && $limit != $conf->liste_limit) {
302		$param .= '&limit='.urlencode($limit);
303	}
304	if ($sall) {
305		$param .= "&sall=".urlencode($sall);
306	}
307	if ($search_ref) {
308		$param = "&search_ref=".urlencode($search_ref);
309	}
310	if ($search_label) {
311		$param .= "&search_label=".urlencode($search_label);
312	}
313
314	// Add $param from extra fields
315	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php';
316
317	// List of mass actions available
318	$arrayofmassactions = array(
319		'generate_doc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("ReGeneratePDF"),
320		//'builddoc'=>$langs->trans("PDFMerge"),
321		//'presend'=>$langs->trans("SendByMail"),
322	);
323	if ($user->rights->intracommreport->delete) {
324		$arrayofmassactions['predelete'] = img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete");
325	}
326	if (in_array($massaction, array('presend', 'predelete'))) {
327		$arrayofmassactions = array();
328	}
329	$massactionbutton = $form->selectMassAction('', $arrayofmassactions);
330
331	$newcardbutton = '';
332	if ($user->rights->intracommreport->write) {
333		$newcardbutton .= dolGetButtonTitle($langs->trans("NewDeclaration"), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/intracommreport/card.php?action=create&amp;type='.$type);
334	}
335
336	print '<form action="'.$_SERVER["PHP_SELF"].'" method="post" name="formulaire">';
337	if ($optioncss != '') {
338		print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
339	}
340	print '<input type="hidden" name="token" value="'.newToken().'">';
341	print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
342	print '<input type="hidden" name="action" value="list">';
343	print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
344	print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
345	print '<input type="hidden" name="page" value="'.$page.'">';
346	print '<input type="hidden" name="type" value="'.$type.'">';
347	if (empty($arrayfields['i.fk_product_type']['checked'])) {
348		print '<input type="hidden" name="search_type" value="'.dol_escape_htmltag($search_type).'">';
349	}
350
351	print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'title_products.png', 0, $newcardbutton, '', $limit);
352
353	$topicmail = "Information";
354	$modelmail = "product";
355	$objecttmp = new IntracommReport($db);
356	$trackid = 'prod'.$object->id;
357	include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
358
359	if ($sall) {
360		foreach ($fieldstosearchall as $key => $val) {
361			$fieldstosearchall[$key] = $langs->trans($val);
362		}
363		print '<div class="divsearchfieldfilter">'.$langs->trans("FilterOnInto", $sall).join(', ', $fieldstosearchall).'</div>';
364	}
365
366	$parameters = array();
367	$reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
368	if (empty($reshook)) {
369		$moreforfilter .= $hookmanager->resPrint;
370	} else {
371		$moreforfilter = $hookmanager->resPrint;
372	}
373
374	if ($moreforfilter) {
375		print '<div class="liste_titre liste_titre_bydiv centpercent">';
376		print $moreforfilter;
377		print '</div>';
378	}
379
380	$varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
381	$selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
382	if ($massactionbutton) {
383		$selectedfields .= $form->showCheckAddButtons('checkforselect', 1);
384	}
385
386	print '<div class="div-table-responsive">';
387	print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
388
389	// Lines with input filters
390	print '<tr class="liste_titre_filter">';
391	if (!empty($arrayfields['i.ref']['checked'])) {
392		print '<td class="liste_titre left">';
393		print '<input class="flat" type="text" name="search_ref" size="8" value="'.dol_escape_htmltag($search_ref).'">';
394		print '</td>';
395	}
396	if (!empty($arrayfields['i.label']['checked'])) {
397		print '<td class="liste_titre left">';
398		print '<input class="flat" type="text" name="search_label" size="12" value="'.dol_escape_htmltag($search_label).'">';
399		print '</td>';
400	}
401	// Type
402	// Type (customer/prospect/supplier)
403	if (!empty($arrayfields['customerorsupplier']['checked'])) {
404		print '<td class="liste_titre maxwidthonsmartphone center">';
405		if ($type != '') {
406			print '<input type="hidden" name="type" value="'.$type.'">';
407		}
408		print $formcompany->selectProspectCustomerType($search_type, 'search_type', 'search_type', 'list');
409		print '</select></td>';
410	}
411
412	if (!empty($arrayfields['i.fk_product_type']['checked'])) {
413		print '<td class="liste_titre left">';
414		$array = array('-1'=>'&nbsp;', '0'=>$langs->trans('Product'), '1'=>$langs->trans('Service'));
415		print $form->selectarray('search_type', $array, $search_type);
416		print '</td>';
417	}
418
419	/*
420	// Extra fields
421	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_input.tpl.php';
422	*/
423	// Fields from hook
424	$parameters = array('arrayfields'=>$arrayfields);
425	$reshook = $hookmanager->executeHooks('printFieldListOption', $parameters); // Note that $action and $object may have been modified by hook
426	print $hookmanager->resPrint;
427	// Date creation
428	if (!empty($arrayfields['i.datec']['checked'])) {
429		print '<td class="liste_titre">';
430		print '</td>';
431	}
432	// Date modification
433	if (!empty($arrayfields['i.tms']['checked'])) {
434		print '<td class="liste_titre">';
435		print '</td>';
436	}
437	print '<td class="liste_titre center maxwidthsearch">';
438	$searchpicto = $form->showFilterButtons();
439	print $searchpicto;
440	print '</td>';
441
442	print '</tr>';
443
444	print '<tr class="liste_titre">';
445	if (!empty($arrayfields['i.ref']['checked'])) {
446		print_liste_field_titre($arrayfields['i.ref']['label'], $_SERVER["PHP_SELF"], "i.ref", "", $param, "", $sortfield, $sortorder);
447	}
448	if (!empty($arrayfields['i.label']['checked'])) {
449		print_liste_field_titre($arrayfields['i.label']['label'], $_SERVER["PHP_SELF"], "i.label", "", $param, "", $sortfield, $sortorder);
450	}
451	if (!empty($arrayfields['i.fk_product_type']['checked'])) {
452		print_liste_field_titre($arrayfields['i.fk_product_type']['label'], $_SERVER["PHP_SELF"], "i.fk_product_type", "", $param, "", $sortfield, $sortorder);
453	}
454
455	/*
456	// Extra fields
457	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_title.tpl.php';
458	*/
459	// Hook fields
460	$parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
461	$reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters); // Note that $action and $object may have been modified by hook
462	print $hookmanager->resPrint;
463	if (!empty($arrayfields['i.datec']['checked'])) {
464		print_liste_field_titre($arrayfields['i.datec']['label'], $_SERVER["PHP_SELF"], "i.datec", "", $param, '', $sortfield, $sortorder, 'center nowrap ');
465	}
466	if (!empty($arrayfields['i.tms']['checked'])) {
467		print_liste_field_titre($arrayfields['i.tms']['label'], $_SERVER["PHP_SELF"], "i.tms", "", $param, '', $sortfield, $sortorder, 'center nowrap ');
468	}
469
470	print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
471	print "</tr>\n";
472
473
474	$intracommreport_static = new IntracommReport($db);
475
476	$i = 0;
477	$totalarray = array();
478	while ($i < min($num, $limit)) {
479		$obj = $db->fetch_object($resql);
480
481		$intracommreport_static->id = $obj->rowid;
482		$intracommreport_static->ref = $obj->ref;
483		$intracommreport_static->ref_fourn = $obj->ref_supplier;
484		$intracommreport_static->label = $obj->label;
485		$intracommreport_static->type = $obj->fk_product_type;
486		$intracommreport_static->status_buy = $obj->tobuy;
487		$intracommreport_static->status     = $obj->tosell;
488		$intracommreport_static->status_batch = $obj->tobatch;
489		$intracommreport_static->entity = $obj->entity;
490
491		print '<tr class="oddeven">';
492
493		// Ref
494		if (!empty($arrayfields['i.ref']['checked'])) {
495			print '<td class="tdoverflowmax200">';
496			print $intracommreport_static->getNomUrl(1);
497			print "</td>\n";
498			if (!$i) {
499				$totalarray['nbfield']++;
500			}
501		}
502		// Label
503		if (!empty($arrayfields['i.label']['checked'])) {
504			print '<td class="tdoverflowmax200">'.dol_trunc($obj->label, 80).'</td>';
505			if (!$i) {
506				$totalarray['nbfield']++;
507			}
508		}
509		// Type
510		if (!empty($arrayfields['i.fk_product_type']['checked'])) {
511			print '<td>'.$obj->fk_product_type.'</td>';
512			if (!$i) {
513				$totalarray['nbfield']++;
514			}
515		}
516		// Action
517		print '<td class="nowrap center">';
518		if ($massactionbutton || $massaction) {   // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
519			$selected = 0;
520			if (in_array($obj->rowid, $arrayofselected)) {
521				$selected = 1;
522			}
523			print '<input id="cb'.$obj->rowid.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->rowid.'"'.($selected ? ' checked="checked"' : '').'>';
524		}
525		print '</td>';
526		if (!$i) {
527			$totalarray['nbfield']++;
528		}
529
530		print "</tr>\n";
531		$i++;
532	}
533
534	$db->free($resql);
535
536	print "</table>";
537	print "</div>";
538	print '</form>';
539} else {
540	dol_print_error($db);
541}
542
543// End of page
544llxFooter();
545$db->close();
546