View | Details | Raw Unified | Return to bug 20447
Collapse All | Expand All

(-)a/Koha/Exporter/Record.pm (-17 / +67 lines)
Lines 82-108 sub _get_record_for_export { Link Here
82
        }
82
        }
83
    }
83
    }
84
84
85
    if ($dont_export_fields) {
85
    _strip_unwanted_fields($record, $dont_export_fields) if $dont_export_fields;
86
        for my $f ( split / /, $dont_export_fields ) {
86
    C4::Biblio::RemoveAllNsb($record) if $clean;
87
            if ( $f =~ m/^(\d{3})(.)?$/ ) {
87
    return $record;
88
                my ( $field, $subfield ) = ( $1, $2 );
88
}
89
89
90
                # skip if this record doesn't have this field
90
sub _strip_unwanted_fields {
91
                if ( defined $record->field($field) ) {
91
    my ($record, $dont_export_fields) = @_;
92
                    if ( defined $subfield ) {
92
93
                        my @tags = $record->field($field);
93
    for my $f ( split / /, $dont_export_fields ) {
94
                        foreach my $t (@tags) {
94
        if ( $f =~ m/^(\d{3})(.)?$/ ) {
95
                            $t->delete_subfields($subfield);
95
            my ( $field, $subfield ) = ( $1, $2 );
96
                        }
96
97
                    } else {
97
            # skip if this record doesn't have this field
98
                        $record->delete_fields( $record->field($field) );
98
            if ( defined $record->field($field) ) {
99
                if ( defined $subfield ) {
100
                    my @tags = $record->field($field);
101
                    foreach my $t (@tags) {
102
                        $t->delete_subfields($subfield);
99
                    }
103
                    }
104
                } else {
105
                    $record->delete_fields( $record->field($field) );
100
                }
106
                }
101
            }
107
            }
102
        }
108
        }
103
    }
109
    }
104
    C4::Biblio::RemoveAllNsb($record) if $clean;
105
    return $record;
