Bug 30181: (follow-up) Remove redundant queries and parameters
[srvgit] / Koha / BackgroundJob / BatchCancelHold.pm
1 package Koha::BackgroundJob::BatchCancelHold;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use JSON qw( encode_json decode_json );
20
21 use Koha::DateUtils qw( dt_from_string );
22 use Koha::Holds;
23 use Koha::Patrons;
24 use Koha::Holds;
25
26 use base 'Koha::BackgroundJob';
27
28 =head1 NAME
29
30 Koha::BackgroundJob::BatchCancelHold - Batch cancel holds
31
32 This is a subclass of Koha::BackgroundJob.
33
34 =head1 API
35
36 =head2 Class methods
37
38 =head3 job_type
39
40 Define the job type of this job: batch_hold_cancel
41
42 =cut
43
44 sub job_type {
45     return 'batch_hold_cancel';
46 }
47
48 =head3 process
49
50 Process the modification.
51
52 =cut
53
54 sub process {
55     my ( $self, $args ) = @_;
56
57     if ( $self->status eq 'cancelled' ) {
58         return;
59     }
60
61     my $job_progress = 0;
62     $self->started_on(dt_from_string)->progress($job_progress)
63       ->status('started')->store;
64
65     my @hold_ids = @{ $args->{hold_ids} };
66
67     my $report = {
68         total_holds   => scalar @hold_ids,
69         total_success => 0,
70     };
71     my @messages;
72       HOLD_IDS: for my $hold_id ( sort { $a <=> $b } @hold_ids ) {
73         next unless $hold_id;
74
75         # Authorities
76         my ( $hold, $patron, $biblio );
77         $hold = Koha::Holds->find($hold_id);
78
79         my $error = eval {
80             $patron = $hold->patron;
81             $biblio = $hold->biblio;
82             $hold->cancel( { cancellation_reason => $args->{reason} } );
83         };
84
85         if ( $error and $error != $hold or $@ ) {
86             push @messages,
87               {
88                 type        => 'error',
89                 code        => 'hold_not_cancelled',
90                 patron_id   => defined $patron ? $patron->borrowernumber : '',
91                 biblio_id    => defined $biblio ? $biblio->biblionumber : '',
92                 hold_id      => $hold_id,
93                 error        => defined $hold
94                 ? ( $@ ? "$@" : 0 )
95                 : 'hold_not_found',
96               };
97         }
98         else {
99             push @messages,
100               {
101                 type      => 'success',
102                 code      => 'hold_cancelled',
103                 patron_id => $patron->borrowernumber,
104                 biblio_id    => $biblio->biblionumber,
105                 hold_id      => $hold_id,
106               };
107             $report->{total_success}++;
108         }
109         $self->progress( ++$job_progress )->store;
110     }
111
112     my $job_data = decode_json $self->data;
113     $job_data->{messages} = \@messages;
114     $job_data->{report}   = $report;
115
116     $self->ended_on(dt_from_string)->data( encode_json $job_data);
117     $self->status('finished') if $self->status ne 'cancelled';
118     $self->store;
119
120 }
121
122 =head3 enqueue
123
124 Enqueue the new job
125
126 =cut
127
128 sub enqueue {
129     my ( $self, $args ) = @_;
130
131     # TODO Raise exception instead
132     return unless exists $args->{hold_ids};
133
134     my @hold_ids = @{ $args->{hold_ids} };
135
136     $self->SUPER::enqueue(
137         {
138             job_size => scalar @hold_ids,
139             job_args => { hold_ids => \@hold_ids, reason => $args->{reason} }
140         }
141     );
142 }
143
144 =head3 additional_report
145
146 Pass the biblio's title and patron's name
147
148 =cut
149
150 sub additional_report {
151     my ( $self, $args ) = @_;
152
153     my $messages = $self->messages;
154     for my $m ( @$messages ) {
155         $m->{patron} = Koha::Patrons->find($m->{patron_id});
156         $m->{biblio} = Koha::Biblios->find($m->{biblio_id});
157     }
158     return { report_messages => $messages };
159 }
160
161 1;