+ if ($dedup_barcode && grep { exists $_->{error_code} && $_->{error_code} eq 'duplicate_barcode' } @$errors_ref) {
+ # Find the record called 'barcode'
+ my ($tag, $sub) = C4::Biblio::GetMarcFromKohaField('items.barcode', $framework);
+ # Now remove any items that didn't have a duplicate_barcode error,
+ # erase the barcodes on items that did, and re-add those items.
+ my %dupes;
+ foreach my $i (0 .. $#{$errors_ref}) {
+ my $ref = $errors_ref->[$i];
+ if ($ref && ($ref->{error_code} eq 'duplicate_barcode')) {
+ $dupes{$ref->{item_sequence}} = 1;
+ # Delete the error message because we're going to
+ # retry this one.
+ delete $errors_ref->[$i];
+ }
+ }
+ my $seq = 0;
+ foreach my $field ($record->field($tag)) {
+ $seq++;
+ if ($dupes{$seq}) {
+ # Here we remove the barcode
+ $field->delete_subfield(code => $sub);
+ } else {
+ # otherwise we delete the field because we don't want
+ # two of them
+ $record->delete_fields($field);
+ }
+ }
+ # Now re-add the record as before, adding errors to the prev list
+ my $more_errors;
+ eval { ( $itemnumbers_ref, $more_errors ) = AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' ); };
+ if ( $@ ) {
+ warn "ERROR: Adding items to bib $biblionumber failed: $@\n";
+ printlog({id=>$id||$originalid||$biblionumber, op=>"insertitem",status=>"ERROR"}) if ($logfile);
+ # if we failed because of an exception, assume that
+ # the MARC columns in biblioitems were not set.
+ ModBiblioMarc( $record, $biblionumber, $framework );
+ next RECORD;
+ } else {
+ printlog({id=>$id||$originalid||$biblionumber, op=>"insert",status=>"ok"}) if ($logfile);
+ }
+ push @$errors_ref, @{ $more_errors };
+ }
+ if ($#{ $errors_ref } > -1) {