Merge branch 'master' of git@lx-office.linet-services.de:lx-office-erp
[kivitendo-erp.git] / SL / AM.pm
index 5ffb6a1..bf5c97d 100644 (file)
--- a/SL/AM.pm
+++ b/SL/AM.pm
@@ -39,8 +39,11 @@ package AM;
 
 use Carp;
 use Data::Dumper;
+use Encode;
 use SL::DBUtils;
 
+use strict;
+
 sub get_account {
   $main::lxdebug->enter_sub();
 
@@ -64,11 +67,11 @@ sub get_account {
     };
 
 
-  $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
+  $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
   my $sth = $dbh->prepare($query);
   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
-  my $ref = $sth->fetchrow_hashref(NAME_lc);
+  my $ref = $sth->fetchrow_hashref("NAME_lc");
 
   foreach my $key (keys %$ref) {
     $form->{"$key"} = $ref->{"$key"};
@@ -79,13 +82,13 @@ sub get_account {
   # get default accounts
   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
               FROM defaults|;
-  $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
+  $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
   $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
-  $ref = $sth->fetchrow_hashref(NAME_lc);
+  $ref = $sth->fetchrow_hashref("NAME_lc");
 
-  map { $form->{$_} = $ref->{$_} } keys %ref;
+  map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
   $sth->finish;
 
@@ -102,13 +105,13 @@ sub get_account {
       rate
     FROM tax ORDER BY taxkey
   };
-  $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
+  $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
   $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
   $form->{TAXKEY} = [];
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     push @{ $form->{TAXKEY} }, $ref;
   }
 
@@ -119,12 +122,12 @@ sub get_account {
                 FROM chart
                 WHERE link = ?
                 ORDER BY accno|;
-    $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
+    $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
     $sth = $dbh->prepare($query);
     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
 
     $form->{NEWACCOUNT} = [];
-    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+    while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
       push @{ $form->{NEWACCOUNT} }, $ref;
     }
 
@@ -150,14 +153,14 @@ sub get_account {
       WHERE tk.chart_id = ?
       ORDER BY startdate DESC
     };
-    $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
+    $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
     $sth = $dbh->prepare($query);
 
     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
     $form->{ACCOUNT_TAXKEYS} = [];
 
-    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+    while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
     }
 
@@ -167,7 +170,7 @@ sub get_account {
   # check if we have any transactions
   $query = qq|SELECT a.trans_id FROM acc_trans a
               WHERE a.chart_id = ?|;
-  $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
+  $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
   $sth = $dbh->prepare($query);
   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
@@ -180,7 +183,7 @@ sub get_account {
   if ($form->{new_chart_id}) {
     $query = qq|SELECT current_date-valid_from FROM chart
                 WHERE id = ?|;
-    $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
+    $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
     if ($count >=0) {
       $form->{new_chart_valid} = 1;
@@ -448,11 +451,11 @@ sub departments {
                  FROM department d
                  ORDER BY 2|;
 
-  $sth = $dbh->prepare($query);
+  my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
   $form->{ALL} = [];
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     push @{ $form->{ALL} }, $ref;
   }
 
@@ -476,7 +479,7 @@ sub get_department {
   my $sth = $dbh->prepare($query);
   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
-  my $ref = $sth->fetchrow_hashref(NAME_lc);
+  my $ref = $sth->fetchrow_hashref("NAME_lc");
 
   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
@@ -499,6 +502,7 @@ sub save_department {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
+  my ($query);
 
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
@@ -525,6 +529,7 @@ sub delete_department {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
+  my ($query);
 
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
@@ -550,10 +555,10 @@ sub lead {
                  FROM leads
                  ORDER BY 2|;
 
-  $sth = $dbh->prepare($query);
+  my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     push @{ $form->{ALL} }, $ref;
   }
 
@@ -578,7 +583,7 @@ sub get_lead {
   my $sth = $dbh->prepare($query);
   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
-  my $ref = $sth->fetchrow_hashref(NAME_lc);
+  my $ref = $sth->fetchrow_hashref("NAME_lc");
 
   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
@@ -593,6 +598,7 @@ sub save_lead {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
+  my ($query);
 
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
@@ -620,6 +626,7 @@ sub delete_lead {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
+  my ($query);
 
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
@@ -641,14 +648,14 @@ sub business {
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
 
-  my $query = qq|SELECT id, description, discount, customernumberinit
+  my $query = qq|SELECT id, description, discount, customernumberinit, salesman
                  FROM business
                  ORDER BY 2|;
 
-  $sth = $dbh->prepare($query);
+  my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     push @{ $form->{ALL} }, $ref;
   }
 
@@ -667,13 +674,13 @@ sub get_business {
   my $dbh = $form->dbconnect($myconfig);
 
   my $query =
-    qq|SELECT b.description, b.discount, b.customernumberinit
+    qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
        FROM business b
        WHERE b.id = ?|;
   my $sth = $dbh->prepare($query);
   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
-  my $ref = $sth->fetchrow_hashref(NAME_lc);
+  my $ref = $sth->fetchrow_hashref("NAME_lc");
 
   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
@@ -688,24 +695,25 @@ sub save_business {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
+  my ($query);
 
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
 
-  my @values = ($form->{description}, $form->{discount},
-                $form->{customernumberinit});
+  my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f');
   # id is the old record
   if ($form->{id}) {
     $query = qq|UPDATE business SET
                 description = ?,
                 discount = ?,
-                customernumberinit = ?
+                customernumberinit = ?,
+                salesman = ?
                 WHERE id = ?|;
     push(@values, $form->{id});
   } else {
     $query = qq|INSERT INTO business
-                (description, discount, customernumberinit)
-                VALUES (?, ?, ?)|;
+                (description, discount, customernumberinit, salesman)
+                VALUES (?, ?, ?, ?)|;
   }
   do_query($form, $dbh, $query, @values);
 
@@ -722,7 +730,7 @@ sub delete_business {
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
 
-  $query = qq|DELETE FROM business
+  my $query = qq|DELETE FROM business
               WHERE id = ?|;
   do_query($form, $dbh, $query, $form->{id});
 
@@ -745,12 +753,12 @@ sub language {
     "  output_numberformat, output_dateformat, output_longdates " .
     "FROM language ORDER BY description";
 
-  $sth = $dbh->prepare($query);
+  my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
   my $ary = [];
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     push(@{ $ary }, $ref);
   }
 
@@ -781,7 +789,7 @@ sub get_language {
   my $sth = $dbh->prepare($query);
   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
-  my $ref = $sth->fetchrow_hashref(NAME_lc);
+  my $ref = $sth->fetchrow_hashref("NAME_lc");
 
   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
@@ -852,12 +860,13 @@ sub delete_language {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
+  my $query;
 
   # connect to database
   my $dbh = $form->dbconnect_noauto($myconfig);
 
   foreach my $table (qw(translation_payment_terms units_language)) {
-    my $query = qq|DELETE FROM $table WHERE language_id = ?|;
+    $query = qq|DELETE FROM $table WHERE language_id = ?|;
     do_query($form, $dbh, $query, $form->{"id"});
   }
 
@@ -901,11 +910,11 @@ sub buchungsgruppe {
                  FROM buchungsgruppen
                  ORDER BY sortkey|;
 
-  $sth = $dbh->prepare($query);
+  my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
   $form->{ALL} = [];
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     push @{ $form->{ALL} }, $ref;
   }
 
@@ -949,7 +958,7 @@ sub get_buchungsgruppe {
     my $sth = $dbh->prepare($query);
     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
-    my $ref = $sth->fetchrow_hashref(NAME_lc);
+    my $ref = $sth->fetchrow_hashref("NAME_lc");
 
     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
@@ -977,7 +986,7 @@ sub get_buchungsgruppe {
 
   my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     foreach my $key (split(/:/, $ref->{link})) {
       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
         $form->{"std_inventory_accno_id"} = $ref->{"id"};
@@ -1064,7 +1073,7 @@ sub delete_buchungsgruppe {
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
 
-  $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
+  my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
   do_query($form, $dbh, $query, $form->{id});
 
   $dbh->disconnect;
@@ -1100,105 +1109,6 @@ sub swap_sortkeys {
   $main::lxdebug->leave_sub();
 }
 
-sub printer {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $myconfig, $form) = @_;
-
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
-  my $query = qq|SELECT id, printer_description, template_code, printer_command
-                 FROM printers
-                 ORDER BY 2|;
-
-  $sth = $dbh->prepare($query);
-  $sth->execute || $form->dberror($query);
-
-  $form->{"ALL"} = [];
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-    push @{ $form->{ALL} }, $ref;
-  }
-
-  $sth->finish;
-  $dbh->disconnect;
-
-  $main::lxdebug->leave_sub();
-}
-
-sub get_printer {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $myconfig, $form) = @_;
-
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
-  my $query =
-    qq|SELECT p.printer_description, p.template_code, p.printer_command
-       FROM printers p
-       WHERE p.id = ?|;
-  my $sth = $dbh->prepare($query);
-  $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
-
-  my $ref = $sth->fetchrow_hashref(NAME_lc);
-
-  map { $form->{$_} = $ref->{$_} } keys %$ref;
-
-  $sth->finish;
-
-  $dbh->disconnect;
-
-  $main::lxdebug->leave_sub();
-}
-
-sub save_printer {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $myconfig, $form) = @_;
-
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
-  my @values = ($form->{printer_description},
-                $form->{template_code},
-                $form->{printer_command});
-
-  # id is the old record
-  if ($form->{id}) {
-    $query = qq|UPDATE printers SET
-                printer_description = ?, template_code = ?, printer_command = ?
-                WHERE id = ?|;
-    push(@values, $form->{id});
-  } else {
-    $query = qq|INSERT INTO printers
-                (printer_description, template_code, printer_command)
-                VALUES (?, ?, ?)|;
-  }
-  do_query($form, $dbh, $query, @values);
-
-  $dbh->disconnect;
-
-  $main::lxdebug->leave_sub();
-}
-
-sub delete_printer {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $myconfig, $form) = @_;
-
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
-  $query = qq|DELETE FROM printers
-              WHERE id = ?|;
-  do_query($form, $dbh, $query, $form->{id});
-
-  $dbh->disconnect;
-
-  $main::lxdebug->leave_sub();
-}
-
 sub payment {
   $main::lxdebug->enter_sub();
 
@@ -1209,11 +1119,11 @@ sub payment {
 
   my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
 
-  $sth = $dbh->prepare($query);
+  my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
   $form->{ALL} = [];
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     push @{ $form->{ALL} }, $ref;
   }
 
@@ -1235,7 +1145,7 @@ sub get_payment {
   my $sth = $dbh->prepare($query);
   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
-  my $ref = $sth->fetchrow_hashref(NAME_lc);
+  my $ref = $sth->fetchrow_hashref("NAME_lc");
   map { $form->{$_} = $ref->{$_} } keys %$ref;
   $sth->finish();
 
@@ -1252,7 +1162,7 @@ sub get_payment {
   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
   my %mapping;
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     $mapping{ $ref->{"language_id"} } = $ref
       unless (defined($mapping{ $ref->{"language_id"} }));
   }
@@ -1406,6 +1316,8 @@ sub load_template {
     close(TEMPLATE);
   }
 
+  $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
+
   $main::lxdebug->leave_sub();
 
   return ($content, $lines);
@@ -1421,6 +1333,7 @@ sub save_template {
   my $error = "";
 
   if (open(TEMPLATE, ">$filename")) {
+    $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
     $content =~ s/\r\n/\n/g;
     print(TEMPLATE $content);
     close(TEMPLATE);
@@ -1445,7 +1358,7 @@ sub save_defaults {
   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
   my %accnos;
-  map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
+  map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
 
   $form->{curr}  =~ s/ //g;
   my @currencies =  grep { $_ ne '' } split m/:/, $form->{curr};
@@ -1460,6 +1373,7 @@ sub save_defaults {
         expense_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
         fxgain_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
         fxloss_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
+        ar_paid_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
         invnumber          = ?,
         cnnumber           = ?,
         sonumber           = ?,
@@ -1472,19 +1386,19 @@ sub save_defaults {
         servicenumber      = ?,
         sdonumber          = ?,
         pdonumber          = ?,
-        yearend            = ?,
         curr               = ?,
-        businessnumber     = ?|;
+        businessnumber     = ?,
+        weightunit         = ?|;
   my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
-                $accnos{fxgain_accno},    $accnos{fxloss_accno},
+                $accnos{fxgain_accno},    $accnos{fxloss_accno}, $accnos{ar_paid_accno},
                 $form->{invnumber},       $form->{cnnumber},
                 $form->{sonumber},        $form->{ponumber},
                 $form->{sqnumber},        $form->{rfqnumber},
                 $form->{customernumber},  $form->{vendornumber},
                 $form->{articlenumber},   $form->{servicenumber},
                 $form->{sdonumber},       $form->{pdonumber},
-                $form->{yearend},         $currency,
-                $form->{businessnumber});
+                $currency,
+                $form->{businessnumber},  $form->{weightunit});
   do_query($form, $dbh, $query, @values);
 
   $dbh->commit();
@@ -1496,7 +1410,7 @@ sub save_defaults {
 sub save_preferences {
   $main::lxdebug->enter_sub();
 
-  my ($self, $myconfig, $form, $webdav) = @_;
+  my ($self, $myconfig, $form) = @_;
 
   my $dbh = $form->get_standard_dbh($myconfig);
 
@@ -1514,66 +1428,41 @@ sub save_preferences {
 
   $form->{businessnumber} =  $businessnumber;
 
-  my $myconfig = new User($form->{login});
+  $myconfig = new User($form->{login});
 
   foreach my $item (keys %$form) {
     $myconfig->{$item} = $form->{$item};
   }
 
-  $myconfig->save_member($memberfile);
+  $myconfig->save_member;
 
   my $auth = $main::auth;
 
-  if ($auth->can_change_password()
-      && defined $form->{new_password}
-      && ($form->{new_password} ne '********')) {
-    $auth->change_password($form->{login}, $form->{new_password});
-
-    $form->{password} = $form->{new_password};
-    $auth->set_session_value('password', $form->{password});
-    $auth->create_or_refresh_session();
-  }
-
-  if ($webdav) {
-    @webdavdirs =
-      qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
-    foreach $directory (@webdavdirs) {
-      $file = "webdav/" . $directory . "/webdav-user";
-      if ($myconfig->{$directory}) {
-        open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
-        while (<HTACCESS>) {
-          ($login, $password) = split(/:/, $_);
-          if ($login ne $form->{login}) {
-            $newfile .= $_;
-          }
-        }
-        close(HTACCESS);
-        open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
-        $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
-        print(HTACCESS $newfile);
-        close(HTACCESS);
-      } else {
-        $form->{$directory} = 0;
-        open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
-        while (<HTACCESS>) {
-          ($login, $password) = split(/:/, $_);
-          if ($login ne $form->{login}) {
-            $newfile .= $_;
-          }
-        }
-        close(HTACCESS);
-        open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
-        print(HTACCESS $newfile);
-        close(HTACCESS);
-      }
-    }
-  }
-
   $main::lxdebug->leave_sub();
 
   return $rc;
 }
 
+sub get_defaults {
+  $main::lxdebug->enter_sub();
+
+  my $self     = shift;
+  my %params   = @_;
+
+  my $myconfig = \%main::myconfig;
+  my $form     = $main::form;
+
+  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+
+  my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
+
+  $defaults->{weightunit} ||= 'kg';
+
+  $main::lxdebug->leave_sub();
+
+  return $defaults;
+}
+
 sub defaultaccounts {
   $main::lxdebug->enter_sub();
 
@@ -1587,12 +1476,15 @@ sub defaultaccounts {
   my $sth   = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
-  $form->{defaults}             = $sth->fetchrow_hashref(NAME_lc);
-  $form->{defaults}{IC}         = $form->{defaults}{inventory_accno_id};
-  $form->{defaults}{IC_income}  = $form->{defaults}{income_accno_id};
-  $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
-  $form->{defaults}{FX_gain}    = $form->{defaults}{fxgain_accno_id};
-  $form->{defaults}{FX_loss}    = $form->{defaults}{fxloss_accno_id};
+  $form->{defaults}               = $sth->fetchrow_hashref("NAME_lc");
+  $form->{defaults}{IC}           = $form->{defaults}{inventory_accno_id};
+  $form->{defaults}{IC_income}    = $form->{defaults}{income_accno_id};
+  $form->{defaults}{IC_expense}   = $form->{defaults}{expense_accno_id};
+  $form->{defaults}{FX_gain}      = $form->{defaults}{fxgain_accno_id};
+  $form->{defaults}{FX_loss}      = $form->{defaults}{fxloss_accno_id};
+  $form->{defaults}{AR_paid}      = $form->{defaults}{ar_paid_accno_id};
+
+  $form->{defaults}{weightunit} ||= 'kg';
 
   $sth->finish;
 
@@ -1603,10 +1495,10 @@ sub defaultaccounts {
   $sth = $dbh->prepare($query);
   $sth->execute || $self->dberror($query);
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     foreach my $key (split(/:/, $ref->{link})) {
       if ($key =~ /IC/) {
-        $nkey = $key;
+        my $nkey = $key;
         if ($key =~ /cogs/) {
           $nkey = "IC_expense";
         }
@@ -1630,7 +1522,7 @@ sub defaultaccounts {
   $sth = $dbh->prepare($query);
   $sth->execute || $self->dberror($query);
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
                                              id          => $ref->{id},
                                              description => $ref->{description}
@@ -1646,7 +1538,7 @@ sub defaultaccounts {
   $sth = $dbh->prepare($query);
   $sth->execute || $self->dberror($query);
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
                                              id          => $ref->{id},
                                              description => $ref->{description}
@@ -1663,13 +1555,31 @@ sub defaultaccounts {
   $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
       if $ref->{taxnumber};
     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
   }
+  # Abfrage für Standard Umlaufvermögenskonto
+  $query =
+    qq|SELECT id, accno, description, link | .
+    qq|FROM chart | .
+    qq|WHERE link LIKE ? |.
+    qq|ORDER BY accno|;
+  $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
+  $sth->execute || $form->dberror($query);#
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
+    foreach my $item (split(/:/, $ref->{link})) {
+      if ($item eq "AR_paid") {
+        %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
+                                             id          => $ref->{id},
+                                             description => $ref->{description}
+          );
+      }
+    }
+  }
 
   $sth->finish;
   $dbh->disconnect;
@@ -1745,7 +1655,7 @@ sub retrieve_units {
 
   my ($self, $myconfig, $form, $prefix) = @_;
 
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = $form->get_standard_dbh;
 
   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
@@ -1761,7 +1671,7 @@ sub retrieve_units {
   $sth = $dbh->prepare($query_lang);
   $sth->execute() || $form->dberror($query_lang);
   my @languages;
-  while ($ref = $sth->fetchrow_hashref()) {
+  while (my $ref = $sth->fetchrow_hashref()) {
     push(@languages, $ref);
   }
   $sth->finish();
@@ -1781,13 +1691,11 @@ sub retrieve_units {
     }
 
     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
-    while ($ref = $sth->fetchrow_hashref()) {
+    while (my $ref = $sth->fetchrow_hashref()) {
       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
     }
   }
-  $sth->finish();
-
-  $dbh->disconnect();
+  $sth->finish;
 
   $main::lxdebug->leave_sub();
 
@@ -1910,11 +1818,12 @@ sub convertible_units {
 # else return 1
 sub convert_unit {
   $main::lxdebug->enter_sub(2);
-  ($this, $a, $b, $all_units) = @_;
+  my ($this, $a, $b, $all_units) = @_;
 
+  $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
-  $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor}; 
+  $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 }
 
 sub unit_select_data {
@@ -1992,7 +1901,7 @@ sub sum_with_unit {
 
   $main::lxdebug->leave_sub();
 
-  return wantarray ? ($sum, $baseunit) : $sum;
+  return wantarray ? ($sum, $base_unit) : $sum;
 }
 
 sub add_unit {
@@ -2138,11 +2047,11 @@ sub taxes {
                  FROM tax t
                  ORDER BY taxkey|;
 
-  $sth = $dbh->prepare($query);
+  my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
   $form->{TAX} = [];
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     push @{ $form->{TAX} }, $ref;
   }
 
@@ -2168,11 +2077,11 @@ sub get_tax_accounts {
                ORDER BY accno
              };
 
-  $sth = $dbh->prepare($query);
+  my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
   $form->{ACCOUNTS} = [];
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     push @{ $form->{ACCOUNTS} }, $ref;
   }
 
@@ -2202,7 +2111,7 @@ sub get_tax {
   my $sth = $dbh->prepare($query);
   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
-  my $ref = $sth->fetchrow_hashref(NAME_lc);
+  my $ref = $sth->fetchrow_hashref("NAME_lc");
 
   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
@@ -2228,7 +2137,7 @@ sub get_tax {
     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
     $form->{TAXINUSE} = [];
-    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+    while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
       push @{ $form->{TAXINUSE} }, $ref;
     }
 
@@ -2244,6 +2153,7 @@ sub save_tax {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
+  my $query;
 
   # connect to database
   my $dbh = $form->get_standard_dbh($myconfig);
@@ -2283,6 +2193,7 @@ sub delete_tax {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
+  my $query;
 
   # connect to database
   my $dbh = $form->get_standard_dbh($myconfig);