- $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
- "FROM units_language ul " .
- "LEFT JOIN language l ON ul.language_id = l.id " .
- "WHERE ul.unit = ?";
- $sth = $dbh->prepare($query_lang);
-
- foreach my $unit (values(%{$units})) {
- ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
-
- $unit->{"LANGUAGES"} = {};
- foreach my $lang (@languages) {
- $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
- }
-
- $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
- while ($ref = $sth->fetchrow_hashref()) {
- map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
- }
- }
- $sth->finish();
-
- $dbh->disconnect();
-
- $main::lxdebug->leave_sub();
-
- return $units;
-}
-
-sub translate_units {
- $main::lxdebug->enter_sub();
-
- my ($self, $form, $template_code, $unit, $amount) = @_;
-
- my $units = $self->retrieve_units(\%main::myconfig, $form);
-
- my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
- my $new_unit = $unit;
- if ($h) {
- if (($amount != 1) && $h->{"localized_plural"}) {
- $new_unit = $h->{"localized_plural"};
- } elsif ($h->{"localized"}) {
- $new_unit = $h->{"localized"};
- }
- }
-
- $main::lxdebug->leave_sub();
-
- return $new_unit;
-}
-
-sub units_in_use {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form, $units) = @_;
-
- my $dbh = $form->dbconnect($myconfig);
-
- foreach my $unit (values(%{$units})) {
- my $base_unit = $unit->{"original_base_unit"};
- while ($base_unit) {
- $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
- push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
- $base_unit = $units->{$base_unit}->{"original_base_unit"};
- }
- }
-
- foreach my $unit (values(%{$units})) {
- $unit->{"in_use"} = 0;
- map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
-
- foreach my $table (qw(parts invoice orderitems)) {
- my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
-
- if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
- $query .= "= " . $dbh->quote($unit->{"name"});
- } else {
- $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," . join(",", @{$unit->{"DEPENDING_UNITS"}}) . ")";
- }
-
- my ($count) = $dbh->selectrow_array($query);
- $form->dberror($query) if ($dbh->err);
-
- if ($count) {
- $unit->{"in_use"} = 1;
- last;
- }
- }
- }
-
- $dbh->disconnect();