Line 0
Link Here
|
0 |
- |
1 |
package Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; |
|
|
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 JSON qw( encode_json decode_json ); |
21 |
use Try::Tiny; |
22 |
|
23 |
use Koha::BackgroundJobs; |
24 |
use Koha::Exceptions; |
25 |
|
26 |
use C4::HoldsQueue |
27 |
qw(load_branches_to_pull_from TransportCostMatrix update_queue_for_biblio); |
28 |
|
29 |
use base 'Koha::BackgroundJob'; |
30 |
|
31 |
=head1 NAME |
32 |
|
33 |
Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue - Update the holds queue |
34 |
for a specified list of biblios. |
35 |
|
36 |
This is a subclass of Koha::BackgroundJob. |
37 |
|
38 |
=head1 API |
39 |
|
40 |
=head2 Class methods |
41 |
|
42 |
=head3 job_type |
43 |
|
44 |
Returns a string representing the job type. In this case I<update_holds_queue_for_biblios>. |
45 |
|
46 |
=cut |
47 |
|
48 |
sub job_type { |
49 |
return 'update_holds_queue_for_biblios'; |
50 |
} |
51 |
|
52 |
=head3 process |
53 |
|
54 |
Perform the expected action. |
55 |
|
56 |
=cut |
57 |
|
58 |
sub process { |
59 |
my ( $self, $args ) = @_; |
60 |
|
61 |
my $job = Koha::BackgroundJobs->find( $args->{job_id} ); |
62 |
|
63 |
if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { |
64 |
return; |
65 |
} |
66 |
|
67 |
my $job_progress = 0; |
68 |
|
69 |
$job->set( |
70 |
{ |
71 |
started_on => \'NOW()', |
72 |
progress => $job_progress, |
73 |
status => 'started', |
74 |
} |
75 |
)->store; |
76 |
|
77 |
my @biblio_ids = @{ $args->{biblio_id} }; |
78 |
|
79 |
my $report = { |
80 |
total_biblios => scalar @biblio_ids, |
81 |
total_success => 0, |
82 |
}; |
83 |
|
84 |
my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix"); |
85 |
my $transport_cost_matrix = $use_transport_cost_matrix ? TransportCostMatrix() : undef; |
86 |
my $branches_to_use = load_branches_to_pull_from($use_transport_cost_matrix); |
87 |
|
88 |
my @messages; |
89 |
|
90 |
foreach my $biblio_id (@biblio_ids) { |
91 |
try { |
92 |
my $result = update_queue_for_biblio( |
93 |
{ |
94 |
biblio_id => $biblio_id, |
95 |
branches_to_use => $branches_to_use, |
96 |
delete => 1, |
97 |
transport_cost_matrix => $transport_cost_matrix |
98 |
} |
99 |
); |
100 |
push @messages, |
101 |
{ |
102 |
type => 'success', |
103 |
code => 'holds_queue_updated', |
104 |
biblio_id => $biblio_id, |
105 |
availabe_items => $result->{available_items}, |
106 |
mapped_items => $result->{mapped_items}, |
107 |
requests => $result->{requests}, |
108 |
}; |
109 |
$report->{total_success}++; |
110 |
} |
111 |
catch { |
112 |
push @messages, { |
113 |
type => 'error', |
114 |
code => 'holds_queue_update_error', |
115 |
biblio_id => $biblio_id, |
116 |
error => "$_", |
117 |
}; |
118 |
}; |
119 |
|
120 |
$job->progress( $job_progress++ )->store; |
121 |
} |
122 |
|
123 |
my $job_data = decode_json $job->data; |
124 |
$job_data->{messages} = \@messages; |
125 |
$job_data->{report} = $report; |
126 |
|
127 |
$job->set( |
128 |
{ |
129 |
ended_on => \'NOW()', |
130 |
data => encode_json $job_data, |
131 |
} |
132 |
); |
133 |
$job->status('finished') |
134 |
unless $job->status ne 'cancelled'; |
135 |
|
136 |
$job->store; |
137 |
} |
138 |
|
139 |
=head3 enqueue |
140 |
|
141 |
Enqueue the new job |
142 |
|
143 |
=cut |
144 |
|
145 |
sub enqueue { |
146 |
my ( $self, $args ) = @_; |
147 |
|
148 |
Koha::Exceptions::MissingParameter->throw( |
149 |
"Missing biblio_ids parameter is mandatory") |
150 |
unless exists $args->{biblio_ids}; |
151 |
|
152 |
my @biblio_ids = @{ $args->{biblio_ids} }; |
153 |
|
154 |
$self->SUPER::enqueue( |
155 |
{ |
156 |
job_size => scalar @biblio_ids, |
157 |
job_args => |
158 |
{ biblio_ids => \@biblio_ids, reason => $args->{reason} } |
159 |
} |
160 |
); |
161 |
} |
162 |
|
163 |
=head3 additional_report |
164 |
|
165 |
Pass the biblio's title and patron's name |
166 |
|
167 |
=cut |
168 |
|
169 |
sub additional_report { |
170 |
my ( $self, $args ) = @_; |
171 |
|
172 |
my $job = Koha::BackgroundJobs->find( $args->{job_id} ); |
173 |
my $messages = $job->messages; |
174 |
for my $m (@$messages) { |
175 |
$m->{biblio} = Koha::Biblios->find( $m->{biblio_id} ); |
176 |
} |
177 |
return { report_messages => $messages }; |
178 |
} |
179 |
|
180 |
1; |