1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
30 #=====================================================================
32 # user related functions
34 #=====================================================================
43 use SL::DB::AuthClient;
50 use SL::System::InstallationLock;
51 use SL::DefaultManager;
55 use constant LOGIN_OK => 0;
56 use constant LOGIN_BASIC_TABLES_MISSING => -1;
57 use constant LOGIN_DBUPDATE_AVAILABLE => -2;
58 use constant LOGIN_AUTH_DBUPDATE_AVAILABLE => -3;
59 use constant LOGIN_GENERAL_ERROR => -4;
62 $main::lxdebug->enter_sub();
64 my ($type, %params) = @_;
68 if ($params{id} || $params{login}) {
69 my %user_data = $main::auth->read_user(%params);
70 map { $self->{$_} = $user_data{$_} } keys %user_data;
73 $main::lxdebug->leave_sub();
79 $main::lxdebug->enter_sub();
86 # scan the locale directory and read in the LANGUAGE files
87 opendir(DIR, "locale");
89 my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
91 foreach my $dir (@dir) {
92 next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE");
96 $cc{$dir} = "@language";
101 $main::lxdebug->leave_sub();
107 my ($self, $form) = @_;
109 return LOGIN_GENERAL_ERROR() if !$self->{login} || !$::auth->client;
111 my %myconfig = $main::auth->read_user(login => $self->{login});
113 # Auth DB upgrades available?
114 my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
115 return LOGIN_AUTH_DBUPDATE_AVAILABLE() if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
117 # check if database is down
118 my $dbh = SL::DB->client->dbh;
120 # we got a connection, check the version
121 my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|);
124 return LOGIN_BASIC_TABLES_MISSING();
127 $self->create_schema_info_table($form, $dbh);
129 my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
130 my @unapplied_scripts = $dbupdater->unapplied_upgrade_scripts($dbh);
133 if (!@unapplied_scripts) {
134 SL::DB::Manager::Employee->update_entries_for_authorized_users;
138 # Store the fact that we're applying database upgrades at the
139 # moment. That way functions called from the layout modules that may
140 # require updated tables can chose only to use basic features.
141 $::request->applying_database_upgrades(1);
143 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
144 $form->{$_} = $myconfig{$_} for qw(datestyle);
146 $form->{"title"} = $main::locale->text("Dataset upgrade");
147 $form->header(no_layout => $form->{no_layout});
148 print $form->parse_html_template("dbupgrade/header");
150 $form->{dbupdate} = "db" . $::auth->client->{dbname};
152 if ($form->{"show_dbupdate_warning"}) {
153 print $form->parse_html_template("dbupgrade/warning", { unapplied_scripts => \@unapplied_scripts });
154 $::dispatcher->end_request;
158 SL::System::InstallationLock->lock;
160 # ignore HUP, QUIT in case the webserver times out
161 $SIG{HUP} = 'IGNORE';
162 $SIG{QUIT} = 'IGNORE';
164 $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
166 # If $self->dbupdate2 returns than this means all upgrade scripts
167 # have been applied successfully, none required user
168 # interaction. Otherwise the deeper layers would have called
169 # $::dispatcher->end_request already, and return would not have returned to
170 # us. Therefore we can now use RDBO instances because their supposed
171 # table structures do match the actual structures. So let's ensure
172 # that the "employee" table contains the appropriate entries for all
173 # users authorized for the current client.
174 SL::DB::Manager::Employee->update_entries_for_authorized_users;
176 SL::System::InstallationLock->unlock;
178 print $form->parse_html_template("dbupgrade/footer");
180 return LOGIN_DBUPDATE_AVAILABLE();
184 $main::lxdebug->enter_sub();
186 my ($form, $db) = @_;
189 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
190 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
191 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
192 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
193 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
196 $form->{dboptions} = $dboptions{ $form->{dateformat} };
197 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
199 $main::lxdebug->leave_sub();
203 $main::lxdebug->enter_sub();
205 my ($self, $form) = @_;
210 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
211 &dbconnect_vars($form, $form->{dbdefault});
213 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
217 qq|SELECT datname FROM pg_database | .
218 qq|WHERE NOT datname IN ('template0', 'template1')|;
219 $sth = $dbh->prepare($query);
220 $sth->execute() || $form->dberror($query);
222 while (my ($db) = $sth->fetchrow_array) {
224 if ($form->{only_acc_db}) {
226 next if ($db =~ /^template/);
228 &dbconnect_vars($form, $db);
229 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
233 qq|SELECT tablename FROM pg_tables | .
234 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
235 my $sth = $dbh->prepare($query);
236 $sth->execute($form->{dbuser}) ||
237 $form->dberror($query . " ($form->{dbuser})");
239 if ($sth->fetchrow_array) {
240 push(@dbsources, $db);
246 push(@dbsources, $db);
252 $main::lxdebug->leave_sub();
258 $main::lxdebug->enter_sub();
260 my ($self, $form) = @_;
262 &dbconnect_vars($form, $form->{dbdefault});
264 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
266 $form->{db} =~ s/\"//g;
270 push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
271 if ($form->{"dbdefault"}) {
272 my $dbdefault = $form->{"dbdefault"};
273 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
274 push @dboptions, "TEMPLATE = $dbdefault";
277 my $query = qq|CREATE DATABASE "$form->{db}"|;
278 $query .= " WITH " . join(" ", @dboptions) if @dboptions;
280 # Ignore errors if the database exists.
285 &dbconnect_vars($form, $form->{db});
287 # make a shim myconfig so that rose db connections work
288 $::myconfig{$_} = $form->{$_} for qw(dbhost dbport dbuser dbpasswd);
289 $::myconfig{dbname} = $form->{db};
291 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
294 my $dbupdater = SL::DBUpgrade2->new(form => $form, return_on_error => 1, silent => 1)->parse_dbupdate_controls;
296 $dbupdater->process_query($dbh, "sql/lx-office.sql");
297 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql");
299 $query = qq|UPDATE defaults SET coa = ?|;
300 do_query($form, $dbh, $query, map { $form->{$_} } qw(chart));
304 # update new database
305 $self->dbupdate2(form => $form, updater => $dbupdater, database => $form->{db}, silent => 1);
307 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
310 $query = "SELECT * FROM currencies WHERE name = ?";
311 my $curr = selectfirst_hashref_query($form, $dbh, $query, $form->{defaultcurrency});
313 do_query($form, $dbh, "INSERT INTO currencies (name) VALUES (?)", $form->{defaultcurrency});
314 $curr = selectfirst_hashref_query($form, $dbh, $query, $form->{defaultcurrency});
317 $query = qq|UPDATE defaults SET
318 accounting_method = ?,
319 profit_determination = ?,
320 inventory_system = ?,
325 feature_erfolgsrechnung = ?,
326 feature_eurechnung = ?,
329 do_query($form, $dbh, $query,
330 $form->{accounting_method},
331 $form->{profit_determination},
332 $form->{inventory_system},
333 $form->parse_amount(\%::myconfig, $form->{precision_as_number}),
335 $form->{feature_balance},
336 $form->{feature_datev},
337 $form->{feature_erfolgsrechnung},
338 $form->{feature_eurechnung},
339 $form->{feature_ustva}
344 $main::lxdebug->leave_sub();
348 $main::lxdebug->enter_sub();
350 my ($self, $form) = @_;
351 $form->{db} =~ s/\"//g;
353 &dbconnect_vars($form, $form->{dbdefault});
354 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
356 my $query = qq|DROP DATABASE "$form->{db}"|;
357 do_query($form, $dbh, $query);
361 $main::lxdebug->leave_sub();
365 $main::lxdebug->enter_sub(2);
367 my (@v, $version, $i);
369 @v = split(/\./, $_[0]);
370 while (scalar(@v) < 4) {
374 for ($i = 0; $i < 4; $i++) {
379 $main::lxdebug->leave_sub(2);
383 sub cmp_script_version {
384 my ($a_from, $a_to, $b_from, $b_to);
385 my ($i, $res_a, $res_b);
386 my ($my_a, $my_b) = do { no warnings 'once'; ($a, $b) };
388 $my_a =~ s/.*-upgrade-//;
390 $my_b =~ s/.*-upgrade-//;
392 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
393 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
395 $res_a = calc_version($my_a_from);
396 $res_b = calc_version($my_b_from);
398 if ($res_a == $res_b) {
399 $res_a = calc_version($my_a_to);
400 $res_b = calc_version($my_b_to);
403 return $res_a <=> $res_b;
406 sub create_schema_info_table {
407 $main::lxdebug->enter_sub();
409 my ($self, $form, $dbh) = @_;
411 my $query = "SELECT tag FROM schema_info LIMIT 1";
412 if (!$dbh->do($query)) {
415 qq|CREATE TABLE schema_info (| .
418 qq| itime timestamp DEFAULT now(), | .
419 qq| PRIMARY KEY (tag))|;
420 $dbh->do($query) || $form->dberror($query);
423 $main::lxdebug->leave_sub();
427 my ($self, %params) = @_;
429 my $form = $params{form};
430 my $dbupdater = $params{updater};
431 my $db = $params{database};
432 my $silent = $params{silent};
434 map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} };
436 &dbconnect_vars($form, $db);
438 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
440 $dbh->do($form->{dboptions}) if ($form->{dboptions});
442 $self->create_schema_info_table($form, $dbh);
444 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
446 $::lxdebug->log_time("DB upgrades commencing");
448 foreach my $control (@upgradescripts) {
449 # Apply upgrade. Control will only return to us if the upgrade has
450 # been applied correctly and if the update has not requested user
452 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
453 print $form->parse_html_template("dbupgrade/upgrade_message2", $control) unless $silent;
455 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
458 $::lxdebug->log_time("DB upgrades finished");
467 sub get_default_myconfig {
468 my ($self_or_class, %user_config) = @_;
469 my $defaults = SL::DefaultManager->new($::lx_office_conf{system}->{default_manager});
472 countrycode => $defaults->language('de'),
473 css_path => 'css', # Needed for menunew, see SL::Layout::Base::get_stylesheet_for_user
474 dateformat => $defaults->dateformat('dd.mm.yy'),
475 numberformat => $defaults->numberformat('1.000,00'),
476 stylesheet => $defaults->stylesheet('kivitendo.css'),
477 timeformat => $defaults->timeformat('hh:mm'),