@@ -123,6 +123,8 @@ sub harvest_hlisd {
123123
124124 if ( $self -> {type } eq ' patron' && $self -> {mode } eq ' update' ) {
125125 $self -> HLISD_update_patrons();
126+ } elsif ( $self -> {type } eq ' patron' && $self -> {mode } eq ' create' ) {
127+ $self -> HLISD_create_patrons();
126128 } elsif ( $self -> {type } eq ' library' && $self -> {mode } eq ' create' ) {
127129 $self -> HLISD_create_libraries();
128130 } else {
@@ -189,6 +191,85 @@ sub HLISD_create_libraries {
189191 }
190192}
191193
194+ =head3 HLISD_create_patrons
195+
196+ Method that handles a HLISD patron harvest create
197+
198+ This method retrieves a list of libraries from the HLISD API and creates
199+ new patrons of ILL Partner category in Koha based on the mapping between Koha and HLISD fields.
200+
201+ =cut
202+
203+ sub HLISD_create_patrons {
204+ my ($self ) = @_ ;
205+
206+ $self -> required_patron_config_check();
207+
208+ my $res = $self -> {_api }-> Libraries();
209+ my $libraries = $res -> {data };
210+
211+ my $importlibrariesstartingwith = $self -> {config }-> {importlibrariesstartingwith } || ' ' ;
212+
213+ foreach my $library (@$libraries ) {
214+
215+ next if $importlibrariesstartingwith && !$library -> {attributes }-> {' document-supply' };
216+ next
217+ if $importlibrariesstartingwith
218+ and not grep { $library -> {attributes }-> {' document-supply' } =~ / ^\Q $_ \E /i }
219+ map { s / ^\s +|\s +$// gr } split /,/, $importlibrariesstartingwith ;
220+
221+ my $koha_patron = Koha::Patrons-> search(
222+ [
223+ {
224+ ' cardnumber' => $self -> process_HLISD_prefix( $library -> {attributes }-> {' document-supply' } )
225+ },
226+ {
227+ ' extended_attributes.code' => $self -> {config }-> {libraryidfield },
228+ ' extended_attributes.attribute' => { ' =' => $library -> {id } },
229+ }
230+ ],
231+ { ' prefetch' => [' extended_attributes' ] }
232+ );
233+
234+ if ($koha_patron -> count) {
235+ $self -> debug_msg(
236+ sprintf (
237+ " Patron with HLISD ID %s already exists. " .$self -> process_HLISD_prefix( $library -> {attributes }-> {' document-supply' } )." Skipping" ,
238+ $library -> {id }
239+ )
240+ );
241+ next ;
242+ }
243+
244+ $self -> debug_msg( sprintf ( " Importing %s " , $library -> {id } ) );
245+ my $patron = Koha::Patron-> new(
246+ {
247+ categorycode => C4::Context-> preference(' ILLPartnerCode' ),
248+ branchcode => ' IL' , # TODO: Make this abstract. Add config input for ILL libraries branchcode
249+ cardnumber => $self -> process_HLISD_prefix( $library -> {attributes }-> {' document-supply' } ),
250+ surname => substr (
251+ $self -> process_HLISD_prefix( $library -> {attributes }-> {' document-supply' } ) . ' - '
252+ . $library -> {attributes }-> { $self -> get_HLISD_counterpart_field(' surname' ) }, 0, 75
253+ ),
254+ address => $self -> process_HLISD_address(
255+ $library -> {attributes }-> { $self -> get_HLISD_counterpart_field(' address' ) }
256+ ),
257+ phone => $library -> {attributes }-> { $self -> get_HLISD_counterpart_field(' phone' ) },
258+ email => $library -> {attributes }-> { $self -> get_HLISD_counterpart_field(' email' ) },
259+ emailpro => $library -> {attributes }-> { $self -> get_HLISD_counterpart_field(' email' ) },
260+ zipcode => $library -> {attributes }-> { $self -> get_HLISD_counterpart_field(' zipcode' ) }
261+ }
262+ )-> store();
263+
264+ $patron -> add_extended_attribute(
265+ {
266+ code => $self -> {config }-> {libraryidfield },
267+ attribute => $library -> {id }
268+ }
269+ );
270+ }
271+ }
272+
192273=head3 process_HLISD_address
193274
194275Method that handles a HLISD address processing
0 commit comments