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