Und die dazugehoerigen Templates
[kivitendo-erp.git] / SL / AM.pm
index 3737c13..4eddb74 100644 (file)
--- a/SL/AM.pm
+++ b/SL/AM.pm
@@ -345,7 +345,7 @@ sub save_account {
         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
         chart_id  => conv_i($form->{"id"}),
-        pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
+        pos_ustva => $form->{"taxkey_pos_ustva_$tk_count"},
         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
       };
 
@@ -594,7 +594,6 @@ sub lead {
   $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
-  $form->{ALL};
   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
     push @{ $form->{ALL} }, $ref;
   }
@@ -690,7 +689,6 @@ sub business {
   $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
-  $form->{ALL};
   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
     push @{ $form->{ALL} }, $ref;
   }
@@ -962,12 +960,13 @@ sub get_buchungsgruppe {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
+  my $query;
 
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
 
   if ($form->{id}) {
-    my $query =
+    $query =
       qq|SELECT description, inventory_accno_id,
          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
          income_accno_id_0,
@@ -997,7 +996,7 @@ sub get_buchungsgruppe {
 
     $sth->finish;
 
-    my $query =
+    $query =
       qq|SELECT count(id) = 0 AS orphaned
          FROM parts
          WHERE buchungsgruppen_id = ?|;
@@ -1120,27 +1119,24 @@ sub swap_sortkeys {
   my ($self, $myconfig, $form, $table) = @_;
 
   # connect to database
-  my $dbh = $form->dbconnect_noauto($myconfig);
+  my $dbh = $form->get_standard_dbh($myconfig);
 
   my $query =
     qq|SELECT
        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
-  my @values = ($form->{"id1"}, $form->{"id2"});
+  my @values   = ($form->{"id1"}, $form->{"id2"});
   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
-  $main::lxdebug->dump(0, "v", \@values);
-  $main::lxdebug->dump(0, "s", \@sortkeys);
 
-  $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
-  my $sth = $dbh->prepare($query);
-  $sth->execute($sortkeys[1], $form->{"id1"}) ||
-    $form->dberror($query . " ($sortkeys[1], $form->{id1})");
-  $sth->execute($sortkeys[0], $form->{"id2"}) ||
-    $form->dberror($query . " ($sortkeys[0], $form->{id2})");
+  $query  = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
+  my $sth = prepare_query($form, $dbh, $query);
+
+  do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
+  do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
+
   $sth->finish();
 
   $dbh->commit();
-  $dbh->disconnect;
 
   $main::lxdebug->leave_sub();
 }
@@ -1339,12 +1335,10 @@ sub save_payment {
 
   $query = qq|UPDATE payment_terms SET
               description = ?, description_long = ?,
-              ranking = ?,
               terms_netto = ?, terms_skonto = ?,
               percent_skonto = ?
               WHERE id = ?|;
   my @values = ($form->{description}, $form->{description_long},
-                $form->{ranking} * 1,
                 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
                 $form->{percent_skonto} * 1,
                 $form->{id});
@@ -1480,70 +1474,85 @@ sub save_template {
   return $error;
 }
 
-sub save_preferences {
+sub save_defaults {
   $main::lxdebug->enter_sub();
 
-  my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
+  my $self     = shift;
+  my %params   = @_;
 
-  map { ($form->{$_}) = split(/--/, $form->{$_}) }
-    qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
+  my $myconfig = \%main::myconfig;
+  my $form     = $main::form;
 
-  my @a;
-  $form->{curr} =~ s/ //g;
-  map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
-  $form->{curr} = join ':', @a;
+  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
-  # connect to database
-  my $dbh = $form->dbconnect_noauto($myconfig);
+  my %accnos;
+  map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
+
+  $form->{curr}  =~ s/ //g;
+  my @currencies =  grep { $_ ne '' } split m/:/, $form->{curr};
+  my $currency   =  join ':', @currencies;
 
   # these defaults are database wide
-  # user specific variables are in myconfig
-  # save defaults
+
   my $query =
-    qq|UPDATE defaults SET | .
-    qq|inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
-    qq|income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
-    qq|expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
-    qq|fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
-    qq|fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
-    qq|invnumber = ?, | .
-    qq|cnnumber  = ?, | .
-    qq|sonumber = ?, | .
-    qq|ponumber = ?, | .
-    qq|sqnumber = ?, | .
-    qq|rfqnumber = ?, | .
-    qq|customernumber = ?, | .
-    qq|vendornumber = ?, | .
-    qq|articlenumber = ?, | .
-    qq|servicenumber = ?, | .
-    qq|yearend = ?, | .
-    qq|curr = ?, | .
-    qq|businessnumber = ?|;
-  my @values = ($form->{inventory_accno}, $form->{income_accno},
-                $form->{expense_accno},
-                $form->{fxgain_accno}, $form->{fxloss_accno},
-                $form->{invnumber}, $form->{cnnumber},
-                $form->{sonumber}, $form->{ponumber},
-                $form->{sqnumber}, $form->{rfqnumber},
-                $form->{customernumber}, $form->{vendornumber},
-                $form->{articlenumber}, $form->{servicenumber},
-                $form->{yearend}, $form->{curr},
+    qq|UPDATE defaults SET
+        inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
+        income_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
+        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 = ?),
+        invnumber          = ?,
+        cnnumber           = ?,
+        sonumber           = ?,
+        ponumber           = ?,
+        sqnumber           = ?,
+        rfqnumber          = ?,
+        customernumber     = ?,
+        vendornumber       = ?,
+        articlenumber      = ?,
+        servicenumber      = ?,
+        yearend            = ?,
+        curr               = ?,
+        businessnumber     = ?|;
+  my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
+                $accnos{fxgain_accno},    $accnos{fxloss_accno},
+                $form->{invnumber},       $form->{cnnumber},
+                $form->{sonumber},        $form->{ponumber},
+                $form->{sqnumber},        $form->{rfqnumber},
+                $form->{customernumber},  $form->{vendornumber},
+                $form->{articlenumber},   $form->{servicenumber},
+                $form->{yearend},         $currency,
                 $form->{businessnumber});
   do_query($form, $dbh, $query, @values);
 
+  $dbh->commit();
+
+  $main::lxdebug->leave_sub();
+}
+
+
+sub save_preferences {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
+
+  my $dbh = $form->get_standard_dbh($myconfig);
+
+  my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
+
   # update name
-  $query = qq|UPDATE employee
-              SET name = ?
-              WHERE login = ?|;
+  my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
   do_query($form, $dbh, $query, $form->{name}, $form->{login});
 
-  my $rc = $dbh->commit;
-  $dbh->disconnect;
+  my $rc = $dbh->commit();
 
   # save first currency in myconfig
-  $form->{currency} = substr($form->{curr}, 0, 3);
+  $currency               =~ s/:.*//;
+  $form->{currency}       =  $currency;
+
+  $form->{businessnumber} =  $businessnumber;
 
-  my $myconfig = new User "$memberfile", "$form->{login}";
+  $myconfig = new User "$memberfile", "$form->{login}";
 
   foreach my $item (keys %$form) {
     $myconfig->{$item} = $form->{$item};
@@ -1886,6 +1895,17 @@ sub units_in_use {
   $main::lxdebug->leave_sub();
 }
 
+# if $a is translatable to $b, return the factor between them.
+# else return 1
+sub convert_unit {
+  $main::lxdebug->enter_sub(2);
+  ($this, $a, $b, $all_units) = @_;
+
+  $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}; 
+}
+
 sub unit_select_data {
   $main::lxdebug->enter_sub();
 
@@ -2068,10 +2088,9 @@ sub taxes {
                    t.taxkey,
                    t.taxdescription,
                    round(t.rate * 100, 2) AS rate,
-                   c.accno AS taxnumber,
-                   c.description AS account_description
+                   (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
+                   (SELECT description FROM chart WHERE id = chart_id) AS account_description
                  FROM tax t
-                 JOIN chart c on (chart_id = c.id)
                  ORDER BY taxkey|;
 
   $sth = $dbh->prepare($query);
@@ -2146,7 +2165,7 @@ sub get_tax {
 
   # see if it is used by a taxkey
   $query = qq|SELECT count(*) FROM taxkeys
-              WHERE tax_id = ?|;
+              WHERE tax_id = ? AND chart_id >0|;
 
   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
@@ -2156,8 +2175,8 @@ sub get_tax {
   if (!$form->{orphaned} ) {
     $query = qq|SELECT DISTINCT c.id, c.accno
                 FROM taxkeys tk
-                LEFT JOIN   tax t ON (t.id = tk.tax_id)
-                LEFT JOIN chart c ON (c.id = tk.chart_id)
+                JOIN   tax t ON (t.id = tk.tax_id)
+                JOIN chart c ON (c.id = tk.chart_id)
                 WHERE tk.tax_id = ?|;
 
     $sth = $dbh->prepare($query);
@@ -2187,7 +2206,7 @@ sub save_tax {
   $form->{rate} = $form->{rate} / 100;
 
   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
-  if ($form->{id}) {
+  if ($form->{id} ne "") {
     $query = qq|UPDATE tax SET
                   taxkey         = ?,
                   taxdescription = ?,
@@ -2232,6 +2251,77 @@ sub delete_tax {
   $main::lxdebug->leave_sub();
 }
 
+sub save_price_factor {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $myconfig, $form) = @_;
+
+  # connect to database
+  my $dbh = $form->get_standard_dbh($myconfig);
+
+  my $query;
+  my @values = ($form->{description}, conv_i($form->{factor}));
+
+  if ($form->{id}) {
+    $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
+    push @values, conv_i($form->{id});
+
+  } else {
+    $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
+  }
+
+  do_query($form, $dbh, $query, @values);
+
+  $dbh->commit();
+
+  $main::lxdebug->leave_sub();
+}
+
+sub get_all_price_factors {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $myconfig, $form) = @_;
+
+  # connect to database
+  my $dbh = $form->get_standard_dbh($myconfig);
+
+  $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
+
+  $main::lxdebug->leave_sub();
+}
+
+sub get_price_factor {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $myconfig, $form) = @_;
+
+  # connect to database
+  my $dbh = $form->get_standard_dbh($myconfig);
+
+  my $query = qq|SELECT description, factor,
+                   ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
+                    (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
+                    (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
+                 FROM price_factors WHERE id = ?|;
+
+  ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
+
+  $main::lxdebug->leave_sub();
+}
+
+sub delete_price_factor {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $myconfig, $form) = @_;
+
+  # connect to database
+  my $dbh = $form->get_standard_dbh($myconfig);
+
+  do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
+  $dbh->commit();
+
+  $main::lxdebug->leave_sub();
+}
 
 
 1;