- while (my $row = $sth->fetchrow_hashref()) {
- my $item_marc = MARC::Record->new_from_xml(StripNonXmlChars($row->{'marcxml'}), 'UTF-8', $row->{'encoding'});
- # FIXME - duplicate barcode check needs to become part of AddItemFromMarc()
- my $item = TransformMarcToKoha($dbh, $item_marc);
- my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
- if ($duplicate_barcode) {
- my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, import_error = ? WHERE import_items_id = ?");
- $updsth->bind_param(1, 'error');
- $updsth->bind_param(2, 'duplicate item barcode');
- $updsth->bind_param(3, $row->{'import_items_id'});
+
+ while ( my $row = $sth->fetchrow_hashref() ) {
+ my $item_marc = MARC::Record->new_from_xml( StripNonXmlChars( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} );
+
+ # Delete date_due subfield as to not accidentally delete item checkout due dates
+ my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan', GetFrameworkCode($biblionumber) );
+ $item_marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
+
+ my $item = TransformMarcToKoha( $dbh, $item_marc );
+
+ my $duplicate_barcode = exists( $item->{'barcode'} ) && GetItemnumberFromBarcode( $item->{'barcode'} );
+ my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
+
+ my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
+ if ( $action eq "replace" && $duplicate_itemnumber ) {
+ # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
+ ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} );
+ $updsth->bind_param( 1, 'imported' );
+ $updsth->bind_param( 2, $item->{itemnumber} );
+ $updsth->bind_param( 3, $row->{'import_items_id'} );
+ $updsth->execute();
+ $updsth->finish();
+ $num_items_replaced++;
+ } elsif ( $action eq "replace" && $duplicate_barcode ) {
+ my $itemnumber = GetItemnumberFromBarcode( $item->{'barcode'} );
+ ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
+ $updsth->bind_param( 1, 'imported' );
+ $updsth->bind_param( 2, $item->{itemnumber} );
+ $updsth->bind_param( 3, $row->{'import_items_id'} );
+ $updsth->execute();
+ $updsth->finish();
+ $num_items_replaced++;
+ } elsif ($duplicate_barcode) {
+ $updsth->bind_param( 1, 'error' );
+ $updsth->bind_param( 2, 'duplicate item barcode' );
+ $updsth->bind_param( 3, $row->{'import_items_id'} );