Bug 32351: Fix Members.t
[koha.git] / 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::DateUtils qw( dt_from_string );
21 use Koha::Holds;
22 use Koha::Patrons;
23 use Koha::Holds;
24
25 use base 'Koha::BackgroundJob';
26
27 =head1 NAME
28
29 Koha::BackgroundJob::BatchCancelHold - Batch cancel holds
30
31 This is a subclass of Koha::BackgroundJob.
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 job_type
38
39 Define the job type of this job: batch_hold_cancel
40
41 =cut
42
43 sub job_type {
44     return 'batch_hold_cancel';
45 }
46
47 =head3 process
48
49 Process the modification.
50
51 =cut
52
53 sub process {
54     my ( $self, $args ) = @_;
55
56     if ( $self->status eq 'cancelled' ) {
57         return;
58     }
59
60     my $job_progress = 0;
61     $self->started_on(dt_from_string)->progress($job_progress)
62       ->status('started')->store;
63
64     my @hold_ids = @{ $args->{hold_ids} };
65
66     my $report = {
67         total_holds   => scalar @hold_ids,
68         total_success => 0,
69     };
70     my @messages;
71       HOLD_IDS: for my $hold_id ( sort { $a <=> $b } @hold_ids ) {
72         next unless $hold_id;
73
74         # Authorities
75         my ( $hold, $patron, $biblio );
76         $hold = Koha::Holds->find($hold_id);
77
78         my $error = eval {
79             $patron = $hold->patron;
80             $biblio = $hold->biblio;
81             $hold->cancel( { cancellation_reason => $args->{reason} } );
82         };
83
84         if ( $error and $error != $hold or $@ ) {
85             push @messages,
86               {
87                 type        => 'error',
88                 code        => 'hold_not_cancelled',
89                 patron_id   => defined $patron ? $patron->borrowernumber : '',
90                 biblio_id    => defined $biblio ? $biblio->biblionumber : '',
91                 hold_id      => $hold_id,
92                 error        => defined $hold
93                 ? ( $@ ? "$@" : 0 )
94                 : 'hold_not_found',
95               };
96         }
97         else {
98             push @messages,
99               {
100                 type      => 'success',
101                 code      => 'hold_cancelled',
102                 patron_id => $patron->borrowernumber,
103                 biblio_id    => $biblio->biblionumber,
104                 hold_id      => $hold_id,
105               };
106             $report->{total_success}++;
107         }
108         $self->progress( ++$job_progress )->store;
109     }
110
111     my $json = $self->json;
112     my $job_data = $json->decode($self->data);
113     $job_data->{messages} = \@messages;
114     $job_data->{report}   = $report;
115
116     $self->ended_on(dt_from_string)->data($json->encode($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             queue    => 'long_tasks',
141         }
142     );
143 }
144
145 =head3 additional_report
146
147 Pass the biblio's title and patron's name
148
149 =cut
150
151 sub additional_report {
152     my ( $self, $args ) = @_;
153
154     my $messages = $self->messages;
155     for my $m ( @$messages ) {
156         $m->{patron} = Koha::Patrons->find($m->{patron_id});
157         $m->{biblio} = Koha::Biblios->find($m->{biblio_id});
158     }
159     return { report_messages => $messages };
160 }
161
162 1;