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

(-)a/C4/ImportBatch.pm (-7 / +21 lines)
Lines 716-728 sub BatchCommitItems { Link Here
716
716
717
        my $item = TransformMarcToKoha( $item_marc );
717
        my $item = TransformMarcToKoha( $item_marc );
718
718
719
        my $duplicate_barcode = exists( $item->{'barcode'} ) && Koha::Items->find({ barcode => $item->{'barcode'} });
719
        my $item_match;
720
        my $duplicate_barcode = exists( $item->{'barcode'} );
720
        my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
721
        my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
721
722
723
        # We assume that when replacing items we do not want to move them - the onus is on the importer to
724
        # ensure the correct items/records are being updated
722
        my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ?, import_error = ? WHERE import_items_id = ?");
725
        my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ?, import_error = ? WHERE import_items_id = ?");
723
        if ( $action eq "replace" && $duplicate_itemnumber ) {
726
        if (
727
            $action eq "replace" &&
728
            $duplicate_itemnumber &&
729
            ( $item_match = Koha::Items->find( $item->{itemnumber} ))
730
        ) {
724
            # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
731
            # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
725
            ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} );
732
            ModItemFromMarc( $item_marc, $item_match->biblionumber, $item->{itemnumber});
726
            $updsth->bind_param( 1, 'imported' );
733
            $updsth->bind_param( 1, 'imported' );
727
            $updsth->bind_param( 2, $item->{itemnumber} );
734
            $updsth->bind_param( 2, $item->{itemnumber} );
728
            $updsth->bind_param( 3, undef );
735
            $updsth->bind_param( 3, undef );
Lines 730-738 sub BatchCommitItems { Link Here
730
            $updsth->execute();
737
            $updsth->execute();
731
            $updsth->finish();
738
            $updsth->finish();
732
            $num_items_replaced++;
739
            $num_items_replaced++;
733
        } elsif ( $action eq "replace" && $duplicate_barcode ) {
740
        } elsif (
734
            my $itemnumber = $duplicate_barcode->itemnumber;
741
            $action eq "replace" &&
735
            ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
742
            $duplicate_barcode &&
743
            ( $item_match = Koha::Items->find({ barcode => $item->{'barcode'} }) )
744
        ) {
745
            ModItemFromMarc( $item_marc, $item_match->biblionumber, $item_match->itemnumber );
736
            $updsth->bind_param( 1, 'imported' );
746
            $updsth->bind_param( 1, 'imported' );
737
            $updsth->bind_param( 2, $item->{itemnumber} );
747
            $updsth->bind_param( 2, $item->{itemnumber} );
738
            $updsth->bind_param( 3, undef );
748
            $updsth->bind_param( 3, undef );
Lines 740-746 sub BatchCommitItems { Link Here
740
            $updsth->execute();
750
            $updsth->execute();
741
            $updsth->finish();
751
            $updsth->finish();
742
            $num_items_replaced++;
752
            $num_items_replaced++;
743
        } elsif ($duplicate_barcode) {
753
        } elsif (
754
            # We aren't replacing, but the incoming file has a barcode, we need to check if it exists
755
            $duplicate_barcode &&
756
            ( $item_match = Koha::Items->find({ barcode => $item->{'barcode'} }) )
757
        ) {
744
            $updsth->bind_param( 1, 'error' );
758
            $updsth->bind_param( 1, 'error' );
745
            $updsth->bind_param( 2, undef );
759
            $updsth->bind_param( 2, undef );
746
            $updsth->bind_param( 3, 'duplicate item barcode' );
760
            $updsth->bind_param( 3, 'duplicate item barcode' );
(-)a/t/db_dependent/ImportBatch.t (-2 / +50 lines)
Lines 1-7 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use Test::More tests => 18;
4
use Test::More tests => 19;
5
use utf8;
5
use utf8;
6
use File::Basename;
6
use File::Basename;
7
use File::Temp qw/tempfile/;
7
use File::Temp qw/tempfile/;
Lines 328-333 subtest "_get_commit_action" => sub { Link Here
328
328
329
};
329
};
330
330
331
332
subtest "Do not adjust biblionumber when replacing items during import" => sub {
333
    plan tests => 7;
334
335
    my $item1 = $builder->build_sample_item;
336
    my $original_biblionumber = $item1->biblionumber;
337
    my $original_biblioitemnumber = $item1->biblioitemnumber;
338
    my $item2 = $builder->build_sample_item;
339
340
    my $library = $builder->build_object({ class => 'Koha::Libraries' });
341
342
    my $import_item = $builder->build_object({ class => 'Koha::Import::Record::Items', value => {
343
        marcxml => qq{<?xml version="1.0" encoding="UTF-8"?>
344
<collection
345
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
346
  xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
347
  xmlns="http://www.loc.gov/MARC21/slim">
348
349
<record>
350
  <leader>00000    a              </leader>
351
  <datafield tag="952" ind1=" " ind2=" ">
352
    <subfield code="a">${\($library->branchcode)}</subfield>
353
    <subfield code="b">${\($library->branchcode)}</subfield>
354
    <subfield code="c">GEN</subfield>
355
    <subfield code="p">${\($item1->barcode)}</subfield>
356
    <subfield code="y">BK</subfield>
357
  </datafield>
358
</record>
359
</collection>
360
        },
361
    }});
362
363
    isnt( $item1->homebranch, $library->branchcode, "Item's homebranch is currently not the same as our created branch's branchcode" );
364
365
    my ( $num_items_added, $num_items_replaced, $num_items_errored ) =
366
        C4::ImportBatch::BatchCommitItems( $import_item->import_record_id, $item2->biblionumber, 'replace' );
367
368
    $item1->discard_changes();
369
370
    is( $num_items_errored, 0, 'Item was replaced' );
371
    $import_item->discard_changes();
372
    is( $import_item->status, 'imported', 'Import was successful');
373
    is( $import_item->import_error, undef, 'No error was reported' );
374
375
    is( $item1->biblionumber, $original_biblionumber, "Item's biblionumber has not changed" );
376
    is( $item1->biblioitemnumber, $original_biblioitemnumber, "Item's biblioitemnumber has not changed" );
377
    is( $item1->homebranch, $library->branchcode, "Item was overlaid successfully" );
378
};
379
331
sub get_import_record {
380
sub get_import_record {
332
    my $id_import_batch = shift;
381
    my $id_import_batch = shift;
333
    return $dbh->do('SELECT * FROM import_records WHERE import_batch_id = ?', undef, $id_import_batch);
382
    return $dbh->do('SELECT * FROM import_records WHERE import_batch_id = ?', undef, $id_import_batch);
334
- 

Return to bug 32804