Perl-Upgrade-Scripte: db_query nun auch mit Bind-Parametern
[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 package SL::DBUpgrade2::umstellung_eur;
5
6 use strict;
7 use utf8;
8
9 use parent qw(SL::DBUpgrade2::Base);
10
11 # this script relies on $eur still being set in kivitendo.conf, and the
12 # variable available in $::lx_office_conf{system}->{eur}
13
14 sub run {
15   my ($self) = @_;
16
17   # check if accounting_method has already been set (new database), if so return
18   # only set variables according to eur for update of existing database
19
20
21   foreach my $column (qw(accounting_method inventory_system profit_determination)) {
22     # this query will fail if columns already exist (new database)
23     $self->db_query(qq|ALTER TABLE defaults ADD COLUMN ${column} TEXT|, may_fail => 1);
24   }
25
26   my $accounting_method;
27   my $inventory_system;
28   my $profit_determination;
29
30   # check current configuration and set default variables accordingly, so that
31   # kivitendo behaviour isn't changed by this update
32
33   if ($::lx_office_conf{system}->{eur} == 0 ) {
34     $accounting_method = 'accrual';
35     $inventory_system = 'perpetual';
36     $profit_determination = 'balance';
37   } elsif ( $::lx_office_conf{system}->{eur} == 1 ) {
38     $accounting_method = 'cash';
39     $inventory_system = 'periodic';
40     $profit_determination = 'income';
41   } else {
42     die "illegal configuration of eur, must be 0 or 1, not " . $::lx_office_conf{system}->{eur} . "\n";
43     # or maybe just return 1, dont do anything, because we assume everything is
44     # already set, or has maybe already been deleted
45   };
46
47   # only set parameters if they haven't already been set (this in only the case
48   # when upgrading)
49
50   my $update_eur = "UPDATE defaults set accounting_method = '$accounting_method' where accounting_method is null;" .
51                    "UPDATE defaults set inventory_system = '$inventory_system' where inventory_system is null; " .
52                    "UPDATE defaults set profit_determination = '$profit_determination' where profit_determination is null;";
53   $self->db_query($update_eur);
54
55   return 1;
56 }
57
58 1;