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
15 use C4::Members::Attributes qw( UpdateBorrowerAttribute );
16 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::Patrons->find({ cardnumber => $patron->{cardnumber} });
74 my $borrowernumber = $stored_patron->borrowernumber;
76 # FIXME Exceptions must be caught here
77 if ( $stored_patron->set(\%clean_patron)->store ) {
79 my $sync = Koha::Database->new->schema->resultset('BorrowerSync')->find({
80 'synctype' => 'norwegianpatrondb',
81 'borrowernumber' => $borrowernumber,
83 # Update the syncstatus to 'synced'
84 $sync->update( { 'syncstatus' => 'synced' } );
85 # Update the 'synclast' attribute with the "server time" ("server_tid") returned by the method
86 $sync->update( { 'lastsync' => $result->{'result'}->{'server_tid'} } );
87 # Save social security number as attribute
88 UpdateBorrowerAttribute(
90 { code => 'fnr', attribute => $patron->{'_extra'}->{'socsec'} },
98 say "Skipped, local change" if $verbose;
99 $skipped_local_change++;
104 say "-----------------------------";
105 say "Sync succeeded: $sync_success";
106 say "Sync failed : $sync_failed";
107 say "Skipped local change: $skipped_local_change";
116 Actually carry out syncing operations. Without this option, the script will
117 only report what it would have done, but not change any data, locally or
120 =item B<-v --verbose>
122 Report on the progress of the script.
126 Date and time to sync from, if this should be different from "1 second past
127 midnight of the day before". The date should be in this format:
135 =item B<-h, -?, --help>
137 Prints this help message and exits.
154 'f|from=s' => \$from,
155 'v|verbose' => \$verbose,
156 'd|debug' => \$debug,
160 pod2usage( -exitval => 0 ) if $help;
162 return ( $run, $from, $verbose, $debug );
168 Magnus Enger <digitalutvikling@gmail.com>
172 Copyright 2014 Oslo Public Library
176 This file is part of Koha.
178 Koha is free software; you can redistribute it and/or modify it under the terms
179 of the GNU General Public License as published by the Free Software Foundation;
180 either version 3 of the License, or (at your option) any later version.
182 You should have received a copy of the GNU General Public License along with
183 Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
184 Fifth Floor, Boston, MA 02110-1301 USA.