@@ -, +, @@ --> There is no message for the missing branches --> The importation failed --> There is a warning message showing the missing branches --> There is a warning message showing the titles of the wrong items --> The records are imported --> The items with wrong branches are ignored --- C4/ImportBatch.pm | 74 +++++++++++++++++++ .../en/modules/tools/manage-marc-import.tt | 11 +++ tools/manage-marc-import.pl | 22 ++++-- 3 files changed, 101 insertions(+), 6 deletions(-) --- a/C4/ImportBatch.pm +++ a/C4/ImportBatch.pm @@ -41,6 +41,7 @@ use Koha::SearchEngine::Indexer; use Koha::Plugins::Handler; use Koha::Logger; use List::MoreUtils qw( uniq any ); +use Array::Utils qw( array_minus ); our (@ISA, @EXPORT_OK); BEGIN { @@ -72,6 +73,7 @@ BEGIN { GetImportRecordsRange GetItemNumbersFromImportBatch GetImportItemsBranches + GetBadBranchesImportItems GetImportBatchStatus SetImportBatchStatus @@ -1248,6 +1250,78 @@ sub GetImportItemsBranches { return uniq @branch; } +=head2 GetBadBranchesImportItems + + my @results = GetBadBranchesImportItems($batch_id); + +Returns an array of bad branches imported items. + +=cut + +sub GetBadBranchesImportItems { + my ($batch_id) = @_; + + my $dbh = C4::Context->dbh; + my $query = "SELECT import_items_id, import_record_id, import_items.marcxml, encoding + FROM import_items + JOIN import_records USING (import_record_id) + WHERE import_batch_id = ? + ORDER BY import_items_id"; + my @params; + push( @params, $batch_id ); + my $sth = $dbh->prepare($query); + $sth->execute(@params); + my $results = $sth->fetchall_arrayref( {} ); + my @items; + + my @import_items_branches = GetImportItemsBranches($batch_id); + my @branches = map { $_->branchcode } Koha::Libraries->search->as_list; + my @missing_branches = array_minus( @import_items_branches, @branches ); + my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField('items.homebranch'); + my ( $holdingbranchfield, $holdingbranchsubfield ) = GetMarcFromKohaField('items.holdingbranch'); + + foreach my $row (@$results) { + my $item_marc = + MARC::Record->new_from_xml( StripNonXmlChars( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} ); + my $title = GetTitleImportRecord( $row->{'import_record_id'} ); + foreach my $branch (@missing_branches) { + if ( $item_marc->subfield( $homebranchfield, $homebranchsubfield ) eq $branch ) { + push @items, { title => $title, branch => $branch }; + } + if ( $item_marc->subfield( $holdingbranchfield, $holdingbranchsubfield ) eq $branch ) { + push @items, { title => $title, branch => $branch }; + } + } + } + $sth->finish(); + return uniq @items; +} + +=head2 GetTitleImportRecord + + my $status = GetTitleImportRecord($import_record_id); + +=cut + +sub GetTitleImportRecord { + my ($import_record_id) = @_; + + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare( + "SELECT import_record_id, import_records.marcxml, encoding + FROM import_records WHERE import_record_id = ?" + ); + $sth->execute($import_record_id); + my $record = $sth->fetchall_arrayref( {} ); + my $marc_record = MARC::Record->new_from_xml( + StripNonXmlChars( $record->[0]->{'marcxml'} ), 'UTF-8', + $record->[0]->{'encoding'} + ); + $sth->finish(); + my $title = $marc_record->field('245')->subfield('a'); + return $title; +} + =head2 GetBestRecordMatch my $record_id = GetBestRecordMatch($import_record_id); --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt @@ -313,6 +313,17 @@ [% END %] + [% IF ( bad_titles ) %] +
+ The problems come from these items: + + [% FOREACH title IN titles %] + [% title | html %] + | + [% END %] + +
+ [% END %] [% END # /IF can_commit %] [% IF ( can_revert ) %] --- a/tools/manage-marc-import.pl +++ a/tools/manage-marc-import.pl @@ -30,13 +30,14 @@ use C4::Context; use C4::Koha; use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); -use C4::ImportBatch qw( CleanBatch DeleteBatch GetImportBatch GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction BatchFindDuplicates SetImportBatchMatcher GetItemNumbersFromImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches BatchCommitRecords BatchRevertRecords GetImportItemsBranches ); +use C4::ImportBatch qw( GetBadBranchesImportItems CleanBatch DeleteBatch GetImportBatch GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction BatchFindDuplicates SetImportBatchMatcher GetItemNumbersFromImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches BatchCommitRecords BatchRevertRecords GetImportItemsBranches ); use C4::Matcher; use C4::Labels::Batch; use Koha::BiblioFrameworks; use Koha::BackgroundJob::MARCImportCommitBatch; use Koha::BackgroundJob::MARCImportRevertBatch; use Array::Utils qw( array_minus ); +use List::MoreUtils qw( uniq any ); use Koha::Logger; @@ -86,14 +87,23 @@ if ($op eq "") { if ($import_batch_id eq '') { import_batches_list($template, $offset, $results_per_page); } else { - my @import_items_branches = GetImportItemsBranches($import_batch_id); - my @branches = map { $_->branchcode } Koha::Libraries->search->as_list; - my @missing_branches = array_minus( @import_items_branches, @branches ); + my @import_items = GetBadBranchesImportItems($import_batch_id); + my @bad_item_titles; + my @missing_branches; + foreach my $item (@import_items) { + push @bad_item_titles, $item->{title}; + push @missing_branches, $item->{branch}; + } + @bad_item_titles = uniq @bad_item_titles; + @missing_branches = uniq @missing_branches; + $template->param( + bad_titles => scalar @bad_item_titles, + titles => \@bad_item_titles, missing_branches => scalar @missing_branches, - branches => \@missing_branches + branches => \@missing_branches, ); - import_records_list($template, $import_batch_id, $offset, $results_per_page); + import_records_list( $template, $import_batch_id, $offset, $results_per_page ); } } elsif ($op eq "cud-commit-batch") { my $frameworkcode = $input->param('framework'); --