@@ -, +, @@ --> 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 | 70 +++++++++++++++++++ .../en/modules/tools/manage-marc-import.tt | 11 +++ tools/manage-marc-import.pl | 20 ++++-- 3 files changed, 96 insertions(+), 5 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,74 @@ 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 ) %] +