106
}
110
}
107
111
108
sub _get_authority_for_export {
112
sub _get_authority_for_export {
Lines 128-134 sub _get_biblio_for_export { Link Here
128
        C4::Biblio::EmbedItemsInMarcBiblio({
132
        C4::Biblio::EmbedItemsInMarcBiblio({
129
            marc_record  => $record,
133
            marc_record  => $record,
130
            biblionumber => $biblionumber,
134
            biblionumber => $biblionumber,
131
            item_numbers => $itemnumbers });
135
            item_numbers => $itemnumbers,
136
            skip_holdings => 1 });
132
        if ($only_export_items_for_branches && @$only_export_items_for_branches) {
137
        if ($only_export_items_for_branches && @$only_export_items_for_branches) {
133
            my %export_items_for_branches = map { $_ => 1 } @$only_export_items_for_branches;
138
            my %export_items_for_branches = map { $_ => 1 } @$only_export_items_for_branches;
134
            my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField( 'items.homebranch' );
139
            my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField( 'items.homebranch' );
Lines 144-149 sub _get_biblio_for_export { Link Here
144
    return $record;
149
    return $record;
145
}
150
}
146
151
152
sub _get_holdings_for_export {
153
    my ($params)           = @_;
154
    my $dont_export_fields = $params->{dont_export_fields};
155
    my $clean              = $params->{clean};
156
    my $biblionumber       = $params->{biblionumber};
157
158
    my @records;
159
    my $holdings = Koha::Holdings->search({ biblionumber => $biblionumber, deleted_on => undef });
160
    while (my $holding = $holdings->next()) {
161
        my $record = $holding->metadata()->record();
162
        _strip_unwanted_fields($record, $dont_export_fields) if $dont_export_fields;
163
        C4::Biblio::RemoveAllNsb($record) if $clean;
164
        C4::Biblio::UpsertMarcControlField($record, '004', $biblionumber);
165
        my $leader = $record->leader();
166
        if ($leader !~ /^.{6}[uvxy]/) {
167
            $leader =~ s/^(.{6})./$1u/;
168
            $record->leader($leader);
169
        }
170
        push @records, $record;
171
    }
172
    return \@records;
173
}
174
147
sub export {
175
sub export {
148
    my ($params) = @_;
176
    my ($params) = @_;
149
177
Lines 155-160 sub export { Link Here
155
    my $dont_export_fields = $params->{dont_export_fields};
183
    my $dont_export_fields = $params->{dont_export_fields};
156
    my $csv_profile_id     = $params->{csv_profile_id};
184
    my $csv_profile_id     = $params->{csv_profile_id};
157
    my $output_filepath    = $params->{output_filepath};
185
    my $output_filepath    = $params->{output_filepath};
186
    my $export_holdings    = $params->{export_holdings} // 0;
158
187
159
    if( !$record_type ) {
188
    if( !$record_type ) {
160
        Koha::Logger->get->warn( "No record_type given." );
189
        Koha::Logger->get->warn( "No record_type given." );
Lines 184-189 sub export { Link Here
184
                next;
213
                next;
185
            }
214
            }
186
            print $record->as_usmarc();
215
            print $record->as_usmarc();
216
            if ($export_holdings && $record_type eq 'bibs') {
217
                my $holdings = _get_holdings_for_export( { %$params, biblionumber => $record_id } );
218
                foreach my $holding (@$holdings) {
219
                    my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $holding->as_usmarc )->warnings() ) };
220
                    if ( $errorcount_on_decode or $@ ) {
221
                        my $msg = "Holdings record for biblio $record_id could not be exported. " .
222
                            ( $@ // '' );
223
                        chomp $msg;
224
                        Koha::Logger->get->info( $msg );
225
                        next;
226
                    }
227
                    print $holding->as_usmarc();
228
                }
229
            }
187
        }
230
        }
188
    } elsif ( $format eq 'xml' ) {
231
    } elsif ( $format eq 'xml' ) {
189
        my $marcflavour = C4::Context->preference("marcflavour");
232
        my $marcflavour = C4::Context->preference("marcflavour");
Lines 196-201 sub export { Link Here
196
            next unless $record;
239
            next unless $record;
197
            print MARC::File::XML::record($record);
240
            print MARC::File::XML::record($record);
198
            print "\n";
241
            print "\n";
242
            if ($export_holdings && $record_type eq 'bibs') {
243
                my $holdings = _get_holdings_for_export( { %$params, biblionumber => $record_id } );
244
                foreach my $holding (@$holdings) {
245
                    print MARC::File::XML::record($holding);
246
                    print "\n";
247
                }
248
            }
199
        }
249
        }
200
        print MARC::File::XML::footer();
250
        print MARC::File::XML::footer();
201
        print "\n";
251
        print "\n";
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt (-1 / +9 lines)
Lines 1-6 Link Here
1
[% USE raw %]
1
[% USE raw %]
2
[% USE Asset %]
2
[% USE Asset %]
3
[% USE Branches %]
3
[% USE Branches %]
4
[% USE Koha %]
4
[% SET footerjs = 1 %]
5
[% SET footerjs = 1 %]
5
[% INCLUDE 'doc-head-open.inc' %]
6
[% INCLUDE 'doc-head-open.inc' %]
6
<title>Export data &rsaquo; Tools &rsaquo; Koha</title>
7
<title>Export data &rsaquo; Tools &rsaquo; Koha</title>
Lines 131-137 Link Here
131
    </fieldset>
132
    </fieldset>
132
    <fieldset class="rows">
133
    <fieldset class="rows">
133
    <legend> Options</legend>
134
    <legend> Options</legend>
134
<ol>        <li>
135
<ol>
136
        [% IF ( Koha.Preference('SummaryHoldings') ) %]
137
        <li>
138
        <label for="export_holdings">Export holdings records (MARC 21 biblios only):</label>
139
        <input id="export_holdings" type="checkbox" name="export_holdings" />
140
        </li>
141
        [% END %]
142
        <li>
135
        <label for="dont_export_item">Don't export items:</label>
143
        <label for="dont_export_item">Don't export items:</label>
136
        <input id="dont_export_item" type="checkbox" name="dont_export_item" />
144
        <input id="dont_export_item" type="checkbox" name="dont_export_item" />
137
        </li>
145
        </li>
(-)a/misc/export_records.pl (-1 / +14 lines)
Lines 54-59 my ( Link Here
54
    $start_accession,
54
    $start_accession,
55
    $end_accession,
55
    $end_accession,
56
    $marc_conditions,
56
    $marc_conditions,
57
    $export_holdings,
57
    $help
58
    $help
58
);
59
);
59
60
Lines 78-83 GetOptions( Link Here
78
    'start_accession=s'       => \$start_accession,
79
    'start_accession=s'       => \$start_accession,
79
    'end_accession=s'         => \$end_accession,
80
    'end_accession=s'         => \$end_accession,
80
    'marc_conditions=s'       => \$marc_conditions,
81
    'marc_conditions=s'       => \$marc_conditions,
82
    'holdings'                => \$export_holdings,
81
    'h|help|?'                => \$help
83
    'h|help|?'                => \$help
82
) || pod2usage(1);
84
) || pod2usage(1);
83
85
Lines 112-117 if ( $deleted_barcodes and $record_type ne 'bibs' ) { Link Here
112
    pod2usage(q|--deleted_barcodes can only be used with biblios|);
114
    pod2usage(q|--deleted_barcodes can only be used with biblios|);
113
}
115
}
114
116
117
my $marcFlavour = C4::Context->preference('marcflavour') || 'MARC21';
118
if ( $export_holdings and ( $record_type ne 'bibs' or $marcFlavour ne 'MARC21' ) ) {
119
    pod2usage(q|--holdings can only be used with MARC 21 biblios|);
120
}
121
115
$start_accession = dt_from_string( $start_accession ) if $start_accession;
122
$start_accession = dt_from_string( $start_accession ) if $start_accession;
116
$end_accession   = dt_from_string( $end_accession )   if $end_accession;
123
$end_accession   = dt_from_string( $end_accession )   if $end_accession;
117
124
Lines 255-260 else { Link Here
255
            format             => $output_format,
262
            format             => $output_format,
256
            csv_profile_id     => $csv_profile_id,
263
            csv_profile_id     => $csv_profile_id,
257
            export_items       => (not $dont_export_items),
264
            export_items       => (not $dont_export_items),
265
            export_holdings    => $export_holdings,
258
            clean              => $clean || 0,
266
            clean              => $clean || 0,
259
        }
