Bug 9457: [ENH] Ordering branches should be case independent (2)
[koha.git] / reports / catalogue_out.pl
index 0821e2f..8a22a18 100755 (executable)
 # 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., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# 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.
 
 use strict;
+use warnings;
 use CGI;
+
 use C4::Auth;
 use C4::Context;
 use C4::Debug;
 use C4::Branch; # GetBranches
 use C4::Output;
-use C4::Koha;
+use C4::Koha;   # GetItemTypes
+use C4::Reports;    # GetDelimiterChoices
 use C4::Circulation;
-use Date::Manip;
+# use Date::Manip;  # TODO: add not borrowed since date X criteria
 use Data::Dumper;
 
 =head1 catalogue_out
@@ -36,22 +39,24 @@ Report that shows unborrowed items.
 =cut
 
 my $input = new CGI;
-my $fullreportname = "reports/catalogue_out.tmpl";
 my $do_it    = $input->param('do_it');
 my $limit    = $input->param("Limit");
 my $column   = $input->param("Criteria");
 my @filters  = $input->param("Filter");
 my $output   = $input->param("output");
 my $basename = $input->param("basename") || 'catalogue_out';
-my $mime     = $input->param("MIME");
-my ($template, $borrowernumber, $cookie)
-    = get_template_and_user({template_name => $fullreportname,
-        query => $input,
-        type => "intranet",
-        authnotrequired => 0,
-        flagsrequired => {reports => 1},
-        debug => 1,
-    });
+my ($template, $borrowernumber, $cookie) = get_template_and_user({
+    template_name => "reports/catalogue_out.tmpl",
+    query => $input,
+    type => "intranet",
+    authnotrequired => 0,
+    flagsrequired => {reports => '*'},
+    debug => 1,
+});
+
+our $sep     = $input->param("sep");
+$sep = "\t" if ((! defined $sep) or $sep eq 'tabulation');
+
 $template->param(do_it => $do_it);
 if ($do_it) {
     my $results = calculate($limit, $column, \@filters);
@@ -68,7 +73,6 @@ if ($do_it) {
             -filename  =>"$basename.csv" );
         my $cols  = @$results[0]->{loopcol};
         my $lines = @$results[0]->{looprow};
-        my $sep = $input->param("sep") || C4::Context->preference("delimiter");
 # header
         print "num /". @$results[0]->{column} .$sep;
 # Other header
@@ -92,54 +96,28 @@ if ($do_it) {
         }
         print $sep.@$results[0]->{total};
     }
-       exit(1); # in either case, exit after do_it
+    exit; # in either case, exit after do_it
 }
 
 # Displaying choices (i.e., not do_it)
-my $dbh = C4::Context->dbh;
 my @values;
 my %select;
 
-my @mime = (C4::Context->preference("MIME"));
-my $CGIextChoice = CGI::scrolling_list(
-               -name     => 'MIME',
-               -id       => 'MIME',
-               -values   => \@mime,
-               -size     => 1,
-               -multiple => 0 );
-
-my @dels = (C4::Context->preference("delimiter"));
-my $CGIsepChoice = CGI::scrolling_list(
-               -name     => 'sep',
-               -id       => 'sep',
-               -values   => \@dels,
-               -size     => 1,
-               -multiple => 0 );
-
+my @mime  = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
 my $itemtypes = GetItemTypes;
 my @itemtypeloop;
-foreach (keys %$itemtypes) {
+foreach (sort {$itemtypes->{$a}->{description} cmp $itemtypes->{$b}->{description}} keys %$itemtypes) {
        push @itemtypeloop, {
                value => $_,
-#              selected => ($_ eq $itemtype) ? 1 : 0,
                description => $itemtypes->{$_}->{'description'},
    };
 }
-my $branches = GetBranches;
-my @branchloop;
-foreach (keys %$branches) {
-       push @branchloop, {
-               value => $_,
-#              selected => ($_ eq $branch) ? 1 : 0,
-               branchname => $branches->{$_}->{'branchname'},
-       };
-}
 
 $template->param(
-       CGIextChoice => $CGIextChoice,
-       CGIsepChoice => $CGIsepChoice,
-       itemtypeloop =>\@itemtypeloop,
-       branchloop   =>\@branchloop,
+       CGIextChoice => \@mime,
+       CGIsepChoice => GetDelimiterChoices,
+       itemtypeloop => \@itemtypeloop,
+       branchloop   => GetBranchesLoop($input->param("branch") || C4::Context->userenv->{branch}),
 );
 output_html_with_http_headers $input, $cookie, $template->output;
 
@@ -197,29 +175,30 @@ sub calculate {
 # preparing calculation
        my @exe_args = ();
     my $query = "
-        SELECT items.barcode    as barcode,
-               items.homebranch as branch,
+        SELECT items.barcode        as barcode,
+               items.homebranch     as branch,
                items.itemcallnumber as itemcallnumber,
-               biblio.title     as title,
+               biblio.title         as title,
                biblio.biblionumber  as biblionumber,
-               biblio.author    as author";
+               biblio.author        as author";
        ($column) and $query .= ",\n$column as col ";
        $query .= "
         FROM items
-        LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
-        LEFT JOIN biblio      ON biblio.biblionumber   = items.biblionumber
-        LEFT JOIN old_issues  ON old_issues.itemnumber = items.itemnumber
-        WHERE old_issues.itemnumber IS NULL
+        LEFT JOIN biblio      USING (biblionumber)
+        LEFT JOIN     issues  USING (itemnumber)
+        LEFT JOIN old_issues  USING (itemnumber)
+          WHERE       issues.itemnumber IS NULL
+           AND    old_issues.itemnumber IS NULL
        ";
        if ($filters->[0]) {
        $filters->[0]=~ s/\*/%/g;
                push @exe_args, $filters->[0]; 
-       $query .= " AND items.homebranch     LIKE ?";
+       $query .= " AND items.homebranch LIKE ?";
        }
        if ($filters->[1]) {
        $filters->[1]=~ s/\*/%/g;
                push @exe_args, $filters->[1]; 
-       $query .= " AND biblioitems.itemtype LIKE ?";
+       $query .= " AND items.itype      LIKE ?";
        }
        if ($column) {
                $query .= " AND $column = ? GROUP BY items.itemnumber, $column ";       # placeholder handled below