| 1 | package Koha::Contrib::Sudoc::Loader::Biblios; | ||
| 2 | # ABSTRACT: Chargeur de notices biblio | ||
| 3 | $Koha::Contrib::Sudoc::Loader::Biblios::VERSION = '2.50'; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | extends 'Koha::Contrib::Sudoc::Loader'; | ||
| 7 | |||
| 8 | use Modern::Perl; | ||
| 9 | use utf8; | ||
| 10 | use YAML; | ||
| 11 | use C4::Biblio qw/ ModBiblio ModBiblioMarc AddBiblio /; | ||
| 12 | use C4::Items qw/ AddItemBatchFromMarc /; | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | # On cherche les notices doublons SUDOC. On renvoie la liste des notices | ||
| 17 | # Koha correspondantes. | ||
| 18 | sub doublons_sudoc { | ||
| 19 | my ($self, $record) = @_; | ||
| 20 | my @doublons; | ||
| 21 | # On cherche un 035 avec $9 sudoc qui indique une fusion de notices Sudoc 035$a | ||
| 22 | # contient le PPN de la notice qui a été fusionnée avec la notice en cours de | ||
| 23 | # traitement. | ||
| 24 | for my $field ( $record->field('035') ) { | ||
| 25 | my $sudoc = $field->subfield('9'); | ||
| 26 | next unless $sudoc && $sudoc =~ /sudoc/i; | ||
| 27 | my $ppn = $field->subfield('a'); | ||
| 28 | my ($biblionumber, $framework, $koha_record) = | ||
| 29 | $self->sudoc->koha->get_biblio_by_ppn( $ppn ); | ||
| 30 | if ($koha_record) { | ||
| 31 | $self->log->notice(" Fusion Sudoc du ppn $ppn avec le biblionumber $biblionumber\n"); | ||
| 32 | push @doublons, { | ||
| 33 | ppn => $ppn, | ||
| 34 | record => $koha_record, | ||
| 35 | biblionumber => $biblionumber, | ||
| 36 | framework => $framework, | ||
| 37 | }; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | return \@doublons; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub handle_record { | ||
| 45 | my ($self, $record) = @_; | ||
| 46 | |||
| 47 | # FIXME Reset de la connexion tous les x enregistrements | ||
| 48 | $self->sudoc->koha->zconn_reset() unless $self->count % 10; | ||
| 49 | |||
| 50 | $self->converter->build($record); | ||
| 51 | |||
| 52 | my $ppn = $record->field('001')->value; | ||
| 53 | my $rcr_hash = $self->sudoc->c->{rcr}; | ||
| 54 | my @attached_bibs = sort map { $rcr_hash->{$_} } keys %{$self->converter->item}; | ||
| 55 | |||
| 56 | $self->log->notice( | ||
| 57 | "Notice #" . $self->count . " ppn $ppn (" . | ||
| 58 | join(', ', @attached_bibs) . ")\n"); | ||
| 59 | $self->log->debug( $self->sudoc->record_as_text($record) ); | ||
| 60 | |||
| 61 | # On déplace le PPN | ||
| 62 | $self->sudoc->ppn_move($record, $self->sudoc->c->{biblio}->{ppn_move}); | ||
| 63 | |||
| 64 | |||
| 65 | # Est-ce qu'il faut passer la notice ? | ||
| 66 | if ( $self->converter->skip($record) ) { | ||
| 67 | $record = undef; | ||
| 68 | $self->count_skipped( $self->count_skipped + 1 ); | ||
| 69 | $self->log->notice( " * Ignorée\n" ); | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | # On cherche si la notice entrante ne se trouve pas déjà dans le | ||
| 74 | # catalogue Koha. | ||
| 75 | my ($biblionumber, $framework, $koha_record); | ||
| 76 | ($biblionumber, $framework, $koha_record) = | ||
| 77 | $self->sudoc->koha->get_biblio_by_ppn( $ppn ); | ||
| 78 | if ($koha_record) { | ||
| 79 | $self->log->debug(" ppn trouvé dans la notice Koha $biblionumber\n"); | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | # On cherche un 035 avec un $5 contenant un RCR de l'ILN, auquel cas $a contient | ||
| 83 | # le biblionumber d'une notice Koha | ||
| 84 | for my $field ( $record->field('035') ) { | ||
| 85 | my $rcr = $field->subfield('5'); | ||
| 86 | next unless $rcr; | ||
| 87 | next unless my $branch = $rcr_hash->{$rcr}; | ||
| 88 | next unless $biblionumber = $field->subfield('a'); | ||
| 89 | ($framework, $koha_record) = | ||
| 90 | $self->sudoc->koha->get_biblio( $biblionumber ); | ||
| 91 | if ($koha_record) { | ||
| 92 | $self->log->notice( | ||
| 93 | " Fusion de la notice Koha $biblionumber trouvée en 035\$a " . | ||
| 94 | "pour le RCR $rcr ($branch)\n" ); | ||
| 95 | last; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | # Les doublons SUDOC. Il n'y a qu'un seul cas où on peut en faire | ||
| 101 | # quelque chose. Si on a déjà trouvé une notice Koha ci-dessus, on | ||
| 102 | # ne peut rien faire : en effet, on a déjà une cible pour fusionner | ||
| 103 | # la notice entrante. S'il y a plus d'un doublon qui correspond à | ||
| 104 | # des notices Koha, on ne sait à quelle notice Koha fusionner la | ||
| 105 | # notice entrante. | ||
| 106 | my $doublons = $self->doublons_sudoc($record); | ||
| 107 | if ( @$doublons ) { | ||
| 108 | if ( $koha_record || @$doublons > 1 ) { | ||
| 109 | $self->log->warning( | ||
| 110 | " Attention ! la notice entrante doit être fusionnées à plusieurs notices " . | ||
| 111 | "Koha existantes. À FAIRE MANUELLEMENT\n" ); | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | # On fusionne le doublon SUDOC (unique) avec la notice SUDOC entrante | ||
| 115 | my $d = shift @$doublons; | ||
| 116 | ($biblionumber, $framework, $koha_record) = | ||
| 117 | ($d->{biblionumber}, $d->{framework}, $d->{record}); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | $self->converter->init( $record ); | ||
| 121 | $self->converter->authoritize( $record ); | ||
| 122 | $self->converter->linking( $record ); | ||
| 123 | $self->converter->itemize( $record, $koha_record ); | ||
| 124 | |||
| 125 | if ( $koha_record ) { | ||
| 126 | # Modification d'une notice | ||
| 127 | $self->count_replaced( $self->count_replaced + 1 ); | ||
| 128 | $self->converter->merge($record, $koha_record); | ||
| 129 | $self->converter->clean($record); | ||
| 130 | ModBiblio($record->as('Legacy'), $biblionumber, $framework) | ||
| 131 | if $self->doit; | ||
| 132 | $self->converter->biblio_modify($record, $biblionumber, $framework); | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | # Nouvelle notice | ||
| 136 | $self->count_added( $self->count_added + 1 ); | ||
| 137 | $self->converter->clean($record); | ||
| 138 | $framework = $self->converter->framework($record); | ||
| 139 | if ( $self->doit ) { | ||
| 140 | my $marc = $record->as('Legacy'); | ||
| 141 | my ($biblionumber, $biblioitemnumber) = | ||
| 142 | AddBiblio($marc, $framework, { defer_marc_save => 1 }); | ||
| 143 | my ($itemnumbers_ref, $errors_ref) = | ||
| 144 | AddItemBatchFromMarc($marc, $biblionumber, $biblioitemnumber, $framework); | ||
| 145 | $self->log->warning( "erreur pendant l'ajout de l'exemplaire :\n" . Dump($errors_ref) ) | ||
| 146 | if @$errors_ref; | ||
| 147 | C4::Biblio::_strip_item_fields($marc, $framework); | ||
| 148 | ModBiblioMarc($marc, $biblionumber); | ||
| 149 | } | ||
| 150 | $self->converter->biblio_add($record, $biblionumber, $framework); | ||
| 151 | } | ||
| 152 | $self->log->debug("\n"); | ||
| 153 | } | ||
| 154 | |||
| 155 | 1; | ||
| 156 | |||
| 157 | __END__ |