Bug 17602: Koha::ExternalContent->koha_patron() will retutn undef rather than die...
[srvgit] / reports / cash_register_stats.pl
index 2384ae3..0a1ff54 100755 (executable)
@@ -25,8 +25,6 @@ use C4::Circulation;
 use DateTime;
 use Koha::DateUtils;
 use Text::CSV::Encoded;
-#use Data::Dumper;
-#use Smart::Comments;
 
 my $input            = new CGI;
 my $dbh              = C4::Context->dbh;
@@ -68,23 +66,26 @@ if ($do_it) {
     $toDate   = output_pref({ dt => eval { dt_from_string($input->param("to")) } || dt_from_string,
             dateformat => 'sql', dateonly => 1 }); #for sql query
 
-    my $whereTType = '';
+    my $whereTType = q{};
+    my @extra_params; # if we add conditions to the select we need extra params
 
     if ($transaction_type eq 'ALL') { #All Transactons
-        $whereTType = '';
+        $whereTType = q{};
     } elsif ($transaction_type eq 'ACT') { #Active
-        $whereTType = " accounttype NOT IN ('F', 'FU', 'FOR', 'M', 'L') AND ";
+        $whereTType = q{ AND accounttype IN ('Pay','C') };
     } else { #Single transac type
         if ($transaction_type eq 'FORW') {
-            $whereTType = " accounttype = 'FOR' OR accounttype = 'W' AND ";
+            $whereTType = q{ AND accounttype IN ('FOR','W') };
         } else {
-            $whereTType = " accounttype = '$transaction_type' AND ";
+            $whereTType = q{ AND accounttype = ? };
+            push @extra_params, $transaction_type;
         }
     }
 
-    my $whereBranchCode = '';
+    my $whereBranchCode = q{};
     if ($manager_branchcode ne 'ALL') {
-        $whereBranchCode = "AND m.branchcode = '$manager_branchcode'";
+        $whereBranchCode = q{ AND m.branchcode = ?};
+        push @extra_params, $manager_branchcode;
     }
 
 
@@ -100,13 +101,13 @@ if ($do_it) {
         LEFT JOIN branches br ON (br.branchcode = m.branchcode )
         LEFT JOIN items i ON (i.itemnumber = al.itemnumber)
         LEFT JOIN biblio bi ON (bi.biblionumber = i.biblionumber)
-        WHERE $whereTType
-        CAST(al.date AS DATE) BETWEEN ? AND ?
+        WHERE CAST(al.date AS DATE) BETWEEN ? AND ?
+        $whereTType
         $whereBranchCode
         ORDER BY al.date
     ";
-    my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query" . $dbh->errstr;
-    $sth_stats->execute($fromDate, $toDate) or die "Unable to execute query " . $sth_stats->errstr;
+    my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query " . $dbh->errstr;
+    $sth_stats->execute($fromDate, $toDate, @extra_params) or die "Unable to execute query " . $sth_stats->errstr;
 
     my @loopresult;
     my $grantotal = 0;
@@ -139,65 +140,39 @@ if ($do_it) {
             total => $grantotal,
         );
     } else{
-        binmode STDOUT, ':encoding(UTF-8)';
-        my $q_errors;
         my $format = 'csv';
         my $reportname = $input->param('basename');
         my $reportfilename = $reportname ? "$reportname.$format" : "reportresults.$format" ;
         #my $reportfilename = "$reportname.html" ;
         my $delimiter = C4::Context->preference('delimiter') || ',';
-        my ( $type, $content );
-        if ( $format eq 'csv' ) {
-            my $type = 'application/csv';
-            my $csv = Text::CSV::Encoded->new({ encoding_out => 'UTF-8', sep_char => $delimiter});
-            $csv or die "Text::CSV::Encoded->new({binary => 1}) FAILED: " . Text::CSV::Encoded->error_diag();
-            my @headers = ();
-            push @headers, "mfirstname",
-                                   "cardnumber",
-                                   "bfirstname",
-                                   "branchname",
-                                   "date",
-                                   "accounttype",
-                                   "amount",
-                                   "title",
-                                   "barcode",
-                                   "itype";
-            if ($csv->combine(@headers)) {
-                $content .= Encode::decode('UTF-8', $csv->string()) . "\n";
-            } else {
-                push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ;
-            }
+            my @rows;
             foreach my $row (@loopresult) {
-                               my @rowValues = ();
+                my @rowValues;
                 push @rowValues, $row->{mfirstname},
                         $row->{cardnumber},
-                                               $row->{bfirstname},
-                                               $row->{branchname},
-                                               $row->{date},
-                                               $row->{accounttype},
-                                               $row->{amount},
-                                               $row->{title},
-                                               $row->{barcode};
-                                               $row->{itype};
-                if ($csv->combine(@rowValues)) {
-                    $content .= $csv->string() . "\n";
-                } else {
-                    push @$q_errors, { combine => $csv->error_diag() } ;
+                        $row->{bfirstname},
+                        $row->{branchname},
+                        $row->{date},
+                        $row->{accounttype},
+                        $row->{amount},
+                        $row->{title},
+                        $row->{barcode},
+                        $row->{itype};
+                    push (@rows, \@rowValues) ;
                 }
-            }
-        }
+                my @total;
+                for (1..6){push(@total,"")};
+                push(@total, $grantotal);
         print $input->header(
-            -type => $type,
-            -attachment=> $reportfilename
-        );
-        print $content;
-
-        print $delimiter x 6;
-        print $grantotal."\n";
-        foreach my $err (@$q_errors) {
-            print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n";
-        }   # here we print all the non-fatal errors at the end.  Not super smooth, but better than nothing.
-        exit(1);
+            -type       => 'text/csv',
+            -encoding    => 'utf-8',
+            -attachment => $reportfilename,
+            -name       => $reportfilename
+         );
+        my $csvTemplate = C4::Templates::gettemplate('reports/csv/cash_register_stats.tt', 'intranet', $input);
+            $csvTemplate->param(sep => $delimiter, rows => \@rows, total => \@total );
+        print $csvTemplate->output;
+        exit;
     }
 
 }
@@ -206,7 +181,7 @@ $template->param(
     beginDate        => $fromDate,
     endDate          => $toDate,
     transaction_type => $transaction_type,
-    branchloop       => C4::Branch::GetBranchesLoop($manager_branchcode),
+    branchloop       => Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed,
     manualinv_types  => $manualinv_types,
     CGIsepChoice => GetDelimiterChoices,
 );