From 2e5a8be3ab917a1ae8c748f0840491f05770d397 Mon Sep 17 00:00:00 2001
From: Moritz Bunkus <m.bunkus@linet-services.de>
Date: Tue, 5 Dec 2006 14:37:40 +0000
Subject: [PATCH] Ermittlung der Folgekonten: Bei Ausgangsrechnungen wird
 zuerst das Lieferdatum, dann das Rechnungsdatum, dann das aktuelle Datum
 betrachtet. Bei Eingangsrechnungen zuerst das Rechnungs-, dann das aktuelle
 Datum. Bei allen anderen Belegen zuerst transdate (z.B. Bestell- oder
 Angebotsdatum), danach das aktuelle Datum.

---
 SL/IR.pm | 27 +++++++++++----------------
 SL/IS.pm | 35 +++++++++++++++++------------------
 SL/OE.pm |  7 +++----
 3 files changed, 31 insertions(+), 38 deletions(-)

diff --git a/SL/IR.pm b/SL/IR.pm
index 025c65f2e..90a17e0b3 100644
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -866,10 +866,9 @@ sub retrieve_invoice {
     delete($ref->{id});
     map { $form->{$_} = $ref->{$_} } keys %$ref;
     $sth->finish;
-    my $transdate = "current_date";
-    if($form->{invdate}) {
-     $transdate = "'$form->{invdate}'";
-    }
+
+    my $transdate =
+      $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
 
     if(!$form->{taxzone_id}) {
       $form->{taxzone_id} = 0;
@@ -1110,6 +1109,9 @@ sub retrieve_item {
 
   my ($self, $myconfig, $form) = @_;
 
+  # connect to database
+  my $dbh = $form->dbconnect($myconfig);
+
   my $i = $form->{rowcount};
 
   # don't include assemblies or obsolete parts
@@ -1138,20 +1140,13 @@ sub retrieve_item {
 
   my $transdate = "";
   if ($form->{type} eq "invoice") {
-    $transdate = "'$form->{invdate}'";
-  } elsif ($form->{type} eq "purchase_order") {
-    $transdate = "'$form->{transdate}'";
-  } elsif ($form->{type} eq "request_quotation") {
-    $transdate = "'$form->{transdate}'";
-  }
-
-  if ($transdate eq "") {
-    $transdate = "current_date";
+    $transdate =
+      $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
+  } else {
+    $transdate =
+      $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
   }
 
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
                         p.listprice, p.inventory_accno_id,
                         c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
diff --git a/SL/IS.pm b/SL/IS.pm
index f9b95ec8b..7943fa60f 100644
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -1532,12 +1532,12 @@ sub retrieve_invoice {
     $sth->finish;
     map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
 
-    my $transdate = "current_date";
-    if($form->{invdate}) {
-     $transdate = "'$form->{invdate}'";
-    }
+    my $transdate =
+      $form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
+      $form->{invdate} ? $dbh->quote($form->{invdate}) :
+      "current_date";
 
-    if(!$form->{taxzone_id}) {
+    if (!$form->{taxzone_id}) {
       $form->{taxzone_id} = 0;
     }
     # retrieve individual items
@@ -1848,6 +1848,9 @@ sub retrieve_item {
 
   my ($self, $myconfig, $form) = @_;
 
+  # connect to database
+  my $dbh = $form->dbconnect($myconfig);
+
   my $i = $form->{rowcount};
 
   my $where = "NOT p.obsolete = '1'";
@@ -1872,22 +1875,18 @@ sub retrieve_item {
     $where .= " ORDER BY p.partnumber";
   }
 
-  my $transdate = "";
+  my $transdate;
   if ($form->{type} eq "invoice") {
-    $transdate = "'$form->{invdate}'";
-  } elsif ($form->{type} eq "sales_order") {
-    $transdate = "'$form->{transdate}'";
-  } elsif ($form->{type} eq "sales_quotation") {
-    $transdate = "'$form->{transdate}'";
-  }
-
-  if ($transdate eq "") {
-    $transdate = "current_date";
+    $transdate =
+      $form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
+      $form->{invdate} ? $dbh->quote($form->{invdate}) :
+      "current_date";
+  } else {
+    $transdate =
+      $form->{transdate} ? $dbh->quote($form->{transdate}) :
+      "current_date";
   }
 
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
                         p.listprice, p.inventory_accno_id,
                         c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
diff --git a/SL/OE.pm b/SL/OE.pm
index 18e48bdd6..5dc3128f5 100644
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -787,10 +787,9 @@ sub retrieve {
     my %oid = ('Pg'     => 'oid',
                'Oracle' => 'rowid');
 
-    my $transdate = "'$form->{transdate}'";
-    if (!$transdate) {
-      $transdate = "current_date";
-    }
+    my $transdate =
+      $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
+
     if(!$form->{taxzone_id}) {
       $form->{taxzone_id} = 0;
     }
-- 
2.20.1