Bug 30943: Simplify background jobs code using helpers
[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
20 use Koha::Holds;
21 use Koha::Patrons;
22
23 use base 'Koha::BackgroundJob';
24
25 =head1 NAME
26
27 Koha::BackgroundJob::BatchCancelHold - Batch cancel holds
28
29 This is a subclass of Koha::BackgroundJob.
30
31 =head1 API
32
33 =head2 Class methods
34
35 =head3 job_type
36
37 Define the job type of this job: batch_hold_cancel
38
39 =cut
40
41 sub job_type {
42     return 'batch_hold_cancel';
43 }
44
45 =head3 process
46
47 Process the modification.
48
49 =cut
50
51 sub process {
52     my ( $self, $args ) = @_;
53
54     if ( $self->status eq 'cancelled' ) {
55         return;
56     }
57
58     $self->start;
59
60     my @hold_ids = @{ $args->{hold_ids} };
61
62     my $report = {
63         total_holds   => scalar @hold_ids,
64         total_success => 0,
65     };
66     my @messages;
67       HOLD_IDS: for my $hold_id ( sort { $a <=> $b } @hold_ids ) {
68         next unless $hold_id;
69
70         # Authorities
71         my ( $hold, $patron, $biblio );
72         $hold = Koha::Holds->find($hold_id);
73
74         my $error = eval {
75             $patron = $hold->patron;
76             $biblio = $hold->biblio;
77             $hold->cancel( { cancellation_reason => $args->{reason} } );
78         };
79
80         if ( $error and $error != $hold or $@ ) {
81             push @messages,
82               {
83                 type        => 'error',
84                 code        => 'hold_not_cancelled',
85                 patron_id   => defined $patron ? $patron->borrowernumber : '',
86                 biblio_id    => defined $biblio ? $biblio->biblionumber : '',
87                 hold_id      => $hold_id,
88                 error        => defined $hold
89                 ? ( $@ ? "$@" : 0 )
90                 : 'hold_not_found',
91               };
92         }
93         else {
94             push @messages,
95               {
96                 type      => 'success',
97                 code      => 'hold_cancelled',
98                 patron_id => $patron->borrowernumber,
99                 biblio_id    => $biblio->biblionumber,
100                 hold_id      => $hold_id,
101               };
102             $report->{total_success}++;
103         }
104         $self->step;
105     }
106
107     my $data = $self->decoded_data;
108     $data->{messages} = \@messages;
109     $data->{report} = $report;
110
111     $self->finish( $data );
112 }
113
114 =head3 enqueue
115
116 Enqueue the new job
117
118 =cut
119
120 sub enqueue {
121     my ( $self, $args ) = @_;
122
123     # TODO Raise exception instead
124     return unless exists $args->{hold_ids};
125
126     my @hold_ids = @{ $args->{hold_ids} };
127
128     $self->SUPER::enqueue(
129         {
130             job_size  => scalar @hold_ids,
131             job_args  => { hold_ids => \@hold_ids, reason => $args->{reason} },
132             job_queue => 'long_tasks',
133         }
134     );
135 }
136
137 =head3 additional_report
138
139 Pass the biblio's title and patron's name
140
141 =cut
142
143 sub additional_report {
144     my ( $self, $args ) = @_;
145
146     my $messages = $self->messages;
147     for my $m ( @$messages ) {
148         $m->{patron} = Koha::Patrons->find($m->{patron_id});
149         $m->{biblio} = Koha::Biblios->find($m->{biblio_id});
150     }
151     return { report_messages => $messages };
152 }
153
154 1;