Sortierung beim Buchungsjournal leicht angepasst.
[kivitendo-erp.git] / SL / GL.pm
index 4dbd703..0164836 100644 (file)
--- a/SL/GL.pm
+++ b/SL/GL.pm
@@ -39,6 +39,7 @@
 package GL;
 
 use Data::Dumper;
+use SL::DBUtils;
 
 sub delete_transaction {
   my ($self, $myconfig, $form) = @_;
@@ -74,9 +75,6 @@ sub post_transaction {
   # check if debit and credit balances
 
   if ($form->{storno}) {
-    $debit               = $debit * -1;
-    $credit              = $credit * -1;
-    $tax                 = $tax * -1;
     $form->{reference}   = "Storno-" . $form->{reference};
     $form->{description} = "Storno-" . $form->{description};
   }
@@ -179,10 +177,10 @@ sub post_transaction {
       $posted = 0;
     }
 
+    $project_id = conv_i($form->{"project_id_$i"});
+
     # if there is an amount, add the record
     if ($amount != 0) {
-      $project_id =
-        ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
                   source, memo, project_id, taxkey)
                  VALUES
@@ -192,30 +190,24 @@ sub post_transaction {
                   $amount, '$form->{transdate}', |
         . $dbh->quote($form->{"source_$i"}) . qq|, |
         . $dbh->quote($form->{"memo_$i"}) . qq|,
-                 $project_id, $taxkey)|;
+                 ?, $taxkey)|;
 
-      $dbh->do($query) || $form->dberror($query);
+      do_query($form, $dbh, $query, $project_id);
     }
 
     if ($tax != 0) {
-
       # add taxentry
-      $amount = $tax;
-
-      $project_id =
-        ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
                   source, memo, project_id, taxkey)
                   VALUES
                   ($form->{id}, (SELECT t.chart_id
                   FROM tax t
                   WHERE t.id = $form->{"tax_id_$i"}),
-                  $amount, '$form->{transdate}', |
+                  $tax, '$form->{transdate}', |
         . $dbh->quote($form->{"source_$i"}) . qq|, |
-        . $dbh->quote($form->{"memo_$i"}) . qq|,
-                          $project_id, $taxkey)|;
+        . $dbh->quote($form->{"memo_$i"}) . qq|, ?, $taxkey)|;
 
-      $dbh->do($query) || $form->dberror($query);
+      do_query($form, $dbh, $query, $project_id);
     }
   }
 
@@ -305,6 +297,21 @@ sub all_transactions {
     $apwhere .=
       " AND ap.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
   }
