Mehr Perlcode strict gemacht.
[kivitendo-erp.git] / SL / IS.pm
index 2cf1482..3232ddb 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
 
 package IS;
 
+use strict;
+
 use List::Util qw(max);
 
 use SL::AM;
+use SL::ARAP;
+use SL::CVar;
 use SL::Common;
 use SL::DBUtils;
+use SL::DO;
 use SL::MoreCommon;
+use SL::IC;
 use Data::Dumper;
 
 sub invoice_details {
@@ -70,12 +76,12 @@ sub invoice_details {
   }
 
   # sort items by partsgroup
-  for $i (1 .. $form->{rowcount}) {
-    $partsgroup = "";
-    if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
-      $partsgroup = $form->{"partsgroup_$i"};
-    }
-    push @partsgroup, [$i, $partsgroup];
+  for my $i (1 .. $form->{rowcount}) {
+#    $partsgroup = "";
+#    if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
+#      $partsgroup = $form->{"partsgroup_$i"};
+#    }
+#    push @partsgroup, [$i, $partsgroup];
     push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
   }
 
@@ -127,6 +133,10 @@ sub invoice_details {
   my $subtotal_header = 0;
   my $subposition = 0;
 
+  $form->{discount} = [];
+
+  IC->prepare_parts_for_printing();
+
   my @arrays =
     qw(runningnumber number description longdescription qty ship unit bin
        deliverydate_oe ordnumber_oe transdate_oe licensenumber validuntil
@@ -190,7 +200,7 @@ sub invoice_details {
       if ($form->{lizenzen}) {
         if ($form->{"licensenumber_$i"}) {
           $query = qq|SELECT licensenumber, validuntil FROM license WHERE id = ?|;
-          ($licensenumber, $validuntil) = selectrow_query($form, $dbh, $query, conv_i($form->{"licensenumber_$i"}));
+          my ($licensenumber, $validuntil) = selectrow_query($form, $dbh, $query, conv_i($form->{"licensenumber_$i"}));
           push(@{ $form->{licensenumber} }, $licensenumber);
           push(@{ $form->{validuntil} }, $locale->date($myconfig, $validuntil, 0));
 
@@ -207,8 +217,11 @@ sub invoice_details {
       my ($dec)         = ($sellprice =~ /\.(\d+)/);
       my $decimalplaces = max 2, length($dec);
 
-      my $discount             = $form->round_amount($form->{"qty_$i"} * $sellprice * $form->{"discount_$i"} / 100 / $price_factor->{factor}, $decimalplaces);
-      my $linetotal            = $form->round_amount($form->{"qty_$i"} * $sellprice * (100 - $form->{"discount_$i"}) / 100 / $price_factor->{factor}, 2);
+      my $parsed_discount      = $form->parse_amount($myconfig, $form->{"discount_$i"});
+      my $linetotal_exact      =                     $form->{"qty_$i"} * $sellprice * (100 - $parsed_discount) / 100 / $price_factor->{factor};
+      my $linetotal            = $form->round_amount($linetotal_exact, 2);
+      my $discount             = $form->round_amount($form->{"qty_$i"} * $sellprice * $parsed_discount / 100 / $price_factor->{factor} - ($linetotal - $linetotal_exact),
+                                                     $decimalplaces);
       my $nodiscount_linetotal = $form->round_amount($form->{"qty_$i"} * $sellprice / $price_factor->{factor}, 2);
       $form->{"netprice_$i"}   = $form->round_amount($form->{"qty_$i"} ? ($linetotal / $form->{"qty_$i"}) : 0, 2);
 
@@ -216,7 +229,7 @@ sub invoice_details {
 
       $linetotal = ($linetotal != 0) ? $linetotal : '';
 
-      push @{ $form->{discount} },   ($discount  != 0) ? $form->format_amount($myconfig, $discount * -1, $decimalplaces) : '';
+      push @{ $form->{discount} },   ($discount  != 0) ? $form->format_amount($myconfig, $discount * -1, 2) : '';
       push @{ $form->{p_discount} }, $form->{"discount_$i"};
 
       $form->{total}            += $linetotal;
@@ -268,32 +281,28 @@ sub invoice_details {
 
       if ($form->round_amount($taxrate, 7) == 0) {
         if ($form->{taxincluded}) {
-          foreach $item (@taxaccounts) {
-            $taxamount =
-              $form->round_amount($linetotal * $form->{"${item}_rate"} /
-                                    (1 + abs($form->{"${item}_rate"})),
-                                  2);
+          foreach my $accno (@taxaccounts) {
+            $taxamount            = $form->round_amount($linetotal * $form->{"${accno}_rate"} / (1 + abs($form->{"${accno}_rate"})), 2);
 
-            $taxaccounts{$item} += $taxamount;
-            $taxdiff            += $taxamount;
+            $taxaccounts{$accno} += $taxamount;
+            $taxdiff             += $taxamount;
 
-            $taxbase{$item} += $taxbase;
+            $taxbase{$accno}     += $taxbase;
           }
           $taxaccounts{ $taxaccounts[0] } += $taxdiff;
         } else {
-          foreach $item (@taxaccounts) {
-            $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
-            $taxbase{$item}     += $taxbase;
+          foreach my $accno (@taxaccounts) {
+            $taxaccounts{$accno} += $linetotal * $form->{"${accno}_rate"};
+            $taxbase{$accno}     += $taxbase;
           }
         }
       } else {
-        foreach $item (@taxaccounts) {
-          $taxaccounts{$item} +=
-            $taxamount * $form->{"${item}_rate"} / $taxrate;
-          $taxbase{$item} += $taxbase;
+        foreach my $accno (@taxaccounts) {
+          $taxaccounts{$accno} += $taxamount * $form->{"${accno}_rate"} / $taxrate;
+          $taxbase{$accno}     += $taxbase;
         }
       }
-      $tax_rate = $taxrate * 100;
+      my $tax_rate = $taxrate * 100;
       push(@{ $form->{tax_rate} }, qq|$tax_rate|);
       if ($form->{"assembly_$i"}) {
         $sameitem = "";
@@ -315,7 +324,7 @@ sub invoice_details {
              WHERE (a.bom = '1') AND (a.id = ?) $sortorder|;
         $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
 
-        while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+        while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
           if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
             map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
             $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
@@ -391,6 +400,7 @@ sub project_description {
   $main::lxdebug->enter_sub();
 
   my ($self, $dbh, $id) = @_;
+  my $form = \%main::form;
 
   my $query = qq|SELECT description FROM project WHERE id = ?|;
   my ($description) = selectrow_query($form, $dbh, $query, conv_i($id));
@@ -463,6 +473,12 @@ sub customer_details {
 
     map { $form->{"dv_$_"} = $ref->{$_} } keys %$ref;
   }
+
+  my $custom_variables = CVar->get_custom_variables('dbh'      => $dbh,
+                                                    'module'   => 'CT',
+                                                    'trans_id' => $form->{customer_id});
+  map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
+
   $dbh->disconnect;
 
   $main::lxdebug->leave_sub();
@@ -517,9 +533,7 @@ sub post_invoice {
   if ($form->{currency} eq $defaultcurrency) {
     $form->{exchangerate} = 1;
   } else {
-    $exchangerate =
-      $form->check_exchangerate($myconfig, $form->{currency},
-                                $form->{transdate}, 'buy');
+    $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
   }
 
   $form->{exchangerate} =
@@ -543,7 +557,7 @@ sub post_invoice {
       $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
     }
     my $basefactor;
-    my $basqty;
+    my $baseqty;
 
     $form->{"marge_percent_$i"} = $form->parse_amount($myconfig, $form->{"marge_percent_$i"}) * 1;
     $form->{"marge_total_$i"} = $form->parse_amount($myconfig, $form->{"marge_total_$i"}) * 1;
@@ -638,32 +652,11 @@ sub post_invoice {
 
       if ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"}) {
 
-        # adjust parts onhand quantity
-
         if ($form->{"assembly_$i"}) {
-
-          # do not update if assembly consists of all services
-          $query =
-            qq|SELECT sum(p.inventory_accno_id)
-               FROM parts p
-               JOIN assembly a ON (a.parts_id = p.id)
-               WHERE a.id = ?|;
-          $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
-
-          if ($sth->fetchrow_array) {
-            $form->update_balance($dbh, "parts", "onhand", qq|id = ?|,
-                                  $baseqty * -1, $form->{"id_$i"})
-              unless $form->{shipped};
-          }
-          $sth->finish;
-
           # record assembly item as allocated
           &process_assembly($dbh, $form, $form->{"id_$i"}, $baseqty);
-        } else {
-          $form->update_balance($dbh, "parts", "onhand", qq|id = ?|,
-                                $baseqty * -1, $form->{"id_$i"})
-            unless $form->{shipped};
 
+        } else {
           $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $baseqty, $basefactor, $i);
         }
       }
@@ -687,7 +680,7 @@ sub post_invoice {
                  $form->{"description_$i"}, $form->{"longdescription_$i"}, $form->{"qty_$i"},
                  $form->{"sellprice_$i"}, $fxsellprice,
                  $form->{"discount_$i"}, $allocated, 'f',
-                 $form->{"unit_$i"}, conv_date($form->{"deliverydate_$i"}), conv_i($form->{"project_id_$i"}),
+                 $form->{"unit_$i"}, conv_date($form->{"reqdate_$i"}), conv_i($form->{"project_id_$i"}),
                  $form->{"serialnumber_$i"}, conv_i($pricegroup_id),
                  $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
                  $form->{"cusordnumber_$i"}, $baseqty, $form->{"subtotal_$i"} ? 't' : 'f',
@@ -834,13 +827,8 @@ sub post_invoice {
       if ($form->{currency} eq $defaultcurrency) {
         $form->{"exchangerate_$i"} = 1;
       } else {
-        $exchangerate =
-          $form->check_exchangerate($myconfig, $form->{currency},
-                                    $form->{"datepaid_$i"}, 'buy');
-
-        $form->{"exchangerate_$i"} =
-          $exchangerate ? $exchangerate
-            : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
+        $exchangerate              = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
+        $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
       }
 
       # record AR
@@ -990,6 +978,36 @@ sub post_invoice {
 
   Common::webdav_folder($form) if ($main::webdav);
 
+  # Link this record to the records it was created from.
+  RecordLinks->create_links('dbh'        => $dbh,
+                            'mode'       => 'ids',
+                            'from_table' => 'oe',
+                            'from_ids'   => $form->{convert_from_oe_ids},
+                            'to_table'   => 'ar',
+                            'to_id'      => $form->{id},
+    );
+  delete $form->{convert_from_oe_ids};
+
+  my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids};
+
+  if (scalar @convert_from_do_ids) {
+    DO->close_orders('dbh' => $dbh,
+                     'ids' => \@convert_from_do_ids);
+
+    RecordLinks->create_links('dbh'        => $dbh,
+                              'mode'       => 'ids',
+                              'from_table' => 'delivery_orders',
+                              'from_ids'   => \@convert_from_do_ids,
+                              'to_table'   => 'ar',
+                              'to_id'      => $form->{id},
+      );
+  }
+  delete $form->{convert_from_do_ids};
+
+  ARAP->close_orders_if_billed('dbh'     => $dbh,
+                               'arap_id' => $form->{id},
+                               'table'   => 'ar',);
+
   my $rc = 1;
   if (!$provided_dbh) {
     $dbh->commit();
@@ -1121,7 +1139,7 @@ sub process_assembly {
        WHERE (a.id = ?)|;
   my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
 
     my $allocated = 0;
 
@@ -1144,7 +1162,7 @@ sub process_assembly {
     $query =
       qq|INSERT INTO invoice (trans_id, description, parts_id, qty, sellprice, fxsellprice, allocated, assemblyitem, unit)
          VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)|;
-    @values = (conv_i($form->{id}), $ref->{description}, conv_i($ref->{parts_id}), $ref->{qty}, 0, 0, $allocated, 't', $ref->{unit});
+    my @values = (conv_i($form->{id}), $ref->{description}, conv_i($ref->{parts_id}), $ref->{qty}, 0, 0, $allocated, 't', $ref->{unit});
     do_query($form, $dbh, $query, @values);
 
   }
@@ -1179,7 +1197,7 @@ sub cogs {
   my $allocated = 0;
   my $qty;
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
     if (($qty = (($ref->{base_qty} * -1) - $ref->{allocated})) > $totalqty) {
       $qty = $totalqty;
     }
@@ -1188,7 +1206,7 @@ sub cogs {
 
     # total expenses and inventory
     # sellprice is the cost of the item
-    $linetotal = $form->round_amount(($ref->{sellprice} * $qty) / $basefactor, 2);
+    my $linetotal = $form->round_amount(($ref->{sellprice} * $qty) / $basefactor, 2);
 
     if (!$main::eur) {
       $ref->{expense_accno} = ($form->{"expense_accno_$row"}) ? $form->{"expense_accno_$row"} : $ref->{expense_accno};
@@ -1227,20 +1245,9 @@ sub reverse_invoice {
        WHERE i.trans_id = ?|;
   my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id"}));
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-
-    if ($ref->{inventory_accno_id} || $ref->{assembly}) {
-
-      # if the invoice item is not an assemblyitem adjust parts onhand
-      if (!$ref->{assemblyitem}) {
-
-        # adjust onhand in parts table
-        $form->update_balance($dbh, "parts", "onhand", qq|id = $ref->{parts_id}|, $ref->{qty});
-      }
-
-      # loop if it is an assembly
-      next if ($ref->{assembly});
+  while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
 
+    if ($ref->{inventory_accno_id}) {
       # de-allocated purchases
       $query =
         qq|SELECT i.id, i.trans_id, i.allocated
@@ -1249,8 +1256,8 @@ sub reverse_invoice {
            ORDER BY i.trans_id DESC|;
       my $sth2 = prepare_execute_query($form, $dbh, $query, conv_i($ref->{"parts_id"}));
 
-      while (my $inhref = $sth2->fetchrow_hashref(NAME_lc)) {
-        $qty = $ref->{qty};
+      while (my $inhref = $sth2->fetchrow_hashref('NAME_lc')) {
+        my $qty = $ref->{qty};
         if (($ref->{qty} - $inhref->{allocated}) > 0) {
           $qty = $inhref->{allocated};
         }
@@ -1267,7 +1274,7 @@ sub reverse_invoice {
   $sth->finish;
 
   # delete acc_trans
-  @values = (conv_i($form->{id}));
+  my @values = (conv_i($form->{id}));
   do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE trans_id = ?|, @values);
   do_query($form, $dbh, qq|DELETE FROM invoice WHERE trans_id = ?|, @values);
 
@@ -1308,7 +1315,7 @@ sub delete_invoice {
   $dbh->disconnect;
 
   if ($rc) {
-    map { unlink "$spool/$_" if -f "$spool/$_"; } @{ $spoolfiles };
+    map { unlink "$spool/$_" if -f "$spool/$_"; } @spoolfiles;
   }
 
   $main::lxdebug->leave_sub();
@@ -1382,7 +1389,7 @@ sub retrieve_invoice {
     $query = qq|SELECT printed, emailed, spoolfile, formname FROM status WHERE trans_id = ?|;
     $sth = prepare_execute_query($form, $dbh, $query, $id);
 
-    while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
+    while ($ref = $sth->fetchrow_hashref('NAME_lc')) {
       $form->{printed} .= "$ref->{formname} " if $ref->{printed};
       $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
       $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
@@ -1405,7 +1412,7 @@ sub retrieve_invoice {
            c2.accno AS income_accno,    c2.new_chart_id AS income_new_chart,    date($transdate) - c2.valid_from as income_valid,
            c3.accno AS expense_accno,   c3.new_chart_id AS expense_new_chart,   date($transdate) - c3.valid_from AS expense_valid,
 
-           i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice, i.discount, i.parts_id AS id, i.unit, i.deliverydate,
+           i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice, i.discount, i.parts_id AS id, i.unit, i.deliverydate AS reqdate,
            i.project_id, i.serialnumber, i.id AS invoice_pos, i.pricegroup_id, i.ordnumber, i.transdate, i.cusordnumber, i.subtotal, i.lastcost,
            i.price_factor_id, i.price_factor, i.marge_price_factor,
            p.partnumber, p.assembly, p.bin, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id, p.formel,
@@ -1425,7 +1432,7 @@ sub retrieve_invoice {
 
     $sth = prepare_execute_query($form, $dbh, $query, $id);
 
-    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+    while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
       map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)) if !$ref->{"part_inventory_accno_id"};
       delete($ref->{"part_inventory_accno_id"});
 
@@ -1450,7 +1457,7 @@ sub retrieve_invoice {
       my $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
       $ref->{taxaccounts} = "";
       my $i=0;
-      while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
+      while (my $ptr = $stw->fetchrow_hashref('NAME_lc')) {
 
         if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
           $i++;
@@ -1513,11 +1520,17 @@ sub get_customer {
   }
 
   my $cid = conv_i($form->{customer_id});
-  my $payment_id = ($form->{payment_id}) ? "($form->{payment_id} = pt.id) OR" : "";
+  my $payment_id;
+
+  if ($form->{payment_id}) {
+    $payment_id = "(pt.id = ?) OR";
+    push @values, conv_i($form->{payment_id});
+  }
+
   # get customer
   $query =
     qq|SELECT
-         c.name AS customer, c.discount, c.creditlimit, c.terms,
+         c.id AS customer_id, c.name AS customer, c.discount, c.creditlimit, c.terms,
          c.email, c.cc, c.bcc, c.language_id, c.payment_id,
          c.street, c.zipcode, c.city, c.country,
          c.notes AS intnotes, c.klass as customer_klass, c.taxzone_id, c.salesman_id,
@@ -1564,7 +1577,7 @@ sub get_customer {
        WHERE o.customer_id = ?
          AND o.quotation = '0'
          AND o.closed = '0'|;
-  $sth = prepare_execute_query($form, $dbh, $query, $cid);
+  my $sth = prepare_execute_query($form, $dbh, $query, $cid);
 
   while (my ($amount, $exch) = $sth->fetchrow_array) {
     $exch = 1 unless $exch;
@@ -1598,7 +1611,7 @@ sub get_customer {
     $sth = prepare_execute_query($form, $dbh, $query, $cid, $cid);
 
     my $i = 0;
-    while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
+    while ($ref = $sth->fetchrow_hashref('NAME_lc')) {
       if ($ref->{category} eq 'I') {
         $i++;
         $form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}";
@@ -1715,7 +1728,7 @@ sub retrieve_item {
        WHERE $where|;
   my $sth = prepare_execute_query($form, $dbh, $query, @values);
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
 
     # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
     # es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
@@ -1743,7 +1756,7 @@ sub retrieve_item {
     }
 
     # get tax rates and description
-    $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
+    my $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
     $query =
       qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
          FROM tax t
@@ -1757,14 +1770,14 @@ sub retrieve_item {
             LIMIT 1)
          ORDER BY c.accno|;
     @values = ($accno_id, $transdate eq "current_date" ? "now" : $transdate);
-    $stw = $dbh->prepare($query);
+    my $stw = $dbh->prepare($query);
     $stw->execute(@values) || $form->dberror($query);
 
     $ref->{taxaccounts} = "";
     my $i = 0;
-    while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
+    while (my $ptr = $stw->fetchrow_hashref('NAME_lc')) {
 
-      #    if ($customertax{$ref->{accno}}) {
+      #    if ($customertax{$ref->{accno}}) 
       if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
         $i++;
         $ptr->{accno} = $i;
@@ -1812,6 +1825,8 @@ sub retrieve_item {
       }
     }
 
+    $ref->{onhand} *= 1;
+
     push @{ $form->{item_list} }, $ref;
 
     if ($form->{lizenzen}) {
@@ -1821,7 +1836,7 @@ sub retrieve_item {
              FROM license l
              WHERE l.parts_id = ? AND NOT l.id IN (SELECT li.license_id FROM licenseinvoice li)|;
         my $stw = prepare_execute_query($form, $dbh, $query, conv_i($ref->{id}));
-        while (my $ptr = $stw->fetchrow_hashref(NAME_lc)) {
+        while (my $ptr = $stw->fetchrow_hashref('NAME_lc')) {
           push @{ $form->{LIZENZEN}{ $ref->{id} } }, $ptr;
         }
         $stw->finish;
@@ -1852,8 +1867,6 @@ sub get_pricegroups_for_parts {
 
   my $i  = 1;
   my $id = 0;
-  my $dimension_units = AM->retrieve_units($myconfig, $form, "dimension");
-  my $service_units = AM->retrieve_units($myconfig, $form, "service");
   my $all_units = AM->retrieve_units($myconfig, $form);
   while (($form->{"id_$i"}) or ($form->{"new_id_$i"})) {
     $form->{"PRICES"}{$i} = [];
@@ -1865,15 +1878,15 @@ sub get_pricegroups_for_parts {
       $id = $form->{"new_id_$i"};
     }
 
-    ($price, $selectedpricegroup_id) = split(/--/,
+    my ($price, $selectedpricegroup_id) = split(/--/,
       $form->{"sellprice_pg_$i"});
 
-    $pricegroup_old = $form->{"pricegroup_old_$i"};
+    my $pricegroup_old = $form->{"pricegroup_old_$i"};
     $form->{"new_pricegroup_$i"} = $selectedpricegroup_id;
     $form->{"old_pricegroup_$i"} = $pricegroup_old;
 
-    $price_new = $form->{"price_new_$i"};
-    $price_old = $form->{"price_old_$i"};
+    my $price_new = $form->{"price_new_$i"};
+    my $price_old = $form->{"price_old_$i"};
 
     if (!$form->{"unit_old_$i"}) {
       # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
@@ -1886,9 +1899,8 @@ sub get_pricegroups_for_parts {
     # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
     $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
 
-    my $check_units = $form->{"inventory_accno_$i"} ? $dimension_units : $service_units;
-    if (!$check_units->{$form->{"selected_unit_$i"}} ||
-        ($check_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
+    if (!$all_units->{$form->{"selected_unit_$i"}} ||
+        ($all_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
          $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
       # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
       # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
@@ -1910,7 +1922,7 @@ sub get_pricegroups_for_parts {
       $form->{"basefactor_$i"} = 1;
     }
 
-    $query =
+    my $query =
       qq|SELECT
            pricegroup_id,
            (SELECT p.sellprice FROM parts p WHERE p.id = ?) AS default_sellprice,
@@ -1931,10 +1943,10 @@ sub get_pricegroups_for_parts {
           FROM prices
 
           ORDER BY pricegroup|;
-    @values = (conv_i($id), conv_i($id), conv_i($id), conv_i($id));
+    my @values = (conv_i($id), conv_i($id), conv_i($id), conv_i($id));
     my $pkq = prepare_execute_query($form, $dbh, $query, @values);
 
-    while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
+    while (my $pkr = $pkq->fetchrow_hashref('NAME_lc')) {
       $pkr->{id}       = $id;
       $pkr->{selected} = '';