]> git.koha-community.org Git - koha.git/blob - misc/cronjobs/nl-sync-from-koha.pl
Bug 11401: Add support for Norwegian national library card
[koha.git] / misc / cronjobs / nl-sync-from-koha.pl
1 #!/usr/bin/perl
2
3 # Copyright 2014 Oslo Public Library
4
5 =head1 NAME
6
7 nl-sync-from-koha.pl - Sync patrons from Koha to the Norwegian national patron database (NL).
8
9 =head1 SYNOPSIS
10
11  perl nl-sync-from-koha.pl -v --run
12
13 =cut
14
15 use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLSync );
16 use Koha::Database;
17 use Getopt::Long;
18 use Pod::Usage;
19 use Modern::Perl;
20
21 # Get options
22 my ( $run, $verbose, $debug ) = get_options();
23
24 =head1 ACTIONS
25
26 =head2
27
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:
30
31 =over 4
32
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.)
36
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.)
40
41 =back
42
43 =head1 STEPS
44
45 This script performs the following steps:
46
47 =head2 Check sysprefs
48
49 =cut
50
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." };
56     exit 0;
57 }
58
59 unless ( $run ) {
60     say "* You have not specified --run, no real syncing will be done.";
61 }
62
63 =head2 Find patrons that need to be synced
64
65 =cut
66
67 my @needs_sync = Koha::Database->new->schema->resultset('BorrowerSync')->search({
68     -and => [
69       sync     => 1,
70       synctype => 'norwegianpatrondb',
71       -or => [
72         syncstatus => 'edited',
73         syncstatus => 'new',
74         syncstatus => 'delete',
75       ],
76     ],
77 });
78
79 =head2 Do the actual sync
80
81 =cut
82
83 my $sync_success = 0;
84 my $sync_failed  = 0;
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;
91     if ( $run ) {
92         my $response = NLSync({ 'patron' => $borrower->borrowernumber });
93         if ( $response ) {
94             my $result = $response->result;
95             if ( $result->{'status'} && $result->{'status'} == 1 ) {
96                 $sync_success++;
97             } else {
98                 $sync_failed++;
99             }
100             if ( $result->{'melding'} && $verbose ) {
101                 say $result->{'melding'};
102             }
103         }
104     }
105 }
106
107 =head2 Summarize if verbose mode is enabled
108
109 =cut
110
111 if ( $verbose ) {
112     say "-----------------------------";
113     say "Sync succeeded: $sync_success";
114     say "Sync failed   : $sync_failed";
115 }
116
117 =head1 OPTIONS
118
119 =over 4
120
121 =item B<-r, --run>
122
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
125 remotely.
126
127 =item B<-v --verbose>
128
129 Report on the progress of the script.
130
131 =item B<-d --debug>
132
133 Even more output.
134
135 =item B<-h, -?, --help>
136
137 Prints this help message and exits.
138
139 =back
140
141 =cut
142
143 sub get_options {
144
145   # Options
146   my $run     = '',
147   my $verbose = '';
148   my $debug   = '';
149   my $help    = '';
150
151   GetOptions (
152     'r|run'     => \$run,
153     'v|verbose' => \$verbose,
154     'd|debug'   => \$debug,
155     'h|?|help'  => \$help
156   );
157
158   pod2usage( -exitval => 0 ) if $help;
159
160   return ( $run, $verbose, $debug );
161
162 }
163
164 =head1 AUTHOR
165
166 Magnus Enger <digitalutvikling@gmail.com>
167
168 =head1 COPYRIGHT
169
170 Copyright 2014 Oslo Public Library
171
172 =head1 LICENSE
173
174 This file is part of Koha.
175
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.
179
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.
183
184 =cut