Bug 30876: (follow-up) Use the $biblio object even further
[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 = CGI->new;
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       flagsrequired   => { recalls => 'manage_recalls' },
32       debug           => 1,
33     }
34 );
35
36 my $op = $query->param('op') || 'list';
37 my $recall_id = $query->param('recall_id');
38 if ( $op eq 'cancel' ) {
39     my $recall = Koha::Recalls->find( $recall_id );
40     if ( $recall->in_transit ) {
41         C4::Items::ModItemTransfer( $recall->item->itemnumber, $recall->item->holdingbranch, $recall->item->homebranch, 'CancelRecall' );
42     }
43     $recall->set_cancelled;
44     $op = 'list';
45 }
46
47 if ( $op eq 'list' ) {
48     my @recalls = Koha::Recalls->search({ status => [ 'requested','overdue','in_transit' ] })->as_list;
49     my @pull_list;
50     my %seen_bib;
51     foreach my $recall ( @recalls ) {
52         if ( $seen_bib{$recall->biblio_id} ){
53             # we've already looked at the recalls on this biblio
54             next;
55         } else {
56             # this is an unseen biblio
57             $seen_bib{$recall->biblio_id}++;
58
59             # get recall data about this biblio
60             my $biblio = $recall->biblio;
61             my @this_bib_recalls = $biblio->recalls->search(
62                 { status   => [ 'requested', 'overdue', 'in_transit' ] },
63                 { order_by => { -asc => 'created_date' } }
64             )->as_list;
65             my $recalls_count = scalar @this_bib_recalls;
66             my @unique_patrons = do { my %seen; grep { !$seen{$_->patron_id}++ } @this_bib_recalls };
67             my $patrons_count = scalar @unique_patrons;
68             my $first_recall = $this_bib_recalls[0];
69
70             my $items_count = 0;
71             my @callnumbers;
72             my @copynumbers;
73             my @enumchrons;
74             my @itemtypes;
75             my @locations;
76             my @libraries;
77
78             my @items = $biblio->items->as_list;
79             foreach my $item ( @items ) {
80                 if ( $item->can_be_waiting_recall and !$item->checkout ) {
81                     # if item can be pulled to fulfill recall, collect item data
82                     $items_count++;
83                     push( @callnumbers, $item->itemcallnumber ) if ( $item->itemcallnumber );
84                     push( @copynumbers, $item->copynumber ) if ( $item->copynumber );
85                     push( @enumchrons, $item->enumchron ) if ( $item->enumchron );
86                     push( @itemtypes, $item->effective_itemtype ) if ( $item->effective_itemtype );
87                     push( @locations, $item->location ) if ( $item->location );
88                     push( @libraries, $item->holdingbranch ) if ( $item->holdingbranch );
89                 }
90             }
91
92             if ( $items_count > 0 ) {
93             # don't push data if there are no items available for this recall
94
95                 # get unique values
96                 my @unique_callnumbers = do { my %seen; grep { !$seen{$_}++ } @callnumbers };
97                 my @unique_copynumbers = do { my %seen; grep { !$seen{$_}++ } @copynumbers };
98                 my @unique_enumchrons = do { my %seen; grep { !$seen{$_}++ } @enumchrons };
99                 my @unique_itemtypes = do { my %seen; grep { !$seen{$_}++ } @itemtypes };
100                 my @unique_locations = do { my %seen; grep { !$seen{$_}++ } @locations };
101                 my @unique_libraries = do { my %seen; grep { !$seen{$_}++ } @libraries };
102
103                 push( @pull_list, {
104                     biblio => $biblio,
105                     items_count => $items_count,
106                     recalls_count => $recalls_count,
107                     patrons_count => $patrons_count,
108                     pull_count => $items_count <= $recalls_count ? $items_count : $recalls_count,
109                     first_recall => $first_recall,
110                     callnumbers => \@unique_callnumbers,
111                     copynumbers => \@unique_copynumbers,
112                     enumchrons => \@unique_enumchrons,
113                     itemtypes => \@unique_itemtypes,
114                     locations => \@unique_locations,
115                     libraries => \@unique_libraries,
116                 });
117             }
118         }
119     }
120     $template->param(
121         recalls => \@pull_list,
122     );
123 }
124
125 # Checking if there is a Fast Cataloging Framework
126 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
127
128 # writing the template
129 output_html_with_http_headers $query, $cookie, $template->output;