Koha/Contrib/Sudoc/Loader/Biblios.pm untested

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