1 package Koha::BackgroundJob::CreateEHoldingsFromBiblios;
3 # This file is part of Koha.
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.
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.
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>.
19 use JSON qw( decode_json encode_json );
23 use Koha::DateUtils qw( dt_from_string );
24 use Koha::ERM::EHoldings::Titles;
25 use Koha::ERM::EHoldings::Resources;
29 use base 'Koha::BackgroundJob';
33 CreateEHoldingsFromBiblios - Create new eHoldings titles from biblios
35 This is a subclass of Koha::BackgroundJob.
43 Define the job type of this job.
48 return 'create_eholdings_from_biblios';
58 my ( $self, $args ) = @_;
60 if ( $self->status eq 'cancelled' ) {
65 $self->started_on(dt_from_string)
66 ->progress($job_progress)
71 my @record_ids = @{ $args->{record_ids} };
72 my $package_id = $args->{package_id};
74 my $package = Koha::ERM::EHoldings::Packages->find($package_id);
78 code => 'package_do_not_exist',
79 package_id => $package_id,
84 total_records => scalar @record_ids,
87 my $fix_coverage = sub {
88 my $coverage = shift || q{};
89 my @coverages = split '-', $coverage;
90 return ($coverages[0], (@coverages > 1 ? $coverages[1] : q{}));
93 my %existing_biblio_ids = map {
95 map { $_->biblio_id => $resource->resource_id } $resource->title
96 } $package->resources->as_list;
98 RECORD_IDS: for my $biblio_id ( sort { $a <=> $b } @record_ids ) {
100 last if $self->get_from_storage->status eq 'cancelled';
102 next unless $biblio_id;
105 if ( grep { $_ eq $biblio_id } keys %existing_biblio_ids ) {
109 code => 'biblio_already_exists',
110 biblio_id => $biblio_id,
111 resource_id => $existing_biblio_ids{$biblio_id},
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 ?
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,
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,
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;
176 $report->{total_success}++;
180 code => 'eholding_not_created',
184 $self->progress( ++$job_progress )->store;
187 my $job_data = decode_json $self->data;
188 $job_data->{messages} = \@messages;
189 $job_data->{report} = $report;
191 $self->ended_on(dt_from_string)
192 ->data(encode_json $job_data);
193 $self->status('finished') if $self->status ne 'cancelled';
204 my ( $self, $args) = @_;
206 return unless exists $args->{package_id};
207 return unless exists $args->{record_ids};
209 $self->SUPER::enqueue({
210 job_size => scalar @{$args->{record_ids}},
212 job_queue => 'long_tasks',
216 =head3 additional_report
220 sub additional_report {
223 my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;