AM.pm::get_warehouse: keinen teuren Cross Join
[kivitendo-erp.git] / SL / AM.pm
index e2ea0be..adb240d 100644 (file)
--- a/SL/AM.pm
+++ b/SL/AM.pm
@@ -1021,7 +1021,7 @@ sub load_template {
     close(TEMPLATE);
   }
 
-  $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
+  $content = Encode::decode('utf-8-strict', $content);
 
   $main::lxdebug->leave_sub();
 
@@ -1038,7 +1038,7 @@ sub save_template {
   my $error = "";
 
   if (open(TEMPLATE, ">", $filename)) {
-    $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
+    $content = Encode::encode('utf-8-strict', $content);
     $content =~ s/\r\n/\n/g;
     print(TEMPLATE $content);
     close(TEMPLATE);
@@ -1785,11 +1785,12 @@ sub save_warehouse {
            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
   if (0 < $form->{number_of_new_bins}) {
+    my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
     $sth   = prepare_query($form, $dbh, $query);
 
     foreach my $i (1..$form->{number_of_new_bins}) {
-      do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
+      do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
     }
 
     $sth->finish();
@@ -1899,15 +1900,14 @@ sub get_warehouse {
 
   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
-  $query = qq|SELECT b.*, EXISTS
-                (SELECT i.warehouse_id, p.warehouse_id
-                 FROM inventory i, parts p
-                 WHERE i.bin_id = b.id
-                 OR    p.bin_id = b.id
-                 LIMIT 1)
-                AS in_use
-              FROM bin b
-              WHERE b.warehouse_id = ?|;
+  $query = <<SQL;
+    SELECT b.*,
+      (   EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
+       OR EXISTS(SELECT p.bin_id FROM parts     p WHERE p.bin_id = b.id LIMIT 1))
+      AS in_use
+    FROM bin b
+    WHERE b.warehouse_id = ?
+SQL
 
   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));