Abfangen von leerer warehouse_id bevor Objekt gesucht wird
[kivitendo-erp.git] / SL / AP.pm
index 5947285..46b25ea 100644 (file)
--- a/SL/AP.pm
+++ b/SL/AP.pm
@@ -25,7 +25,8 @@
 # GNU General Public License for more details.
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1335, USA.
 #======================================================================
 #
 # Accounts Payables database backend routines
@@ -40,17 +41,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;
@@ -58,8 +66,6 @@ sub post_transaction {
   $form->{defaultcurrency} = $form->get_default_currency($myconfig);
   $form->{taxincluded} = 0 unless $form->{taxincluded};
 
-  ($null, $form->{department_id}) = split(/--/, $form->{department});
-
   if ($form->{currency} eq $form->{defaultcurrency}) {
     $form->{exchangerate} = 1;
   } else {
@@ -319,6 +325,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 +387,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 +399,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 {
@@ -460,13 +461,9 @@ sub ap_transactions {
     $where .= " AND (cp.cp_name ILIKE ? OR cp.cp_givenname ILIKE ?)";
     push(@values, (like($form->{"cp_name"}))x2);
   }
-  if ($form->{department}) {
-    # ähnlich wie commit 0bbfb33b6aa8e38bb6c81d1684ab7d08e5b5c5af abteilung
-    # wird so nicht mehr als zeichenkette zusammengebaut
-    # hätte zu ee9f9f9aa4c3b9d5d20ab10a45c12bcaa6aa78d0 auffallen können ;-) jan
-    #my ($null, $department_id) = split /--/, $form->{department};
+  if ($form->{department_id}) {
     $where .= " AND a.department_id = ?";
-    push(@values, $form->{department});
+    push(@values, $form->{department_id});
   }
   if ($form->{invnumber}) {
     $where .= " AND a.invnumber ILIKE ?";
@@ -563,7 +560,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 +569,6 @@ sub get_transdate {
     "  current_date)";
   ($form->{transdate}) = $dbh->selectrow_array($query);
 
-  $dbh->disconnect;
-
   $main::lxdebug->leave_sub();
 }
 
@@ -617,12 +612,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 +673,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 +743,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 +807,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 +865,7 @@ sub storno {
 
   map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
 
-  $dbh->commit;
-
-  $main::lxdebug->leave_sub();
+  return 1;
 }
 
 1;