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:
49 Check that the necessary sysprefs are set before proceeding.
53 my $check_result = NLCheckSysprefs
();
54 if ( $check_result->{'error'} == 1 ) {
55 if ( $check_result->{'nlenabled'} == 0 ) { say "* Please activate this function with the NorwegianPatronDBEnable system preference." };
56 if ( $check_result->{'endpoint'} == 0 ) { say "* Please specify an endpoint with the NorwegianPatronDBEndpoint system preference." };
57 if ( $check_result->{'userpass'} == 0 ) { say "* Please fill in the NorwegianPatronDBUsername and NorwegianPatronDBPassword system preferences." };
62 say "* You have not specified --run, no real syncing will be done.";
65 =head2 Find patrons that need to be synced
67 Patrons with either of these statuses:
81 my @needs_sync = Koha
::Database
->new->schema->resultset('BorrowerSync')->search({
84 synctype
=> 'norwegianpatrondb',
86 syncstatus
=> 'edited',
88 syncstatus
=> 'delete',
93 =head2 Do the actual sync
95 Data is synced to NL with NLSync.
101 foreach my $borrower ( @needs_sync ) {
102 my $cardnumber = $borrower->borrowernumber->cardnumber;
103 my $firstname = $borrower->borrowernumber->firstname;
104 my $surname = $borrower->borrowernumber->surname;
105 my $syncstatus = $borrower->syncstatus;
106 say "*** Syncing patron: $cardnumber - $firstname $surname ($syncstatus)" if $verbose;
108 my $response = NLSync
({ 'patron' => $borrower->borrowernumber });
110 my $result = $response->result;
111 if ( $result->{'status'} && $result->{'status'} == 1 ) {
116 if ( $result->{'melding'} && $verbose ) {
117 say $result->{'melding'};
123 =head2 Summarize if verbose mode is enabled
125 Specify -v on the command line to get a summary of the syncing operations.
130 say "-----------------------------";
131 say "Sync succeeded: $sync_success";
132 say "Sync failed : $sync_failed";
141 Actually carry out syncing operations. Without this option, the script will
142 only report what it would have done, but not change any data, locally or
145 =item B<-v --verbose>
147 Report on the progress of the script.
153 =item B<-h, -?, --help>
155 Prints this help message and exits.
171 'v|verbose' => \
$verbose,
172 'd|debug' => \
$debug,
176 pod2usage
( -exitval
=> 0 ) if $help;
178 return ( $run, $verbose, $debug );
184 Magnus Enger <digitalutvikling@gmail.com>
188 Copyright 2014 Oslo Public Library
192 This file is part of Koha.
194 Koha is free software; you can redistribute it and/or modify it under the terms
195 of the GNU General Public License as published by the Free Software Foundation;
196 either version 3 of the License, or (at your option) any later version.
198 You should have received a copy of the GNU General Public License along with
199 Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
200 Fifth Floor, Boston, MA 02110-1301 USA.