Beim Anlegen des allerersten Beleges eines Typs dafür sorgen, dass vendor_id bzw...
[kivitendo-erp.git] / SL / IR.pm
index 6950739..72a36ae 100644 (file)
--- a/SL/IR.pm
+++ b/SL/IR.pm
 package IR;
 
 use SL::AM;
+use SL::ARAP;
 use SL::Common;
 use SL::DBUtils;
+use SL::DO;
 use SL::MoreCommon;
 use List::Util qw(min);
 
@@ -178,8 +180,6 @@ sub post_invoice {
       @values = ($form->{"sellprice_$i"}, conv_i($form->{"id_$i"}));
       do_query($form, $dbh, $query, @values);
 
-      $form->update_balance($dbh, "parts", "onhand", qq|id = ?|, $baseqty, $form->{"id_$i"}) if !$form->{shipped};
-
       # check if we sold the item already and
       # make an entry for the expense and inventory
       $query =
@@ -540,8 +540,36 @@ sub post_invoice {
 
   Common::webdav_folder($form) if ($main::webdav);
 
-  my $rc = 1;
+  # Link this record to the records it was created from.
+  RecordLinks->create_links('dbh'        => $dbh,
+                            'mode'       => 'ids',
+                            'from_table' => 'oe',
+                            'from_ids'   => $form->{convert_from_oe_ids},
+                            'to_table'   => 'ap',
+                            'to_id'      => $form->{id},
+    );
+  delete $form->{convert_from_oe_ids};
+
+  my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids};
+  if (scalar @convert_from_do_ids) {
+    DO->close_orders('dbh' => $dbh,
+                     'ids' => \@convert_from_do_ids);
+
+    RecordLinks->create_links('dbh'        => $dbh,
+                              'mode'       => 'ids',
+                              'from_table' => 'delivery_orders',
+                              'from_ids'   => \@convert_from_do_ids,
+                              'to_table'   => 'ap',
+                              '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) {
     $rc = $dbh->commit();
     $dbh->disconnect();
@@ -572,9 +600,6 @@ sub reverse_invoice {
 
     next unless $ref->{inventory_accno_id};
 
-    # update onhand
-    $form->update_balance($dbh, "parts", "onhand", qq|id = $ref->{parts_id}|, $ref->{qty});
-
     # if $ref->{allocated} > 0 than we sold that many items
     next if ($ref->{allocated} <= 0);
 
@@ -841,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,
@@ -1057,6 +1082,8 @@ sub retrieve_item {
     $stw->finish();
     chop $ref->{taxaccounts};
 
+    $ref->{onhand} *= 1;
+
     push @{ $form->{item_list} }, $ref;
 
   }
@@ -1078,7 +1105,7 @@ sub vendor_details {
   my @values;
 
   # get contact id, set it if nessessary
-  $form->{cp_id} = (split /--/, $form->{contact})[1];
+  $form->{cp_id} *= 1;
   my $contact = "";
   if ($form->{cp_id}) {
     $contact = "AND cp.cp_id = ?";
@@ -1108,6 +1135,11 @@ sub vendor_details {
 
   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
+  my $custom_variables = CVar->get_custom_variables('dbh'      => $dbh,
+                                                    'module'   => 'CT',
+                                                    'trans_id' => $form->{vendor_id});
+  map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
+
   $dbh->disconnect();
 
   $main::lxdebug->leave_sub();
@@ -1251,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;