3 # Datenbankupgrade: Einfuehrung von Einheiten
 
   5 die("This script cannot be run from the command line.") unless ($main::form);
 
   9   die($dbup_locale->text("Database update error:") .
 
  10       "<br>$msg<br>" . $DBI::errstr);
 
  16   print $main::form->parse_html_template("dbupgrade/units_error", { "message" => $msg });
 
  21   my ($units, $unit_name, $factor) = @_;
 
  23   $factor = 1 unless ($factor);
 
  25   my $unit = $units->{$unit_name};
 
  27   if (!defined($unit) || !$unit->{"base_unit"} ||
 
  28       ($unit_name eq $unit->{"base_unit"})) {
 
  29     return ($unit_name, $factor);
 
  32   return get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
  36   my ($myconfig, $form, $type, $prefix) = @_;
 
  38   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
  41     $query .= " WHERE (type = ?)";
 
  45   my $sth = $dbh->prepare($query);
 
  46   $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
  49   while (my $ref = $sth->fetchrow_hashref()) {
 
  50     $units->{$ref->{"name"}} = $ref;
 
  54   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
  55   $sth = $dbh->prepare($query_lang);
 
  56   $sth->execute() || $form->dberror($query_lang);
 
  58   while ($ref = $sth->fetchrow_hashref()) {
 
  59     push(@languages, $ref);
 
  63   foreach my $unit (values(%{$units})) {
 
  64     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = get_base_unit($units, $unit->{"name"});
 
  70 sub unit_select_data {
 
  71   my ($units, $selected, $empty_entry) = @_;
 
  76     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
  79   foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
 
  80     push(@{$select}, { "name" => $unit,
 
  81                        "base_unit" => $units->{$unit}->{"base_unit"},
 
  82                        "factor" => $units->{$unit}->{"factor"},
 
  83                        "selected" => ($unit eq $selected) ? "selected" : "" });
 
  89 sub update_units_add_unit {
 
  90   my $form = $main::form;
 
  92   return 0 unless ($form->{"new_name"});
 
  94   return myshowerror($dbup_locale->text("The name is missing."))
 
  95     if ($form->{"new_name"} eq "");
 
  96   my $units = retrieve_units(\%dbup_myconfig, $form);
 
  97   return myshowerror($dbup_locale->text("A unit with this name does already exist."))
 
  98     if ($units->{$form->{"new_name"}});
 
  99   $units = retrieve_units(\%dbup_myconfig, $form, $form->{"unit_type"});
 
 101   my ($base_unit, $factor);
 
 102   if ($form->{"new_base_unit"}) {
 
 103     return myshowerror($dbup_locale->text("The base unit does not exist."))
 
 104       unless (defined($units->{$form->{"new_base_unit"}}));
 
 106     return myshowerror($dbup_locale->text("The factor is missing."))
 
 107       if ($form->{"new_factor"} eq "");
 
 108     $factor = $form->parse_amount(\%dbup_myconfig, $form->{"new_factor"});
 
 109     return myshowerror($dbup_locale->text("The factor is missing."))
 
 111     $base_unit = $form->{"new_base_unit"};
 
 114   my $query = "INSERT INTO units " .
 
 115     "(name, base_unit, factor, type) " .
 
 116     "VALUES (?, ?, ?, ?)";
 
 117   $dbh->do($query, undef, $form->{"new_name"}, $base_unit, $factor,
 
 118            $form->{"unit_type"}) ||
 
 120               " ($form->{new_name}, $base_unit, $factor, $form->{unit_type})");
 
 124   $form->{"saved_message"} = $dbup_locale->text("The unit has been saved.");
 
 129 sub update_units_assign_units {
 
 130   my ($query, $sth, @values);
 
 132   my $form = $main::form;
 
 134   foreach my $table (qw(parts invoice orderitems rmaitems)) {
 
 135     $query = "UPDATE $table SET unit = ? WHERE lower(unit) = ?";
 
 136     $sth = $dbh->prepare($query);
 
 138     for (my $i = 1; $i <= $form->{"rowcount"}; $i++) {
 
 139       next unless ($form->{"new_unit_$i"} && $form->{"old_unit_$i"});
 
 140       @values = ($form->{"new_unit_$i"}, lc($form->{"old_unit_$i"}));
 
 141       $sth->execute(@values) ||
 
 142         mydberror($query . " (" . join(", ", @values) . ")");
 
 151 sub update_units_assign_known {
 
 152   my $form = $main::form;
 
 154   my %unit_name_mapping = (
 
 168   foreach my $k (keys(%unit_name_mapping)) {
 
 169     $form->{"old_unit_$i"} = $k;
 
 170     $form->{"new_unit_$i"} = $unit_name_mapping{$k};
 
 173   $form->{"rowcount"} = scalar(keys(%unit_name_mapping));
 
 175   update_units_assign_units();
 
 178 sub update_units_steps_1_2 {
 
 179   my (%unknown_dimension_units, %unknown_service_units);
 
 181   my $form = $main::form;
 
 183   foreach my $table (qw(parts invoice orderitems rmaitems)) {
 
 184     my ($query, $sth, $ref);
 
 186     if ($table eq "parts") {
 
 187       $query = "SELECT unit, inventory_accno_id, assembly FROM parts " .
 
 188         "WHERE NOT ((unit = '') OR unit ISNULL OR " .
 
 189         "           unit IN (SELECT name FROM units))";
 
 192       $query = "SELECT t.unit, p.inventory_accno_id, p.assembly " .
 
 194         "LEFT JOIN parts p ON p.id = t.parts_id " .
 
 195         "WHERE NOT ((t.unit = '') OR t.unit ISNULL OR " .
 
 196         "           t.unit IN (SELECT name FROM units))";
 
 198     $sth = $dbh->prepare($query);
 
 199     $sth->execute() || mydberror($query);
 
 201     while ($ref = $sth->fetchrow_hashref()) {
 
 202       if ($ref->{"inventory_accno_id"} || $ref->{"assembly"}) {
 
 203         $unknown_dimension_units{$ref->{"unit"}} = 1;
 
 206         $unknown_service_units{$ref->{"unit"}} = 1;
 
 213   if (scalar(keys(%unknown_dimension_units)) != 0) {
 
 214     my $units = retrieve_units(\%dbup_myconfig, $form, "dimension");
 
 215     my $ddbox = unit_select_data($units, undef, 1);
 
 218     map({ push(@unknown_parts, { "name" => $_, "NEW_UNITS" => $ddbox }); }
 
 219         sort({ lc($a) cmp lc($b) } keys(%unknown_dimension_units)));
 
 221     print $form->parse_html_template("dbupgrade/units_parts",
 
 222                                      { "NEW_BASE_UNIT_DDBOX" => $ddbox,
 
 223                                        "UNKNOWN_PART_UNITS"  => \@unknown_parts,
 
 229     print $form->parse_html_template("dbupgrade/units_parts_done");
 
 232   if (scalar(keys(%unknown_service_units)) != 0) {
 
 233     my $units = retrieve_units(\%dbup_myconfig, $form, "service");
 
 234     my $ddbox = unit_select_data($units, undef, 1);
 
 236     my @unknown_services;
 
 237     map({ push(@unknown_services, { "name" => $_, "NEW_UNITS" => $ddbox }); }
 
 238         sort({ lc($a) cmp lc($b) } keys(%unknown_service_units)));
 
 240     print $form->parse_html_template("dbupgrade/units_services",
 
 241                                      { "NEW_BASE_UNIT_DDBOX" => $ddbox,
 
 242                                        "UNKNOWN_PART_UNITS"  => \@unknown_services,
 
 248     print $form->parse_html_template("dbupgrade/units_services_done");
 
 254 sub update_units_step_3 {
 
 255   my $form = $main::form;
 
 257   my $query = "SELECT ";
 
 258   foreach my $table (qw(parts invoice orderitems rmaitems)) {
 
 259     $query .= "(SELECT COUNT(*) FROM $table " .
 
 260       "WHERE (unit ISNULL) OR (unit = '')) +";
 
 262   substr($query, -1, 1) = "AS has_unassigned";
 
 263   my ($has_unassigned) = $dbh->selectrow_array($query);
 
 265   if ($has_unassigned) {
 
 266     my $dimension_units = retrieve_units(\%dbup_myconfig, $form,
 
 268     my $dimension_ddbox = unit_select_data($dimension_units);
 
 270     my $service_units = retrieve_units(\%dbup_myconfig, $form, "service");
 
 271     my $service_ddbox = unit_select_data($service_units);
 
 273     print $form->parse_html_template("dbupgrade/units_set_default",
 
 274                                      { "DIMENSION_DDBOX" => $dimension_ddbox,
 
 275                                        "SERVICE_DDBOX"   => $service_ddbox });
 
 279     print $form->parse_html_template("dbupgrade/units_set_default_done");
 
 284 sub update_units_set_default {
 
 285   my $form = $main::form;
 
 287   foreach my $table (qw(parts invoice orderitems rmaitems)) {
 
 288     my $base_query = "UPDATE $table SET unit = " .
 
 289       $dbh->quote($form->{"default_service_unit"}) . " " .
 
 290       "WHERE ((unit ISNULL) OR (unit = '')) AND ";
 
 293     if ($table eq "parts") {
 
 294       $query = "UPDATE $table SET unit = " .
 
 295         $dbh->quote($form->{"default_dimension_unit"}) . " " .
 
 296         "WHERE ((unit ISNULL) OR (unit = '')) AND " .
 
 297         "(assembly OR (inventory_accno_id > 0))";
 
 299       $query = "UPDATE $table SET unit = " .
 
 300         $dbh->quote($form->{"default_dimension_unit"}) . " " .
 
 301         "WHERE ((unit ISNULL) OR (unit = '')) AND " .
 
 302         "parts_id IN (SELECT id FROM parts WHERE " .
 
 303         "(assembly OR (inventory_accno_id > 0)))";
 
 306     $dbh->do($query) || mydberror($query);
 
 308     if ($table eq "parts") {
 
 309       $query = "UPDATE $table SET unit = " .
 
 310         $dbh->quote($form->{"default_service_unit"}) . " " .
 
 311         "WHERE ((unit ISNULL) OR (unit = '')) AND " .
 
 312         "((inventory_accno_id ISNULL) OR (inventory_accno_id = 0)) AND " .
 
 315       $query = "UPDATE $table SET unit = " .
 
 316         $dbh->quote($form->{"default_service_unit"}) . " " .
 
 317         "WHERE ((unit ISNULL) OR (unit = '')) AND " .
 
 318         "parts_id IN (SELECT id FROM parts " .
 
 319         "WHERE ((inventory_accno_id ISNULL) OR (inventory_accno_id = 0)) " .
 
 323     $dbh->do($query) || mydberror($query);
 
 328   my $form = $main::form;
 
 332   print $form->parse_html_template("dbupgrade/units_header");
 
 334   if ($form->{"action2"} eq "add_unit") {
 
 335     $res = update_units_add_unit();
 
 336     return $res if ($res);
 
 338   } elsif ($form->{"action2"} eq "assign_units") {
 
 339     update_units_assign_units();
 
 341   } elsif ($form->{"action2"} eq "set_default") {
 
 342     update_units_set_default();
 
 346   update_units_assign_known();
 
 348   $res = update_units_steps_1_2();
 
 349   return $res if ($res);
 
 351   return update_units_step_3();