inventory_accno_id aufräumen
[kivitendo-erp.git] / SL / AP.pm
index 92dc733..5839fb5 100644 (file)
--- a/SL/AP.pm
+++ b/SL/AP.pm
@@ -40,17 +40,24 @@ use SL::IO;
 use SL::MoreCommon;
 use SL::DB::Default;
 use SL::Util qw(trim);
+use SL::DB;
 use Data::Dumper;
 
 use strict;
 
 sub post_transaction {
+  my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_post_transaction, $self, $myconfig, $form, $provided_dbh, $payments_only);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _post_transaction {
   my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
-  my $rc = 0; # return code auf false setzen
-  # connect to database
-  my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
+  my $dbh = $provided_dbh || SL::DB->client->dbh;
 
   my ($null, $taxrate, $amount);
   my $exchangerate = 0;
@@ -319,6 +326,15 @@ sub post_transaction {
                              $form->{"exchangerate_$i"}), 2);
 
       if ($amount != 0) {
+        # fetch fxgain and fxloss chart info from defaults if charts aren't already filled in form
+        if ( !$form->{fxgain_accno} && $::instance_conf->get_fxgain_accno_id ) {
+          $form->{fxgain_accno} = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_fxgain_accno_id)->accno;
+        };
+        if ( !$form->{fxloss_accno} && $::instance_conf->get_fxloss_accno_id ) {
+          $form->{fxloss_accno} = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_fxloss_accno_id)->accno;
+        };
+        die "fxloss_accno missing" if $amount < 0 and not $form->{fxloss_accno};
+        die "fxgain_accno missing" if $amount > 0 and not $form->{fxgain_accno};
         $query =
           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) | .
           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
@@ -372,21 +388,11 @@ sub post_transaction {
     $datev->export;
 
     if ($datev->errors) {
-      $dbh->rollback;
       die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
     }
   }
 
-  if (!$provided_dbh) {
-    $dbh->commit();
-    $dbh->disconnect();
-  }
-
-  $rc = 1; #  Den return-code auf true setzen, aber nur falls beim commit alles i.O. ist
-
-  $main::lxdebug->leave_sub();
-
-  return $rc;
+  return 1;
 }
 
 sub delete_transaction {
@@ -394,19 +400,15 @@ sub delete_transaction {
 
   my ($self, $myconfig, $form) = @_;
 
-  # connect to database
-  my $dbh = $form->dbconnect_noauto($myconfig);
-
-  # acc_trans entries are deleted by database triggers.
-  my $query = qq|DELETE FROM ap WHERE id = ?|;
-  do_query($form, $dbh, $query, $form->{id});
-
-  my $rc = $dbh->commit;
-  $dbh->disconnect;
+  SL::DB->client->with_transaction(sub {
+    my $query = qq|DELETE FROM ap WHERE id = ?|;
+    do_query($form, SL::DB->client->dbh, $query, $form->{id});
+    1;
+  }) or do { die SL::DB->client->error };
 
   $main::lxdebug->leave_sub();
 
-  return $rc;
+  return 1;
 }
 
 sub ap_transactions {
@@ -482,7 +484,7 @@ sub ap_transactions {
   }
   if ($form->{project_id}) {
     $where .=
-      qq|AND ((a.globalproject_id = ?) OR EXISTS | .
+      qq| AND ((a.globalproject_id = ?) OR EXISTS | .
       qq|  (SELECT * FROM invoice i | .
       qq|   WHERE i.project_id = ? AND i.trans_id = a.id) | .
       qq| OR EXISTS | .
@@ -509,7 +511,7 @@ sub ap_transactions {
 
   if ($form->{parts_partnumber}) {
     $where .= <<SQL;
     AND EXISTS (
+ AND EXISTS (
         SELECT invoice.trans_id
         FROM invoice
         LEFT JOIN parts ON (invoice.parts_id = parts.id)
@@ -523,7 +525,7 @@ SQL
 
   if ($form->{parts_description}) {
     $where .= <<SQL;
     AND EXISTS (
+ AND EXISTS (
         SELECT invoice.trans_id
         FROM invoice
         WHERE (invoice.trans_id = a.id)
@@ -563,7 +565,7 @@ sub get_transdate {
   my ($self, $myconfig, $form) = @_;
 
   # connect to database
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my $query =
     "SELECT COALESCE(" .
@@ -572,8 +574,6 @@ sub get_transdate {
     "  current_date)";
   ($form->{transdate}) = $dbh->selectrow_array($query);
 
-  $dbh->disconnect;
-
   $main::lxdebug->leave_sub();
 }
 
@@ -617,12 +617,19 @@ sub _delete_payments {
 }
 
 sub post_payment {
+  my ($self, $myconfig, $form, $locale) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_post_payment, $self, $myconfig, $form, $locale);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _post_payment {
   my ($self, $myconfig, $form, $locale) = @_;
 
-  # connect to database, turn off autocommit
-  my $dbh = $form->dbconnect_noauto($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my (%payments, $old_form, $row, $item, $query, %keep_vars);
 
@@ -671,12 +678,7 @@ sub post_payment {
 
   restore_form($old_form);
 
-  my $rc = $dbh->commit();
-  $dbh->disconnect();
-
-  $main::lxdebug->leave_sub();
-
-  return $rc;
+  return 1;
 }
 
 sub setup_form {
@@ -746,7 +748,7 @@ sub setup_form {
           }
 
           $index                 = $form->{acc_trans}{$key}->[$i - 1]->{index};
-          $form->{"tax_$index"}  = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
+          $form->{"tax_$index"}  = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} * -1 / $exchangerate, 2);
           $totaltax             += $form->{"tax_$index"};
 
         } else {
@@ -810,12 +812,20 @@ sub setup_form {
 }
 
 sub storno {
+  my ($self, $form, $myconfig, $id) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_storno, $self, $form, $myconfig, $id);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _storno {
   my ($self, $form, $myconfig, $id) = @_;
 
   my ($query, $new_id, $storno_row, $acc_trans_rows);
-  my $dbh = $form->get_standard_dbh($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   $query = qq|SELECT nextval('glid')|;
   ($new_id) = selectrow_query($form, $dbh, $query);
@@ -860,9 +870,7 @@ sub storno {
 
   map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
 
-  $dbh->commit;
-
-  $main::lxdebug->leave_sub();
+  return 1;
 }
 
 1;