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