]> wagnertech.de Git - mfinanz.git/blobdiff - SL/IR.pm
CsvImport: Aufträge: Auf ungültige Artikel prüfen.
[mfinanz.git] / SL / IR.pm
index 620fa184748dcc480488648fc35ee2a7c45c0f3b..4a5b22e1528b1317e305c93fe3d50f473d8306f7 100644 (file)
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -47,17 +47,25 @@ use SL::IO;
 use SL::MoreCommon;
 use SL::DB::Default;
 use SL::DB::TaxZone;
+use SL::DB;
 use List::Util qw(min);
 
 use strict;
 
 sub post_invoice {
+  my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_post_invoice, $self, $myconfig, $form, $provided_dbh, $payments_only);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _post_invoice {
   my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
 
-  # connect to database, turn off autocommit
-  my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
+  my $dbh = $provided_dbh || SL::DB->client->dbh;
   my $restricter = SL::HTML::Restrict->create;
 
   $form->{defaultcurrency} = $form->get_default_currency($myconfig);
@@ -691,12 +699,7 @@ SQL
     $query = qq|UPDATE ap SET paid = ? WHERE id = ?|;
     do_query($form, $dbh, $query, $form->{paid}, conv_i($form->{id}));
     $form->new_lastmtime('ap');
-    if (!$provided_dbh) {
-      $dbh->commit();
-      $dbh->disconnect();
-    }
 
-    $main::lxdebug->leave_sub();
     return;
   }
 
@@ -716,6 +719,7 @@ SQL
                 invoice      = ?, taxzone_id  = ?, notes         = ?, taxincluded = ?,
                 intnotes     = ?, storno_id   = ?, storno        = ?,
                 cp_id        = ?, employee_id = ?, department_id = ?, delivery_term_id = ?,
+                currency_id = (SELECT id FROM currencies WHERE name = ?),
                 globalproject_id = ?, direct_debit = ?
               WHERE id = ?|;
   @values = (
@@ -725,6 +729,7 @@ SQL
             '1',                             $taxzone_id, $restricter->process($form->{notes}),               $form->{taxincluded} ? 't' : 'f',
                 $form->{intnotes},           conv_i($form->{storno_id}),     $form->{storno}      ? 't' : 'f',
          conv_i($form->{cp_id}),      conv_i($form->{employee_id}), conv_i($form->{department_id}), conv_i($form->{delivery_term_id}),
+                $form->{"currency"},
          conv_i($form->{globalproject_id}),
                 $form->{direct_debit} ? 't' : 'f',
          conv_i($form->{id})
@@ -818,20 +823,11 @@ SQL
     $datev->export;
 
     if ($datev->errors) {
-      $dbh->rollback;
       die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
     }
   }
 
-  my $rc = 1;
-  if (!$provided_dbh) {
-    $rc = $dbh->commit();
-    $dbh->disconnect();
-  }
-
-  $main::lxdebug->leave_sub();
-
-  return $rc;
+  return 1;
 }
 
 sub reverse_invoice {
@@ -917,31 +913,30 @@ sub delete_invoice {
   my ($self, $myconfig, $form) = @_;
   my $query;
   # connect to database
-  my $dbh = $form->dbconnect_noauto($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
-  &reverse_invoice($dbh, $form);
+  SL::DB->client->with_transaction(sub{
 
-  my @values = (conv_i($form->{id}));
-
-  # delete zero entries
-  # wtf? use case for this?
-  $query = qq|DELETE FROM acc_trans WHERE amount = 0|;
-  do_query($form, $dbh, $query);
+    &reverse_invoice($dbh, $form);
 
+    my @values = (conv_i($form->{id}));
 
-  my @queries = (
-    qq|DELETE FROM invoice WHERE trans_id = ?|,
-    qq|DELETE FROM ap WHERE id = ?|,
-  );
+    # delete zero entries
+    # wtf? use case for this?
+    $query = qq|DELETE FROM acc_trans WHERE amount = 0|;
+    do_query($form, $dbh, $query);
 
-  map { do_query($form, $dbh, $_, @values) } @queries;
 
-  my $rc = $dbh->commit;
-  $dbh->disconnect;
+    my @queries = (
+      qq|DELETE FROM invoice WHERE trans_id = ?|,
+      qq|DELETE FROM ap WHERE id = ?|,
+    );
 
-  $main::lxdebug->leave_sub();
+    map { do_query($form, $dbh, $_, @values) } @queries;
+    1;
+  }) or do { die SL::DB->client->error };
 
-  return $rc;
+  return 1;
 }
 
 sub retrieve_invoice {
@@ -1465,8 +1460,8 @@ sub vendor_details {
        LIMIT 1|;
   my $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{vendor_id}, @values);
 
-  # remove id and taxincluded before copy back
-  delete @$ref{qw(id taxincluded)};
+  # remove id,notes (double of vendornotes) and taxincluded before copy back
+  delete @$ref{qw(id taxincluded notes)};
 
   @wanted_vars = grep({ $_ } @wanted_vars);
   if (scalar(@wanted_vars) > 0) {
@@ -1562,12 +1557,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);
 
@@ -1623,12 +1625,7 @@ sub post_payment {
 
   restore_form($old_form);
 
-  my $rc = $dbh->commit();
-  $dbh->disconnect();
-
-  $main::lxdebug->leave_sub();
-
-  return $rc;
+  return 1;
 }
 
 sub get_duedate {