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