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

(-)a/C4/Biblio.pm (-2 / +5 lines)
Lines 3418-3426 Function exported, but should NOT be used, unless you really know what you're do Link Here
3418
=cut
3418
=cut
3419
3419
3420
sub ModBiblioMarc {
3420
sub ModBiblioMarc {
3421
3421
    # pass the MARC::Record to this function, and it will create the records in
3422
    # pass the MARC::Record to this function, and it will create the records in the marc field
3422
    # the marc field
3423
    my ( $record, $biblionumber, $frameworkcode ) = @_;
3423
    my ( $record, $biblionumber, $frameworkcode ) = @_;
3424
3425
    # Clone record as it gets modified
3426
    $record = $record->clone();
3424
    my $dbh    = C4::Context->dbh;
3427
    my $dbh    = C4::Context->dbh;
3425
    my @fields = $record->fields();
3428
    my @fields = $record->fields();
3426
    if ( !$frameworkcode ) {
3429
    if ( !$frameworkcode ) {
(-)a/C4/Items.pm (-1 / +4 lines)
Lines 303-309 Sequence number of original item tag in the MARC record. Link Here
303
Item barcode, provide to assist in the construction of
303
Item barcode, provide to assist in the construction of
304
useful error messages.
304
useful error messages.
305
305
306
=item error_condition
306
=item error_code
307
307
308
Code representing the error condition.  Can be 'duplicate_barcode',
308
Code representing the error condition.  Can be 'duplicate_barcode',
309
'invalid_homebranch', or 'invalid_holdingbranch'.
309
'invalid_homebranch', or 'invalid_holdingbranch'.
Lines 323-328 sub AddItemBatchFromMarc { Link Here
323
    my @errors = ();
323
    my @errors = ();
324
    my $dbh = C4::Context->dbh;
324
    my $dbh = C4::Context->dbh;
325
325
326
    # We modify the record, so lets work on a clone so we don't change the
327
    # original.
328
    $record = $record->clone();
326
    # loop through the item tags and start creating items
329
    # loop through the item tags and start creating items
327
    my @bad_item_fields = ();
330
    my @bad_item_fields = ();
328
    my ($itemtag, $itemsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
331
    my ($itemtag, $itemsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
(-)a/misc/migration_tools/bulkmarcimport.pl (-10 / +64 lines)
Lines 33-40 use Pod::Usage; Link Here
33
binmode(STDOUT, ":utf8");
33
binmode(STDOUT, ":utf8");
34
my ( $input_marc_file, $number, $offset) = ('',0,0);
34
my ( $input_marc_file, $number, $offset) = ('',0,0);
35
my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format,$biblios,$authorities,$keepids,$match, $isbn_check, $logfile);
35
my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format,$biblios,$authorities,$keepids,$match, $isbn_check, $logfile);
36
my ($sourcetag,$sourcesubfield,$idmapfl);
37
my $cleanisbn = 1;
36
my $cleanisbn = 1;
37
my ($sourcetag,$sourcesubfield,$idmapfl, $dedup_barcode);
38
38
39
$|=1;
39
$|=1;
40
40
Lines 61-66 GetOptions( Link Here
61
    'y:s' => \$sourcesubfield,
61
    'y:s' => \$sourcesubfield,
62
    'idmap:s' => \$idmapfl,
62
    'idmap:s' => \$idmapfl,
63
    'cleanisbn!'     => \$cleanisbn,
63
    'cleanisbn!'     => \$cleanisbn,
64
    'dedupbarcode' => \$dedup_barcode,
64
);
65
);
65
$biblios=!$authorities||$biblios;
66
$biblios=!$authorities||$biblios;
66
67
Lines 318-338 RECORD: while ( ) { Link Here
318
				printlog({id=>$id||$originalid||$biblionumber, op=>"insert",status=>"ok"}) if ($logfile);
319
				printlog({id=>$id||$originalid||$biblionumber, op=>"insert",status=>"ok"}) if ($logfile);
319
			}
320
			}
320
            eval { ( $itemnumbers_ref, $errors_ref ) = AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' ); };
321
            eval { ( $itemnumbers_ref, $errors_ref ) = AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' ); };
321
            if ( $@ ) {
322
            my $error_adding = $@;
322
                warn "ERROR: Adding items to bib $biblionumber failed: $@\n";
323
            # Work on a clone so that if there are real errors, we can maybe
324
            # fix them up later.
325
			my $clone_record = $record->clone();
326
            C4::Biblio::_strip_item_fields($clone_record, '');
327
            # This sets the marc fields if there was an error, and also calls
328
            # defer_marc_save.
329
            ModBiblioMarc( $clone_record, $biblionumber, '' );
330
            if ( $error_adding ) {
331
                warn "ERROR: Adding items to bib $biblionumber failed: $error_adding";
323
				printlog({id=>$id||$originalid||$biblionumber, op=>"insertitem",status=>"ERROR"}) if ($logfile);
332
				printlog({id=>$id||$originalid||$biblionumber, op=>"insertitem",status=>"ERROR"}) if ($logfile);
324
                # if we failed because of an exception, assume that 
333
                # if we failed because of an exception, assume that 
325
                # the MARC columns in biblioitems were not set.
334
                # the MARC columns in biblioitems were not set.
326
                C4::Biblio::_strip_item_fields($record, '');
327
                ModBiblioMarc( $record, $biblionumber, '' );
328
                next RECORD;
335
                next RECORD;
329
            } 
336
            }
330
 			else{
337
 			else{
331
                C4::Biblio::_strip_item_fields($record, '');
332
                ModBiblioMarc( $record, $biblionumber, '' ); # need to call because of defer_marc_save
333
				printlog({id=>$id||$originalid||$biblionumber, op=>"insert",status=>"ok"}) if ($logfile);
338
				printlog({id=>$id||$originalid||$biblionumber, op=>"insert",status=>"ok"}) if ($logfile);
334
			}
339
			}
335
            if ($#{ $errors_ref } > -1) { 
340
            if ($dedup_barcode && grep { exists $_->{error_code} && $_->{error_code} eq 'duplicate_barcode' } @$errors_ref) {
341
                # Find the record called 'barcode'
342
                my ($tag, $sub) = C4::Biblio::GetMarcFromKohaField('items.barcode', '');
343
                # Now remove any items that didn't have a duplicate_barcode error,
344
                # erase the barcodes on items that did, and re-add those items.
345
                my %dupes;
346
                foreach my $i (0 .. $#{$errors_ref}) {
347
                    my $ref = $errors_ref->[$i];
348
                    if ($ref && ($ref->{error_code} eq 'duplicate_barcode')) {
349
                        $dupes{$ref->{item_sequence}} = 1;
350
                        # Delete the error message because we're going to
351
                        # retry this one.
352
                        delete $errors_ref->[$i];
353
                    }
354
                }
355
                my $seq = 0;
356
                foreach my $field ($record->field($tag)) {
357
                    $seq++;
358
                    if ($dupes{$seq}) {
359
                        # Here we remove the barcode
360
                        $field->delete_subfield(code => $sub);
361
                    } else {
362
                        # otherwise we delete the field because we don't want
363
                        # two of them
364
                        $record->delete_fields($field);
365
                    }
366
                }
367
                # Now re-add the record as before, adding errors to the prev list
368
                my $more_errors;
369
                eval { ( $itemnumbers_ref, $more_errors ) = AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' ); };
370
                if ( $@ ) {
371
                    warn "ERROR: Adding items to bib $biblionumber failed: $@\n";
372
                    printlog({id=>$id||$originalid||$biblionumber, op=>"insertitem",status=>"ERROR"}) if ($logfile);
373
                    # if we failed because of an exception, assume that
374
                    # the MARC columns in biblioitems were not set.
375
                    ModBiblioMarc( $record, $biblionumber, '' );
376
                    next RECORD;
377
                } else {
378
                    printlog({id=>$id||$originalid||$biblionumber, op=>"insert",status=>"ok"}) if ($logfile);
379
                }
380
                push @$errors_ref, @{ $more_errors };
381
            }
382
            if ($#{ $errors_ref } > -1) {
336
                report_item_errors($biblionumber, $errors_ref);
383
                report_item_errors($biblionumber, $errors_ref);
337
            }
384
            }
338
        }
385
        }
Lines 403-408 sub report_item_errors { Link Here
403
    my $errors_ref = shift;
450
    my $errors_ref = shift;
404
451
405
    foreach my $error (@{ $errors_ref }) {
452
    foreach my $error (@{ $errors_ref }) {
453
        next if !$error;
406
        my $msg = "Item not added (bib $biblionumber, item tag #$error->{'item_sequence'}, barcode $error->{'item_barcode'}): ";
454
        my $msg = "Item not added (bib $biblionumber, item tag #$error->{'item_sequence'}, barcode $error->{'item_barcode'}): ";
407
        my $error_code = $error->{'error_code'};
455
        my $error_code = $error->{'error_code'};
408
        $error_code =~ s/_/ /g;
456
        $error_code =~ s/_/ /g;
Lines 539-544 Store ids in 009 (usefull for authorities, where 001 contains the authid for Link Here
539
Koha, that can contain a very valuable info for authorities coming from LOC or
587
Koha, that can contain a very valuable info for authorities coming from LOC or
540
BNF. useless for biblios probably)
588
BNF. useless for biblios probably)
541
589
590
=item B<-dedupbarcode>
591
592
If set, whenever a duplicate barcode is detected, it is removed and the attempt
593
to add the record is retried, thereby giving the record a blank barcode. This
594
is useful when something has set barcodes to be a biblio ID, or similar
595
(usually other software.)
596
542
=back
597
=back
543
598
544
=cut
599
=cut
545
- 

Return to bug 6199