1<?php
2/* Copyright (C) 2015  Juanjo Menent				<jmenent@2byte.es>
3 * Copyright (C) 2016  Laurent Destailleur          <eldy@users.sourceforge.net>
4 * Copyright (C) 2020  Maxime DEMAREST              <maxime@indelog.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/**
21 *      \file       htdocs/admin/supplier_payment.php
22 *		\ingroup    supplier
23 *		\brief      Page to setup supplier invoices payments
24 */
25
26require '../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/fourn.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
30
31// Load translation files required by the page
32$langs->loadLangs(array("admin", "errors", "other", "bills", "orders"));
33
34if (!$user->admin) accessforbidden();
35
36$action = GETPOST('action', 'aZ09');
37$value = GETPOST('value', 'alpha');
38$label = GETPOST('label', 'alpha');
39$scandir = GETPOST('scandir', 'alpha');
40$type = 'supplier_payment';
41
42
43/*
44 * Actions
45 */
46
47include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
48
49if ($action == 'updateMask')
50{
51	$maskconstsupplierpayment = GETPOST('maskconstsupplierpayment', 'alpha');
52	$masksupplierpayment = GETPOST('masksupplierpayment', 'alpha');
53	if ($maskconstsupplierpayment) $res = dolibarr_set_const($db, $maskconstsupplierpayment, $masksupplierpayment, 'chaine', 0, '', $conf->entity);
54
55	if (!($res > 0)) $error++;
56
57	if (!$error)
58	{
59		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
60	} else {
61		setEventMessages($langs->trans("Error"), null, 'errors');
62	}
63} elseif ($action == 'setmod')
64{
65	dolibarr_set_const($db, "SUPPLIER_PAYMENT_ADDON", $value, 'chaine', 0, '', $conf->entity);
66}
67
68// Activate a model
69elseif ($action == 'set')
70{
71	$ret = addDocumentModel($value, $type, $label, $scandir);
72} elseif ($action == 'del')
73{
74	$ret = delDocumentModel($value, $type);
75	if ($ret > 0)
76	{
77		if ($conf->global->FACTURE_ADDON_PDF == "$value") dolibarr_del_const($db, 'SUPPLIER_PAYMENT_ADDON_PDF', $conf->entity);
78	}
79}
80
81// Set default model
82elseif ($action == 'setdoc')
83{
84	if (dolibarr_set_const($db, "SUPPLIER_PAYMENT_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity))
85	{
86		// La constante qui a ete lue en avant du nouveau set
87		// on passe donc par une variable pour avoir un affichage coherent
88		$conf->global->FACTURE_ADDON_PDF = $value;
89	}
90
91	// On active le modele
92	$ret = delDocumentModel($value, $type);
93	if ($ret > 0)
94	{
95		$ret = addDocumentModel($value, $type, $label, $scandir);
96	}
97} elseif ($action == 'specimen')
98{
99	$modele = GETPOST('module', 'alpha');
100
101	$paiementFourn = new PaiementFourn($db);
102	$paiementFourn->initAsSpecimen();
103
104	// Search template files
105	$file = ''; $classname = ''; $filefound = 0;
106	$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
107	foreach ($dirmodels as $reldir)
108	{
109		$file = dol_buildpath($reldir."core/modules/supplier_payment/doc/pdf_".$modele.".modules.php", 0);
110		if (file_exists($file))
111		{
112			$filefound = 1;
113			$classname = "pdf_".$modele;
114			break;
115		}
116	}
117
118	if ($filefound)
119	{
120		require_once $file;
121
122		$module = new $classname($db);
123
124		if ($module->write_file($paiementFourn, $langs) > 0)
125		{
126			header("Location: ".DOL_URL_ROOT."/document.php?modulepart=supplier_payment&file=SPECIMEN.pdf");
127			return;
128		} else {
129			setEventMessages($module->error, $module->errors, 'errors');
130			dol_syslog($module->error, LOG_ERR);
131		}
132	} else {
133		setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
134		dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
135	}
136} elseif ($action == 'setparams')
137{
138	   $res = dolibarr_set_const($db, "PAYMENTS_FOURN_REPORT_GROUP_BY_MOD", GETPOST('PAYMENTS_FOURN_REPORT_GROUP_BY_MOD', 'int'), 'chaine', 0, '', $conf->entity);
139	   if (!($res > 0)) $error++;
140
141	if ($error)
142	   {
143			setEventMessages($langs->trans("Error"), null, 'errors');
144	}
145	if (!$error)
146	   {
147		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
148	}
149}
150
151/*
152 * View
153 */
154
155$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
156
157llxHeader("", $langs->trans("SupplierPaymentSetup"), 'EN:Supplier_Payment_Configuration|FR:Configuration_module_paiement_fournisseur');
158
159$form = new Form($db);
160
161
162$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
163print load_fiche_titre($langs->trans("SupplierPaymentSetup"), $linkback, 'title_setup');
164
165print "<br>";
166
167$head = supplierorder_admin_prepare_head();
168print dol_get_fiche_head($head, 'supplierpayment', $langs->trans("Suppliers"), -1, 'company');
169
170/*
171 *  Numbering module
172 */
173
174if (empty($conf->global->SUPPLIER_PAYMENT_ADDON)) $conf->global->SUPPLIER_PAYMENT_ADDON = 'mod_supplier_payment_bronan';
175
176print load_fiche_titre($langs->trans("PaymentsNumberingModule"), '', '');
177
178// Load array def with activated templates
179$def = array();
180$sql = "SELECT nom";
181$sql .= " FROM ".MAIN_DB_PREFIX."document_model";
182$sql .= " WHERE type = '".$db->escape($type)."'";
183$sql .= " AND entity = ".$conf->entity;
184$resql = $db->query($sql);
185if ($resql)
186{
187	$i = 0;
188	$num_rows = $db->num_rows($resql);
189	while ($i < $num_rows)
190	{
191		$array = $db->fetch_array($resql);
192		array_push($def, $array[0]);
193		$i++;
194	}
195} else {
196	dol_print_error($db);
197}
198
199print '<table class="noborder centpercent">';
200print '<tr class="liste_titre">';
201print '<td>'.$langs->trans("Name").'</td>';
202print '<td>'.$langs->trans("Description").'</td>';
203print '<td class="nowrap">'.$langs->trans("Example").'</td>';
204print '<td align="center" width="60">'.$langs->trans("Status").'</td>';
205print '<td align="center" width="16">'.$langs->trans("ShortInfo").'</td>';
206print '</tr>'."\n";
207
208clearstatcache();
209
210foreach ($dirmodels as $reldir)
211{
212	$dir = dol_buildpath($reldir."core/modules/supplier_payment/");
213	if (is_dir($dir))
214	{
215		$handle = opendir($dir);
216		if (is_resource($handle))
217		{
218			while (($file = readdir($handle)) !== false)
219			{
220				if (!is_dir($dir.$file) || (substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS'))
221				{
222					$filebis = $file;
223					$classname = preg_replace('/\.php$/', '', $file);
224					// For compatibility
225					if (!is_file($dir.$filebis))
226					{
227						$filebis = $file."/".$file.".modules.php";
228						$classname = "mod_supplier_payment_".$file;
229					}
230					// Check if there is a filter on country
231					preg_match('/\-(.*)_(.*)$/', $classname, $reg);
232					if (!empty($reg[2]) && $reg[2] != strtoupper($mysoc->country_code)) continue;
233
234					$classname = preg_replace('/\-.*$/', '', $classname);
235					if (!class_exists($classname) && is_readable($dir.$filebis) && (preg_match('/mod_/', $filebis) || preg_match('/mod_/', $classname)) && substr($filebis, dol_strlen($filebis) - 3, 3) == 'php')
236					{
237						// Charging the numbering class
238						require_once $dir.$filebis;
239
240						$module = new $classname($db);
241
242						// Show modules according to features level
243						if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
244						if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
245
246						if ($module->isEnabled())
247						{
248							print '<tr class="oddeven"><td width="100">';
249							echo preg_replace('/\-.*$/', '', preg_replace('/mod_supplier_payment_/', '', preg_replace('/\.php$/', '', $file)));
250							print "</td><td>\n";
251
252							print $module->info();
253
254							print '</td>';
255
256							// Show example of numbering module
257							print '<td class="nowrap">';
258							$tmp = $module->getExample();
259							if (preg_match('/^Error/', $tmp)) {
260								$langs->load("errors");
261								print '<div class="error">'.$langs->trans($tmp).'</div>';
262							} elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
263							else print $tmp;
264							print '</td>'."\n";
265
266							print '<td class="center">';
267							//print "> ".$conf->global->SUPPLIER_PAYMENT_ADDON." - ".$file;
268							if ($conf->global->SUPPLIER_PAYMENT_ADDON == $file || $conf->global->SUPPLIER_PAYMENT_ADDON.'.php' == $file)
269							{
270								print img_picto($langs->trans("Activated"), 'switch_on');
271							} else {
272								print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.preg_replace('/\.php$/', '', $file).'&scandir='.$module->scandir.'&label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
273							}
274							print '</td>';
275
276							$payment = new PaiementFourn($db);
277							$payment->initAsSpecimen();
278
279							// Example
280							$htmltooltip = '';
281							$htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
282							$nextval = $module->getNextValue($mysoc, $payment);
283							if ("$nextval" != $langs->trans("NotAvailable")) {  // Keep " on nextval
284									$htmltooltip .= $langs->trans("NextValue").': ';
285								if ($nextval) {
286									if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured')
287										$nextval = $langs->trans($nextval);
288									$htmltooltip .= $nextval.'<br>';
289								} else {
290									$htmltooltip .= $langs->trans($module->error).'<br>';
291								}
292							}
293
294							print '<td class="center">';
295							print $form->textwithpicto('', $htmltooltip, 1, 0);
296
297							if ($conf->global->PAYMENT_ADDON.'.php' == $file)  // If module is the one used, we show existing errors
298							{
299								if (!empty($module->error)) dol_htmloutput_mesg($module->error, '', 'error', 1);
300							}
301
302							print '</td>';
303
304							print "</tr>\n";
305						}
306					}
307				}
308			}
309			closedir($handle);
310		}
311	}
312}
313
314print '</table>';
315
316
317/*
318 *  Document templates generators
319 */
320print '<br>';
321print load_fiche_titre($langs->trans("PaymentsPDFModules"), '', '');
322
323print '<table class="noborder centpercent">'."\n";
324print '<tr class="liste_titre">'."\n";
325print '<td width="100">'.$langs->trans("Name").'</td>'."\n";
326print '<td>'.$langs->trans("Description").'</td>'."\n";
327print '<td align="center" width="60">'.$langs->trans("Status").'</td>'."\n";
328print '<td align="center" width="60">'.$langs->trans("Default").'</td>'."\n";
329print '<td align="center" width="40">'.$langs->trans("ShortInfo").'</td>';
330print '<td align="center" width="40">'.$langs->trans("Preview").'</td>';
331print '</tr>'."\n";
332
333clearstatcache();
334
335foreach ($dirmodels as $reldir)
336{
337	$realpath = $reldir."core/modules/supplier_payment/doc";
338	$dir = dol_buildpath($realpath);
339
340	if (is_dir($dir))
341	{
342		$handle = opendir($dir);
343
344
345		if (is_resource($handle))
346		{
347			while (($file = readdir($handle)) !== false)
348			{
349				if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file))
350				{
351					$name = substr($file, 4, dol_strlen($file) - 16);
352					$classname = substr($file, 0, dol_strlen($file) - 12);
353
354					require_once $dir.'/'.$file;
355					$module = new $classname($db, new PaiementFourn($db));
356
357					print "<tr class=\"oddeven\">\n";
358					print "<td>";
359					print (empty($module->name) ? $name : $module->name);
360					print "</td>\n";
361					print "<td>\n";
362					require_once $dir.'/'.$file;
363					$module = new $classname($db, $specimenthirdparty);
364					if (method_exists($module, 'info')) print $module->info($langs);
365					else print $module->description;
366
367					print "</td>\n";
368
369					// Active
370					if (in_array($name, $def))
371					{
372						print '<td class="center">'."\n";
373						//if ($conf->global->SUPPLIER_PAYMENT_ADDON_PDF != "$name")
374						//{
375							// Even if choice is the default value, we allow to disable it: For supplier invoice, we accept to have no doc generation at all
376							print '<a href="'.$_SERVER["PHP_SELF"].'?action=del&amp;value='.$name.'&amp;scandir='.$module->scandir.'&amp;label='.urlencode($module->name).'&amp;type=SUPPLIER_PAYMENT">';
377							print img_picto($langs->trans("Enabled"), 'switch_on');
378							print '</a>';
379						/*}
380                        else
381                        {
382                            print img_picto($langs->trans("Enabled"),'switch_on');
383                        }*/
384						print "</td>";
385					} else {
386						print '<td class="center">'."\n";
387						print '<a href="'.$_SERVER["PHP_SELF"].'?action=set&amp;token='.newToken().'&amp;value='.$name.'&amp;scandir='.$module->scandir.'&amp;label='.urlencode($module->name).'&amp;type=SUPPLIER_PAYMENT">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
388						print "</td>";
389					}
390
391					// Default
392					print '<td class="center">';
393					if ($conf->global->SUPPLIER_PAYMENT_ADDON_PDF == "$name")
394					{
395						//print img_picto($langs->trans("Default"),'on');
396						// Even if choice is the default value, we allow to disable it: For supplier invoice, we accept to have no doc generation at all
397						print '<a href="'.$_SERVER["PHP_SELF"].'?action=unsetdoc&amp;token='.newToken().'&amp;value='.$name.'&amp;scandir='.$module->scandir.'&amp;label='.urlencode($module->name).'&amp;type=SUPPLIER_PAYMENT"" alt="'.$langs->trans("Disable").'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
398					} else {
399						print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdoc&amp;token='.newToken().'&amp;value='.$name.'&amp;scandir='.$module->scandir.'&amp;label='.urlencode($module->name).'&amp;type=SUPPLIER_PAYMENT"" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
400					}
401					print '</td>';
402
403					// Info
404					$htmltooltip = ''.$langs->trans("Name").': '.$module->name;
405					$htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
406					$htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
407					$htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
408
409					$htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
410					$htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
411					print '<td class="center">';
412					print $form->textwithpicto('', $htmltooltip, 1, 0);
413					print '</td>';
414					print '<td class="center">';
415					print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&amp;module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
416					print '</td>';
417
418					print "</tr>\n";
419				}
420			}
421
422			closedir($handle);
423		}
424	}
425}
426
427print '</table>';
428
429/*
430 *  Other Options
431 */
432
433print "<br>";
434
435print load_fiche_titre($langs->trans("OtherOptions"), '', '');
436
437print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
438print '<input type="hidden" name="token" value="'.newToken().'" />';
439print '<input type="hidden" name="action" value="setparams" />';
440
441print '<div class="div-table-responsive-no-min">';
442print '<table class="noborder centpercent">';
443print '<tr class="liste_titre">';
444print '<td>'.$langs->trans("Parameter").'</td>';
445print '<td align="center" width="60">'.$langs->trans("Value").'</td>';
446print '<td width="80">&nbsp;</td>';
447print "</tr>\n";
448
449// Allow to group payments by mod in rapports
450print '<tr class="oddeven"><td>';
451print $langs->trans("GroupPaymentsByModOnReports");
452print '</td><td width="60" align="center">';
453print $form->selectyesno("PAYMENTS_FOURN_REPORT_GROUP_BY_MOD", $conf->global->PAYMENTS_FOURN_REPORT_GROUP_BY_MOD, 1);
454print '</td><td class="right">';
455print "</td></tr>\n";
456
457print '</table>';
458
459print dol_get_fiche_end();
460
461print '<br>';
462print '<div class="center">';
463print '<input type="submit" class="button" value="'.$langs->trans("Modify").'" />';
464print '</div>';
465print '<br>';
466
467print '</form>';
468
469// End of page
470llxFooter();
471$db->close();
472