3 # Copyright 2014 Oslo Public Library
7 nl-sync-to-koha.pl - Sync patrons from the Norwegian national patron database (NL) to Koha.
11 perl nl-sync-to-koha.pl -v --run
16 use C4
::Members
::Attributes
qw( UpdateBorrowerAttribute );
17 use Koha
::NorwegianPatronDB
qw( NLCheckSysprefs NLGetChanged );
24 my ( $run, $from, $verbose, $debug ) = get_options
();
26 my $check_result = NLCheckSysprefs
();
27 if ( $check_result->{'error'} == 1 ) {
28 if ( $check_result->{'nlenabled'} == 0 ) { say "* Please activate this function with the NorwegianPatronDBEnable system preference." };
29 if ( $check_result->{'endpoint'} == 0 ) { say "* Please specify an endpoint with the NorwegianPatronDBEndpoint system preference." };
30 if ( $check_result->{'userpass'} == 0 ) { say "* Please fill in the NorwegianPatronDBUsername and NorwegianPatronDBPassword system preferences." };
35 say "* You have not specified --run, no real syncing will be done.";
41 my $skipped_local_change = 0;
43 # Get the borrowers that have been changed
44 my $result = NLGetChanged
( $from );
47 say 'Number of records: ' . $result->{'antall_poster_returnert'};
48 say 'Number of hits: ' . $result->{'antall_treff'};
49 say 'Message: ' . $result->{'melding'};
50 say 'Status: ' . $result->{'status'};
51 say 'Server time: ' . $result->{'server_tid'};
52 say "-----------------------------";
55 # Loop through the patrons
56 foreach my $patron ( @
{ $result->{'kohapatrons'} } ) {
58 if ( $patron->{'surname'} ) {
59 say "*** Name: " . $patron->{'surname'};
63 say 'Created by: ' . $patron->{'_extra'}->{'created_by'};
64 say 'Last change by: ' . $patron->{'_extra'}->{'last_change_by'};
66 # Only sync in changes made by other libraries
67 if ( C4
::Context
->preference("NorwegianPatronDBUsername") ne $patron->{'_extra'}->{'last_change_by'} ) {
68 # Make a copy of the data in the hashref and store it as a hash
69 my %clean_patron = %$patron;
70 # Delete the extra data from the copy of the hashref
71 delete $clean_patron{'_extra'};
72 # Find the borrowernumber based on cardnumber
73 my $stored_patron = Koha
::Database
->new->schema->resultset('Borrower')->find({
74 'cardnumber' => $patron->{'cardnumber'}
76 my $borrowernumber = $stored_patron->borrowernumber;
79 my $success = ModMember
(
80 'borrowernumber' => $borrowernumber,
85 my $sync = Koha
::Database
->new->schema->resultset('BorrowerSync')->find({
86 'synctype' => 'norwegianpatrondb',
87 'borrowernumber' => $borrowernumber,
89 # Update the syncstatus to 'synced'
90 $sync->update( { 'syncstatus' => 'synced' } );
91 # Update the 'synclast' attribute with the "server time" ("server_tid") returned by the method
92 $sync->update( { 'lastsync' => $result->{'result'}->{'server_tid'} } );
93 # Save social security number as attribute
94 UpdateBorrowerAttribute
(
96 { code
=> 'fnr', attribute
=> $patron->{'_extra'}->{'socsec'} },
104 say "Skipped, local change" if $verbose;
105 $skipped_local_change++;
110 say "-----------------------------";
111 say "Sync succeeded: $sync_success";
112 say "Sync failed : $sync_failed";
113 say "Skipped local change: $skipped_local_change";
122 Actually carry out syncing operations. Without this option, the script will
123 only report what it would have done, but not change any data, locally or
126 =item B<-v --verbose>
128 Report on the progress of the script.
132 Date and time to sync from, if this should be different from "1 second past
133 midnight of the day before". The date should be in this format:
141 =item B<-h, -?, --help>
143 Prints this help message and exits.
160 'f|from=s' => \
$from,
161 'v|verbose' => \
$verbose,
162 'd|debug' => \
$debug,
166 pod2usage
( -exitval
=> 0 ) if $help;
168 return ( $run, $from, $verbose, $debug );
174 Magnus Enger <digitalutvikling@gmail.com>
178 Copyright 2014 Oslo Public Library
182 This file is part of Koha.
184 Koha is free software; you can redistribute it and/or modify it under the terms
185 of the GNU General Public License as published by the Free Software Foundation;
186 either version 3 of the License, or (at your option) any later version.
188 You should have received a copy of the GNU General Public License along with
189 Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
190 Fifth Floor, Boston, MA 02110-1301 USA.