3 # Copyright 2014 Oslo Public Library
7 nl-sync-from-koha.pl - Sync patrons from Koha to the Norwegian national patron database (NL).
11 perl nl-sync-from-koha.pl -v --run
15 use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLSync );
22 my ( $run, $verbose, $debug ) = get_options();
28 Find local patrons that have been changed and need to be sent upstream to NL.
29 These patrons will be distinguished by two borrower attributes:
33 =item * The "nlstatus" attribute will have a value of "needsync". (Which means
34 that the patron has been changed in Koha, but not yet successfully synced
37 =item * The "nlsync" attribute will have a value of 1. (Which means that this
38 patron has accepted to be synced with NL, as opposed to a value of 0 which
39 would indicate that the patron has asked not to be synced with NL.)
45 This script performs the following steps:
51 my $check_result = NLCheckSysprefs();
52 if ( $check_result->{'error'} == 1 ) {
53 if ( $check_result->{'nlenabled'} == 0 ) { say "* Please activate this function with the NorwegianPatronDBEnable system preference." };
54 if ( $check_result->{'endpoint'} == 0 ) { say "* Please specify an endpoint with the NorwegianPatronDBEndpoint system preference." };
55 if ( $check_result->{'userpass'} == 0 ) { say "* Please fill in the NorwegianPatronDBUsername and NorwegianPatronDBPassword system preferences." };
60 say "* You have not specified --run, no real syncing will be done.";
63 =head2 Find patrons that need to be synced
67 my @needs_sync = Koha::Database->new->schema->resultset('BorrowerSync')->search({
70 synctype => 'norwegianpatrondb',
72 syncstatus => 'edited',
74 syncstatus => 'delete',
79 =head2 Do the actual sync
85 foreach my $borrower ( @needs_sync ) {
86 my $cardnumber = $borrower->borrowernumber->cardnumber;
87 my $firstname = $borrower->borrowernumber->firstname;
88 my $surname = $borrower->borrowernumber->surname;
89 my $syncstatus = $borrower->syncstatus;
90 say "*** Syncing patron: $cardnumber - $firstname $surname ($syncstatus)" if $verbose;
92 my $response = NLSync({ 'patron' => $borrower->borrowernumber });
94 my $result = $response->result;
95 if ( $result->{'status'} && $result->{'status'} == 1 ) {
100 if ( $result->{'melding'} && $verbose ) {
101 say $result->{'melding'};
107 =head2 Summarize if verbose mode is enabled
112 say "-----------------------------";
113 say "Sync succeeded: $sync_success";
114 say "Sync failed : $sync_failed";
123 Actually carry out syncing operations. Without this option, the script will
124 only report what it would have done, but not change any data, locally or
127 =item B<-v --verbose>
129 Report on the progress of the script.
135 =item B<-h, -?, --help>
137 Prints this help message and exits.
153 'v|verbose' => \$verbose,
154 'd|debug' => \$debug,
158 pod2usage( -exitval => 0 ) if $help;
160 return ( $run, $verbose, $debug );
166 Magnus Enger <digitalutvikling@gmail.com>
170 Copyright 2014 Oslo Public Library
174 This file is part of Koha.
176 Koha is free software; you can redistribute it and/or modify it under the terms
177 of the GNU General Public License as published by the Free Software Foundation;
178 either version 3 of the License, or (at your option) any later version.
180 You should have received a copy of the GNU General Public License along with
181 Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
182 Fifth Floor, Boston, MA 02110-1301 USA.