Sortierung in Berichten über Kunden und Lieferanten auch auf- und absteigbar sortierb...
[kivitendo-erp.git] / SL / RP.pm
index 76c44e1..365d6e0 100644 (file)
--- a/SL/RP.pm
+++ b/SL/RP.pm
@@ -803,13 +803,23 @@ sub trial_balance {
     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
   }
 
+  my $acc_cash_where = "";
+  my $ar_cash_where = "";
+  my $ap_cash_where = "";
+
+
+  if ($form->{method} eq "cash") {
+    $acc_cash_where = qq| AND (ac.trans_id IN (SELECT id FROM ar WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM ap WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM gl WHERE transdate>='$form->{fromdate}' AND transdate<='$form->{todate}')) |;
+    $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
+  }
+
   # get beginning balances
   $query =
     qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
         FROM acc_trans ac
-        JOIN chart c ON (ac.chart_id = c.id)
+        LEFT JOIN chart c ON (ac.chart_id = c.id)
         $dpt_join
-        WHERE (ac.transdate < (select date_trunc('year', date ?)))
+        WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction $acc_cash_where
           $dpt_where
           $project
         GROUP BY c.accno, c.category, c.description |;
@@ -821,12 +831,11 @@ sub trial_balance {
     if ($ref->{amount} != 0 || $form->{all_accounts}) {
       $trb{ $ref->{accno} }{description} = $ref->{description};
       $trb{ $ref->{accno} }{charttype}   = 'A';
-      if ($ref->{category} ne "I" &&  $ref->{category} ne "E") {
-        if ($ref->{amount} > 0) {
-          $trb{ $ref->{accno} }{haben_eb}   = $ref->{amount};
-        } else {
-          $trb{ $ref->{accno} }{soll_eb}   = $ref->{amount} * -1;
-        }
+   
+      if ($ref->{amount} > 0) {
+        $trb{ $ref->{accno} }{haben_eb}   = $ref->{amount};
+      } else {
+        $trb{ $ref->{accno} }{soll_eb}   = $ref->{amount} * -1;
       }
       $trb{ $ref->{accno} }{category}    = $ref->{category};
     }
@@ -865,9 +874,9 @@ sub trial_balance {
       $tofrom   .= " AND (ac.transdate >= $fromdate)";
       $subwhere .= " AND (transdate >= $fromdate)";
       $sumsubwhere .= " AND (transdate >= (select date_trunc('year', date $fromdate))) ";
-      $saldosubwhere .= " AND (((c.category='I' OR c.category='E') AND transdate>=(select date_trunc('year', date $fromdate))) OR (c.category NOT IN ('I', 'E'))) ";
+      $saldosubwhere .= " AND transdate>=(select date_trunc('year', date $fromdate))  ";
       $invwhere .= " AND (a.transdate >= $fromdate)";
-      $glsaldowhere .= " AND (((c.category='I' OR c.category='E') AND ac.transdate>=(select date_trunc('year', date $fromdate))) OR (c.category NOT IN ('I', 'E'))) ";
+      $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
       $glwhere = " AND (ac.transdate >= $fromdate)";
       $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
     }
@@ -965,9 +974,9 @@ qq| AND ((ac.trans_id IN (SELECT id from ar) AND
               )|;
 
   } else {
-    $where .= $tofrom;
-    $saldowhere .= $glsaldowhere;
-    $sumwhere .= $glsumwhere;
+    $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
+    $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
+    $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
   }
 
   $query = qq|
@@ -1159,6 +1168,7 @@ qq| AND ((ac.trans_id IN (SELECT id from ar) AND
     $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
   }
 
+
   my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
 
   foreach my $accno (sort keys %trb) {
@@ -1274,23 +1284,28 @@ sub aging {
   # connect to database
   my $dbh     = $form->dbconnect($myconfig);
 
-  my ($invoice, $arap, $buysell, $ct, $ct_id);
+  my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
 
   if ($form->{ct} eq "customer") {
     $invoice = "is";
     $arap = "ar";
     $buysell = "buy";
     $ct = "customer";
+    $ml = -1;
   } else {
     $invoice = "ir";
     $arap = "ap";
     $buysell = "sell";
     $ct = "vendor";
+    $ml = 1;
   }
   $ct_id = "${ct}_id";
 
   $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
   my $todate = conv_dateq($form->{todate});
+  my $fromdate = conv_dateq($form->{fromdate});
+
+  my $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
 
   my $where = " 1 = 1 ";
   my ($name, $null);
@@ -1315,84 +1330,18 @@ sub aging {
       street, zipcode, city, country, contact, email,
       phone as customerphone, fax as customerfax, ${ct}number,
       "invnumber", "transdate",
-      (amount - paid) as "c0", 0.00 as "c30", 0.00 as "c60", 0.00 as "c90",
-      "duedate", invoice, ${arap}.id,
-      (SELECT $buysell
-       FROM exchangerate
-       WHERE (${arap}.curr = exchangerate.curr)
-         AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
-    FROM ${arap}, ${ct}
-    WHERE (paid != amount)
-      AND (${arap}.storno IS FALSE)
-      AND (${arap}.${ct}_id = ${ct}.id)
-      AND (${ct}.id = ?)
-      AND (transdate <= (date $todate - interval '0 days'))
-      AND (transdate >= (date $todate - interval '30 days'))
-
-    UNION
-
-    -- between 31-60 days
-
-    SELECT ${ct}.id AS ctid, ${ct}.name,
-      street, zipcode, city, country, contact, email,
-      phone as customerphone, fax as customerfax, ${ct}number,
-      "invnumber", "transdate",
-      0.00 as "c0", (amount - paid) as "c30", 0.00 as "c60", 0.00 as "c90",
+      (amount - COALESCE((SELECT sum(amount)*$ml FROM acc_trans LEFT JOIN chart ON (acc_trans.chart_id=chart.id) WHERE link ilike '%paid%' AND acc_trans.trans_id=${arap}.id AND acc_trans.transdate <= (date $todate)),0)) as "open", "amount",
       "duedate", invoice, ${arap}.id,
       (SELECT $buysell
        FROM exchangerate
        WHERE (${arap}.curr = exchangerate.curr)
          AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
     FROM ${arap}, ${ct}
-    WHERE (paid != amount)
-      AND (${arap}.storno IS FALSE)
-      AND (${arap}.${ct}_id = ${ct}.id)
-      AND (${ct}.id = ?)
-      AND (transdate < (date $todate - interval '30 days'))
-      AND (transdate >= (date $todate - interval '60 days'))
-
-    UNION
-
-    -- between 61-90 days
-
-    SELECT ${ct}.id AS ctid, ${ct}.name,
-      street, zipcode, city, country, contact, email,
-      phone as customerphone, fax as customerfax, ${ct}number,
-      "invnumber", "transdate",
-      0.00 as "c0", 0.00 as "c30", (amount - paid) as "c60", 0.00 as "c90",
-      "duedate", invoice, ${arap}.id,
-      (SELECT $buysell
-       FROM exchangerate
-       WHERE (${arap}.curr = exchangerate.curr)
-         AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
-    FROM ${arap}, ${ct}
-    WHERE (paid != amount)
-      AND (${arap}.storno IS FALSE)
-      AND (${arap}.${ct}_id = ${ct}.id)
-      AND (${ct}.id = ?)
-      AND (transdate < (date $todate - interval '60 days'))
-      AND (transdate >= (date $todate - interval '90 days'))
-
-    UNION
-
-    -- over 90 days
-
-    SELECT ${ct}.id AS ctid, ${ct}.name,
-      street, zipcode, city, country, contact, email,
-      phone as customerphone, fax as customerfax, ${ct}number,
-      "invnumber", "transdate",
-      0.00 as "c0", 0.00 as "c30", 0.00 as "c60", (amount - paid) as "c90",
-      "duedate", invoice, ${arap}.id,
-      (SELECT $buysell
-       FROM exchangerate
-       WHERE (${arap}.curr = exchangerate.curr)
-       AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
-    FROM ${arap}, ${ct}
-    WHERE (paid != amount)
+    WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
       AND (${arap}.storno IS FALSE)
       AND (${arap}.${ct}_id = ${ct}.id)
       AND (${ct}.id = ?)
-      AND (transdate < (date $todate - interval '90 days'))
+      AND (transdate <= (date $todate) $fromwhere )
 
     ORDER BY ctid, transdate, invnumber |;
 
@@ -1405,8 +1354,8 @@ sub aging {
        $dpt_join
        WHERE $where
          AND (a.${ct_id} = ct.id)
-         AND (a.paid != a.amount)
-         AND (a.transdate <= $todate)
+         AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
+         AND (a.transdate <= $todate $fromwhere)
        ORDER BY ct.name|;
 
   my $sth = prepare_execute_query($form, $dbh, $query);
@@ -1414,7 +1363,7 @@ sub aging {
   $form->{AG} = [];
   # for each company that has some stuff outstanding
   while (my ($id) = $sth->fetchrow_array) {
-    do_statement($form, $sth_details, $q_details, $id, $id, $id, $id);
+    do_statement($form, $sth_details, $q_details, $id);
 
     while (my $ref = $sth_details->fetchrow_hashref(NAME_lc)) {
       $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;