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