| 1 | package Koha::Contrib::Sudoc::Loader::Biblios; | ||
| 2 | # ABSTRACT: Chargeur de notices biblio | ||
| 3 | $Koha::Contrib::Sudoc::Loader::Biblios::VERSION = '2.49'; | ||
| 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 | my $ppn = $record->field('001')->value; | ||
| 51 | $self->log->notice("Notice #" . $self->count . " ppn $ppn\n"); | ||
| 52 | $self->log->debug( $self->sudoc->record_as_text($record) ); | ||
| 53 | |||
| 54 | # On déplace le PPN | ||
| 55 | $self->sudoc->ppn_move($record, $self->sudoc->c->{biblio}->{ppn_move}); | ||
| 56 | |||
| 57 | $self->converter->build($record); | ||
| 58 | |||
| 59 | # Est-ce qu'il faut passer la notice ? | ||
| 60 | if ( $self->converter->skip($record) ) { | ||
| 61 | $record = undef; | ||
| 62 | $self->count_skipped( $self->count_skipped + 1 ); | ||
| 63 | $self->log->notice( " * Ignorée\n" ); | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | # On cherche si la notice entrante ne se trouve pas déjà dans le | ||
| 68 | # catalogue Koha. | ||
| 69 | my ($biblionumber, $framework, $koha_record); | ||
| 70 | ($biblionumber, $framework, $koha_record) = | ||
| 71 | $self->sudoc->koha->get_biblio_by_ppn( $ppn ); | ||
| 72 | if ($koha_record) { | ||
| 73 | $self->log->debug(" ppn trouvé dans la notice Koha $biblionumber\n"); | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | # On cherche un 035 avec un $5 contenant un RCR de l'ILN, auquel cas $a contient | ||
| 77 | # le biblionumber d'une notice Koha | ||
| 78 | my $rcr_hash = $self->sudoc->c->{rcr}; | ||
| 79 | for my $field ( $record->field('035') ) { | ||
| 80 | my $rcr = $field->subfield('5'); | ||
| 81 | next unless $rcr; | ||
| 82 | next unless my $branch = $rcr_hash->{$rcr}; | ||
| 83 | next unless $biblionumber = $field->subfield('a'); | ||
| 84 | ($framework, $koha_record) = | ||
| 85 | $self->sudoc->koha->get_biblio( $biblionumber ); | ||
| 86 | if ($koha_record) { | ||
| 87 | $self->log->notice( | ||
| 88 | " Fusion de la notice Koha $biblionumber trouvée en 035\$a " . | ||
| 89 | "pour le RCR $rcr ($branch)\n" ); | ||
| 90 | last; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | # Les doublons SUDOC. Il n'y a qu'un seul cas où on peut en faire | ||
| 96 | # quelque chose. Si on a déjà trouvé une notice Koha ci-dessus, on | ||
| 97 | # ne peut rien faire : en effet, on a déjà une cible pour fusionner | ||
| 98 | # la notice entrante. S'il y a plus d'un doublon qui correspond à | ||
| 99 | # des notices Koha, on ne sait à quelle notice Koha fusionner la | ||
| 100 | # notice entrante. | ||
| 101 | my $doublons = $self->doublons_sudoc($record); | ||
| 102 | if ( @$doublons ) { | ||
| 103 | if ( $koha_record || @$doublons > 1 ) { | ||
| 104 | $self->log->warning( | ||
| 105 | " Attention ! la notice entrante doit être fusionnées à plusieurs notices " . | ||
| 106 | "Koha existantes. À FAIRE MANUELLEMENT\n" ); | ||
| 107 | } | ||
| 108 | else { | ||
| 109 | # On fusionne le doublon SUDOC (unique) avec la notice SUDOC entrante | ||
| 110 | my $d = shift @$doublons; | ||
| 111 | ($biblionumber, $framework, $koha_record) = | ||
| 112 | ($d->{biblionumber}, $d->{framework}, $d->{record}); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | $self->converter->init( $record ); | ||
| 116 | $self->converter->authoritize( $record ); | ||
| 117 | $self->converter->linking( $record ); | ||
| 118 | $self->converter->itemize( $record, $koha_record ); | ||
| 119 | |||
| 120 | if ( $koha_record ) { | ||
| 121 | # Modification d'une notice | ||
| 122 | $self->count_replaced( $self->count_replaced + 1 ); | ||
| 123 | $self->converter->merge($record, $koha_record); | ||
| 124 | $self->converter->clean($record); | ||
| 125 | ModBiblio($record->as('Legacy'), $biblionumber, $framework) | ||
| 126 | if $self->doit; | ||
| 127 | $self->converter->biblio_modify($record, $biblionumber, $framework); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | # Nouvelle notice | ||
| 131 | $self->count_added( $self->count_added + 1 ); | ||
| 132 | $self->converter->clean($record); | ||
| 133 | $framework = $self->converter->framework($record); | ||
| 134 | if ( $self->doit ) { | ||
| 135 | my $marc = $record->as('Legacy'); | ||
| 136 | my ($biblionumber, $biblioitemnumber) = | ||
| 137 | AddBiblio($marc, $framework, { defer_marc_save => 1 }); | ||
| 138 | my ($itemnumbers_ref, $errors_ref) = | ||
| 139 | AddItemBatchFromMarc($marc, $biblionumber, $biblioitemnumber, $framework); | ||
| 140 | $self->log->warning( "erreur pendant l'ajout de l'exemplaire :\n" . Dump($errors_ref) ) | ||
| 141 | if @$errors_ref; | ||
| 142 | C4::Biblio::_strip_item_fields($marc, $framework); | ||
| 143 | ModBiblioMarc($marc, $biblionumber); | ||
| 144 | } | ||
| 145 | $self->converter->biblio_add($record, $biblionumber, $framework); | ||
| 146 | } | ||
| 147 | $self->log->debug("\n"); | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | __END__ | ||
| 153 | |||
| 154 | =pod | ||
| 155 | |||
| 156 | =encoding UTF-8 | ||
| 157 | |||
| 158 | =head1 NAME | ||
| 159 | |||
| 160 | Koha::Contrib::Sudoc::Loader::Biblios - Chargeur de notices biblio | ||
| 161 | |||
| 162 | =head1 VERSION | ||
| 163 | |||
| 164 | version 2.49 | ||
| 165 | |||
| 166 | =head1 AUTHOR | ||
| 167 | |||
| 168 | Frédéric Demians <f.demians@tamil.fr> | ||
| 169 | |||
| 170 | =head1 COPYRIGHT AND LICENSE | ||
| 171 | |||
| 172 | This software is Copyright (c) 2026 by Fréderic Demians. | ||
| 173 | |||
| 174 | This is free software, licensed under: | ||
| 175 | |||
| 176 | The GNU General Public License, Version 3, June 2007 | ||
| 177 | |||
| 178 | =cut |