301a2eb1b896d494a833dfa9fcd241b618cdfdf1
[koha-ffzg.git] / Koha / BackgroundJob / BatchUpdateBiblio.pm
1 package Koha::BackgroundJob::BatchUpdateBiblio;
2
3 use Modern::Perl;
4 use JSON qw( encode_json decode_json );
5
6 use Koha::BackgroundJobs;
7 use Koha::DateUtils qw( dt_from_string );
8 use C4::Biblio;
9 use C4::MarcModificationTemplates;
10
11 use base 'Koha::BackgroundJob';
12
13 sub job_type {
14     return 'batch_biblio_record_modification';
15 }
16
17 sub process {
18     my ( $self, $args ) = @_;
19
20     my $job_type = $args->{job_type};
21
22     my $job = Koha::BackgroundJobs->find( $args->{job_id} );
23
24     if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
25         return;
26     }
27
28     # FIXME If the job has already been started, but started again (worker has been restart for instance)
29     # Then we will start from scratch and so double process the same records
30
31     my $job_progress = 0;
32     $job->started_on(dt_from_string)
33         ->progress($job_progress)
34         ->status('started')
35         ->store;
36
37     my $mmtid = $args->{mmtid};
38     my @record_ids = @{ $args->{record_ids} };
39
40     my $report = {
41         total_records => scalar @record_ids,
42         total_success => 0,
43     };
44     my @messages;
45     my $dbh = C4::Context->dbh;
46     $dbh->{RaiseError} = 1;
47     RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
48
49         last if $job->get_from_storage->status eq 'cancelled';
50
51         next unless $biblionumber;
52
53         # Modify the biblio
54         my $error = eval {
55             my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
56             C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
57             my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
58             C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
59         };
60         if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
61             push @messages, {
62                 type => 'error',
63                 code => 'biblio_not_modified',
64                 biblionumber => $biblionumber,
65                 error => ($@ ? $@ : $error),
66             };
67         } else {
68             push @messages, {
69                 type => 'success',
70                 code => 'biblio_modified',
71                 biblionumber => $biblionumber,
72             };
73             $report->{total_success}++;
74         }
75         $job->progress( ++$job_progress )->store;
76     }
77
78     my $job_data = decode_json $job->data;
79     $job_data->{messages} = \@messages;
80     $job_data->{report} = $report;
81
82     $job->ended_on(dt_from_string)
83         ->data(encode_json $job_data);
84     $job->status('finished') if $job->status ne 'cancelled';
85     $job->store;
86 }
87
88 sub enqueue {
89     my ( $self, $args) = @_;
90
91     # TODO Raise exception instead
92     return unless exists $args->{mmtid};
93     return unless exists $args->{record_ids};
94
95     my $mmtid = $args->{mmtid};
96     my @record_ids = @{ $args->{record_ids} };
97
98     $self->SUPER::enqueue({
99         job_size => scalar @record_ids,
100         job_args => {mmtid => $mmtid, record_ids => \@record_ids,}
101     });
102 }
103
104 1;