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