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