1<?php
2/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2011 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2004      Sebastien Di Cintio  <sdicintio@ressource-toi.org>
5 * Copyright (C) 2004      Benoit Mortier       <benoit.mortier@opensides.be>
6 * Copyright (C) 2004      Eric Seigne          <eric.seigne@ryxeo.com>
7 * Copyright (C) 2005-2014 Regis Houssin        <regis.houssin@inodbox.com>
8 * Copyright (C) 2011-2013 Juanjo Menent        <jmenent@2byte.es>
9 * Copyright (C) 2011-2018 Philippe Grand       <philippe.grand@atoo-net.com>
10 * Copyright (C) 2015	   Claudio Aschieri		<c.aschieri@19.coop>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26/**
27 *      \file       htdocs/admin/delivery.php
28 *      \ingroup    delivery
29 *      \brief      age to setup extra fields of delivery
30 */
31require '../main.inc.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/expedition.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/delivery/class/delivery.class.php';
36
37// Load translation files required by the page
38$langs->loadLangs(array("admin", "sendings", "deliveries", "other"));
39
40if (!$user->admin) accessforbidden();
41
42$action  = GETPOST('action', 'alpha');
43$value   = GETPOST('value', 'alpha');
44$label   = GETPOST('label', 'alpha');
45$scandir = GETPOST('scan_dir', 'alpha');
46$type = 'delivery';
47
48
49/*
50 * Actions
51 */
52
53include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
54
55if ($action == 'updateMask')
56{
57	$maskconstdelivery = GETPOST('maskconstdelivery', 'alpha');
58	$maskdelivery = GETPOST('maskdelivery', 'alpha');
59	if ($maskconstdelivery)  $res = dolibarr_set_const($db, $maskconstdelivery, $maskdelivery, 'chaine', 0, '', $conf->entity);
60
61	if (!($res > 0)) $error++;
62
63 	if (!$error)
64	{
65		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
66	} else {
67		setEventMessages($langs->trans("Error"), null, 'errors');
68	}
69}
70
71if ($action == 'set_DELIVERY_FREE_TEXT')
72{
73	$free = GETPOST('DELIVERY_FREE_TEXT', 'restricthtml'); // No alpha here, we want exact string
74	$res = dolibarr_set_const($db, "DELIVERY_FREE_TEXT", $free, 'chaine', 0, '', $conf->entity);
75
76	if (!($res > 0)) $error++;
77
78 	if (!$error)
79	{
80		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
81	} else {
82		setEventMessages($langs->trans("Error"), null, 'errors');
83	}
84}
85
86if ($action == 'specimen')
87{
88	$modele = GETPOST('module', 'alpha');
89
90	$sending = new Delivery($db);
91	$sending->initAsSpecimen();
92
93	// Search template files
94	$file = ''; $classname = ''; $filefound = 0;
95	$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
96	foreach ($dirmodels as $reldir)
97	{
98		$file = dol_buildpath($reldir."core/modules/delivery/doc/pdf_".$modele.".modules.php", 0);
99		if (file_exists($file))
100		{
101			$filefound = 1;
102			$classname = "pdf_".$modele;
103			break;
104		}
105	}
106
107	if ($filefound)
108	{
109		require_once $file;
110
111		$module = new $classname($db);
112
113		if ($module->write_file($sending, $langs) > 0)
114		{
115			header("Location: ".DOL_URL_ROOT."/document.php?modulepart=delivery&file=SPECIMEN.pdf");
116			return;
117		} else {
118			setEventMessages($module->error, $module->errors, 'errors');
119			dol_syslog($module->error, LOG_ERR);
120		}
121	} else {
122		setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
123		dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
124	}
125}
126
127if ($action == 'set')
128{
129	$ret = addDocumentModel($value, $type, $label, $scandir);
130}
131
132if ($action == 'del')
133{
134	$ret = delDocumentModel($value, $type);
135	if ($ret > 0)
136	{
137		if ($conf->global->DELIVERY_ADDON_PDF == "$value") dolibarr_del_const($db, 'DELIVERY_ADDON_PDF', $conf->entity);
138	}
139}
140
141if ($action == 'setdoc')
142{
143	if (dolibarr_set_const($db, "DELIVERY_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity))
144	{
145		// La constante qui a ete lue en avant du nouveau set
146		// on passe donc par une variable pour avoir un affichage coherent
147		$conf->global->DELIVERY_ADDON_PDF = $value;
148	}
149
150	// On active le modele
151	$ret = delDocumentModel($value, $type);
152	if ($ret > 0)
153	{
154		$ret = addDocumentModel($value, $type, $label, $scandir);
155	}
156}
157
158if ($action == 'setmod')
159{
160	// TODO Verifier si module numerotation choisi peut etre active
161	// par appel methode canBeActivated
162
163	dolibarr_set_const($db, "DELIVERY_ADDON_NUMBER", $value, 'chaine', 0, '', $conf->entity);
164}
165
166
167/*
168 * View
169 */
170
171$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
172
173llxHeader("", "");
174
175$form = new Form($db);
176
177$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
178print load_fiche_titre($langs->trans("SendingsSetup"), $linkback, 'title_setup');
179print '<br>';
180$head = expedition_admin_prepare_head();
181
182print dol_get_fiche_head($head, 'receivings', $langs->trans("Receivings"), -1, 'shipment');
183
184
185// Delivery numbering model
186
187print load_fiche_titre($langs->trans("DeliveryOrderNumberingModules"), '', '');
188
189print '<table class="noborder centpercent">';
190print '<tr class="liste_titre">';
191print '<td width="100">'.$langs->trans("Name").'</td>';
192print '<td>'.$langs->trans("Description").'</td>';
193print '<td class="nowrap">'.$langs->trans("Example").'</td>';
194print '<td align="center" width="60">'.$langs->trans("Status").'</td>';
195print '<td align="center" width="16">'.$langs->trans("ShortInfo").'</td>';
196print '</tr>'."\n";
197
198clearstatcache();
199
200foreach ($dirmodels as $reldir)
201{
202	$dir = dol_buildpath($reldir."core/modules/delivery/");
203
204	if (is_dir($dir))
205	{
206		$handle = opendir($dir);
207		if (is_resource($handle))
208		{
209			while (($file = readdir($handle)) !== false)
210			{
211				if (preg_match('/^mod_delivery_([a-z0-9_]*)\.php$/', $file)) {
212					$file = substr($file, 0, dol_strlen($file) - 4);
213
214					require_once $dir.$file.'.php';
215
216					$module = new $file;
217
218					if ($module->isEnabled())
219					{
220						// Show modules according to features level
221						if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
222						if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
223
224						print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
225						print $module->info();
226						print '</td>';
227
228						// Show example of numbering module
229						print '<td class="nowrap">';
230						$tmp = $module->getExample();
231						if (preg_match('/^Error/', $tmp)) {
232							$langs->load("errors");
233							print '<div class="error">'.$langs->trans($tmp).'</div>';
234						} elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
235						else print $tmp;
236						print '</td>'."\n";
237
238						print '<td class="center">';
239						if ($conf->global->DELIVERY_ADDON_NUMBER == "$file")
240						{
241							print img_picto($langs->trans("Activated"), 'switch_on');
242						} else {
243							print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&amp;token='.newToken().'&amp;value='.urlencode($file).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
244						}
245						print '</td>';
246
247						$delivery = new Delivery($db);
248						$delivery->initAsSpecimen();
249
250						// Info
251						$htmltooltip = '';
252						$htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
253						$nextval = $module->getNextValue($mysoc, $delivery);
254						if ("$nextval" != $langs->trans("NotAvailable")) {  // Keep " on nextval
255							$htmltooltip .= ''.$langs->trans("NextValue").': ';
256							if ($nextval) {
257								if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured')
258									$nextval = $langs->trans($nextval);
259								$htmltooltip .= $nextval.'<br>';
260							} else {
261								$htmltooltip .= $langs->trans($module->error).'<br>';
262							}
263						}
264
265						print '<td class="center">';
266						print $form->textwithpicto('', $htmltooltip, 1, 0);
267						print '</td>';
268
269						print '</tr>';
270					}
271				}
272			}
273			closedir($handle);
274		}
275	}
276}
277
278print '</table>';
279
280
281/*
282 *  Documents Models for delivery
283 */
284print '<br>';
285print load_fiche_titre($langs->trans("DeliveryOrderModel"), '', '');
286
287// Defini tableau def de modele
288$type = "delivery";
289$def = array();
290
291$sql = "SELECT nom";
292$sql .= " FROM ".MAIN_DB_PREFIX."document_model";
293$sql .= " WHERE type = '".$db->escape($type)."'";
294$sql .= " AND entity = ".$conf->entity;
295
296$resql = $db->query($sql);
297if ($resql)
298{
299	$i = 0;
300	$num_rows = $db->num_rows($resql);
301	while ($i < $num_rows)
302	{
303		$array = $db->fetch_array($resql);
304		array_push($def, $array[0]);
305		$i++;
306	}
307} else {
308	dol_print_error($db);
309}
310
311print '<table class="noborder centpercent">';
312print '<tr class="liste_titre">';
313print '<td width="140">'.$langs->trans("Name").'</td>';
314print '<td>'.$langs->trans("Description").'</td>';
315print '<td align="center" width="60">'.$langs->trans("Status").'</td>';
316print '<td align="center" width="60">'.$langs->trans("Default").'</td>';
317print '<td align="center" width="32">'.$langs->trans("ShortInfo").'</td>';
318print '<td align="center" width="32">'.$langs->trans("Preview").'</td>';
319print "</tr>\n";
320
321clearstatcache();
322
323foreach ($dirmodels as $reldir)
324{
325	$dir = dol_buildpath($reldir."core/modules/delivery/doc/");
326
327	if (is_dir($dir))
328	{
329		$handle = opendir($dir);
330		if (is_resource($handle))
331		{
332			while (($file = readdir($handle)) !== false)
333			{
334				$filelist[] = $file;
335			}
336			closedir($handle);
337			arsort($filelist);
338
339			foreach ($filelist as $file)
340			{
341				if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file))
342				{
343					if (file_exists($dir.'/'.$file))
344					{
345						$name = substr($file, 4, dol_strlen($file) - 16);
346						$classname = substr($file, 0, dol_strlen($file) - 12);
347
348						require_once $dir.'/'.$file;
349						$module = new $classname($db);
350
351						$modulequalified = 1;
352						if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) $modulequalified = 0;
353						if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) $modulequalified = 0;
354
355						if ($modulequalified)
356						{
357							print '<tr class="oddeven"><td width="100">';
358							print (empty($module->name) ? $name : $module->name);
359							print "</td><td>\n";
360							if (method_exists($module, 'info')) print $module->info($langs);
361							else print $module->description;
362							print '</td>';
363
364							// Active
365							if (in_array($name, $def))
366							{
367								print "<td align=\"center\">\n";
368								print '<a href="'.$_SERVER["PHP_SELF"].'?action=del&amp;token='.newToken().'&amp;value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'">';
369								print img_picto($langs->trans("Enabled"), 'switch_on');
370								print '</a>';
371								print "</td>";
372							} else {
373								print "<td align=\"center\">\n";
374								print '<a href="'.$_SERVER["PHP_SELF"].'?action=set&amp;token='.newToken().'&amp;value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
375								print "</td>";
376							}
377
378							// Default
379							print "<td align=\"center\">";
380							if ($conf->global->DELIVERY_ADDON_PDF == "$name")
381							{
382								print img_picto($langs->trans("Default"), 'on');
383							} else {
384								print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdoc&amp;token='.newToken().'&amp;value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
385							}
386							print '</td>';
387
388							// Info
389							$htmltooltip = ''.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
390							$htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
391							$htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").'</u>:';
392							$htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
393							print '<td class="center">';
394							print $form->textwithpicto('', $htmltooltip, 1, 0);
395							print '</td>';
396
397							// Preview
398							print '<td class="center">';
399							if ($module->type == 'pdf')
400							{
401								print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
402							} else {
403								print img_object($langs->trans("PreviewNotAvailable"), 'generic');
404							}
405							print '</td>';
406
407							print '</tr>';
408						}
409					}
410				}
411			}
412		}
413	}
414}
415
416print '</table>';
417/*
418 *  Autres Options
419 */
420print "<br>";
421print load_fiche_titre($langs->trans("OtherOptions"), '', '');
422
423print '<table class="noborder centpercent">';
424print '<tr class="liste_titre">';
425print '<td>'.$langs->trans("Parameter").'</td>';
426print '<td align="center" width="60">'.$langs->trans("Value").'</td>';
427print '<td width="80">&nbsp;</td>';
428print "</tr>\n";
429
430$substitutionarray = pdf_getSubstitutionArray($langs, null, null, 2);
431$substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
432$htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
433foreach ($substitutionarray as $key => $val)	$htmltext .= $key.'<br>';
434$htmltext .= '</i>';
435
436print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
437print '<input type="hidden" name="token" value="'.newToken().'">';
438print '<input type="hidden" name="action" value="set_DELIVERY_FREE_TEXT">';
439print '<tr class="oddeven"><td colspan="2">';
440print $form->textwithpicto($langs->trans("FreeLegalTextOnDeliveryReceipts"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
441$variablename = 'DELIVERY_FREE_TEXT';
442if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT))
443{
444	print '<textarea name="'.$variablename.'" class="flat" cols="120">'.$conf->global->$variablename.'</textarea>';
445} else {
446	include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
447	$doleditor = new DolEditor($variablename, $conf->global->$variablename, '', 80, 'dolibarr_notes');
448	print $doleditor->Create();
449}
450print '</td><td class="right">';
451print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
452print "</td></tr>\n";
453print '</form>';
454
455print '</table>';
456
457// End of page
458llxFooter();
459$db->close();
460