Überbleibsel von der Unterstützung für html2ps entfernt.
[kivitendo-erp.git] / SL / USTVA.pm
index 9f1551e..b10fea6 100644 (file)
 
 package USTVA;
 
+use SL::DBUtils;
+
+sub get_coa {
+  
+  my ( $self, $form, $myconfig) = @_;
+  
+  my $query = q{ SELECT coa FROM defaults };
+  
+  my $dbh = $form->dbconnect($myconfig);
+  my $sth = $dbh->prepare($query);
+  $sth->execute() || $form->dberror($query);
+
+  my ($coa) = selectrow_query($form, $dbh, $query);  
+  
+  $sth->finish;
+  $dbh->disconnect;
+  
+  $form->{coa} = $coa;
+  $form->{"COA_$coa"} = '1';
+  $form->{COA_Germany} = '1' if ( $coa eq 'Germany-DATEV-SKR03EU' or $coa eq 'Germany-DATEV-SKR04EU');
+
+  return;
+}
+
+
 sub report_variables {
   # Get all positions for taxreport out of the database
   # Needs Databaseupdate Pg-upgrade2/USTVA_abstraction.pl
@@ -53,8 +78,6 @@ sub report_variables {
     $where_dcp  
   |;
   
-  $main::lxdebug->message(LXDebug::QUERY, "\$query= \n $query\n");
-  
   my $dbh = $form->dbconnect($myconfig);
   my $sth = $dbh->prepare($query);
 
@@ -363,11 +386,7 @@ sub info {
 
   } else {
 
-    if ($form->{error_function}) {
-      &{ $form->{error_function} }($msg);
-    } else {
-      die "Hinweis: $msg\n";
-    }
+    die "Hinweis: $msg\n";
   }
 
   $main::lxdebug->leave_sub();
@@ -562,12 +581,12 @@ sub process_query {
 
   #  return unless (-f $filename);
 
-  open(FH, "$filename") or $form->error("$filename : $!\n");
+  open my $FH, "<", "$filename" or $form->error("$filename : $!\n");
   my $query = "";
   my $sth;
   my @quote_chars;
 
-  while (<FH>) {
+  while (<$FH>) {
 
     # Remove DOS and Unix style line endings.
     s/[\r\n]//g;
@@ -606,7 +625,7 @@ sub process_query {
     }
   }
 
-  close FH;
+  close $FH;
 
   $main::lxdebug->leave_sub();
 }
@@ -751,12 +770,7 @@ sub get_accounts_ustva {
 
   my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
 
-  my ($null, $department_id) = split /--/, $form->{department};
-
   my $query;
-  my $dpt_where;
-  my $dpt_join;
-  my $project;
   my $where    = "";
   my $glwhere  = "";
   my $subwhere = "";
@@ -782,20 +796,21 @@ sub get_accounts_ustva {
     $ARwhere  .= " AND acc.transdate <= '$todate'";
   }
 
-  if ($department_id) {
-    $dpt_join = qq|
-               JOIN department t ON (a.department_id = t.id)
-                 |;
-    $dpt_where = qq|
-               AND t.id = $department_id
-                  |;
-  }
+  my $acc_trans_where = '1=1';
+  if ($fromdate || $todate) {
+    $acc_trans_where = "ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE ";
 
-  if ($form->{project_id}) {
-    $project = qq|
-                 AND ac.project_id = $form->{project_id}
-                |;
+    if ($fromdate) {
+      $acc_trans_where .= "transdate >= '$fromdate'";
+    }
+    if ($todate) {
+      $acc_trans_where .= " AND " if ($fromdate);
+      $acc_trans_where .= "transdate <= '$todate'";
+    }
+
+    $acc_trans_where .= ")";
   }
+
   ############################################
   # Method eq 'cash' = IST Versteuerung
   ############################################
@@ -843,9 +858,7 @@ sub get_accounts_ustva {
          )
        )
        WHERE 
-       1=1 
-       -- Here no where, please. All Transactions ever should be
-       -- testet if they are paied in the USTVA report period.
+       $acc_trans_where
        GROUP BY tk.pos_ustva
     |;
    
@@ -854,16 +867,6 @@ sub get_accounts_ustva {
     # Method eq 'accrual' = Soll Versteuerung
     #########################################
 
-    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
-             |;
-    }
-
-
     $query = qq|
        -- Alle Einnahmen AR und pos_ustva erfassen
        SELECT
@@ -883,14 +886,12 @@ sub get_accounts_ustva {
        $dpt_join
        WHERE 1 = 1
        $where
-       $dpt_where
-       $project
        GROUP BY tk.pos_ustva
   |;
    
   } else {
   
-    $self->error("Unknown tax method: $form->{method}")
+    $form->error("Unknown tax method: $form->{method}")
 
   }
   
@@ -912,7 +913,7 @@ sub get_accounts_ustva {
              SELECT id FROM taxkeys 
              WHERE 1=1
                AND chart_id=ac.chart_id 
-               AND taxkey_id = ac.taxkey 
+               --AND taxkey_id = ac.taxkey 
                AND startdate <= COALESCE(AP.transdate)
              ORDER BY startdate DESC LIMIT 1
            )
@@ -920,8 +921,6 @@ sub get_accounts_ustva {
        WHERE
        1=1
        $where
-       $dpt_where
-       $project
        GROUP BY tk.pos_ustva
 
      UNION -- Einnahmen direkter gl Buchungen erfassen
@@ -945,8 +944,6 @@ sub get_accounts_ustva {
        $dpt_join
        WHERE 1 = 1
        $where
-       $dpt_from
-       $project
        GROUP BY tk.pos_ustva
 
 
@@ -971,8 +968,6 @@ sub get_accounts_ustva {
        $dpt_join
        WHERE 1 = 1
        $where
-       $dpt_from
-       $project
        GROUP BY tk.pos_ustva
 
   |;
@@ -989,18 +984,10 @@ sub get_accounts_ustva {
   
   $sth->execute || $form->dberror($query);
 
-  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
+  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
     # Bug 365 solved?!
     $ref->{amount} *= -1;
-    if ($category eq "pos_bwa") {
-      if ($last_period) {
-        $form->{ $ref->{$category} }{kumm} += $ref->{amount};
-      } else {
-        $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
-      }
-    } else {
-      $form->{ $ref->{$category} } += $ref->{amount};
-    }
+    $form->{ $ref->{$category} } += $ref->{amount};
   }
 
   $sth->finish;
@@ -1012,19 +999,26 @@ sub get_accounts_ustva {
 sub get_config {
   $main::lxdebug->enter_sub();
 
-  my ($self, $userpath, $filename) = @_;
+  my ($self, $userspath, $filename) = @_;
+
+  $form->error("Missing Parameter: @_") if !$userspath || !$filename;
 
   my $form = $main::form;
 
-  if (!open(FACONF, "$userpath/$form->{login}_$filename")) {
-    open(FANEW, ">$userpath/$form->{login}_$filename") ||
-      $form->error("$userpath/$filename : $!");
-    close(FANEW);
-    open(FACONF, "$userpath/$form->{login}_$filename") ||
-      $form->error("$userpath/$form->{username}_$filename : $!");
-  }
+  $filename = "$form->{login}_$filename";
+  $filename =~ s|.*/||;
+  $filename = "$userspath/$filename";
+  open my $FACONF, "<", $filename or sub {# Annon Sub
+    # catch open error
+    # create file if file does not exist
+    open my $FANEW, ">", $filename  or $form->error("CREATE: $filename : $!");
+    close $FANEW                    or $form->error("CLOSE: $filename : $!");
+    
+    #try again open file
+    open my $FACONF, "<", $filename or $form->error("OPEN: $filename : $!");
+  };
 
-  while (<FACONF>) {
+  while (<$FACONF>) {
     last if (/^\[/);
     next if (/^(\#|\s)/);
 
@@ -1039,7 +1033,7 @@ sub get_config {
 
   }
 
-  close(FACONF);
+  close $FACONF;
 
   $main::lxdebug->leave_sub();
 }