DB Handling: Einige executes mit Fehlerbehandlung versehen
[kivitendo-erp.git] / SL / SEPA.pm
index 0746732..3231ac9 100644 (file)
@@ -24,6 +24,7 @@ sub retrieve_open_invoices {
   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
   my $arap     = $params{vc} eq 'customer' ? 'ar'       : 'ap';
   my $vc       = $params{vc} eq 'customer' ? 'customer' : 'vendor';
+  my $vc_vc_id = $params{vc} eq 'customer' ? 'c_vendor_id' : 'v_customer_id';
 
   my $mandate  = $params{vc} eq 'customer' ? " AND COALESCE(vc.mandator_id, '') <> '' AND vc.mandate_date_of_signature IS NOT NULL " : '';
 
@@ -43,10 +44,13 @@ sub retrieve_open_invoices {
          (${arap}.amount - (${arap}.amount * pt.percent_skonto)) as amount_less_skonto,
          (${arap}.amount * pt.percent_skonto) as skonto_amount,
          vc.name AS vcname, vc.language_id, ${arap}.duedate as duedate, ${arap}.direct_debit,
+         vc.${vc_vc_id} as vc_vc_id,
 
          COALESCE(vc.iban, '') <> '' AND COALESCE(vc.bic, '') <> '' ${mandate} AS vc_bank_info_ok,
 
-         ${arap}.amount - ${arap}.paid - COALESCE(open_transfers.amount, 0) AS open_amount
+         ${arap}.amount - ${arap}.paid - COALESCE(open_transfers.amount, 0) AS open_amount,
+         COALESCE(open_transfers.amount, 0) AS transfer_amount,
+         pt.description as pt_description
 
        FROM ${arap}
        LEFT JOIN ${vc} vc ON (${arap}.${vc}_id = vc.id)
@@ -64,6 +68,7 @@ sub retrieve_open_invoices {
 
        ORDER BY lower(vc.name) ASC, lower(${arap}.invnumber) ASC
 |;
+    #  $main::lxdebug->message(LXDebug->DEBUG2(),"sepa add query:".$query);
 
   my $results = selectall_hashref_query($form, $dbh, $query);
 
@@ -163,7 +168,7 @@ sub _create_export {
       $transfer->{reference} = "${invnumber}-${num_payments}";
     }
 
-    $h_item_id->execute();
+    $h_item_id->execute() || $::form->dberror($q_item_id);
     my ($item_id)      = $h_item_id->fetchrow_array();
 
     my $end_to_end_id  = strftime "LXO%Y%m%d%H%M%S", localtime;
@@ -280,7 +285,8 @@ sub close_export {
     my $query        = qq|UPDATE sepa_export SET closed = TRUE WHERE id IN ($placeholders)|;
 
     do_query($form, $dbh, $query, map { conv_i($_) } @ids);
-  });
+    1;
+  }) or do { die SL::DB->client->error };
 
   $main::lxdebug->leave_sub();
 }