267
        }
260
    );
268
    );
Lines 268-274 export records - This script exports record (biblios or authorities) Link Here
268
276
269
=head1 SYNOPSIS
277
=head1 SYNOPSIS
270
278
271
export_records.pl [-h|--help] [--format=format] [--date=datetime] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] [--id_list_file=PATH] --filename=outputfile
279
export_records.pl [-h|--help] [--format=format] [--date=datetime] [--record-type=TYPE] [--holdings] [--dont_export_items] [--deleted_barcodes] [--clean] [--id_list_file=PATH] --filename=outputfile
272
280
273
=head1 OPTIONS
281
=head1 OPTIONS
274
282
Lines 293-298 Print a brief help message. Link Here
293
301
294
 --record-type=TYPE     TYPE is 'bibs' or 'auths'.
302
 --record-type=TYPE     TYPE is 'bibs' or 'auths'.
295
303
304
=item B<--holdings>
305
306
 --holdings             Export MARC 21 holdings records interleaved with bibs. Used only if TYPE
307
                        is 'bibs' and FORMAT is 'xml' or 'marc'.
308
296
=item B<--dont_export_items>
309
=item B<--dont_export_items>
297
310
298
 --dont_export_items    If enabled, the item infos won't be exported.
311
 --dont_export_items    If enabled, the item infos won't be exported.
