Bug 17578 [QA Followup] - Update number of tests
[koha.git] / misc / cronjobs / nl-sync-from-koha.pl
blob81e321823222b1b31604e79d629c01bd88f75b2b
1 #!/usr/bin/perl
3 # Copyright 2014 Oslo Public Library
5 =head1 NAME
7 nl-sync-from-koha.pl - Sync patrons from Koha to the Norwegian national patron database (NL).
9 =head1 SYNOPSIS
11 perl nl-sync-from-koha.pl -v --run
13 =cut
15 use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLSync );
16 use Koha::Database;
17 use Getopt::Long;
18 use Pod::Usage;
19 use Modern::Perl;
21 # Get options
22 my ( $run, $verbose, $debug ) = get_options();
24 =head1 ACTIONS
26 =head2
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:
31 =over 4
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
35 upstream.)
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.)
41 =back
43 =head1 STEPS
45 This script performs the following steps:
47 =head2 Check sysprefs
49 Check that the necessary sysprefs are set before proceeding.
51 =cut
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." };
58 exit 0;
61 unless ( $run ) {
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:
69 =over 4
71 =item * edited
73 =item * new
75 =item * deleted
77 =back
79 =cut
81 my @needs_sync = Koha::Database->new->schema->resultset('BorrowerSync')->search({
82 -and => [
83 sync => 1,
84 synctype => 'norwegianpatrondb',
85 -or => [
86 syncstatus => 'edited',
87 syncstatus => 'new',
88 syncstatus => 'delete',
91 });
93 =head2 Do the actual sync
95 Data is synced to NL with NLSync.
97 =cut
99 my $sync_success = 0;
100 my $sync_failed = 0;
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;
107 if ( $run ) {
108 my $response = NLSync({ 'patron' => $borrower->borrowernumber });
109 if ( $response ) {
110 my $result = $response->result;
111 if ( $result->{'status'} && $result->{'status'} == 1 ) {
112 $sync_success++;
113 } else {
114 $sync_failed++;
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.
127 =cut
129 if ( $verbose ) {
130 say "-----------------------------";
131 say "Sync succeeded: $sync_success";
132 say "Sync failed : $sync_failed";
135 =head1 OPTIONS
137 =over 4
139 =item B<-r, --run>
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
143 remotely.
145 =item B<-v --verbose>
147 Report on the progress of the script.
149 =item B<-d --debug>
151 Even more output.
153 =item B<-h, -?, --help>
155 Prints this help message and exits.
157 =back
159 =cut
161 sub get_options {
163 # Options
164 my $run = '',
165 my $verbose = '';
166 my $debug = '';
167 my $help = '';
169 GetOptions (
170 'r|run' => \$run,
171 'v|verbose' => \$verbose,
172 'd|debug' => \$debug,
173 'h|?|help' => \$help
176 pod2usage( -exitval => 0 ) if $help;
178 return ( $run, $verbose, $debug );
182 =head1 AUTHOR
184 Magnus Enger <digitalutvikling@gmail.com>
186 =head1 COPYRIGHT
188 Copyright 2014 Oslo Public Library
190 =head1 LICENSE
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.
202 =cut