Bug 32394: (follow-up) Add param for Koha::BackgroundJob::StageMARCForImport->enqueue
[koha-ffzg.git] / Koha / BackgroundJob / CreateEHoldingsFromBiblios.pm
1 package Koha::BackgroundJob::CreateEHoldingsFromBiblios;
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( decode_json encode_json );
20 use Try::Tiny;
21
22 use Koha::Biblios;
23 use Koha::DateUtils qw( dt_from_string );
24 use Koha::ERM::EHoldings::Titles;
25 use Koha::ERM::EHoldings::Resources;
26
27 use C4::Context;
28
29 use base 'Koha::BackgroundJob';
30
31 =head1 NAME
32
33 CreateEHoldingsFromBiblios - Create new eHoldings titles from 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 Define the job type of this job.
44
45 =cut
46
47 sub job_type {
48     return 'create_eholdings_from_biblios';
49 }
50
51 =head3 process
52
53 Process the import.
54
55 =cut
56
57 sub process {
58     my ( $self, $args ) = @_;
59
60     if ( $self->status eq 'cancelled' ) {
61         return;
62     }
63
64     my $job_progress = 0;
65     $self->started_on(dt_from_string)
66         ->progress($job_progress)
67         ->status('started')
68         ->store;
69
70     my @messages;
71     my @record_ids = @{ $args->{record_ids} };
72     my $package_id = $args->{package_id};
73
74     my $package = Koha::ERM::EHoldings::Packages->find($package_id);
75     unless ( $package ) {
76         push @messages, {
77             type => 'error',
78             code => 'package_do_not_exist',
79             package_id => $package_id,
80         };
81     }
82
83     my $report = {
84         total_records => scalar @record_ids,
85         total_success => 0,
86     };
87     my $fix_coverage = sub {
88         my $coverage = shift || q{};
89         my @coverages = split '-', $coverage;
90         return ($coverages[0], (@coverages > 1 ? $coverages[1] : q{}));
91     };
92
93     my %existing_biblio_ids = map {
94         my $resource = $_;
95         map { $_->biblio_id => $resource->resource_id } $resource->title
96     } $package->resources->as_list;
97
98     RECORD_IDS: for my $biblio_id ( sort { $a <=> $b } @record_ids ) {
99
100         last if $self->get_from_storage->status eq 'cancelled';
101
102         next unless $biblio_id;
103
104         try {
105             if ( grep { $_ eq $biblio_id } keys %existing_biblio_ids ) {
106                 push @messages,
107                   {
108                     type        => 'warning',
109                     code        => 'biblio_already_exists',
110                     biblio_id   => $biblio_id,
111                     resource_id => $existing_biblio_ids{$biblio_id},
112                   };
113                 return;
114             }
115             my $biblio = Koha::Biblios->find($biblio_id);
116             my $record = $biblio->metadata->record;
117             my $publication_title = $biblio->title;
118             my $print_identifier =
119                  $record->subfield( '020', 'a' )
120               || $record->subfield( '020', 'z' )
121               || $record->subfield( '022', 'a' )
122               || $record->subfield( '022', 'y' );
123             my $online_identifier = $print_identifier;
124             my ( $date_first_issue_online, $date_last_issue_online ) =
125               $fix_coverage->( $record->subfield( '866', 'a' ) );
126             my ( $num_first_vol_online, $num_last_vol_online ) =
127               fix_coverage->( $record->subfield( '863', 'a' ) );
128             my ( $num_first_issue_online, $num_last_issue_online ) =
129               ( '', '' );    # FIXME ?
130             my $title_url = $record->subfield( '856', 'u' );
131             my $first_author = $biblio->author;
132             my $embargo_info     = '';                                 # FIXME ?
133             my $coverage_depth   = $title_url ? 'fulltext' : 'print';
134             my $notes            = $record->subfield( '852', 'z' );
135             my $publisher_name   = $record->subfield( '260', 'b' );
136             my $publication_type = '';                                 # FIXME ?
137             my $date_monograph_published_print  = '';                  # FIXME ?
138             my $date_monograph_published_online = '';                  # FIXME ?
139             my $monograph_volume                = '';                  # FIXME ?
140             my $monograph_edition               = '';                  # FIXME ?
141             my $first_editor                    = '';                  # FIXME ?
142             my $parent_publication_title_id     = '';                  # FIXME ?
143             my $preceeding_publication_title_id = '';                  # FIXME ?
144             my $access_type                     = '';                  # FIXME ?
145
146             my $eholding_title = {
147                 biblio_id                       => $biblio_id,
148                 publication_title               => $publication_title,
149                 print_identifier                => $print_identifier,
150                 online_identifier               => $online_identifier,
151                 date_first_issue_online         => $date_first_issue_online,
152                 num_first_vol_online            => $num_first_vol_online,
153                 num_first_issue_online          => $num_first_issue_online,
154                 date_last_issue_online          => $date_last_issue_online,
155                 num_last_vol_online             => $num_last_vol_online,
156                 num_last_issue_online           => $num_last_issue_online,
157                 title_url                       => $title_url,
158                 first_author                    => $first_author,
159                 embargo_info                    => $embargo_info,
160                 coverage_depth                  => $coverage_depth,
161                 notes                           => $notes,
162                 publisher_name                  => $publisher_name,
163                 publication_type                => $publication_type,
164                 date_monograph_published_print  => $date_monograph_published_print,
165                 date_monograph_published_online => $date_monograph_published_online,
166                 monograph_volume                => $monograph_volume,
167                 monograph_edition               => $monograph_edition,
168                 first_editor                    => $first_editor,
169                 parent_publication_title_id     => $parent_publication_title_id,
170                 preceeding_publication_title_id => $preceeding_publication_title_id,
171                 access_type                     => $access_type,
172               };
173               $eholding_title = Koha::ERM::EHoldings::Title->new($eholding_title)->store;
174               Koha::ERM::EHoldings::Resource->new({ title_id => $eholding_title->title_id, package_id => $package_id })->store;
175
176             $report->{total_success}++;
177         } catch {
178             push @messages, {
179                 type => 'error',
180                 code => 'eholding_not_created',
181                 error => $_,
182             };
183         };
184         $self->progress( ++$job_progress )->store;
185     }
186
187     my $job_data = decode_json $self->data;
188     $job_data->{messages} = \@messages;
189     $job_data->{report} = $report;
190
191     $self->ended_on(dt_from_string)
192         ->data(encode_json $job_data);
193     $self->status('finished') if $self->status ne 'cancelled';
194     $self->store;
195 }
196
197 =head3 enqueue
198
199 Enqueue the new job
200
201 =cut
202
203 sub enqueue {
204     my ( $self, $args) = @_;
205
206     return unless exists $args->{package_id};
207     return unless exists $args->{record_ids};
208
209     $self->SUPER::enqueue({
210         job_size  => scalar @{$args->{record_ids}},
211         job_args  => $args,
212         job_queue => 'long_tasks',
213     });
214 }
215
216 =head3 additional_report
217
218 =cut
219
220 sub additional_report {
221     my ($self) = @_;
222
223     my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
224     return {};
225 }
226
227 1;