438d2ee7f7071210fb62ec3ed23d6efd5333f7ad
[kivitendo-erp.git] / sql / Pg-upgrade2 / umstellung_eur.pl
1 # @tag: umstellung_eur
2 # @description: Variable eur umstellen: bitte in doc/dokumentation.pdf das entsprechende Kapitel zur Konfiguration von EUR lesen
3 # @depends: release_2_6_3
4 # @charset: utf-8
5
6 # this script relies on $eur still being set in kivitendo.conf, and the
7 # variable available in $::lx_office_conf{system}->{eur}
8
9 use utf8;
10 #use strict;
11 use Data::Dumper;
12 die("This script cannot be run from the command line.") unless ($main::form);
13
14 sub mydberror {
15   my ($msg) = @_;
16   die($dbup_locale->text("Database update error:") .
17       "<br>$msg<br>" . $DBI::errstr);
18 }
19
20 sub do_query {
21   my ($query, $may_fail) = @_;
22
23   if (!$dbh->do($query)) {
24     mydberror($query) unless ($may_fail);
25     $dbh->rollback();
26     $dbh->begin_work();
27   }
28 }
29
30 sub do_update {
31
32   # check if accounting_method has already been set (new database), if so return
33   # only set variables according to eur for update of existing database
34
35
36   foreach my $column (qw(accounting_method inventory_system profit_determination)) {
37     # this query will fail if columns already exist (new database)
38     do_query(qq|ALTER TABLE defaults ADD COLUMN ${column} TEXT|, 1);
39   }
40
41   my $accounting_method;
42   my $inventory_system;
43   my $profit_determination;
44
45   # check current configuration and set default variables accordingly, so that
46   # kivitendo behaviour isn't changed by this update
47
48   if ($::lx_office_conf{system}->{eur} == 0 ) {
49     $accounting_method = 'accrual';
50     $inventory_system = 'perpetual';
51     $profit_determination = 'balance';
52   } elsif ( $::lx_office_conf{system}->{eur} == 1 ) {
53     $accounting_method = 'cash';
54     $inventory_system = 'periodic';
55     $profit_determination = 'income';
56   } else {
57     die "illegal configuration of eur, must be 0 or 1, not " . $::lx_office_conf{system}->{eur} . "\n";
58     # or maybe just return 1, dont do anything, because we assume everything is
59     # already set, or has maybe already been deleted
60   };
61
62   # only set parameters if they haven't already been set (this in only the case
63   # when upgrading)
64
65   my $update_eur = "UPDATE defaults set accounting_method = '$accounting_method' where accounting_method is null;" .
66                    "UPDATE defaults set inventory_system = '$inventory_system' where inventory_system is null; " .
67                    "UPDATE defaults set profit_determination = '$profit_determination' where profit_determination is null;";
68   do_query($update_eur);
69
70   return 1;
71 }
72
73 return do_update();
74