+  if ($form->{project_id}) {
+    $glwhere .= " AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")";
+    $arwhere .=
+      " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
+      "      (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
+    $apwhere .=
+      " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
+      "      (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
+  }
+
+  my ($project_columns, %project_join);
+  if ($form->{"l_projectnumbers"}) {
+    $project_columns = ", ac.project_id, pr.projectnumber";
+    $project_join = "LEFT JOIN project pr ON (ac.project_id = pr.id)";
+  }
 
   if ($form->{accno}) {
 
@@ -380,7 +387,8 @@ sub all_transactions {
     qq|SELECT ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
                  g.description, ac.transdate, ac.source, ac.trans_id,
                 ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
-                 FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
+                 $project_columns
+                 FROM gl g, acc_trans ac $project_join, chart c LEFT JOIN tax t ON
                  (t.chart_id=c.id)
                  WHERE $glwhere
                 AND ac.chart_id = c.id
@@ -389,7 +397,8 @@ sub all_transactions {
                 SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
                 ct.name, ac.transdate, ac.source, ac.trans_id,
                 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
-                FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
+                 $project_columns
+                FROM ar a, acc_trans ac $project_join, customer ct, chart c LEFT JOIN tax t ON
                  (t.chart_id=c.id)
                 WHERE $arwhere
                 AND ac.chart_id = c.id
@@ -399,13 +408,14 @@ sub all_transactions {
                 SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
                 ct.name, ac.transdate, ac.source, ac.trans_id,
                 ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
-                FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
+                 $project_columns
+                FROM ap a, acc_trans ac $project_join, vendor ct, chart c LEFT JOIN tax t ON
                  (t.chart_id=c.id)
                 WHERE $apwhere
                 AND ac.chart_id = c.id
                 AND a.vendor_id = ct.id
                 AND a.id = ac.trans_id
-                ORDER BY $sortorder transdate,acoid, trans_id, taxkey DESC|;
+                ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
 
   # Show all $query in Debuglevel LXDebug::QUERY
   $callingdetails = (caller (0))[3];
@@ -452,7 +462,10 @@ sub all_transactions {
           $ref->{module} = "ar";
         }
       }
-    
+
+      $ref->{"projectnumbers"} = {};
+      $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
+
       $balance = $ref->{amount};
     
       # Linenumbers of General Ledger  
@@ -487,13 +500,13 @@ sub all_transactions {
           $ref->{debit}{$k}        = $ref->{amount} * -1;
           $ref->{debit_accno}{$k}  = $ref->{accno};
           $ref->{debit_taxkey}{$k} = $ref->{taxkey};
-          $ref->{transdate}{$k}    = $ref->{transdate};
+          $ref->{ac_transdate}{$k} = $ref->{transdate};
 
         } else {
           $ref->{credit}{$l}        = $ref->{amount} * 1;
           $ref->{credit_accno}{$l}  = $ref->{accno};
           $ref->{credit_taxkey}{$l} = $ref->{taxkey};
-          $ref->{transdate}{$l}     = $ref->{transdate};
+          $ref->{ac_transdate}{$l}  = $ref->{transdate};
 
 
         }
@@ -508,6 +521,7 @@ sub all_transactions {
       $balance =
         (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
 
+      $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
 
       if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
         if ($ref2->{amount} < 0) {
@@ -549,7 +563,7 @@ sub all_transactions {
           $ref->{debit}{$k}        = $ref2->{amount} * - 1;
           $ref->{debit_accno}{$k}  = $ref2->{accno};
           $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
-          $ref->{transdate}{$k}    = $ref2->{transdate};
+          $ref->{ac_transdate}{$k} = $ref2->{transdate};
         } else {
           if ($ref->{credit_accno}{$l} ne "") {
             $l++;
@@ -557,7 +571,7 @@ sub all_transactions {
           $ref->{credit}{$l}        = $ref2->{amount};
           $ref->{credit_accno}{$l}  = $ref2->{accno};
           $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
-          $ref->{transdate}{$l}     = $ref2->{transdate};
+          $ref->{ac_transdate}{$l}  = $ref2->{transdate};
         }
       }
     }
@@ -621,8 +635,8 @@ sub transaction {
 
     # retrieve individual rows
     $query = qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
-                a.transdate, a.cleared, a.project_id, p.projectnumber,(SELECT p.projectnumber FROM project p
-                WHERE a.project_id = p.id) AS projectnumber, a.taxkey, t.rate AS taxrate, t.id, (SELECT c1.accno FROM chart c1, tax t1 WHERE t1.id=t.id AND c1.id=t.chart_id) AS taxaccno, (SELECT tk.tax_id FROM taxkeys tk WHERE tk.chart_id =a.chart_id AND tk.startdate<=a.transdate ORDER BY tk.startdate desc LIMIT 1) AS tax_id
+                a.transdate, a.cleared, a.project_id, p.projectnumber,
+                a.taxkey, t.rate AS taxrate, t.id, (SELECT c1.accno FROM chart c1, tax t1 WHERE t1.id=t.id AND c1.id=t.chart_id) AS taxaccno, (SELECT tk.tax_id FROM taxkeys tk WHERE tk.chart_id =a.chart_id AND tk.startdate<=a.transdate ORDER BY tk.startdate desc LIMIT 1) AS tax_id
                FROM acc_trans a
                JOIN chart c ON (c.id = a.chart_id)
                LEFT JOIN project p ON (p.id = a.project_id)