Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / SL / RP.pm
index 41cd390..f98d475 100644 (file)
--- a/SL/RP.pm
+++ b/SL/RP.pm
@@ -53,6 +53,8 @@ use strict;
 sub get_openbalance_date {
   my ($closedto, $target) = map { $::locale->parse_date_to_object(\%::myconfig, $_) } @_;
 
+  return unless $closedto;
+
   $closedto->subtract(years => 1) while ($target - $closedto)->is_negative;
   $closedto->add(days => 1);
   return $::locale->format_date(\%::myconfig, $closedto);
@@ -74,7 +76,7 @@ sub balance_sheet {
   }
 
   # get end of financial year and convert to Date format
-  my ($closedto) = selectfirst_arrayref_query($form, $dbh, 'SELECT closedto FROM defaults');
+  my ($closedto) = selectfirst_array_query($form, $dbh, 'SELECT closedto FROM defaults');
 
   # get date of last opening balance
   my $startdate = get_openbalance_date($closedto, $form->{asofdate});
@@ -344,12 +346,10 @@ sub get_accounts {
     }
 
   } else {                      # if ($form->{method} eq 'cash')
-    # ich sehe keinen sinn das nochmal explizit ohne conv_i aufzurufen
-    # bitte prüfen und löschen jan 15.11.2009
-  #  if ($department_id) {
-  #    $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
-  #    $dpt_where = qq| AND t.department_id = $department_id |;
-  #  }
+    if ($department_id) {
+      $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
+      $dpt_where = qq| AND t.department_id = | . conv_i($department_id);
+    }
 
     $query = qq|
       SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
@@ -595,10 +595,10 @@ sub get_accounts_g {
     }
 
   } else {                      # if ($form->{method} eq 'cash')
-    # s.o. jan 15.11.2009
-#    if ($department_id) {
-#      ($dpt_join, $dpt_where) = sql_department($department_id);
-#    }
+    if ($department_id) {
+      $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
+      $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
+    }
 
     $query = qq|
         SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
@@ -1193,10 +1193,12 @@ sub aging {
   }
 
   my $dpt_join;
+  my $where_dpt;
   if ($form->{department}) {
     my ($null, $department_id) = split /--/, $form->{department};
     $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
     $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
+    $where_dpt = qq| AND (${arap}.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
   }
   my $review_of_aging_list;
   if ($form->{review_of_aging_list}) {
@@ -1217,7 +1219,7 @@ sub aging {
       phone as customerphone, fax as customerfax, ${ct}number,
       "invnumber", "transdate",
       (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,
+      "duedate", invoice, ${arap}.id, date_part('days', now() - duedate) as overduedays,
       (SELECT $buysell
        FROM exchangerate
        WHERE (${arap}.curr = exchangerate.curr)
@@ -1226,6 +1228,7 @@ sub aging {
     WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
       AND NOT COALESCE (${arap}.storno, 'f')
       AND (${arap}.${ct}_id = ${ct}.id)
+      $where_dpt 
       AND (${ct}.id = ?)
       AND (transdate <= (date $todate) $fromwhere )
       $review_of_aging_list
@@ -1290,27 +1293,6 @@ sub get_customer {
   $main::lxdebug->leave_sub();
 }
 
-sub get_taxaccounts {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $myconfig, $form) = @_;
-
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
-  # get tax accounts
-  my $query =
-    qq|SELECT c.accno, c.description, t.rate
-       FROM chart c, tax t
-       WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id)
-       ORDER BY c.accno|;
-  $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query);
-
-  $dbh->disconnect;
-
-  $main::lxdebug->leave_sub();
-}
-
 sub tax_report {
   $main::lxdebug->enter_sub();