Beim Anlegen des allerersten Beleges eines Typs dafür sorgen, dass vendor_id bzw...
[kivitendo-erp.git] / SL / IR.pm
index f3c2674..72a36ae 100644 (file)
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -35,6 +35,7 @@
 package IR;
 
 use SL::AM;
+use SL::ARAP;
 use SL::Common;
 use SL::DBUtils;
 use SL::DO;
@@ -562,6 +563,11 @@ sub post_invoice {
                               'to_id'      => $form->{id},
       );
   }
+  delete $form->{convert_from_do_ids};
+
+  ARAP->close_orders_if_billed('dbh'     => $dbh,
+                               'arap_id' => $form->{id},
+                               'table'   => 'ap',);
 
   my $rc = 1;
   if (!$provided_dbh) {
@@ -860,7 +866,7 @@ sub get_vendor {
   }
   my $query =
     qq|SELECT
-         v.name AS vendor, v.creditlimit, v.terms, v.notes AS intnotes,
+         v.id AS vendor_id, v.name AS vendor, v.creditlimit, v.terms, v.notes AS intnotes,
          v.email, v.cc, v.bcc, v.language_id, v.payment_id,
          v.street, v.zipcode, v.city, v.country, v.taxzone_id,
          $duedate + COALESCE(pt.terms_netto, 0) AS duedate,
@@ -1277,4 +1283,42 @@ sub post_payment {
   return $rc;
 }
 
+sub get_duedate {
+  $main::lxdebug->enter_sub();
+
+  my $self     = shift;
+  my %params   = @_;
+
+  if (!$params{vendor_id} || !$params{invdate}) {
+    $main::lxdebug->leave_sub();
+    return $params{default};
+  }
+
+  my $myconfig = \%main::myconfig;
+  my $form     = $main::form;
+
+  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+
+  my $query    = qq|SELECT ?::date + pt.terms_netto
+                    FROM vendor v
+                    LEFT JOIN payment_terms pt ON (pt.id = v.payment_id)
+                    WHERE v.id = ?|;
+
+  my ($sth, $duedate);
+
+  if (($sth = $dbh->prepare($query)) && $sth->execute($params{invdate}, conv_i($params{vendor_id}))) {
+    ($duedate) = $sth->fetchrow_array();
+    $sth->finish();
+  } else {
+    $dbh->rollback();
+  }
+
+  $duedate ||= $params{default};
+
+  $main::lxdebug->leave_sub();
+
+  return $duedate;
+}
+
+
 1;