Bug 32782: Koha to KBart mapping for UNIMARC
[koha.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 Try::Tiny;
21
22 use Koha::Biblios;
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     $self->start;
63
64     my @biblio_ids = @{ $args->{biblio_ids} };
65
66     my $report = {
67         total_biblios => scalar @biblio_ids,
68         total_success => 0,
69     };
70
71     my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
72     my $transport_cost_matrix = $use_transport_cost_matrix ? TransportCostMatrix() : undef;
73     my $branches_to_use = load_branches_to_pull_from($use_transport_cost_matrix);
74
75     my @messages;
76
77     foreach my $biblio_id (@biblio_ids) {
78         try {
79
80             $schema->storage->txn_begin;
81
82             my $result = update_queue_for_biblio(
83                 {
84                     biblio_id             => $biblio_id,
85                     branches_to_use       => $branches_to_use,
86                     delete                => 1,
87                     transport_cost_matrix => $transport_cost_matrix
88                 }
89             );
90             push @messages,
91               {
92                 type           => 'success',
93                 code           => 'holds_queue_updated',
94                 biblio_id      => $biblio_id,
95               };
96             $report->{total_success}++;
97
98             $schema->storage->txn_commit;
99         }
100         catch {
101
102             push @messages,
103               {
104                 type      => 'error',
105                 code      => 'holds_queue_update_error',
106                 biblio_id => $biblio_id,
107                 error     => "$_",
108               };
109
110             $schema->storage->txn_rollback;
111         };
112
113         $self->step;
114     }
115
116     my $data = $self->decoded_data;
117     $data->{messages} = \@messages;
118     $data->{report} = $report;
119
120     $self->finish( $data );
121 }
122
123 =head3 enqueue
124
125 Enqueue the new job
126
127 =cut
128
129 sub enqueue {
130     my ( $self, $args ) = @_;
131
132     Koha::Exceptions::MissingParameter->throw(
133         "Missing biblio_ids parameter is mandatory")
134       unless exists $args->{biblio_ids};
135
136     my @biblio_ids = @{ $args->{biblio_ids} };
137
138     $self->SUPER::enqueue(
139         {
140             job_size => scalar @biblio_ids,
141             job_args => { biblio_ids => \@biblio_ids }
142         }
143     );
144 }
145
146 =head3 additional_report
147
148 Pass the biblio's title and patron's name
149
150 =cut
151
152 sub additional_report {
153     my ( $self, $args ) = @_;
154
155     my $messages = $self->messages;
156     for my $m (@$messages) {
157         $m->{biblio} = Koha::Biblios->find( $m->{biblio_id} );
158     }
159     return { report_messages => $messages };
160 }
161
162 1;