Bug 16039: Remove useless exit
[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 Check that the necessary sysprefs are set before proceeding.
50
51 =cut
52
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;
59 }
60
61 unless ( $run ) {
62     say "* You have not specified --run, no real syncing will be done.";
63 }
64
65 =head2 Find patrons that need to be synced
66
67 Patrons with either of these statuses:
68
69 =over 4
70
71 =item * edited
72
73 =item * new
74
75 =item * deleted
76
77 =back
78
79 =cut
80
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',
89       ],
90     ],
91 });
92
93 =head2 Do the actual sync
94
95 Data is synced to NL with NLSync.
96
97 =cut
98
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++;
115             }
116             if ( $result->{'melding'} && $verbose ) {
117                 say $result->{'melding'};
118             }
119         }
120     }
121 }
122
123 =head2 Summarize if verbose mode is enabled
124
125 Specify -v on the command line to get a summary of the syncing operations.
126
127 =cut
128
129 if ( $verbose ) {
130     say "-----------------------------";
131     say "Sync succeeded: $sync_success";
132     say "Sync failed   : $sync_failed";
133 }
134
135 =head1 OPTIONS
136
137 =over 4
138
139 =item B<-r, --run>
140
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.
144
145 =item B<-v --verbose>
146
147 Report on the progress of the script.
148
149 =item B<-d --debug>
150
151 Even more output.
152
153 =item B<-h, -?, --help>
154
155 Prints this help message and exits.
156
157 =back
158
159 =cut
160
161 sub get_options {
162
163   # Options
164   my $run     = '',
165   my $verbose = '';
166   my $debug   = '';
167   my $help    = '';
168
169   GetOptions (
170     'r|run'     => \$run,
171     'v|verbose' => \$verbose,
172     'd|debug'   => \$debug,
173     'h|?|help'  => \$help
174   );
175
176   pod2usage( -exitval => 0 ) if $help;
177
178   return ( $run, $verbose, $debug );
179
180 }
181
182 =head1 AUTHOR
183
184 Magnus Enger <digitalutvikling@gmail.com>
185
186 =head1 COPYRIGHT
187
188 Copyright 2014 Oslo Public Library
189
190 =head1 LICENSE
191
192 This file is part of Koha.
193
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.
197
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.
201
202 =cut