2 # @description: Erstellt neue Tabelle currencies. Währungen können dann einfacher eingegeben und unkritisch geändert werden.
3 # @depends: release_3_0_0 rm_whitespaces
5 package SL::DBUpgrade2::currencies;
10 use parent qw(SL::DBUpgrade2::Base);
15 #Check wheather default currency exists
16 my $query = qq|SELECT curr FROM defaults|;
17 my ($currencies) = $self->dbh->selectrow_array($query);
19 if (length($currencies) == 0 and length($main::form->{defaultcurrency}) == 0){
20 print_no_default_currency();
23 if (length($main::form->{defaultcurrency}) == 0){
24 $main::form->{defaultcurrency} = (split m/:/, $currencies)[0];
27 my @currency_array = grep {$_ ne '' } split m/:/, $currencies;
29 $query = qq|SELECT DISTINCT curr FROM ar
31 SELECT DISTINCT curr FROM ap
33 SELECT DISTINCT curr FROM oe
35 SELECT DISTINCT curr FROM customer
37 SELECT DISTINCT curr FROM delivery_orders
39 SELECT DISTINCT curr FROM exchangerate
41 SELECT DISTINCT curr FROM vendor|;
43 my $sth = $self->dbh->prepare($query);
44 $sth->execute || $self->dberror($query);
46 $main::form->{ORPHANED_CURRENCIES} = [];
49 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
50 next unless length($ref->{curr}) > 0;
52 foreach my $key (split(/:/, $currencies)) {
53 if ($ref->{curr} eq $key) {
59 push @{ $main::form->{ORPHANED_CURRENCIES} }, $ref;
60 $main::form->{ORPHANED_CURRENCIES}[$rowcount]->{name} = "curr_$rowcount";
67 if (scalar @{ $main::form->{ORPHANED_CURRENCIES} } > 0 and not ($main::form->{continue_options})) {
68 print_orphaned_currencies();
72 if (defined $::form->{continue_options}) {
73 if ($::form->{continue_options} eq 'break_up') {
77 if ($::form->{continue_options} eq 'insert') {
78 for my $i (0..($rowcount-1)){
79 push @currency_array, $main::form->{"curr_$i"};
81 create_and_fill_table($self, @currency_array);
86 if ($::form->{continue_options} eq 'replace') {
87 for my $i (0..($rowcount - 1)){
89 for my $item (@currency_array){
90 if ($main::form->{"curr_$i"} eq $item){
92 $query = qq|DELETE FROM exchangerate WHERE curr = '| . $main::form->{"old_curr_$i"} . qq|'|;
93 $self->db_query($query);
94 $query = qq|UPDATE ap SET curr = '| . $main::form->{"curr_$i"} . qq|' WHERE curr = '| . $main::form->{"old_curr_$i"} . qq|'|;
95 $self->db_query($query);
96 $query = qq|UPDATE ar SET curr = '| . $main::form->{"curr_$i"} . qq|' WHERE curr = '| . $main::form->{"old_curr_$i"} . qq|'|;
97 $self->db_query($query);
98 $query = qq|UPDATE oe SET curr = '| . $main::form->{"curr_$i"} . qq|' WHERE curr = '| . $main::form->{"old_curr_$i"} . qq|'|;
99 $self->db_query($query);
100 $query = qq|UPDATE customer SET curr = '| . $main::form->{"curr_$i"} . qq|' WHERE curr = '| . $main::form->{"old_curr_$i"} . qq|'|;
101 $self->db_query($query);
102 $query = qq|UPDATE delivery_orders SET curr = '| . $main::form->{"curr_$i"} . qq|' WHERE curr = '| . $main::form->{"old_curr_$i"} . qq|'|;
103 $self->db_query($query);
104 $query = qq|UPDATE vendor SET curr = '| . $main::form->{"curr_$i"} . qq|' WHERE curr = '| . $main::form->{"old_curr_$i"} . qq|'|;
105 $self->db_query($query);
109 if ($still_orphaned){
110 $main::form->{continue_options} = '';
114 create_and_fill_table($self, @currency_array);
119 #No orphaned currencies, so create table:
120 create_and_fill_table($self, @currency_array);
124 sub create_and_fill_table {
126 #Create an fill table currencies:
127 my $query = qq|CREATE TABLE currencies (id SERIAL PRIMARY KEY,
128 name TEXT NOT NULL UNIQUE)|;
129 $self->db_query($query);
130 foreach my $item ( @_ ) {
131 $query = qq|INSERT INTO currencies (name) VALUES ('| . $item . qq|')|;
132 $self->db_query($query);
135 #Set default currency if no currency was chosen:
136 $query = qq|UPDATE ap SET curr = '| . $main::form->{"defaultcurrency"} . qq|' WHERE curr IS NULL or curr='';|;
137 $query .= qq|UPDATE ar SET curr = '| . $main::form->{"defaultcurrency"} . qq|' WHERE curr IS NULL or curr='';|;
138 $query .= qq|UPDATE oe SET curr = '| . $main::form->{"defaultcurrency"} . qq|' WHERE curr IS NULL or curr='';|;
139 $query .= qq|UPDATE customer SET curr = '| . $main::form->{"defaultcurrency"} . qq|' WHERE curr IS NULL or curr='';|;
140 $query .= qq|UPDATE delivery_orders SET curr = '| . $main::form->{"defaultcurrency"} . qq|' WHERE curr IS NULL or curr='';|;
141 $query .= qq|UPDATE vendor SET curr = '| . $main::form->{"defaultcurrency"} . qq|' WHERE curr IS NULL or curr='';|;
142 $query .= qq|DELETE FROM exchangerate WHERE curr IS NULL or curr='';|;
143 $self->db_query($query);
145 #Check wheather defaultcurrency is already in table currencies:
146 $query = qq|SELECT name FROM currencies WHERE name = '| . $main::form->{defaultcurrency} . qq|'|;
147 my ($insert_default) = $self->dbh->selectrow_array($query);
149 if (!$insert_default) {
150 $query = qq|INSERT INTO currencies (name) VALUES ('| . $main::form->{defaultcurrency} . qq|')|;
151 $self->db_query($query);
154 #Create a new columns currency_id and update with curr.id:
155 $query = qq|ALTER TABLE ap ADD currency_id INTEGER;
156 ALTER TABLE ar ADD currency_id INTEGER;
157 ALTER TABLE oe ADD currency_id INTEGER;
158 ALTER TABLE customer ADD currency_id INTEGER;
159 ALTER TABLE delivery_orders ADD currency_id INTEGER;
160 ALTER TABLE exchangerate ADD currency_id INTEGER;
161 ALTER TABLE vendor ADD currency_id INTEGER;
162 ALTER TABLE defaults ADD currency_id INTEGER;|;
163 $self->db_query($query);
164 #Set defaultcurrency:
165 $query = qq|UPDATE defaults SET currency_id= (SELECT id FROM currencies WHERE name = '| . $main::form->{defaultcurrency} . qq|')|;
166 $self->db_query($query);
167 $query = qq|UPDATE ap SET currency_id = (SELECT id FROM currencies c WHERE c.name = ap.curr);
168 UPDATE ar SET currency_id = (SELECT id FROM currencies c WHERE c.name = ar.curr);
169 UPDATE oe SET currency_id = (SELECT id FROM currencies c WHERE c.name = oe.curr);
170 UPDATE customer SET currency_id = (SELECT id FROM currencies c WHERE c.name = customer.curr);
171 UPDATE delivery_orders SET currency_id = (SELECT id FROM currencies c WHERE c.name = delivery_orders.curr);
172 UPDATE exchangerate SET currency_id = (SELECT id FROM currencies c WHERE c.name = exchangerate.curr);
173 UPDATE vendor SET currency_id = (SELECT id FROM currencies c WHERE c.name = vendor.curr);|;
174 $self->db_query($query);
177 $query = qq|ALTER TABLE ap DROP COLUMN curr;
178 ALTER TABLE ar DROP COLUMN curr;
179 ALTER TABLE oe DROP COLUMN curr;
180 ALTER TABLE customer DROP COLUMN curr;
181 ALTER TABLE delivery_orders DROP COLUMN curr;
182 ALTER TABLE exchangerate DROP COLUMN curr;
183 ALTER TABLE vendor DROP COLUMN curr;
184 ALTER TABLE defaults DROP COLUMN curr;|;
185 $self->db_query($query);
187 #Set NOT NULL constraints:
188 $query = qq|ALTER TABLE ap ALTER COLUMN currency_id SET NOT NULL;
189 ALTER TABLE ar ALTER COLUMN currency_id SET NOT NULL;
190 ALTER TABLE oe ALTER COLUMN currency_id SET NOT NULL;
191 ALTER TABLE customer ALTER COLUMN currency_id SET NOT NULL;
192 ALTER TABLE delivery_orders ALTER COLUMN currency_id SET NOT NULL;
193 ALTER TABLE exchangerate ALTER COLUMN currency_id SET NOT NULL;
194 ALTER TABLE vendor ALTER COLUMN currency_id SET NOT NULL;
195 ALTER TABLE defaults ALTER COLUMN currency_id SET NOT NULL;|;
196 $self->db_query($query);
199 $query = qq|ALTER TABLE ap ADD FOREIGN KEY (currency_id) REFERENCES currencies(id);
200 ALTER TABLE ar ADD FOREIGN KEY (currency_id) REFERENCES currencies(id);
201 ALTER TABLE oe ADD FOREIGN KEY (currency_id) REFERENCES currencies(id);
202 ALTER TABLE customer ADD FOREIGN KEY (currency_id) REFERENCES currencies(id);
203 ALTER TABLE delivery_orders ADD FOREIGN KEY (currency_id) REFERENCES currencies(id);
204 ALTER TABLE exchangerate ADD FOREIGN KEY (currency_id) REFERENCES currencies(id);
205 ALTER TABLE vendor ADD FOREIGN KEY (currency_id) REFERENCES currencies(id);
206 ALTER TABLE defaults ADD FOREIGN KEY (currency_id) REFERENCES currencies(id);|;
207 $self->db_query($query);
211 sub print_no_default_currency {
212 print $main::form->parse_html_template("dbupgrade/no_default_currency");
215 sub print_orphaned_currencies {
216 print $main::form->parse_html_template("dbupgrade/orphaned_currencies");