1<?php
2/* Copyright (C) 2002-2003  Rodolphe Quiedeville    <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2015  Laurent Destailleur     <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012  Regis Houssin           <regis.houssin@inodbox.com>
5 * Copyright (C) 2013       Florian Henry           <florian.henry@open-concept.pro>
6 * Copyright (C) 2013       Juanjo Menent           <jmenent@2byte.es>
7 * Copyright (C) 2015       Jean-François Ferry     <jfefe@aternatik.fr>
8 * Copyright (C) 2012       Cedric Salvador         <csalvador@gpcsolutions.fr>
9 * Copyright (C) 2015       Alexandre Spangaro      <aspangaro@open-dsi.fr>
10 * Copyright (C) 2016-2018  Charlie Benke           <charlie@patas-monkey.com>
11 * Copyright (C) 2018-2019  Frédéric France         <frederic.france@netlogic.fr>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
27/**
28 *  \file        fichinter/card-rec.php
29 *  \ingroup     intervention
30 *  \brief       Page to show predefined fichinter
31 */
32
33require '../main.inc.php';
34require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
35require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinterrec.class.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/fichinter.lib.php';
37
38require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
39require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
40if (!empty($conf->projet->enabled)) {
41	require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
42	require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
43}
44if (!empty($conf->contrat->enabled)) {
45	require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
46	require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcontract.class.php';
47}
48
49// Load translation files required by the page
50$langs->loadLangs(array("interventions", "admin", "compta", "bills"));
51
52// Security check
53$id = (GETPOST('fichinterid', 'int') ?GETPOST('fichinterid', 'int') : GETPOST('id', 'int'));
54$action = GETPOST('action', 'aZ09');
55if ($user->socid) $socid = $user->socid;
56$objecttype = 'fichinter_rec';
57if ($action == "create" || $action == "add") $objecttype = '';
58$result = restrictedArea($user, 'ficheinter', $id, $objecttype);
59
60if ($page == -1)
61	$page = 0;
62
63$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
64$offset = $limit * $page;
65
66if ($sortorder == "")
67	$sortorder = "DESC";
68
69if ($sortfield == "")
70	$sortfield = "f.datec";
71
72$object = new FichinterRec($db);
73$extrafields = new ExtraFields($db);
74
75
76$arrayfields = array(
77	'f.titre'=>array('label'=>$langs->trans("Ref"), 'checked'=>1),
78	's.nom'=>array('label'=>$langs->trans("ThirdParty"), 'checked'=>1),
79	'f.fk_contrat'=>array('label'=>$langs->trans("Contract"), 'checked'=>1),
80	'f.duree'=>array('label'=>$langs->trans("Duration"), 'checked'=>1),
81	'f.total_ttc'=>array('label'=>$langs->trans("AmountTTC"), 'checked'=>1),
82	'f.frequency'=>array('label'=>$langs->trans("RecurringInvoiceTemplate"), 'checked'=>1),
83	'f.nb_gen_done'=>array('label'=>$langs->trans("NbOfGenerationDone"), 'checked'=>1),
84	'f.date_last_gen'=>array('label'=>$langs->trans("DateLastGeneration"), 'checked'=>1),
85	'f.date_when'=>array('label'=>$langs->trans("NextDateToExecution"), 'checked'=>1),
86	'f.datec'=>array('label'=>$langs->trans("DateCreation"), 'checked'=>0, 'position'=>500),
87	'f.tms'=>array('label'=>$langs->trans("DateModificationShort"), 'checked'=>0, 'position'=>500),
88);
89
90
91/*
92 * Actions
93 */
94
95
96// Create predefined intervention
97if ($action == 'add') {
98	if (!GETPOST('titre')) {
99		setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("Title")), null, 'errors');
100		$action = "create";
101		$error++;
102	}
103
104	if (!GETPOST('socid')) {
105		setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("Customer")), null, 'errors');
106		$action = "create";
107		$error++;
108	}
109
110	// gestion des fréquences et des échéances
111	$frequency = GETPOST('frequency', 'int');
112	$reyear = GETPOST('reyear');
113	$remonth = GETPOST('remonth');
114	$reday = GETPOST('reday');
115	$rehour = GETPOST('rehour');
116	$remin = GETPOST('remin');
117	$nb_gen_max = (GETPOST('nb_gen_max', 'int') ?GETPOST('nb_gen_max', 'int') : 0);
118	if (GETPOST('frequency')) {
119		if (empty($reyear) || empty($remonth) || empty($reday)) {
120			setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("Date")), null, 'errors');
121			$action = "create";
122			$error++;
123		}
124		if ($nb_gen_max === '') {
125			setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("MaxPeriodNumber")), null, 'errors');
126			$action = "create";
127			$error++;
128		}
129	}
130
131	if (!$error) {
132		$object->id_origin = $id;
133		$object->title			= GETPOST('titre', 'alpha');
134		$object->description	= GETPOST('description', 'restricthtml');
135		$object->socid			= GETPOST('socid', 'alpha');
136		$object->fk_project		= GETPOST('projectid', 'int');
137		$object->fk_contract	= GETPOST('contractid', 'int');
138
139		$object->frequency = $frequency;
140		$object->unit_frequency = GETPOST('unit_frequency', 'alpha');
141		$object->nb_gen_max = $nb_gen_max;
142		$object->auto_validate = GETPOST('auto_validate', 'int');
143
144		$date_next_execution = dol_mktime($rehour, $remin, 0, $remonth, $reday, $reyear);
145		$object->date_when = $date_next_execution;
146
147		if ($object->create($user) > 0) {
148			$id = $object->id;
149			$action = '';
150		} else {
151			setEventMessages($object->error, $object->errors, 'errors');
152			$action = "create";
153		}
154	}
155} elseif ($action == 'createfrommodel') {
156	$newinter = new Fichinter($db);
157
158	// on récupère les enregistrements
159	$object->fetch($id);
160
161	// on transfert les données de l'un vers l'autre
162	if ($object->socid > 0) {
163		$newinter->socid = $object->socid;
164		$newinter->fk_project = $object->fk_project;
165		$newinter->fk_contrat = $object->fk_contrat;
166	} else {
167		$newinter->socid = GETPOST("socid");
168	}
169
170	$newinter->entity = $object->entity;
171	$newinter->duree = $object->duree;
172
173	$newinter->description = $object->description;
174	$newinter->note_private = $object->note_private;
175	$newinter->note_public = $object->note_public;
176
177	// on créer un nouvelle intervention
178	$extrafields->fetch_name_optionals_label($newinter->table_element);
179
180	$array_options = $extrafields->getOptionalsFromPost($newinter->table_element);
181	$newinter->array_options = $array_options;
182
183	$newfichinterid = $newinter->create($user);
184
185	if ($newfichinterid > 0) {
186		// Now we add line of details
187		foreach ($object->lines as $line) {
188			$newinter->addline($user, $newfichinterid, $line->desc, '', $line->duree, '');
189		}
190
191		// on update le nombre d'inter crée à partir du modèle
192		$object->updateNbGenDone();
193		//on redirige vers la fiche d'intervention nouvellement crée
194		header('Location: '.DOL_URL_ROOT.'/fichinter/card.php?id='.$newfichinterid);
195		exit;
196	} else {
197		setEventMessages($newinter->error, $newinter->errors, 'errors');
198		$action = '';
199	}
200} elseif ($action == 'delete' && $user->rights->ficheinter->supprimer) {
201	// delete modele
202	$object->fetch($id);
203	$object->delete();
204	$id = 0;
205	header('Location: '.$_SERVER["PHP_SELF"]);
206	exit;
207} elseif ($action == 'setfrequency' && $user->rights->ficheinter->creer) {
208	// Set frequency and unit frequency
209	$object->fetch($id);
210	$object->setFrequencyAndUnit(GETPOST('frequency', 'int'), GETPOST('unit_frequency', 'alpha'));
211} elseif ($action == 'setdate_when' && $user->rights->ficheinter->creer) {
212	// Set next date of execution
213	$object->fetch($id);
214	$date = dol_mktime(GETPOST('date_whenhour'), GETPOST('date_whenmin'), 0, GETPOST('date_whenmonth'), GETPOST('date_whenday'), GETPOST('date_whenyear'));
215	if (!empty($date)) $object->setNextDate($date);
216} elseif ($action == 'setnb_gen_max' && $user->rights->ficheinter->creer) {
217	// Set max period
218	$object->fetch($id);
219	$object->setMaxPeriod(GETPOST('nb_gen_max', 'int'));
220}
221
222
223/*
224 *	View
225 */
226
227llxHeader('', $langs->trans("RepeatableIntervention"), 'ch-fichinter.html#s-fac-fichinter-rec');
228
229$form = new Form($db);
230$companystatic = new Societe($db);
231if (!empty($conf->contrat->enabled)) {
232	$contratstatic = new Contrat($db);
233}
234if (!empty($conf->projet->enabled)) {
235	$projectstatic = new Project($db);
236}
237
238$now = dol_now();
239$tmparray = dol_getdate($now);
240$today = dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year']); // Today is last second of current day
241
242
243
244/*
245 * Create mode
246 */
247if ($action == 'create') {
248	print load_fiche_titre($langs->trans("CreateRepeatableIntervention"), '', 'intervention');
249
250	$object = new Fichinter($db); // Source invoice
251	//$object = new Managementfichinter($db);   // Source invoice
252
253	if ($object->fetch($id, $ref) > 0) {
254		print '<form action="card-rec.php" method="post">';
255		print '<input type="hidden" name="token" value="'.newToken().'">';
256		print '<input type="hidden" name="action" value="add">';
257		print '<input type="hidden" name="fichinterid" value="'.$object->id.'">';
258
259		print dol_get_fiche_head();
260
261		$rowspan = 4;
262		if (!empty($conf->projet->enabled) && $object->fk_project > 0) $rowspan++;
263		if (!empty($conf->contrat->enabled) && $object->fk_contrat > 0) $rowspan++;
264
265		print '<table class="border centpercent">';
266
267		$object->fetch_thirdparty();
268
269		// Third party
270		print '<tr><td>'.$langs->trans("Customer").'</td><td>';
271		print $form->select_company($object->thirdparty->id, 'socid', '', 0, 1);
272
273		//.$object->thirdparty->getNomUrl(1,'customer').
274		print '</td><td>';
275		print $langs->trans("Comment");
276		print '</td></tr>';
277
278		// Title
279		print '<tr><td class="fieldrequired">'.$langs->trans("Title").'</td><td>';
280		print '<input class="flat quatrevingtpercent" type="text" name="titre" value="'.dol_escape_htmltag(GETPOST("titre", "alphanohtml")).'">';
281		print '</td>';
282
283		// Note
284		print '<td rowspan="'.$rowspan.'" valign="top">';
285		print '<textarea class="flat" name="description" wrap="soft" cols="60" rows="'.ROWS_4.'">';
286		print $object->description.'</textarea>';
287		print '</td></tr>';
288
289		// Author
290		print "<tr><td>".$langs->trans("Author")."</td><td>".$user->getFullName($langs)."</td></tr>";
291
292		if (empty($conf->global->FICHINTER_DISABLE_DETAILS)) {
293			// Duration
294			print '<tr><td>'.$langs->trans("TotalDuration").'</td>';
295			print '<td colspan="3">'.convertSecondToTime($object->duration, 'all', $conf->global->MAIN_DURATION_OF_WORKDAY).'</td>';
296			print '</tr>';
297		}
298
299		// Project
300		if (!empty($conf->projet->enabled)) {
301			$formproject = new FormProjets($db);
302			print "<tr><td>".$langs->trans("Project")."</td><td>";
303			$projectid = GETPOST('projectid') ?GETPOST('projectid') : $object->fk_project;
304
305			$numprojet = $formproject->select_projects($object->thirdparty->id, $projectid, 'projectid', 0, 0, 1, 0, 0, 0, 0, '', 0, 0, '');
306			print ' &nbsp; <a href="'.DOL_URL_ROOT.'/projet/card.php?socid='.$object->thirdparty->id;
307			print '&action=create&status=1&backtopage='.urlencode($_SERVER["PHP_SELF"]).'?action=create';
308			print '&socid='.$object->thirdparty->id.(!empty($id) ? '&id='.$id : '').'">';
309			print $langs->trans("AddProject").'</a>';
310			print "</td></tr>";
311		}
312
313		// Contrat
314		if (!empty($conf->contrat->enabled)) {
315			$formcontract = new FormContract($db);
316			print "<tr><td>".$langs->trans("Contract")."</td><td>";
317			$contractid = GETPOST('contractid') ?GETPOST('contractid') : $object->fk_contract;
318			$numcontract = $formcontract->select_contract($object->thirdparty->id, $contractid, 'contracttid');
319			print "</td></tr>";
320		}
321		print "</table>";
322
323		print '<br><br>';
324
325		/// frequency & duration
326		// Autogeneration
327		$title = $langs->trans("Recurrence");
328		print load_fiche_titre($title, '', 'calendar');
329
330		print '<table class="border centpercent">';
331
332		// Frequency
333		print '<tr><td class="titlefieldcreate">';
334		print $form->textwithpicto($langs->trans("Frequency"), $langs->transnoentitiesnoconv('toolTipFrequency'));
335		print "</td><td>";
336		print '<input type="text" name="frequency" value="'.GETPOST('frequency', 'int').'" size="4">&nbsp;';
337		print $form->selectarray('unit_frequency', array('d'=>$langs->trans('Day'), 'm'=>$langs->trans('Month'), 'y'=>$langs->trans('Year')), (GETPOST('unit_frequency') ?GETPOST('unit_frequency') : 'm'));
338		print "</td></tr>";
339
340		// First date of execution for cron
341		print "<tr><td>".$langs->trans('NextDateToExecution')."</td><td>";
342		if ($date_next_execution != "") {
343			$date_next_execution = (GETPOST('remonth') ? dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear')) : -1);
344		}
345		print $form->selectDate($date_next_execution, '', 1, 1, '', "add", 1, 1);
346		print "</td></tr>";
347
348		// Number max of generation
349		print "<tr><td>".$langs->trans("MaxPeriodNumber")."</td><td>";
350		print '<input type="text" name="nb_gen_max" value="'.GETPOST('nb_gen_max', 'int').'" size="5">';
351		print "</td></tr>";
352
353		print "</table>";
354
355		print '<br>';
356
357		$title = $langs->trans("ProductsAndServices");
358		if (empty($conf->service->enabled)) {
359			$title = $langs->trans("Products");
360		} elseif (empty($conf->product->enabled)) {
361			$title = $langs->trans("Services");
362		}
363
364		print load_fiche_titre($title, '', '');
365
366		/*
367		 * Fichinter lines
368		 */
369		print '<table class="notopnoleftnoright" width="100%">';
370		print '<tr><td colspan="3">';
371
372		$sql = 'SELECT l.rowid, l.description, l.duree';
373		$sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet as l";
374		$sql .= " WHERE l.fk_fichinter= ".$object->id;
375		//$sql.= " AND l.fk_product is null ";
376		$sql .= " ORDER BY l.rang";
377
378		$result = $db->query($sql);
379		if ($result) {
380			$num = $db->num_rows($result);
381			$i = 0;
382			$total = 0;
383
384			echo '<table class="noborder centpercent">';
385			if ($num) {
386				print '<tr class="liste_titre">';
387				print '<td>'.$langs->trans("Description").'</td>';
388				print '<td class="center">'.$langs->trans("Duration").'</td>';
389				print "</tr>\n";
390			}
391			while ($i < $num) {
392				$objp = $db->fetch_object($result);
393				print '<tr class="oddeven">';
394
395				// Show product and description
396
397				print '<td>';
398				print '<a name="'.$objp->rowid.'"></a>'; // ancre pour retourner sur la ligne
399
400				$text = img_object($langs->trans('Service'), 'service');
401
402				print $text.' '.nl2br($objp->description);
403
404				// Duration
405				print '<td class="center">'.convertSecondToTime($objp->duree).'</td>';
406				print "</tr>";
407
408				$i++;
409			}
410			$db->free($result);
411		} else {
412			print $db->error();
413		}
414		print "</table>";
415
416		print '</td></tr>';
417
418		print "</table>\n";
419
420		print dol_get_fiche_end();
421
422		print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Create").'">';
423		print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
424		print '<input type="button" class="button button-cancel" value="'.$langs->trans("Cancel").'" onClick="javascript:history.go(-1)">';
425		print '</div>';
426		print "</form>\n";
427	} else {
428		dol_print_error('', "Error, no fichinter ".$object->id);
429	}
430} elseif ($action == 'selsocforcreatefrommodel') {
431	print load_fiche_titre($langs->trans("CreateRepeatableIntervention"), '', 'intervention');
432	print dol_get_fiche_head('');
433
434	print '<form name="fichinter" action="'.$_SERVER['PHP_SELF'].'" method="POST">';
435	print '<table class="border centpercent">';
436	print '<tr><td class="fieldrequired">'.$langs->trans("ThirdParty").'</td><td>';
437	print $form->select_company('', 'socid', '', 1, 1);
438	print '</td></tr>';
439	print '</table>';
440
441	print dol_get_fiche_end();
442
443	print '<div class="center">';
444	print '<input type="hidden" name="action" value="createfrommodel">';
445	print '<input type="hidden" name="id" value="'.$id.'">';
446	print '<input type="submit" class="button" value="'.$langs->trans("CreateDraftIntervention").'">';
447	print '</div>';
448
449	print '</form>';
450} else {
451	/*
452	 * View mode
453	 *
454	 */
455	if ($id > 0) {
456		if ($object->fetch($id) > 0) {
457			$object->fetch_thirdparty();
458
459			$author = new User($db);
460			$author->fetch($object->user_author);
461
462			$head = fichinter_rec_prepare_head($object);
463
464			print dol_get_fiche_head($head, 'card', $langs->trans("PredefinedInterventional"), 0, 'intervention');
465
466			// Intervention card
467			$linkback = '<a href="card-rec.php">'.$langs->trans("BackToList").'</a>';
468
469			$morehtmlref = '<div class="refidno">';
470			// Thirdparty
471
472			$morehtmlref .= $langs->trans('ThirdParty').' : '.$object->thirdparty->getNomUrl(1);
473			// Project
474			if (!empty($conf->projet->enabled)) {
475				$formproject = new FormProjets($db);
476				$langs->load("projects");
477				$morehtmlref .= '<br>'.$langs->trans('Project').' ';
478				if ($user->rights->ficheinter->creer) {
479					if ($action != 'classify') {
480						$morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&amp;id='.$object->id.'">';
481						$morehtmlref .= img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> : ';
482					}
483					if ($action == 'classify') {
484						$morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
485						$morehtmlref .= '<input type="hidden" name="action" value="classin">';
486						$morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
487						$morehtmlref .= $formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
488						$morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
489						$morehtmlref .= '</form>';
490					} else {
491						$morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
492					}
493				} else {
494					if (!empty($object->fk_project)) {
495						$proj = new Project($db);
496						$proj->fetch($object->fk_project);
497						$morehtmlref .= '<a href="'.DOL_URL_ROOT.'/projet/card.php?id='.$object->fk_project.'"';
498						$morehtmlref .= 'title="'.$langs->trans('ShowProject').'">';
499						$morehtmlref .= $proj->ref;
500						$morehtmlref .= '</a>';
501					} else {
502						$morehtmlref .= '';
503					}
504				}
505			}
506			$morehtmlref .= '</div>';
507
508			dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
509
510			print '<div class="fichecenter">';
511			print '<div class="fichehalfleft">';
512			print '<div class="underbanner clearboth"></div>';
513
514			print '<table class="border centpercent">';
515
516			print "<tr><td>".$langs->trans("Author").'</td><td colspan="3">'.$author->getFullName($langs)."</td></tr>";
517
518			if (empty($conf->global->FICHINTER_DISABLE_DETAILS)) {
519				// Duration
520				print '<tr><td class="titlefield">'.$langs->trans("TotalDuration").'</td>';
521				print '<td colspan="3">';
522				print convertSecondToTime($object->duration, 'all', $conf->global->MAIN_DURATION_OF_WORKDAY);
523				print '</td></tr>';
524			}
525
526			print '<tr><td>'.$langs->trans("Description").'</td><td colspan="3">'.nl2br($object->description)."</td></tr>";
527
528			// Contract
529			if (!empty($conf->contrat->enabled)) {
530				$langs->load('contracts');
531				print '<tr>';
532				print '<td>';
533
534				print '<table class="nobordernopadding" width="100%"><tr><td>';
535				print $langs->trans('Contract');
536				print '</td>';
537				if ($action != 'contrat') {
538					print '<td class="right"><a href="'.$_SERVER["PHP_SELF"].'?action=contrat&amp;id='.$object->id.'">';
539					print img_edit($langs->trans('SetContract'), 1);
540					print '</a></td>';
541				}
542				print '</tr></table>';
543				print '</td><td>';
544				if ($action == 'contrat') {
545					$formcontract = new Formcontract($db);
546					$formcontract->formSelectContract($_SERVER["PHP_SELF"].'?id='.$object->id, $object->socid, $object->fk_contrat, 'contratid', 0, 1);
547				} else {
548					if ($object->fk_contrat) {
549						$contratstatic = new Contrat($db);
550						$contratstatic->fetch($object->fk_contrat);
551						print $contratstatic->getNomUrl(0, '', 1);
552					} else {
553						print "&nbsp;";
554					}
555				}
556				print '</td>';
557				print '</tr>';
558			}
559			print "</table>";
560			print '</div>';
561
562			print '<div class="fichehalfright">';
563			print '<div class="ficheaddleft">';
564			print '<div class="underbanner clearboth"></div>';
565
566			print '<table class="border centpercent">';
567
568			$title = $langs->trans("Recurrence");
569			print load_fiche_titre($title, '', 'calendar');
570
571			print '<table class="border centpercent">';
572
573			// if "frequency" is empty or = 0, the reccurence is disabled
574			print '<tr><td style="width: 50%">';
575			print '<table class="nobordernopadding" width="100%"><tr><td>';
576			print $langs->trans('Frequency');
577			print '</td>';
578			if ($action != 'editfrequency' && $user->rights->ficheinter->creer) {
579				print '<td class="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editfrequency&amp;id='.$id.'">';
580				print img_edit($langs->trans('Edit'), 1).'</a></td>';
581			}
582			print '</tr></table>';
583			print '</td><td>';
584			if ($action == 'editfrequency') {
585				print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">';
586				print '<input type="hidden" name="action" value="setfrequency">';
587				print '<input type="hidden" name="token" value="'.newToken().'">';
588				print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
589				print '<tr><td>';
590				print '<input type="text" name="frequency" value="'.$object->frequency.'" size="5">&nbsp;';
591				print $form->selectarray('unit_frequency', array('d'=>$langs->trans('Day'), 'm'=>$langs->trans('Month'), 'y'=>$langs->trans('Year')), ($object->unit_frequency ? $object->unit_frequency : 'm'));
592				print '</td>';
593				print '<td class="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
594				print '</tr></table></form>';
595			} else {
596				if ($object->frequency > 0)
597					print $langs->trans('FrequencyPer_'.$object->unit_frequency, $object->frequency);
598				else print $langs->trans("NotARecurringInterventionalTemplate");
599			}
600			print '</td></tr>';
601
602			// Date when
603			print '<tr><td>';
604			if ($user->rights->ficheinter->creer && ($action == 'date_when' || $object->frequency > 0)) {
605				print $form->editfieldkey($langs->trans("NextDateToExecution"), 'date_when', $object->date_when, $object, $user->rights->facture->creer, 'day');
606			} else {
607				print $langs->trans("NextDateToExecution");
608			}
609			print '</td><td>';
610			if ($action == 'date_when' || $object->frequency > 0) {
611				print $form->editfieldval($langs->trans("NextDateToExecution"), 'date_when', $object->date_when, $object, $user->rights->facture->creer, 'day');
612			}
613			print '</td>';
614			print '</tr>';
615
616			// Max period / Rest period
617			print '<tr><td>';
618			if ($user->rights->ficheinter->creer && ($action == 'nb_gen_max' || $object->frequency > 0)) {
619				print $form->editfieldkey($langs->trans("MaxPeriodNumber"), 'nb_gen_max', $object->nb_gen_max, $object, $user->rights->facture->creer);
620			} else print $langs->trans("MaxPeriodNumber");
621
622			print '</td><td>';
623			if ($action == 'nb_gen_max' || $object->frequency > 0) {
624				print $form->editfieldval($langs->trans("MaxPeriodNumber"), 'nb_gen_max', $object->nb_gen_max ? $object->nb_gen_max : '', $object, $user->rights->facture->creer);
625			} else print '';
626
627			print '</td>';
628			print '</tr>';
629
630			print '</table>';
631
632			// Frequencry/Recurring section
633			if ($object->frequency > 0) {
634				print '<br>';
635				if (empty($conf->cron->enabled)) {
636					$txtinfoadmin = $langs->trans("EnableAndSetupModuleCron", $langs->transnoentitiesnoconv("Module2300Name"));
637					print info_admin($txtinfoadmin);
638				}
639				print '<div class="underbanner clearboth"></div>';
640				print '<table class="border centpercent">';
641
642				// Nb of generation already done
643				print '<tr><td style="width: 50%">'.$langs->trans("NbOfGenerationDone").'</td>';
644				print '<td>';
645				print $object->nb_gen_done ? $object->nb_gen_done : '0';
646				print '</td>';
647				print '</tr>';
648
649				// Date last
650				print '<tr><td>';
651				print $langs->trans("DateLastGeneration");
652				print '</td><td>';
653				print dol_print_date($object->date_last_gen, 'dayhour');
654				print '</td>';
655				print '</tr>';
656				print '</table>';
657				print '<br>';
658			}
659
660			print '</div>';
661			print '</div>';
662			print '</div>';
663
664			print '<div class="clearboth"></div><br>';
665
666			/*
667			 * Lines
668			 */
669
670			$title = $langs->trans("ProductsAndServices");
671			if (empty($conf->service->enabled)) {
672				$title = $langs->trans("Products");
673			} elseif (empty($conf->product->enabled)) {
674				$title = $langs->trans("Services");
675			}
676
677			print load_fiche_titre($title);
678
679			print '<table class="noborder centpercent">';
680			print '<tr class="liste_titre">';
681			print '<td>'.$langs->trans("Description").'</td>';
682			print '<td class="center">'.$langs->trans("Duration").'</td>';
683			print '</tr>';
684
685			$num = count($object->lines);
686			$i = 0;
687			while ($i < $num) {
688				// Show product and description
689				if (isset($object->lines[$i]->product_type))
690					$type = $object->lines[$i]->product_type;
691				else $object->lines[$i]->fk_product_type;
692				// Try to enhance type detection using date_start and date_end for free lines when type
693				// was not saved.
694				if (!empty($objp->date_start)) $type = 1;
695				if (!empty($objp->date_end)) $type = 1;
696
697				// Show line
698				print '<tr class="oddeven">';
699				print '<td>';
700				$text = img_object($langs->trans('Service'), 'service');
701				print $text.' '.nl2br($object->lines[$i]->desc);
702				print '</td>';
703
704				print '<td class="center">'.convertSecondToTime($object->lines[$i]->duree).'</td>';
705				print "</tr>\n";
706				$i++;
707			}
708			print '</table>';
709
710			/**
711			 * Barre d'actions
712			 */
713			print '<div class="tabsAction">';
714
715			if ($user->rights->ficheinter->creer) {
716				print '<div class="inline-block divButAction">';
717				print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=createfrommodel&token='.newToken().'';
718				print '&socid='.$object->thirdparty->id.'&id='.$object->id.'">';
719				print $langs->trans("CreateFichInter").'</a></div>';
720			}
721
722			if ($user->rights->ficheinter->supprimer) {
723				print '<div class="inline-block divButAction">';
724				print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&id='.$object->id.'">';
725				print $langs->trans('Delete').'</a></div>';
726			}
727			print '</div>';
728		} else print $langs->trans("ErrorRecordNotFound");
729	} else {
730		/*
731		 *  List mode
732		 */
733		$sql = "SELECT f.rowid as fich_rec, s.nom as name, s.rowid as socid, f.rowid as facid, f.titre as title,";
734		$sql .= " f.duree, f.fk_contrat, f.fk_projet as fk_project, f.frequency, f.nb_gen_done, f.nb_gen_max,";
735		$sql .= " f.date_last_gen, f.date_when, f.datec";
736
737		$sql .= " FROM ".MAIN_DB_PREFIX."fichinter_rec as f";
738		$sql .= " , ".MAIN_DB_PREFIX."societe as s ";
739		if (!$user->rights->societe->client->voir && !$socid) {
740			$sql .= " , ".MAIN_DB_PREFIX."societe_commerciaux as sc";
741		}
742		$sql .= " WHERE f.fk_soc = s.rowid";
743		$sql .= " AND f.entity = ".$conf->entity;
744		if ($socid)	$sql .= " AND s.rowid = ".$socid;
745		if (!$user->rights->societe->client->voir && !$socid) {
746			$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
747		}
748		if ($search_ref) $sql .= natural_search('f.titre', $search_ref);
749		if ($search_societe) $sql .= natural_search('s.nom', $search_societe);
750		if ($search_frequency == '1') $sql .= ' AND f.frequency > 0';
751		if ($search_frequency == '0') $sql .= ' AND (f.frequency IS NULL or f.frequency = 0)';
752
753
754		//$sql .= " ORDER BY $sortfield $sortorder, rowid DESC ";
755		//	$sql .= $db->plimit($limit + 1, $offset);
756
757		$resql = $db->query($sql);
758		if ($resql) {
759			$num = $db->num_rows($resql);
760
761			print_barre_liste($langs->trans("RepeatableIntervention"), $page, $_SERVER['PHP_SELF'], "&socid=$socid", $sortfield, $sortorder, '', $num, '', 'intervention');
762
763			print '<span class="opacitymedium">'.$langs->trans("ToCreateAPredefinedIntervention").'</span><br><br>';
764
765			$i = 0;
766			print '<table class="noborder centpercent">';
767			print '<tr class="liste_titre">';
768			print_liste_field_titre("Ref", $_SERVER['PHP_SELF'], "f.titre", "", "", 'width="200px"', $sortfield, $sortorder, 'left ');
769			print_liste_field_titre("Company", $_SERVER['PHP_SELF'], "s.nom", "", "", 'width="200px"', $sortfield, $sortorder, 'left ');
770			if (!empty($conf->contrat->enabled)) {
771				print_liste_field_titre("Contract", $_SERVER['PHP_SELF'], "f.fk_contrat", "", "", 'width="100px"', $sortfield, $sortorder, 'left ');
772			}
773			if (!empty($conf->projet->enabled)) {
774				print_liste_field_titre("Project", $_SERVER['PHP_SELF'], "f.fk_project", "", "", 'width="100px"', $sortfield, $sortorder, 'left ');
775			}
776			print_liste_field_titre("Duration", $_SERVER['PHP_SELF'], 'f.duree', '', '', 'width="50px"', $sortfield, $sortorder, 'right ');
777			// Recurring or not
778			print_liste_field_titre("Frequency", $_SERVER['PHP_SELF'], "f.frequency", "", "", 'width="100px"', $sortfield, $sortorder, 'center ');
779			print_liste_field_titre("NbOfGenerationDone", $_SERVER['PHP_SELF'], "f.nb_gen_done", "", "", 'width="100px"', $sortfield, $sortorder, 'center ');
780			print_liste_field_titre("DateLastGeneration", $_SERVER['PHP_SELF'], "f.date_last_gen", "", "", 'width="100px"', $sortfield, $sortorder, 'center ');
781			print_liste_field_titre("NextDateToIntervention", $_SERVER['PHP_SELF'], "f.date_when", "", "", 'width="100px"', $sortfield, $sortorder, 'center ');
782			print '<th width="100px"></th>';
783			print "</tr>\n";
784
785
786			// les filtres à faire ensuite
787
788			if ($num > 0) {
789				while ($i < min($num, $limit)) {
790					$objp = $db->fetch_object($resql);
791
792					print '<tr class="oddeven">';
793					print '<td><a href="'.$_SERVER['PHP_SELF'].'?id='.$objp->fich_rec.'">';
794					print img_object($langs->trans("ShowIntervention"), "intervention").' '.$objp->title;
795					print "</a></td>\n";
796					if ($objp->socid) {
797						$companystatic->id = $objp->socid;
798						$companystatic->name = $objp->name;
799						print '<td>'.$companystatic->getNomUrl(1, 'customer').'</td>';
800					} else {
801						print '<td>'.$langs->trans("None").'</td>';
802					}
803
804					if (!empty($conf->contrat->enabled)) {
805						print '<td>';
806						if ($objp->fk_contrat > 0) {
807							$contratstatic->fetch($objp->fk_contrat);
808							print $contratstatic->getNomUrl(1);
809						}
810						print '</td>';
811					}
812					if (!empty($conf->projet->enabled)) {
813						print '<td>';
814						if ($objp->fk_project > 0) {
815							$projectstatic->fetch($objp->fk_project);
816							print $projectstatic->getNomUrl(1);
817						}
818						print '</td>';
819					}
820
821					print '<td class=right>'.convertSecondToTime($objp->duree).'</td>';
822
823					print '<td class="center">'.yn($objp->frequency ? 1 : 0).'</td>';
824
825					print '<td class="center">';
826					if ($objp->frequency) {
827						print $objp->nb_gen_done.($objp->nb_gen_max > 0 ? ' / '.$objp->nb_gen_max : '');
828						print '</td>';
829
830						print '<td class="center">';
831						print dol_print_date($db->jdate($objp->date_last_gen), 'day');
832						print '</td>';
833
834						print '<td class="center">';
835						print dol_print_date($db->jdate($objp->date_when), 'day');
836						print '</td>';
837					} else {
838						print '<span class="opacitymedium">'.$langs->trans('NA').'</span>';
839						print '</td>';
840						print '<td class="center">';
841						print '<span class="opacitymedium">'.$langs->trans('NA').'</span>';
842						print '</td>';
843						print '<td class="center">';
844						print '<span class="opacitymedium">'.$langs->trans('NA').'</span>';
845						print '</td>';
846					}
847
848					if ($user->rights->ficheinter->creer) {
849						// Action column
850						print '<td class="center">';
851						if ($user->rights->ficheinter->creer) {
852							if (empty($objp->frequency) || $db->jdate($objp->date_when) <= $today) {
853								print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=createfrommodel';
854								print '&socid='.$objp->socid.'&id='.$objp->fich_rec.'">';
855								print $langs->trans("CreateFichInter").'</a>';
856							} else print $langs->trans("DateIsNotEnough");
857						} else print "&nbsp;";
858
859						print "</td>";
860
861						print "</tr>\n";
862						$i++;
863					}
864				}
865			} else {
866				print '<tr class="oddeven"><td colspan="10">'.$langs->trans("NoneF").'</td></tr>';
867			}
868
869			print "</table>";
870			$db->free($resql);
871		} else {
872			dol_print_error($db);
873		}
874	}
875}
876llxFooter();
877$db->close();
878