(-)a/misc/migration_tools/bulkmarcimport.pl (-9 / +55 lines)
Lines 40-45 use Pod::Usage qw( pod2usage ); Link Here
40
40
41
use Koha::Logger;
41
use Koha::Logger;
42
use Koha::Biblios;
42
use Koha::Biblios;
43
use Koha::Holdings;
43
use Koha::SearchEngine;
44
use Koha::SearchEngine;
44
use Koha::SearchEngine::Search;
45
use Koha::SearchEngine::Search;
45
46
Lines 47-53 use open qw( :std :encoding(UTF-8) ); Link Here
47
binmode( STDOUT, ":encoding(UTF-8)" );
48
binmode( STDOUT, ":encoding(UTF-8)" );
48
my ( $input_marc_file, $number, $offset) = ('',0,0);
49
my ( $input_marc_file, $number, $offset) = ('',0,0);
49
my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format,$biblios,$authorities,$keepids,$match, $isbn_check, $logfile);
50
my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format,$biblios,$authorities,$keepids,$match, $isbn_check, $logfile);
50
my ( $insert, $filters, $update, $all, $yamlfile, $authtypes, $append );
51
my ( $insert, $filters, $update, $all, $yamlfile, $authtypes, $append, $import_holdings );
51
my $cleanisbn = 1;
52
my $cleanisbn = 1;
52
my ($sourcetag,$sourcesubfield,$idmapfl, $dedup_barcode);
53
my ($sourcetag,$sourcesubfield,$idmapfl, $dedup_barcode);
53
my $framework = '';
54
my $framework = '';
Lines 91-103 GetOptions( Link Here
91
    'framework=s' => \$framework,
92
    'framework=s' => \$framework,
92
    'custom:s'    => \$localcust,
93
    'custom:s'    => \$localcust,
93
    'marcmodtemplate:s' => \$marc_mod_template,
94
    'marcmodtemplate:s' => \$marc_mod_template,
95
    'holdings' => \$import_holdings,
94
);
96
);
95
$biblios ||= !$authorities;
97
$biblios ||= !$authorities;
96
$insert  ||= !$update;
98
$insert  ||= !$update;
97
my $writemode = ($append) ? "a" : "w";
99
my $writemode = ($append) ? "a" : "w";
100
my $marcFlavour = C4::Context->preference('marcflavour') || 'MARC21';
98
101
99
pod2usage( -msg => "\nYou must specify either --biblios or --authorities, not both.\n", -exitval ) if $biblios && $authorities;
102
pod2usage( -msg => "\nYou must specify either --biblios or --authorities, not both.\n", -exitval ) if $biblios && $authorities;
100
103
104
pod2usage( -msg => "\nHoldings only supported for MARC 21 biblios.\n", -exitval ) if $import_holdings && (!$biblios || $marcFlavour ne 'MARC21');
105
101
if ($all) {
106
if ($all) {
102
    $insert = 1;
107
    $insert = 1;
103
    $update = 1;
108
    $update = 1;
Lines 175-186 if ($fk_off) { Link Here
175
180
176
181
177
if ($delete) {
182
if ($delete) {
178
	if ($biblios){
183
    if ($biblios) {
179
    	print "deleting biblios\n";
184
        print "deleting biblios\n";
180
        $dbh->do("DELETE FROM biblio");
185
        $dbh->do("DELETE FROM biblio");
181
        $dbh->do("ALTER TABLE biblio AUTO_INCREMENT = 1");
186
        $dbh->do("ALTER TABLE biblio AUTO_INCREMENT = 1");
182
        $dbh->do("DELETE FROM biblioitems");
187
        $dbh->do("DELETE FROM biblioitems");
183
        $dbh->do("ALTER TABLE biblioitems AUTO_INCREMENT = 1");
188
        $dbh->do("ALTER TABLE biblioitems AUTO_INCREMENT = 1");
189
        $dbh->do("DELETE FROM holdings");
190
        $dbh->do("ALTER TABLE holdings AUTO_INCREMENT = 1");
184
        $dbh->do("DELETE FROM items");
191
        $dbh->do("DELETE FROM items");
185
        $dbh->do("ALTER TABLE items AUTO_INCREMENT = 1");
192
        $dbh->do("ALTER TABLE items AUTO_INCREMENT = 1");
186
	}
193
	}
Lines 197-204 if ($test_parameter) { Link Here
197
    print "TESTING MODE ONLY\n    DOING NOTHING\n===============\n";
204
    print "TESTING MODE ONLY\n    DOING NOTHING\n===============\n";
198
}
205
}
199
206
200
my $marcFlavour = C4::Context->preference('marcflavour') || 'MARC21';
201
202
# The definition of $searcher must be before MARC::Batch->new
207
# The definition of $searcher must be before MARC::Batch->new
203
my $searcher = Koha::SearchEngine::Search->new(
208
my $searcher = Koha::SearchEngine::Search->new(
204
    {
209
    {
Lines 236-241 if (defined $format && $format =~ /XML/i) { Link Here
236
$batch->warnings_off();
241
$batch->warnings_off();
237
$batch->strict_off();
242
$batch->strict_off();
238
my $i=0;
243
my $i=0;
244
my $holdings_count = 0;
239
my $commitnum = $commit ? $commit : 50;
245
my $commitnum = $commit ? $commit : 50;
240
my $yamlhash;
246
my $yamlhash;
241
247
Lines 265-270 if ($logfile){ Link Here
265
}
271
}
266
272
267
my $logger = Koha::Logger->get;
273
my $logger = Koha::Logger->get;
274
my $biblionumber;
268
my $schema = Koha::Database->schema;
275
my $schema = Koha::Database->schema;
269
$schema->txn_begin;
276
$schema->txn_begin;
270
RECORD: while (  ) {
277
RECORD: while (  ) {
Lines 315-325 RECORD: while ( ) { Link Here
315
            $field->update('a' => $isbn);
322
            $field->update('a' => $isbn);
316
        }
323
        }
317
    }
324
    }
325
326
    # check for holdings records
327
    my $holdings_record = 0;
328
    if ($biblios && $marcFlavour eq 'MARC21') {
329
        my $leader = $record->leader();
330
        $holdings_record = $leader =~ /^.{6}[uvxy]/;
331
    }
332
318
    my $id;
333
    my $id;
319
    # search for duplicates (based on Local-number)
334
    # search for duplicates (based on Local-number)
320
    my $originalid;
335
    my $originalid;
321
    $originalid = GetRecordId( $record, $tagid, $subfieldid );
336
    $originalid = GetRecordId( $record, $tagid, $subfieldid );
322
    if ($match) {
337
    if ($match && !$holdings_record) {
323
        require C4::Search;
338
        require C4::Search;
324
        my $query = build_query( $match, $record );
339
        my $query = build_query( $match, $record );
325
        my $server = ( $authorities ? 'authorityserver' : 'biblioserver' );
340
        my $server = ( $authorities ? 'authorityserver' : 'biblioserver' );
Lines 424-431 RECORD: while ( ) { Link Here
424
            $yamlhash->{$originalid}->{'updated'} = 1;
439
            $yamlhash->{$originalid}->{'updated'} = 1;
425
            }
440
            }
426
        }
441
        }
442
        elsif ($holdings_record) {
443
            if ($import_holdings) {
444
                if (!defined $biblionumber) {
445
                    warn "ERROR: Encountered holdings record without preceding biblio record\n";
446
                } else {
447
                    if ($insert) {
448
                        my $holding = Koha::Holding->new({ biblionumber => $biblionumber });
449
                        $holding->set_marc({ record => $record });
450
                        my $branchcode = $holding->holdingbranch();
451
                        if (defined $branchcode && !Koha::Libraries->find({ branchcode => $branchcode})) {
452
                            printlog( { id => 0, op => 'insert', status => "warning : library $branchcode not defined, holdings record skipped" } ) if ($logfile);
453
                        } else {
454
                            $holding->store();
455
                            ++$holdings_count;
456
                            printlog( { id => $holding->holding_id(), op => 'insert', status => 'ok' } ) if ($logfile);
457
                        }
458
                    } else {
459
                        printlog( { id => 0, op => 'update', status => 'warning : not in database' } ) if ($logfile);
460
                    }
461
                }
462
            }
463
        }
427
        else {
464
        else {
428
            my ( $biblionumber, $biblioitemnumber, $itemnumbers_ref, $errors_ref );
465
            my ( $biblioitemnumber, $itemnumbers_ref, $errors_ref );
429
            $biblionumber = $id;
466
            $biblionumber = $id;
430
            # check for duplicate, based on ISBN (skip it if we already have found a duplicate with match parameter
467
            # check for duplicate, based on ISBN (skip it if we already have found a duplicate with match parameter
431
            if (!$biblionumber && $isbn_check && $isbn) {
468
            if (!$biblionumber && $isbn_check && $isbn) {
Lines 562-568 delete $ENV{OVERRIDE_SYSPREF_CataloguingLog}; Link Here
562
delete $ENV{OVERRIDE_SYSPREF_AuthoritiesLog};
599
delete $ENV{OVERRIDE_SYSPREF_AuthoritiesLog};
563
600
564
my $timeneeded = gettimeofday - $starttime;
601
my $timeneeded = gettimeofday - $starttime;
565
print "\n$i MARC records done in $timeneeded seconds\n";
602
if ($import_holdings) {
603
    printf("\n%i MARC bibliographic records and %i holdings records done in %0.3f seconds\n", $i, $holdings_count, $timeneeded);
604
} else {
605
    printf("\n%i MARC records done in %0.3f seconds\n", $i, $timeneeded);
606
}
566
if ($logfile){
607
if ($logfile){
567
  print $loghandle "file : $input_marc_file\n";
608
  print $loghandle "file : $input_marc_file\n";
568
  print $loghandle "$i MARC records done in $timeneeded seconds\n";
609
  print $loghandle "$i MARC records done in $timeneeded seconds\n";
Lines 683-688 Type of import: authority records Link Here
683
724
684
The I<FILE> to import
725
The I<FILE> to import
685
726
727
=item B<-holdings>
728
729
Import MARC 21 holdings records when interleaved with bibliographic records
730
(insert only, update not supported). Used only with -biblios.
731
686
=item  B<-v>
732
=item  B<-v>
687
733
688
Verbose mode. 1 means "some infos", 2 means "MARC dumping"
734
Verbose mode. 1 means "some infos", 2 means "MARC dumping"
Lines 728-734 I<UNIMARC> are supported. MARC21 by default. Link Here
728
=item B<-d>
774
=item B<-d>
729
775
730
Delete EVERYTHING related to biblio in koha-DB before import. Tables: biblio,
776
Delete EVERYTHING related to biblio in koha-DB before import. Tables: biblio,
731
biblioitems, items
777
biblioitems, items, holdings
732
778
733
=item B<-m>=I<FORMAT>
779
=item B<-m>=I<FORMAT>
734
780
(-)a/t/db_dependent/Exporter/Record.t (-1 / +115 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 6;
20
use Test::More tests => 8;
21
use Test::Warn;
21
use Test::Warn;
22
use t::lib::TestBuilder;
22
use t::lib::TestBuilder;
23
23
Lines 34-40 use Koha::Database; Link Here
34
use Koha::Biblio;
34
use Koha::Biblio;
35
use Koha::Biblioitem;
35
use Koha::Biblioitem;
36
use Koha::Exporter::Record;
36
use Koha::Exporter::Record;
37
use Koha::Biblio::Metadata;
37
use Koha::Biblio::Metadatas;
38
use Koha::Biblio::Metadatas;
39
use Koha::BiblioFramework;
40
use Koha::BiblioFrameworks;
41
use Koha::Holdings;
38
42
39
my $schema  = Koha::Database->new->schema;
43
my $schema  = Koha::Database->new->schema;
40
$schema->storage->txn_begin;
44
$schema->storage->txn_begin;
Lines 62-68 my $bad_biblio = Koha::Biblio->new()->store(); Link Here
62
Koha::Biblio::Metadata->new( { biblionumber => $bad_biblio->id, format => 'marcxml', metadata => 'something wrong', schema => C4::Context->preference('marcflavour') } )->store();
66
Koha::Biblio::Metadata->new( { biblionumber => $bad_biblio->id, format => 'marcxml', metadata => 'something wrong', schema => C4::Context->preference('marcflavour') } )->store();
63
my $bad_biblionumber = $bad_biblio->id;
67
my $bad_biblionumber = $bad_biblio->id;
64
68
69
# Add a framework for holdings
70
my $frameworkcode = 'HLD';
71
my $existing_mss = Koha::MarcSubfieldStructures->search({frameworkcode => $frameworkcode});
72
$existing_mss->delete() if $existing_mss;
73
my $existing_fw = Koha::BiblioFrameworks->find({frameworkcode => $frameworkcode});
74
$existing_fw->delete() if $existing_fw;
75
Koha::BiblioFramework->new({
76
    frameworkcode => $frameworkcode,
77
    frameworktext => 'Holdings'
78
})->store();
79
Koha::MarcSubfieldStructure->new({
80
    frameworkcode => $frameworkcode,
81
    tagfield => 852,
82
    tagsubfield => 'b',
83
    kohafield => 'holdings.holdingbranch'
84
})->store();
85
Koha::MarcSubfieldStructure->new({
86
    frameworkcode => $frameworkcode,
87
    tagfield => 852,
88
    tagsubfield => 'c',
89
    kohafield => 'holdings.location'
90
})->store();
91
Koha::MarcSubfieldStructure->new({
92
    frameworkcode => $frameworkcode,
93
    tagfield => 942,
94
    tagsubfield => 'n',
95
    kohafield => 'holdings.suppress'
96
})->store();
97
Koha::MarcSubfieldStructure->new({
98
    frameworkcode => $frameworkcode,
99
    tagfield => 999,
100
    tagsubfield => 'c',
101
    kohafield => 'biblio.biblionumber'
102
})->store();
103
Koha::MarcSubfieldStructure->new({
104
    frameworkcode => $frameworkcode,
105
    tagfield => 999,
106
    tagsubfield => 'e',
107
    kohafield => 'holdings.holding_id'
108
})->store();
109
65
my $builder = t::lib::TestBuilder->new;
110
my $builder = t::lib::TestBuilder->new;
111
my $holdingbranch = $builder->build({ source => 'Branch' });
112
113
my $holding_marc_1 = MARC::Record->new();
114
$holding_marc_1->leader('00202cx  a22000973  4500');
115
$holding_marc_1->append_fields(
116
    MARC::Field->new('852', '8', ' ', b => $holdingbranch->{branchcode}, c => 'Location', k => '1973', h => 'Tb', t => 'Copy 1')
117
);
118
my $holding = Koha::Holding->new();
119
$holding->biblionumber($biblionumber_1);
120
$holding->set_marc({ record => $holding_marc_1 });
121
$holding->store();
122
66
my $item_1_1 = $builder->build_sample_item(
123
my $item_1_1 = $builder->build_sample_item(
67
    {
124
    {
68
        biblionumber => $biblionumber_1,
125
        biblionumber => $biblionumber_1,
Lines 192-197 subtest 'export iso2709' => sub { Link Here
192
    is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' );
249
    is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' );
193
};
250
};
194
251
252
subtest 'export xml with holdings' => sub {
253
    plan tests => 3;
254
    my $generated_xml_file = '/tmp/test_export.xml';
255
    Koha::Exporter::Record::export(
256
        {   record_type     => 'bibs',
257
            record_ids      => [ $biblionumber_1, $biblionumber_2 ],
258
            format          => 'xml',
259
            output_filepath => $generated_xml_file,
260
            export_holdings => 1,
261
        }
262
    );
263
264
    my $generated_xml_content = read_file( $generated_xml_file );
265
    $MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8';
266
    open my $fh, '<', $generated_xml_file;
267
    my $records = MARC::Batch->new( 'XML', $fh );
268
    my @records;
269
    # The following statement produces
270
    # Use of uninitialized value in concatenation (.) or string at /usr/share/perl5/MARC/File/XML.pm line 398, <$fh> chunk 5.
271
    # Why?
272
    while ( my $record = $records->next ) {
273
        push @records, $record;
274
    }
275
    is( scalar( @records ), 3, 'Export XML with holdings: 3 records should have been exported' );
276
    my $holding_record = $records[1];
277
    my $branch = $holding_record->subfield('852', 'b');
278
    is( $branch, $holdingbranch->{branchcode}, 'Export XML with holdings: The holdings record contains correct branch code' );
279
    my $location = $holding_record->subfield('852', 'c');
280
    is( $location, 'Location', 'Export XML with holdings: The holdings record contains correct location' );
281
};
282
283
subtest 'export iso2709 with holdings' => sub {
284
    plan tests => 3;
285
    my $generated_mrc_file = '/tmp/test_export.mrc';
286
    # Get all item infos
287
    Koha::Exporter::Record::export(
288
        {   record_type     => 'bibs',
289
            record_ids      => [ $biblionumber_1, $biblionumber_2 ],
290
            format          => 'iso2709',
291
            output_filepath => $generated_mrc_file,
292
            export_holdings => 1,
293
        }
294
    );
295
296
    my $records = MARC::File::USMARC->in( $generated_mrc_file );
297
    my @records;
298
    while ( my $record = $records->next ) {
299
        push @records, $record;
300
    }
301
    is( scalar( @records ), 3, 'Export ISO2709 with holdings: 3 records should have been exported' );
302
    my $holding_record = $records[1];
303
    my $branch = $holding_record->subfield('852', 'b');
304
    is( $branch, $holdingbranch->{branchcode}, 'Export ISO2709 with holdings: The holdings record contains correct branch code' );
305
    my $location = $holding_record->subfield('852', 'c');
306
    is( $location, 'Location', 'Export ISO2709 with holdings: The holdings record contains correct location' );
307
};
308
195
subtest 'export without record_type' => sub {
309
subtest 'export without record_type' => sub {
196
    plan tests => 1;
310
    plan tests => 1;
197
311
(-)a/tools/export.pl (-1 / +2 lines)
Lines 35-40 use Koha::Libraries; Link Here
35
my $query = CGI->new;
35
my $query = CGI->new;
36
36
37
my $dont_export_items = $query->param("dont_export_item") || 0;
37
my $dont_export_items = $query->param("dont_export_item") || 0;
38
my $export_holdings   = $query->param("export_holdings") || 0;
38
my $record_type       = $query->param("record_type");
39
my $record_type       = $query->param("record_type");
39
my $op                = $query->param("op") || '';
40
my $op                = $query->param("op") || '';
40
my $output_format     = $query->param("format") || $query->param("output_format") || 'iso2709';
41
my $output_format     = $query->param("format") || $query->param("output_format") || 'iso2709';
Lines 209-214 if ( $op eq "export" ) { Link Here
209
                dont_export_fields => $export_remove_fields,
210
                dont_export_fields => $export_remove_fields,
210
                csv_profile_id     => $csv_profile_id,
211
                csv_profile_id     => $csv_profile_id,
211
                export_items       => (not $dont_export_items),
212
                export_items       => (not $dont_export_items),
213
                export_holdings    => $export_holdings,
212
                only_export_items_for_branches => $only_export_items_for_branches,
214
                only_export_items_for_branches => $only_export_items_for_branches,
213
            }
215
            }
214
        );
216
        );
215
- 

Return to bug 20447