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

(-)a/C4/ImportBatch.pm (-36 / +39 lines)
Lines 682-736 sub BatchCommitRecords { Link Here
682
=cut
682
=cut
683
683
684
sub BatchCommitItems {
684
sub BatchCommitItems {
685
    my ($import_record_id, $biblionumber, $action) = @_;
685
    my ( $import_record_id, $biblionumber, $action ) = @_;
686
686
687
    my $dbh = C4::Context->dbh;
687
    my $dbh = C4::Context->dbh;
688
688
689
    my ($num_items_added, $num_items_errored, $num_items_replaced) = 0;
689
    my ( $num_items_added, $num_items_errored, $num_items_replaced ) = 0;
690
    my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding
690
    my $sth = $dbh->prepare("
691
                             FROM import_items
691
        SELECT import_items_id, import_items.marcxml, encoding
692
                             JOIN import_records USING (import_record_id)
692
        FROM import_items
693
                             WHERE import_record_id = ?
693
        JOIN import_records USING (import_record_id)
694
                             ORDER BY import_items_id");
694
        WHERE import_record_id = ?
695
    $sth->bind_param(1, $import_record_id);
695
        ORDER BY import_items_id
696
    ");
697
    $sth->bind_param( 1, $import_record_id );
696
    $sth->execute();
698
    $sth->execute();
697
    while (my $row = $sth->fetchrow_hashref()) {
699
    while ( my $row = $sth->fetchrow_hashref() ) {
698
        my $item_marc = MARC::Record->new_from_xml(StripNonXmlChars($row->{'marcxml'}), 'UTF-8', $row->{'encoding'});
700
        my $item_marc = MARC::Record->new_from_xml( StripNonXmlChars( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} );
699
    #delete date_due subfield as to not accidentally delete item checkout due dates
701
700
        my ($MARCfield,$MARCsubfield) = GetMarcFromKohaField('items.onloan', GetFrameworkCode($biblionumber));
702
        #delete date_due subfield as to not accidentally delete item checkout due dates
701
 $item_marc->field($MARCfield)->delete_subfield(code => $MARCsubfield);
703
        my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan', GetFrameworkCode($biblionumber) );
702
        # FIXME - duplicate barcode check needs to become part of AddItemFromMarc()
704
        $item_marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
703
        my $item = TransformMarcToKoha($dbh, $item_marc);
705
704
        my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
706
        my $item = TransformMarcToKoha( $dbh, $item_marc );
705
        my $duplicate_itemnumber = exists($item->{'itemnumber'});
707
706
     my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
708
        my $duplicate_barcode = exists( $item->{'barcode'} ) && GetItemnumberFromBarcode( $item->{'barcode'} );
707
    if($action eq "replace" && $duplicate_itemnumber){
709
        my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
708
         ModItemFromMarc($item_marc, $biblionumber, $item->{itemnumber});
710
709
            $updsth->bind_param(1, 'imported');
711
        my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
710
            $updsth->bind_param(2, $item->{itemnumber});
712
        if ( $action eq "replace" && $duplicate_itemnumber ) {
711
            $updsth->bind_param(3, $row->{'import_items_id'});
713
            ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} );
714
            $updsth->bind_param( 1, 'imported' );
715
            $updsth->bind_param( 2, $item->{itemnumber} );
716
            $updsth->bind_param( 3, $row->{'import_items_id'} );
712
            $updsth->execute();
717
            $updsth->execute();
713
            $updsth->finish();
718
            $updsth->finish();
714
        $num_items_replaced++;
719
            $num_items_replaced++;
715
     }
720
        } elsif ($duplicate_barcode) {
716
        elsif ($duplicate_barcode) {
721
            $updsth->bind_param( 1, 'error' );
717
            $updsth->bind_param(1, 'error');
722
            $updsth->bind_param( 2, 'duplicate item barcode' );
718
            $updsth->bind_param(2, 'duplicate item barcode');
723
            $updsth->bind_param( 3, $row->{'import_items_id'} );
719
            $updsth->bind_param(3, $row->{'import_items_id'});
720
            $updsth->execute();
724
            $updsth->execute();
721
            $num_items_errored++;
725
            $num_items_errored++;
722
        } else {
726
        } else {
723
            my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItemFromMarc($item_marc, $biblionumber);
727
            my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber );
724
            $updsth->bind_param(1, 'imported');
728
            $updsth->bind_param( 1, 'imported' );
725
            $updsth->bind_param(2, $itemnumber);
729
            $updsth->bind_param( 2, $itemnumber );
726
            $updsth->bind_param(3, $row->{'import_items_id'});
730
            $updsth->bind_param( 3, $row->{'import_items_id'} );
727
            $updsth->execute();
731
            $updsth->execute();
728
            $updsth->finish();
732
            $updsth->finish();
729
            $num_items_added++;
733
            $num_items_added++;
730
        }
734
        }
731
    }
735
    }
732
    $sth->finish();
736
733
    return ($num_items_added, $num_items_replaced, $num_items_errored);
737
    return ( $num_items_added, $num_items_replaced, $num_items_errored );
734
}
738
}
735
739
736
=head2 BatchRevertRecords
740
=head2 BatchRevertRecords
737
- 

Return to bug 7131