1<?php
2/* Copyright (C) 2005      Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2005-2014 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2010 Regis Houssin        <regis.houssin@inodbox.com>
5 * Copyright (C) 2010-2013 Juanjo Menent        <jmenent@2byte.es>
6 * Copyright (C) 2019      Markus Welters       <markus@welters.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22/**
23 *	\file       htdocs/admin/prelevement.php
24 *	\ingroup    prelevement
25 *	\brief      Page to setup Withdrawals
26 */
27
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array("admin", "withdrawals"));
35
36// Security check
37if (!$user->admin) accessforbidden();
38
39$action = GETPOST('action', 'aZ09');
40$type = 'paymentorder';
41
42$error = 0;
43
44
45/*
46 * Actions
47 */
48
49if ($action == "set")
50{
51	$db->begin();
52
53	$id = GETPOST('PRELEVEMENT_ID_BANKACCOUNT', 'int');
54	$account = new Account($db);
55	if ($account->fetch($id) > 0)
56	{
57		$res = dolibarr_set_const($db, "PRELEVEMENT_ID_BANKACCOUNT", $id, 'chaine', 0, '', $conf->entity);
58		if (!($res > 0)) $error++;
59		/*
60        $res = dolibarr_set_const($db, "PRELEVEMENT_CODE_BANQUE", $account->code_banque,'chaine',0,'',$conf->entity);
61        if (! $res > 0) $error++;
62        $res = dolibarr_set_const($db, "PRELEVEMENT_CODE_GUICHET", $account->code_guichet,'chaine',0,'',$conf->entity);
63        if (! $res > 0) $error++;
64        $res = dolibarr_set_const($db, "PRELEVEMENT_NUMERO_COMPTE", $account->number,'chaine',0,'',$conf->entity);
65        if (! $res > 0) $error++;
66        $res = dolibarr_set_const($db, "PRELEVEMENT_NUMBER_KEY", $account->cle_rib,'chaine',0,'',$conf->entity);
67        if (! $res > 0) $error++;
68        $res = dolibarr_set_const($db, "PRELEVEMENT_IBAN", $account->iban,'chaine',0,'',$conf->entity);
69        if (! $res > 0) $error++;
70        $res = dolibarr_set_const($db, "PRELEVEMENT_BIC", $account->bic,'chaine',0,'',$conf->entity);
71        if (! $res > 0) $error++;
72        $res = dolibarr_set_const($db, "PRELEVEMENT_RAISON_SOCIALE", $account->proprio,'chaine',0,'',$conf->entity);
73        if (! $res > 0) $error++;
74        */
75	} else $error++;
76
77	$res = dolibarr_set_const($db, "PRELEVEMENT_ICS", GETPOST("PRELEVEMENT_ICS"), 'chaine', 0, '', $conf->entity);
78	if (!($res > 0)) $error++;
79
80    if (GETPOST("PRELEVEMENT_USER") > 0) {
81        $res = dolibarr_set_const($db, "PRELEVEMENT_USER", GETPOST("PRELEVEMENT_USER"), 'chaine', 0, '', $conf->entity);
82        if (! ($res > 0)) $error++;
83    }
84    if (GETPOST("PRELEVEMENT_END_TO_END") || GETPOST("PRELEVEMENT_END_TO_END") == "") {
85        $res = dolibarr_set_const($db, "PRELEVEMENT_END_TO_END", GETPOST("PRELEVEMENT_END_TO_END"), 'chaine', 0, '', $conf->entity);
86        if (! ($res > 0)) $error++;
87    }
88    if (GETPOST("PRELEVEMENT_USTRD") || GETPOST("PRELEVEMENT_USTRD") == "") {
89        $res = dolibarr_set_const($db, "PRELEVEMENT_USTRD", GETPOST("PRELEVEMENT_USTRD"), 'chaine', 0, '', $conf->entity);
90        if (! ($res > 0)) $error++;
91    }
92
93    $res = dolibarr_set_const($db, "PRELEVEMENT_ADDDAYS", GETPOST("PRELEVEMENT_ADDDAYS"), 'chaine', 0, '', $conf->entity);
94    if (! ($res > 0)) $error++;
95
96    if (! $error) {
97		$db->commit();
98		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
99	} else {
100		$db->rollback();
101		setEventMessages($langs->trans("Error"), null, 'errors');
102	}
103}
104
105if ($action == "addnotif")
106{
107	$bon = new BonPrelevement($db);
108	$bon->AddNotification($db, GETPOST('user', 'int'), $action);
109
110	header("Location: ".$_SERVER["PHP_SELF"]);
111	exit;
112}
113
114if ($action == "deletenotif")
115{
116	$bon = new BonPrelevement($db);
117	$bon->DeleteNotificationById(GETPOST('notif', 'int'));
118
119	header("Location: ".$_SERVER["PHP_SELF"]);
120	exit;
121}
122
123
124/*
125 *	View
126 */
127
128$form = new Form($db);
129
130$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
131
132llxHeader('', $langs->trans("WithdrawalsSetup"));
133
134$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
135
136print load_fiche_titre($langs->trans("WithdrawalsSetup"), $linkback, 'title_setup');
137print '<br>';
138
139print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=set">';
140print '<input type="hidden" name="token" value="'.newToken().'">';
141
142print '<table class="noborder centpercent">';
143
144print '<tr class="liste_titre">';
145print '<td class="titlefieldmiddle">'.$langs->trans("Parameter").'</td>';
146print '<td>'.$langs->trans("Value").'</td>';
147print "</tr>";
148
149// Bank account (from Banks module)
150print '<tr class="oddeven"><td class="fieldrequired">'.$langs->trans("BankToReceiveWithdraw").'</td>';
151print '<td class="left">';
152$form->select_comptes($conf->global->PRELEVEMENT_ID_BANKACCOUNT, 'PRELEVEMENT_ID_BANKACCOUNT', 0, "courant=1", 1);
153print '</td></tr>';
154
155// ICS
156print '<tr class="oddeven"><td class="fieldrequired">';
157$htmltext = $langs->trans("AskThisIDToYourBank");
158print $form->textwithpicto($langs->trans("ICS"), $htmltext);
159print '</td>';
160print '<td class="left">';
161print '<input type="text" name="PRELEVEMENT_ICS" value="'.$conf->global->PRELEVEMENT_ICS.'" size="15" >';
162print '</td>';
163print '</td></tr>';
164
165//User
166print '<tr class="oddeven"><td class="fieldrequired">'.$langs->trans("ResponsibleUser").'</td>';
167print '<td class="left">';
168print $form->select_dolusers($conf->global->PRELEVEMENT_USER, 'PRELEVEMENT_USER', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
169print '</td>';
170print '</tr>';
171
172//EntToEnd
173print '<tr class="oddeven"><td>';
174$htmltext = $langs->trans("KeepThisEmptyInMostCases");
175print $form->textwithpicto($langs->trans("END_TO_END"), $htmltext);
176print '</td>';
177print '<td class="left">';
178print '<input type="text" name="PRELEVEMENT_END_TO_END" value="'.$conf->global->PRELEVEMENT_END_TO_END.'" size="15" ></td>';
179print '</td></tr>';
180
181//USTRD
182print '<tr class="oddeven"><td>';
183$htmltext = $langs->trans("KeepThisEmptyInMostCases");
184print $form->textwithpicto($langs->trans("USTRD"), $htmltext);
185print '</td>';
186print '<td class="left">';
187print '<input type="text" name="PRELEVEMENT_USTRD" value="'.$conf->global->PRELEVEMENT_USTRD.'" size="15" ></td>';
188print '</td></tr>';
189
190//ADDDAYS
191print '<tr class="oddeven"><td>'.$langs->trans("ADDDAYS").'</td>';
192print '<td class="left">';
193if (empty($conf->global->PRELEVEMENT_ADDDAYS)) $conf->global->PRELEVEMENT_ADDDAYS = 0;
194print '<input type="text" name="PRELEVEMENT_ADDDAYS" value="'.$conf->global->PRELEVEMENT_ADDDAYS.'" size="5" ></td>';
195print '</td></tr>';
196
197print '</table>';
198print '<br>';
199
200print '<div class="center"><input type="submit" class="button button-save" value="'.$langs->trans("Save").'"></div>';
201
202print '</form>';
203
204
205print '<br>';
206
207
208/*
209 * Document templates generators
210 */
211/*
212print load_fiche_titre($langs->trans("OrdersModelModule"),'','');
213
214// Load array def with activated templates
215$def = array();
216$sql = "SELECT nom";
217$sql.= " FROM ".MAIN_DB_PREFIX."document_model";
218$sql.= " WHERE type = '".$db->escape($type)."'";
219$sql.= " AND entity = ".$conf->entity;
220$resql=$db->query($sql);
221if ($resql)
222{
223    $i = 0;
224    $num_rows=$db->num_rows($resql);
225    while ($i < $num_rows)
226    {
227        $array = $db->fetch_array($resql);
228        array_push($def, $array[0]);
229        $i++;
230    }
231}
232else
233{
234    dol_print_error($db);
235}
236
237
238print "<table class=\"noborder\" width=\"100%\">\n";
239print "<tr class=\"liste_titre\">\n";
240print '<td>'.$langs->trans("Name").'</td>';
241print '<td>'.$langs->trans("Description").'</td>';
242print '<td align="center" width="60">'.$langs->trans("Status")."</td>\n";
243print '<td align="center" width="60">'.$langs->trans("Default")."</td>\n";
244print '<td align="center" width="38">'.$langs->trans("ShortInfo").'</td>';
245print '<td align="center" width="38">'.$langs->trans("Preview").'</td>';
246print "</tr>\n";
247
248clearstatcache();
249
250foreach ($dirmodels as $reldir)
251{
252    foreach (array('','/doc') as $valdir)
253    {
254        $dir = dol_buildpath($reldir."core/modules/paymentorders".$valdir);
255
256        if (is_dir($dir))
257        {
258            $handle=opendir($dir);
259            if (is_resource($handle))
260            {
261                while (($file = readdir($handle))!==false)
262                {
263                    $filelist[]=$file;
264                }
265                closedir($handle);
266                arsort($filelist);
267
268                foreach($filelist as $file)
269                {
270                    if (preg_match('/\.modules\.php$/i',$file) && preg_match('/^(pdf_|doc_)/',$file))
271                    {
272
273                        if (file_exists($dir.'/'.$file))
274                        {
275                            $name = substr($file, 4, dol_strlen($file) -16);
276                            $classname = substr($file, 0, dol_strlen($file) -12);
277
278                            require_once $dir.'/'.$file;
279                            $module = new $classname($db);
280
281                            $modulequalified=1;
282                            if ($module->version == 'development'  && $conf->global->MAIN_FEATURES_LEVEL < 2) $modulequalified=0;
283                            if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) $modulequalified=0;
284
285                            if ($modulequalified)
286                            {
287                                $var = !$var;
288                                print '<tr class="oddeven"><td width="100">';
289                                print (empty($module->name)?$name:$module->name);
290                                print "</td><td>\n";
291                                if (method_exists($module,'info')) print $module->info($langs);
292                                else print $module->description;
293                                print '</td>';
294
295                                // Active
296                                if (in_array($name, $def))
297                                {
298                                    print '<td class="center">'."\n";
299                                    print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=del&value='.$name.'">';
300                                    print img_picto($langs->trans("Enabled"),'switch_on');
301                                    print '</a>';
302                                    print '</td>';
303                                }
304                                else
305                                {
306                                    print '<td class="center">'."\n";
307                                    print '<a class="reposition" 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>';
308                                    print "</td>";
309                                }
310
311                                // Default
312                                print '<td class="center">';
313                                if ($conf->global->PAYMENTORDER_ADDON_PDF == $name)
314                                {
315                                    print img_picto($langs->trans("Default"),'on');
316                                }
317                                else
318                                {
319                                    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>';
320                                }
321                                print '</td>';
322
323                                // Info
324                                $htmltooltip =    ''.$langs->trans("Name").': '.$module->name;
325                                $htmltooltip.='<br>'.$langs->trans("Type").': '.($module->type?$module->type:$langs->trans("Unknown"));
326                                if ($module->type == 'pdf')
327                                {
328                                    $htmltooltip.='<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
329                                }
330                                $htmltooltip.='<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
331                                $htmltooltip.='<br>'.$langs->trans("Logo").': '.yn($module->option_logo,1,1);
332                                $htmltooltip.='<br>'.$langs->trans("PaymentMode").': '.yn($module->option_modereg,1,1);
333                                $htmltooltip.='<br>'.$langs->trans("PaymentConditions").': '.yn($module->option_condreg,1,1);
334                                $htmltooltip.='<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang,1,1);
335                                //$htmltooltip.='<br>'.$langs->trans("Discounts").': '.yn($module->option_escompte,1,1);
336                                //$htmltooltip.='<br>'.$langs->trans("CreditNote").': '.yn($module->option_credit_note,1,1);
337                                $htmltooltip.='<br>'.$langs->trans("WatermarkOnDraftOrders").': '.yn($module->option_draft_watermark,1,1);
338
339
340                                print '<td class="center">';
341                                print $form->textwithpicto('',$htmltooltip,1,0);
342                                print '</td>';
343
344                                // Preview
345                                print '<td class="center">';
346                                if ($module->type == 'pdf')
347                                {
348                                    print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
349                                }
350                                else
351                                {
352                                    print img_object($langs->trans("PreviewNotAvailable"),'generic');
353                                }
354                                print '</td>';
355
356                                print "</tr>\n";
357                            }
358                        }
359                    }
360                }
361            }
362        }
363    }
364}
365
366*/
367
368
369print dol_get_fiche_end();
370
371print '<br>';
372
373
374/*
375 * Notifications
376 */
377
378/* Disable this, there is no trigger with elementtype 'withdraw'
379if (! empty($conf->global->MAIN_MODULE_NOTIFICATION))
380{
381    $langs->load("mails");
382    print load_fiche_titre($langs->trans("Notifications"));
383
384    $sql = "SELECT u.rowid, u.lastname, u.firstname, u.fk_soc, u.email";
385    $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
386    $sql.= " WHERE entity IN (".getEntity('invoice').")";
387
388    $resql=$db->query($sql);
389    if ($resql)
390    {
391        $num = $db->num_rows($resql);
392        $i = 0;
393        while ($i < $num)
394        {
395            $obj = $db->fetch_object($resql);
396
397            if (!$obj->fk_soc)
398            {
399                $username=dolGetFirstLastname($obj->firstname,$obj->lastname);
400                $internalusers[$obj->rowid] = $username;
401            }
402
403            $i++;
404        }
405        $db->free($resql);
406    }
407
408    // Get list of triggers for module withdraw
409    $sql = "SELECT rowid, code, label";
410    $sql.= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
411    $sql.= " WHERE elementtype = 'withdraw'";
412    $sql.= " ORDER BY rang ASC";
413
414    $resql = $db->query($sql);
415    if ($resql)
416    {
417        $num = $db->num_rows($resql);
418        $i = 0;
419        while ($i < $num)
420        {
421            $obj = $db->fetch_object($resql);
422            $label=($langs->trans("Notify_".$obj->code)!="Notify_".$obj->code?$langs->trans("Notify_".$obj->code):$obj->label);
423            $actions[$obj->rowid]=$label;
424            $i++;
425        }
426        $db->free($resql);
427    }
428
429
430    print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=addnotif">';
431    print '<input type="hidden" name="token" value="'.newToken().'">';
432    print '<table class="noborder centpercent">';
433    print '<tr class="liste_titre">';
434    print '<td>'.$langs->trans("User").'</td>';
435    print '<td>'.$langs->trans("Value").'</td>';
436    print '<td class="right">'.$langs->trans("Action").'</td>';
437    print "</tr>\n";
438
439    print '<tr class="impair"><td class="left">';
440    print $form->selectarray('user',$internalusers);//  select_dolusers(0,'user',0);
441    print '</td>';
442
443    print '<td>';
444    print $form->selectarray('action',$actions);//  select_dolusers(0,'user',0);
445    print '</td>';
446
447    print '<td class="right"><input type="submit" class="button" value="'.$langs->trans("Add").'"></td></tr>';
448
449	// List of current notifications for objet_type='withdraw'
450	$sql = "SELECT u.lastname, u.firstname,";
451	$sql.= " nd.rowid, ad.code, ad.label";
452	$sql.= " FROM ".MAIN_DB_PREFIX."user as u,";
453	$sql.= " ".MAIN_DB_PREFIX."notify_def as nd,";
454	$sql.= " ".MAIN_DB_PREFIX."c_action_trigger as ad";
455	$sql.= " WHERE u.rowid = nd.fk_user";
456	$sql.= " AND nd.fk_action = ad.rowid";
457	$sql.= " AND u.entity IN (0,".$conf->entity.")";
458
459	$resql = $db->query($sql);
460	if ($resql)
461	{
462	    $num = $db->num_rows($resql);
463	    $i = 0;
464	    while ($i < $num)
465	    {
466	        $obj = $db->fetch_object($resql);
467
468
469	        print '<tr class="oddeven">';
470	        print '<td>'.dolGetFirstLastname($obj->firstname,$obj->lastname).'</td>';
471	        $label=($langs->trans("Notify_".$obj->code)!="Notify_".$obj->code?$langs->trans("Notify_".$obj->code):$obj->label);
472	        print '<td>'.$label.'</td>';
473	        print '<td class="right"><a href="'.$_SERVER["PHP_SELF"].'?action=deletenotif&token='.newToken().'&notif='.$obj->rowid.'">'.img_delete().'</a></td>';
474	        print '</tr>';
475	        $i++;
476	    }
477	    $db->free($resql);
478	}
479
480	print '</table>';
481	print '</form>';
482}
483*/
484
485// End of page
486llxFooter();
487$db->close();
488