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