Tabell lead erzeugen
[kivitendo-erp.git] / sql / Pg-upgrade / Pg-upgrade-2.2.0.25-2.2.0.26.pl
1 #!/usr/bin/perl
2
3 die("This script cannot be run from the command line.") unless ($main::form);
4
5 use SL::AM;
6
7 sub mydberror {
8   my ($msg) = @_;
9   die($dbup_locale->text("Database update error:") .
10       "<br>$msg<br>" . $DBI::errstr);
11 }
12
13 sub myshowerror {
14   my ($msg) = @_;
15
16   print($main::form->parse_html_template("dbupgrade/units_error",
17                                          { "message" => $msg }));
18   return 2;
19 }
20
21 sub update_units_add_unit {
22   my $form = $main::form;
23
24   return 0 unless ($form->{"new_name"});
25
26   return myshowerror($dbup_locale->text("The name is missing."))
27     if ($form->{"new_name"} eq "");
28   my $units = AM->retrieve_units(\%dbup_myconfig, $form);
29   return myshowerror($dbup_locale->text("A unit with this name does already exist."))
30     if ($units->{$form->{"new_name"}});
31   $units = AM->retrieve_units(\%dbup_myconfig, $form, $form->{"unit_type"});
32
33   my ($base_unit, $factor);
34   if ($form->{"new_base_unit"}) {
35     return myshowerror($dbup_locale->text("The base unit does not exist."))
36       unless (defined($units->{$form->{"new_base_unit"}}));
37
38     return myshowerror($dbup_locale->text("The factor is missing."))
39       if ($form->{"new_factor"} eq "");
40     $factor = $form->parse_amount(\%dbup_myconfig, $form->{"new_factor"});
41     return myshowerror($dbup_locale->text("The factor is missing."))
42       unless ($factor);
43     $base_unit = $form->{"new_base_unit"};
44   }
45
46   my $query = "INSERT INTO units " .
47     "(name, base_unit, factor, type) " .
48     "VALUES (?, ?, ?, ?)";
49   $dbh->do($query, undef, $form->{"new_name"}, $base_unit, $factor,
50            $form->{"unit_type"}) ||
51     mydberror($query .
52               " ($form->{new_name}, $base_unit, $factor, $form->{unit_type})");
53   $dbh->commit();
54   $dbh->begin_work();
55
56   $form->{"saved_message"} = $dbup_locale->text("The unit has been saved.");
57
58   return 0;
59 }
60
61 sub update_units_assign_units {
62   my ($query, $sth, @values);
63
64   my $form = $main::form;
65
66   foreach my $table (qw(parts invoice orderitems rmaitems)) {
67     $query = "UPDATE $table SET unit = ? WHERE lower(unit) = ?";
68     $sth = $dbh->prepare($query);
69
70     for (my $i = 1; $i <= $form->{"rowcount"}; $i++) {
71       next unless ($form->{"new_unit_$i"} && $form->{"old_unit_$i"});
72       @values = ($form->{"new_unit_$i"}, lc($form->{"old_unit_$i"}));
73       $sth->execute(@values) ||
74         mydberror($query . " (" . join(", ", @values) . ")");
75     }
76   }
77
78   $sth->finish();
79   $dbh->commit();
80   $dbh->begin_work();
81 }
82
83 sub update_units_assign_known {
84   my $form = $main::form;
85
86   my %unit_name_mapping = (
87     "st" => "Stck",
88     "st." => "Stck",
89     "stk" => "Stck",
90     "pc" => "Stck",
91     "pcs" => "Stck",
92     "ea" => "Stck",
93
94     "h" => "Std",
95     "stunde" => "Std",
96     "tage" => "Tag",
97     );
98
99   my $i = 1;
100   foreach my $k (keys(%unit_name_mapping)) {
101     $form->{"old_unit_$i"} = $k;
102     $form->{"new_unit_$i"} = $unit_name_mapping{$k};
103     $i++;
104   }
105   $form->{"rowcount"} = scalar(keys(%unit_name_mapping));
106
107   update_units_assign_units();
108 }
109
110 sub update_units_steps_1_2 {
111   my (%unknown_dimension_units, %unknown_service_units);
112
113   my $form = $main::form;
114
115   foreach my $table (qw(parts invoice orderitems rmaitems)) {
116     my ($query, $sth, $ref);
117
118     if ($table eq "parts") {
119       $query = "SELECT unit, inventory_accno_id FROM parts " .
120         "WHERE NOT ((unit = '') OR unit ISNULL OR " .
121         "           unit IN (SELECT name FROM units))";
122
123     } else {
124       $query = "SELECT t.unit, p.inventory_accno_id " .
125         "FROM $table t " .
126         "LEFT JOIN parts p ON p.id = t.parts_id " .
127         "WHERE NOT ((t.unit = '') OR t.unit ISNULL OR " .
128         "           t.unit IN (SELECT name FROM units))";
129     }
130     $sth = $dbh->prepare($query);
131     $sth->execute() || mydberror($query);
132
133     while ($ref = $sth->fetchrow_hashref()) {
134       if ($ref->{"inventory_accno_id"}) {
135         $unknown_dimension_units{$ref->{"unit"}} = 1;
136
137       } else {
138         $unknown_service_units{$ref->{"unit"}} = 1;
139       }
140     }
141
142     $sth->finish();
143   }
144
145   if (scalar(keys(%unknown_dimension_units)) != 0) {
146     my $units = AM->retrieve_units(\%dbup_myconfig, $form, "dimension");
147     my $ddbox = AM->unit_select_data($units, undef, 1);
148
149     my @unknown_parts;
150     map({ push(@unknown_parts, { "name" => $_, "NEW_UNITS" => $ddbox }); }
151         sort({ lc($a) cmp lc($b) } keys(%unknown_dimension_units)));
152
153     print($form->parse_html_template("dbupgrade/units_parts",
154                                      { "NEW_BASE_UNIT_DDBOX" => $ddbox,
155                                        "UNKNOWN_PART_UNITS" => \@unknown_parts,
156                                      }));
157
158     return 2;
159
160   } else {
161     print($form->parse_html_template("dbupgrade/units_parts_done"));
162   }
163
164   if (scalar(keys(%unknown_service_units)) != 0) {
165     my $units = AM->retrieve_units(\%dbup_myconfig, $form, "service");
166     my $ddbox = AM->unit_select_data($units, undef, 1);
167
168     my @unknown_services;
169     map({ push(@unknown_services, { "name" => $_, "NEW_UNITS" => $ddbox }); }
170         sort({ lc($a) cmp lc($b) } keys(%unknown_service_units)));
171
172     print($form->parse_html_template("dbupgrade/units_services",
173                                      { "NEW_BASE_UNIT_DDBOX" => $ddbox,
174                                        "UNKNOWN_PART_UNITS" => \@unknown_services,
175                                      }));
176
177     return 2;
178
179   } else {
180     print($form->parse_html_template("dbupgrade/units_services_done"));
181   }
182
183   return 0;
184 }
185
186 sub update_units_step_3 {
187   my $form = $main::form;
188
189   my $query = "SELECT ";
190   foreach my $table (qw(parts invoice orderitems rmaitems)) {
191     $query .= "(SELECT COUNT(*) FROM $table " .
192       "WHERE (unit ISNULL) OR (unit = '')) +";
193   }
194   substr($query, -1, 1) = "AS has_unassigned";
195   my ($has_unassigned) = $dbh->selectrow_array($query);
196
197   if ($has_unassigned) {
198     my $dimension_units = AM->retrieve_units(\%dbup_myconfig, $form,
199                                              "dimension");
200     my $dimension_ddbox = AM->unit_select_data($dimension_units);
201
202     my $service_units = AM->retrieve_units(\%dbup_myconfig, $form, "service");
203     my $service_ddbox = AM->unit_select_data($service_units);
204
205     print($form->parse_html_template("dbupgrade/units_set_default",
206                                      { "DIMENSION_DDBOX" => $dimension_ddbox,
207                                        "SERVICE_DDBOX" => $service_ddbox }));
208     return 2;
209
210   } else {
211     print($form->parse_html_template("dbupgrade/units_set_default_done"));
212     return 1;
213   }
214 }
215
216 sub update_units_set_default {
217   my $form = $main::form;
218
219   foreach my $table (qw(parts invoice orderitems rmaitems)) {
220     my $base_query = "UPDATE $table SET unit = " .
221       $dbh->quote($form->{"default_service_unit"}) . " " .
222       "WHERE ((unit ISNULL) OR (unit = '')) AND ";
223     my $query;
224
225     if ($table eq "parts") {
226       $query = "UPDATE $table SET unit = " .
227         $dbh->quote($form->{"default_dimension_unit"}) . " " .
228         "WHERE ((unit ISNULL) OR (unit = '')) AND (inventory_accno_id > 0)";
229     } else {
230       $query = "UPDATE $table SET unit = " .
231         $dbh->quote($form->{"default_dimension_unit"}) . " " .
232         "WHERE ((unit ISNULL) OR (unit = '')) AND " .
233         "parts_id IN (SELECT id FROM parts WHERE (inventory_accno_id > 0))";
234     }
235
236     $dbh->do($query) || mydberror($query);
237
238     if ($table eq "parts") {
239       $query = "UPDATE $table SET unit = " .
240         $dbh->quote($form->{"default_service_unit"}) . " " .
241         "WHERE ((unit ISNULL) OR (unit = '')) AND " .
242         "(inventory_accno_id ISNULL) OR (inventory_accno_id = 0)";
243     } else {
244       $query = "UPDATE $table SET unit = " .
245         $dbh->quote($form->{"default_service_unit"}) . " " .
246         "WHERE ((unit ISNULL) OR (unit = '')) AND " .
247         "parts_id IN (SELECT id FROM parts " .
248         "WHERE (inventory_accno_id ISNULL) OR (inventory_accno_id = 0))";
249     }
250
251     $dbh->do($query) || mydberror($query);
252   }
253 }
254
255 sub update_units {
256   my $form = $main::form;
257
258   my $res;
259
260   print($form->parse_html_template("dbupgrade/units_header"));
261
262   if ($form->{"action2"} eq "add_unit") {
263     $res = update_units_add_unit();
264     return $res if ($res);
265
266   } elsif ($form->{"action2"} eq "assign_units") {
267     update_units_assign_units();
268
269   } elsif ($form->{"action2"} eq "set_default") {
270     update_units_set_default();
271
272   }
273
274   update_units_assign_known();
275
276   $res = update_units_steps_1_2();
277   return $res if ($res);
278
279   return update_units_step_3();
280 }
281
282 update_units();