Bug 24665: Add support for filtering by cash register
[koha-ffzg.git] / reports / cash_register_stats.pl
index d2f8c0f..8e8f46c 100755 (executable)
@@ -2,17 +2,18 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation;
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 use C4::Auth;
@@ -30,14 +31,13 @@ use List::Util qw/any/;
 use Koha::Account::CreditTypes;
 use Koha::Account::DebitTypes;
 
-my $input            = new CGI;
+my $input            = CGI->new;
 my $dbh              = C4::Context->dbh;
 
 my ($template, $borrowernumber, $cookie) = get_template_and_user({
     template_name => "reports/cash_register_stats.tt",
     query => $input,
     type => "intranet",
-    authnotrequired => 0,
     flagsrequired => {reports => '*'},
     debug => 1,
 });
@@ -61,12 +61,13 @@ my @debit_types =
   Koha::Account::DebitTypes->search()->as_list;
 my @credit_types =
   Koha::Account::CreditTypes->search()->as_list;
+my $registerid;
 
 if ($do_it) {
 
-    $fromDate = output_pref({ dt => eval { dt_from_string($input->param("from")) } || dt_from_string,
+    $fromDate = output_pref({ dt => eval { dt_from_string(scalar $input->param("from")) } || dt_from_string,
             dateformat => 'sql', dateonly => 1 }); #for sql query
-    $toDate   = output_pref({ dt => eval { dt_from_string($input->param("to")) } || dt_from_string,
+    $toDate   = output_pref({ dt => eval { dt_from_string(scalar $input->param("to")) } || dt_from_string,
             dateformat => 'sql', dateonly => 1 }); #for sql query
 
     my $whereTType = q{};
@@ -94,6 +95,12 @@ if ($do_it) {
         push @extra_params, $manager_branchcode;
     }
 
+    my $whereRegister = q{};
+    $registerid = $input->param("registerid");
+    if ($registerid) {
+        $whereRegister = q{ AND al.register_id = ?};
+        push @extra_params, $registerid;
+    }
 
     my $query = "
     SELECT round(amount,2) AS amount, description,
@@ -110,6 +117,7 @@ if ($do_it) {
         WHERE CAST(al.date AS DATE) BETWEEN ? AND ?
         $whereTType
         $whereBranchCode
+        $whereRegister
         ORDER BY al.date
     ";
     my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query " . $dbh->errstr;
@@ -191,6 +199,7 @@ $template->param(
     branchloop       => Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed,
     debit_types      => \@debit_types,
     credit_types     => \@credit_types,
+    registerid       => $registerid,
     CGIsepChoice => GetDelimiterChoices,
 );