Bug 19532: Recalls on intranet
[koha.git] / recalls / recalls_to_pull.pl
1 #!/usr/bin/perl
2
3 # Copyright 2020 Aleisha Amohia <aleisha@catalyst.net.nz>
4 #
5 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20 use CGI qw ( -utf8 );
21 use C4::Auth qw( get_template_and_user );
22 use C4::Output qw( output_html_with_http_headers );
23 use Koha::BiblioFrameworks;
24
25 my $query = new CGI;
26 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
27     {
28       template_name   => "recalls/recalls_to_pull.tt",
29       query           => $query,
30       type            => "intranet",
31       authnotrequired => 0,
32       flagsrequired   => { recalls => 'manage_recalls' },
33       debug           => 1,
34     }
35 );
36
37 my $op = $query->param('op') || 'list';
38 my $recall_id = $query->param('recall_id');
39 if ( $op eq 'cancel' ) {
40     my $recall = Koha::Recalls->find( $recall_id );
41     if ( $recall->in_transit ) {
42         C4::Items::ModItemTransfer( $recall->item->itemnumber, $recall->item->holdingbranch, $recall->item->homebranch, 'CancelRecall' );
43     }
44     $recall->set_cancelled;
45     $op = 'list';
46 }
47
48 if ( $op eq 'list' ) {
49     my @recalls = Koha::Recalls->search({ status => [ 'R','O','T' ] });
50     my @pull_list;
51     my %seen_bib;
52     foreach my $recall ( @recalls ) {
53         if ( $seen_bib{$recall->biblionumber} ){
54             # we've already looked at the recalls on this biblio
55             next;
56         } else {
57             # this is an unseen biblio
58             $seen_bib{$recall->biblionumber}++;
59
60             # get recall data about this biblio
61             my @this_bib_recalls = Koha::Recalls->search({ biblionumber => $recall->biblionumber, status => [ 'R','O','T' ] }, { order_by => { -asc => 'recalldate' } });
62             my $recalls_count = scalar @this_bib_recalls;
63             my @unique_patrons = do { my %seen; grep { !$seen{$_->borrowernumber}++ } @this_bib_recalls };
64             my $patrons_count = scalar @unique_patrons;
65             my $first_recall = $this_bib_recalls[0];
66
67             my $items_count = 0;
68             my @callnumbers;
69             my @copynumbers;
70             my @enumchrons;
71             my @itemtypes;
72             my @locations;
73             my @libraries;
74
75             my @items = Koha::Items->search({ biblionumber => $recall->biblionumber });
76             foreach my $item ( @items ) {
77                 if ( $item->can_be_waiting_recall and !$item->checkout ) {
78                     # if item can be pulled to fulfill recall, collect item data
79                     $items_count++;
80                     push( @callnumbers, $item->itemcallnumber ) if ( $item->itemcallnumber );
81                     push( @copynumbers, $item->copynumber ) if ( $item->copynumber );
82                     push( @enumchrons, $item->enumchron ) if ( $item->enumchron );
83                     push( @itemtypes, $item->effective_itemtype ) if ( $item->effective_itemtype );
84                     push( @locations, $item->location ) if ( $item->location );
85                     push( @libraries, $item->holdingbranch ) if ( $item->holdingbranch );
86                 }
87             }
88
89             if ( $items_count > 0 ) {
90             # don't push data if there are no items available for this recall
91
92                 # get unique values
93                 my @unique_callnumbers = do { my %seen; grep { !$seen{$_}++ } @callnumbers };
94                 my @unique_copynumbers = do { my %seen; grep { !$seen{$_}++ } @copynumbers };
95                 my @unique_enumchrons = do { my %seen; grep { !$seen{$_}++ } @enumchrons };
96                 my @unique_itemtypes = do { my %seen; grep { !$seen{$_}++ } @itemtypes };
97                 my @unique_locations = do { my %seen; grep { !$seen{$_}++ } @locations };
98                 my @unique_libraries = do { my %seen; grep { !$seen{$_}++ } @libraries };
99
100                 push( @pull_list, {
101                     biblio => $recall->biblio,
102                     items_count => $items_count,
103                     recalls_count => $recalls_count,
104                     patrons_count => $patrons_count,
105                     pull_count => $items_count <= $recalls_count ? $items_count : $recalls_count,
106                     first_recall => $first_recall,
107                     callnumbers => \@unique_callnumbers,
108                     copynumbers => \@unique_copynumbers,
109                     enumchrons => \@unique_enumchrons,
110                     itemtypes => \@unique_itemtypes,
111                     locations => \@unique_locations,
112                     libraries => \@unique_libraries,
113                 });
114             }
115         }
116     }
117     $template->param(
118         recalls => \@pull_list,
119     );
120 }
121
122 # Checking if there is a Fast Cataloging Framework
123 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
124
125 # writing the template
126 output_html_with_http_headers $query, $cookie, $template->output;