1<?php
2/* Copyright (C) 2009       Laurent Destailleur    <eldy@users.sourceforge.net>
3 * Copyright (C) 2010-2016  Juanjo Menent	       <jmenent@2byte.es>
4 * Copyright (C) 2013-2018  Philippe Grand         <philippe.grand@atoo-net.com>
5 * Copyright (C) 2015       Jean-François Ferry    <jfefe@aternatik.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22/**
23 *      \file       htdocs/admin/bank.php
24 * 		\ingroup    bank
25 * 		\brief      Page to setup the bank module
26 */
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
31require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
32require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
33
34// Load translation files required by the page
35$langs->loadLangs(array("admin", "companies", "bills", "other", "banks"));
36
37if (!$user->admin)
38	accessforbidden();
39
40$action = GETPOST('action', 'aZ09');
41$actionsave = GETPOST('save', 'alpha');
42$value = GETPOST('value', 'alpha');
43$label = GETPOST('label', 'alpha');
44$scandir = GETPOST('scan_dir', 'alpha');
45$type = 'bankaccount';
46
47
48/*
49 * Actions
50 */
51
52// Order display of bank account
53if ($action == 'setbankorder') {
54	if (dolibarr_set_const($db, "BANK_SHOW_ORDER_OPTION", GETPOST('value', 'alpha'), 'chaine', 0, '', $conf->entity) > 0)
55	{
56		header("Location: ".$_SERVER["PHP_SELF"]);
57		exit;
58	} else {
59		dol_print_error($db);
60	}
61}
62
63// Auto report last num releve on conciliate
64if ($action == 'setreportlastnumreleve') {
65	if (dolibarr_set_const($db, "BANK_REPORT_LAST_NUM_RELEVE", 1, 'chaine', 0, '', $conf->entity) > 0)
66	{
67		header("Location: ".$_SERVER["PHP_SELF"]);
68		exit;
69	} else {
70		dol_print_error($db);
71	}
72} elseif ($action == 'unsetreportlastnumreleve') {
73	if (dolibarr_set_const($db, "BANK_REPORT_LAST_NUM_RELEVE", 0, 'chaine', 0, '', $conf->entity) > 0)
74	{
75		header("Location: ".$_SERVER["PHP_SELF"]);
76		exit;
77	} else {
78		dol_print_error($db);
79	}
80}
81
82// Colorize movements
83if ($action == 'setbankcolorizemovement') {
84	if (dolibarr_set_const($db, "BANK_COLORIZE_MOVEMENT", 1, 'chaine', 0, '', $conf->entity) > 0)
85	{
86		header("Location: ".$_SERVER["PHP_SELF"]);
87		exit;
88	} else {
89		dol_print_error($db);
90	}
91} elseif ($action == 'unsetbankcolorizemovement') {
92	if (dolibarr_set_const($db, "BANK_COLORIZE_MOVEMENT", 0, 'chaine', 0, '', $conf->entity) > 0)
93	{
94		header("Location: ".$_SERVER["PHP_SELF"]);
95		exit;
96	} else {
97		dol_print_error($db);
98	}
99}
100
101if ($actionsave)
102{
103	$db->begin();
104
105	$i = 1; $errorsaved = 0;
106	$error = 0;
107
108	// Save colors
109	while ($i <= 2)
110	{
111		$color = GETPOST('BANK_COLORIZE_MOVEMENT_COLOR'.$i, 'alpha');
112		if ($color == '-1') $color = '';
113
114		$res = dolibarr_set_const($db, 'BANK_COLORIZE_MOVEMENT_COLOR'.$i, $color, 'chaine', 0, '', $conf->entity);
115		if (!($res > 0)) $error++;
116		$i++;
117	}
118
119	if (!$error)
120	{
121		$db->commit();
122		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
123	} else {
124		$db->rollback();
125		if (empty($errorsaved))	setEventMessages($langs->trans("Error"), null, 'errors');
126	}
127}
128
129
130if ($action == 'specimen') {
131	$modele = GETPOST('module', 'alpha');
132
133	if ($modele == 'sepamandate') {
134		$object = new CompanyBankAccount($db);
135	} else {
136		$object = new Account($db);
137	}
138	$object->initAsSpecimen();
139
140	// Search template files
141	$file = '';
142	$classname = '';
143	$filefound = 0;
144	$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
145	foreach ($dirmodels as $reldir) {
146		$file = dol_buildpath($reldir."core/modules/bank/doc/pdf_".$modele.".modules.php", 0);
147		if (file_exists($file)) {
148			$filefound = 1;
149			$classname = "pdf_".$modele;
150			break;
151		}
152	}
153
154	if ($filefound) {
155		require_once $file;
156
157		$module = new $classname($db);
158
159		if ($module->write_file($object, $langs) > 0) {
160			header("Location: ".DOL_URL_ROOT."/document.php?modulepart=bank&file=SPECIMEN.pdf");
161			return;
162		} else {
163			setEventMessages($module->error, null, 'errors');
164			dol_syslog($module->error, LOG_ERR);
165		}
166	} else {
167		setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
168		dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
169	}
170}
171
172// Activate a model
173if ($action == 'set') {
174	$ret = addDocumentModel($value, $type, $label, $scandir);
175} elseif ($action == 'del') {
176	$ret = delDocumentModel($value, $type);
177	if ($ret > 0) {
178		if ($conf->global->BANKADDON_PDF == "$value")
179			dolibarr_del_const($db, 'BANKADDON_PDF', $conf->entity);
180	}
181}
182// Set default model
183elseif ($action == 'setdoc') {
184	if (dolibarr_set_const($db, "BANKADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
185		// The constant that was read before the new set
186		// We therefore requires a variable to have a coherent view
187		$conf->global->BANKADDON_PDF = $value;
188	}
189
190	// On active le modele
191	$ret = delDocumentModel($value, $type);
192	if ($ret > 0) {
193		$ret = addDocumentModel($value, $type, $label, $scandir);
194	}
195}
196
197
198
199/*
200 * View
201 */
202
203$form = new Form($db);
204$formother = new FormOther($db);
205
206$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
207
208llxHeader("", $langs->trans("BankSetupModule"));
209
210$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
211print load_fiche_titre($langs->trans("BankSetupModule"), $linkback, 'title_setup');
212
213print '<form name="bankmovementcolorconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">';
214print '<input type="hidden" name="token" value="'.newToken().'">';
215print '<input type="hidden" name="action" value="save">';
216
217$head = bank_admin_prepare_head(null);
218print dol_get_fiche_head($head, 'general', $langs->trans("BankSetupModule"), -1, 'account');
219
220//Show bank account order
221print load_fiche_titre($langs->trans("BankOrderShow"), '', '');
222
223print '<table class="noborder centpercent">';
224print '<tr class="liste_titre">';
225print '<td width="140">'.$langs->trans("Name").'</td>';
226print '<td>'.$langs->trans("Description").'</td>';
227print '<td>'.$langs->trans("Example").'</td>';
228print '<td class="center">'.$langs->trans("Status").'</td>';
229print '<td class="center" width="60">&nbsp;</td>';
230print "</tr>\n";
231
232$bankorder[0][0] = $langs->trans("BankOrderGlobal");
233$bankorder[0][1] = $langs->trans("BankOrderGlobalDesc");
234$bankorder[0][2] = 'BankCode DeskCode BankAccountNumber BankAccountNumberKey';
235$bankorder[1][0] = $langs->trans("BankOrderES");
236$bankorder[1][1] = $langs->trans("BankOrderESDesc");
237$bankorder[1][2] = 'BankCode DeskCode BankAccountNumberKey BankAccountNumber';
238
239$i = 0;
240
241$nbofbank = count($bankorder);
242while ($i < $nbofbank) {
243	print '<tr class="oddeven">';
244	print '<td>'.$bankorder[$i][0]."</td><td>\n";
245	print $bankorder[$i][1];
246	print '</td>';
247	print '<td class="nowrap">';
248	$tmparray = explode(' ', $bankorder[$i][2]);
249	foreach ($tmparray as $key => $val) {
250		if ($key > 0)
251			print ', ';
252		print $langs->trans($val);
253	}
254	print "</td>\n";
255
256	if ($conf->global->BANK_SHOW_ORDER_OPTION == $i) {
257		print '<td class="center">';
258		print img_picto($langs->trans("Activated"), 'on');
259		print '</td>';
260	} else {
261		print '<td class="center"><a href="'.$_SERVER['PHP_SELF'].'?action=setbankorder&amp;token='.newToken().'&amp;value='.$i.'">';
262		print img_picto($langs->trans("Disabled"), 'off');
263		print '</a></td>';
264	}
265	print '<td>&nbsp;</td>';
266	print '</tr>'."\n";
267	$i++;
268}
269
270print '</table>'."\n";
271
272print '<br><br>';
273
274
275/*
276 * Document templates generators
277 */
278//if (! empty($conf->global->MAIN_FEATURES_LEVEL))
279//{
280print load_fiche_titre($langs->trans("BankAccountModelModule"), '', '');
281
282// Load array def with activated templates
283$def = array();
284$sql = "SELECT nom";
285$sql .= " FROM ".MAIN_DB_PREFIX."document_model";
286$sql .= " WHERE type = '".$db->escape($type)."'";
287$sql .= " AND entity = ".$conf->entity;
288$resql = $db->query($sql);
289if ($resql) {
290	$i = 0;
291	$num_rows = $db->num_rows($resql);
292	while ($i < $num_rows) {
293		$array = $db->fetch_array($resql);
294		array_push($def, $array[0]);
295		$i++;
296	}
297} else {
298	dol_print_error($db);
299}
300
301print "<table class=\"noborder\" width=\"100%\">\n";
302print "<tr class=\"liste_titre\">\n";
303print '<td>'.$langs->trans("Name").'</td>';
304print '<td>'.$langs->trans("Description").'</td>';
305print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
306print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
307print '<td class="center" width="38">'.$langs->trans("ShortInfo").'</td>';
308print '<td class="center" width="38">'.$langs->trans("Preview").'</td>';
309print "</tr>\n";
310
311clearstatcache();
312
313foreach ($dirmodels as $reldir) {
314	foreach (array('', '/doc') as $valdir) {
315		$dir = dol_buildpath($reldir."core/modules/bank".$valdir);
316
317		if (is_dir($dir)) {
318			$handle = opendir($dir);
319			if (is_resource($handle)) {
320				while (($file = readdir($handle)) !== false) {
321					$filelist[] = $file;
322				}
323				closedir($handle);
324				arsort($filelist);
325
326				foreach ($filelist as $file) {
327					if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
328						if (file_exists($dir.'/'.$file)) {
329							$name = substr($file, 4, dol_strlen($file) - 16);
330							$classname = substr($file, 0, dol_strlen($file) - 12);
331
332							require_once $dir.'/'.$file;
333							$module = new $classname($db);
334
335							$modulequalified = 1;
336							if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2)
337								$modulequalified = 0;
338							if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1)
339								$modulequalified = 0;
340
341							if ($modulequalified) {
342								print '<tr class="oddeven"><td width="100">';
343								print(empty($module->name) ? $name : $module->name);
344								print "</td><td>\n";
345								if (method_exists($module, 'info'))
346									print $module->info($langs);
347								else print $module->description;
348								print '</td>';
349
350								// Active
351								if (in_array($name, $def)) {
352									print '<td class="center">'."\n";
353									print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=del&amp;token='.newToken().'&amp;value='.$name.'">';
354									print img_picto($langs->trans("Enabled"), 'switch_on');
355									print '</a>';
356									print '</td>';
357								} else {
358									print '<td class="center">'."\n";
359									print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=set&amp;value='.$name.'&amp;token='.newToken().'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
360									print "</td>";
361								}
362
363								// Default
364								print '<td class="center">';
365								if ($conf->global->BANKADDON_PDF == $name) {
366									print img_picto($langs->trans("Default"), 'on');
367								} else {
368									print '<a class="reposition" 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>';
369								}
370								print '</td>';
371
372								// Info
373								$htmltooltip = ''.$langs->trans("Name").': '.$module->name;
374								$htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
375								if ($module->type == 'pdf') {
376									$htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
377								}
378								$htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
379								$htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
380								//$htmltooltip .= '<br>' . $langs->trans("PaymentMode") . ': ' . yn($module->option_modereg, 1, 1);
381								//$htmltooltip .= '<br>' . $langs->trans("PaymentConditions") . ': ' . yn($module->option_condreg, 1, 1);
382								$htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
383								// $htmltooltip.='<br>'.$langs->trans("Discounts").': '.yn($module->option_escompte,1,1);
384								// $htmltooltip.='<br>'.$langs->trans("CreditNote").': '.yn($module->option_credit_note,1,1);
385								//$htmltooltip .= '<br>' . $langs->trans("WatermarkOnDraftOrders") . ': ' . yn($module->option_draft_watermark, 1, 1);
386
387								print '<td class="center">';
388								print $form->textwithpicto('', $htmltooltip, 1, 0);
389								print '</td>';
390
391								// Preview
392								print '<td class="center">';
393								if ($module->type == 'pdf') {
394									print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
395								} else {
396									print img_object($langs->trans("PreviewNotAvailable"), 'generic');
397								}
398								print '</td>';
399
400								print "</tr>\n";
401							}
402						}
403					}
404				}
405			}
406		}
407	}
408}
409print '</table>';
410//}
411
412print '<br><br>';
413
414print load_fiche_titre($langs->trans("BankColorizeMovement"), '', '');
415print '<table class="noborder centpercent">';
416print "<tr class=\"liste_titre\">\n";
417print '<td colspan="4">'.$langs->trans("Name").'</td>';
418print '<td align="center" width="100">'.$langs->trans("Value").'</td>'."\n";
419print "</tr>\n";
420
421print '<tr class="oddeven"><td colspan="4" width="100">';
422print $langs->trans('BankColorizeMovementDesc');
423print "</td>";
424// Active
425if ($conf->global->BANK_COLORIZE_MOVEMENT) {
426	print '<td class="center">'."\n";
427	print '<a href="'.$_SERVER["PHP_SELF"].'?action=unsetbankcolorizemovement&amp;token='.newToken().'">';
428	print img_picto($langs->trans("Enabled"), 'switch_on');
429	print '</a>';
430	print '</td>';
431} else {
432	print '<td class="center">'."\n";
433	print '<a href="'.$_SERVER["PHP_SELF"].'?action=setbankcolorizemovement&amp;token='.newToken().'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
434	print "</td>";
435}
436
437print "</tr>\n";
438
439if (!empty($conf->global->BANK_COLORIZE_MOVEMENT))
440{
441	$i = 1;
442	while ($i <= 2)
443	{
444		$key = $i;
445		$color = 'BANK_COLORIZE_MOVEMENT_COLOR'.$key;
446
447		print '<tr class="oddeven">';
448
449		// Label
450		print '<td colspan="4" width="180" class="nowrap">'.$langs->trans("BankColorizeMovementName".$key)."</td>";
451		// Color
452		print '<td class="nowrap right">';
453		print $formother->selectColor((GETPOST("BANK_COLORIZE_MOVEMENT_COLOR".$key) ?GETPOST("BANK_COLORIZE_MOVEMENT_COLOR".$key) : $conf->global->$color), "BANK_COLORIZE_MOVEMENT_COLOR".$key, 'bankmovementcolorconfig', 1, '', 'right hideifnotset');
454		print '</td>';
455		print "</tr>";
456		$i++;
457	}
458}
459print '</table>';
460
461print '<br><br>';
462
463
464/*
465 * Document templates generators
466 */
467//if (! empty($conf->global->MAIN_FEATURES_LEVEL))
468//{
469print load_fiche_titre($langs->trans("Other"), '', '');
470
471print "<table class=\"noborder\" width=\"100%\">\n";
472print "<tr class=\"liste_titre\">\n";
473print '<td>'.$langs->trans("Name").'</td>';
474print '<td>'.$langs->trans("Description").'</td>';
475print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
476print "</tr>\n";
477
478print '<tr class="oddeven"><td width="100">';
479print $langs->trans('AccountStatement');
480print "</td><td>\n";
481print $langs->trans('AutoReportLastAccountStatement');
482print '</td>';
483// Active
484if ($conf->global->BANK_REPORT_LAST_NUM_RELEVE) {
485	print '<td class="center">'."\n";
486	print '<a href="'.$_SERVER["PHP_SELF"].'?action=unsetreportlastnumreleve&amp;token='.newToken().'">';
487	print img_picto($langs->trans("Enabled"), 'switch_on');
488	print '</a>';
489	print '</td>';
490} else {
491	print '<td class="center">'."\n";
492	print '<a href="'.$_SERVER["PHP_SELF"].'?action=setreportlastnumreleve">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
493	print "</td>";
494}
495
496print "</tr>\n";
497print '</table>';
498print dol_get_fiche_end();
499
500print '<div class="center">';
501print '<input type="submit" id="save" name="save" class="button hideifnotset button-save" value="'.$langs->trans("Save").'">';
502print '</div>';
503
504print "</form>\n";
505
506// End of page
507llxFooter();
508$db->close();
509