Bug 11401: QA followup
[koha.git] / misc / cronjobs / nl-sync-to-koha.pl
1 #!/usr/bin/perl
2
3 # Copyright 2014 Oslo Public Library
4
5 =head1 NAME
6
7 nl-sync-to-koha.pl - Sync patrons from the Norwegian national patron database (NL) to Koha.
8
9 =head1 SYNOPSIS
10
11  perl nl-sync-to-koha.pl -v --run
12
13 =cut
14
15 use C4::Members;
16 use C4::Members::Attributes qw( UpdateBorrowerAttribute );
17 use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLGetChanged );
18 use Koha::Database;
19 use Getopt::Long;
20 use Pod::Usage;
21 use Modern::Perl;
22
23 # Get options
24 my ( $run, $from, $verbose, $debug ) = get_options();
25
26 my $check_result = NLCheckSysprefs();
27 if ( $check_result->{'error'} == 1 ) {
28     if ( $check_result->{'nlenabled'} == 0 ) { say "* Please activate this function with the NorwegianPatronDBEnable system preference." };
29     if ( $check_result->{'endpoint'}  == 0 ) { say "* Please specify an endpoint with the NorwegianPatronDBEndpoint system preference." };
30     if ( $check_result->{'userpass'}  == 0 ) { say "* Please fill in the NorwegianPatronDBUsername and NorwegianPatronDBPassword system preferences." };
31     exit 0;
32 }
33
34 unless ( $run ) {
35     say "* You have not specified --run, no real syncing will be done.";
36 }
37
38 # Do the sync
39 my $sync_success         = 0;
40 my $sync_failed          = 0;
41 my $skipped_local_change = 0;
42
43 # Get the borrowers that have been changed
44 my $result = NLGetChanged( $from );
45
46 if ( $verbose ) {
47     say 'Number of records: ' . $result->{'antall_poster_returnert'};
48     say 'Number of hits:    ' . $result->{'antall_treff'};
49     say 'Message:           ' . $result->{'melding'};
50     say 'Status:            ' . $result->{'status'};
51     say 'Server time:       ' . $result->{'server_tid'};
52     say "-----------------------------";
53 }
54
55 # Loop through the patrons
56 foreach my $patron ( @{ $result->{'kohapatrons'} } ) {
57     if ( $verbose ) {
58         if ( $patron->{'surname'} ) {
59             say "*** Name: " . $patron->{'surname'};
60         } else {
61             say "*** No name";
62         }
63         say 'Created by:     ' . $patron->{'_extra'}->{'created_by'};
64         say 'Last change by: ' . $patron->{'_extra'}->{'last_change_by'};
65     }
66     # Only sync in changes made by other libraries
67     if ( C4::Context->preference("NorwegianPatronDBUsername") ne $patron->{'_extra'}->{'last_change_by'} ) {
68         # Make a copy of the data in the hashref and store it as a hash
69         my %clean_patron = %$patron;
70         # Delete the extra data from the copy of the hashref
71         delete $clean_patron{'_extra'};
72         # Find the borrowernumber based on cardnumber
73         my $stored_patron = Koha::Database->new->schema->resultset('Borrower')->find({
74             'cardnumber' => $patron->{'cardnumber'}
75         });
76         my $borrowernumber = $stored_patron->borrowernumber;
77         if ( $run ) {
78             # Call ModMember
79             my $success = ModMember(
80                 'borrowernumber' => $borrowernumber,
81                 %clean_patron,
82             );
83             if ( $success ) {
84                 # Get the sync object
85                 my $sync = Koha::Database->new->schema->resultset('BorrowerSync')->find({
86                     'synctype'       => 'norwegianpatrondb',
87                     'borrowernumber' => $borrowernumber,
88                 });
89                 # Update the syncstatus to 'synced'
90                 $sync->update( { 'syncstatus' => 'synced' } );
91                 # Update the 'synclast' attribute with the "server time" ("server_tid") returned by the method
92                 $sync->update( { 'lastsync' => $result->{'result'}->{'server_tid'} } );
93                 # Save social security number as attribute
94                 UpdateBorrowerAttribute(
95                     $borrowernumber,
96                     { code => 'fnr', attribute => $patron->{'_extra'}->{'socsec'} },
97                 );
98                 $sync_success++;
99             } else {
100                 $sync_failed++;
101             }
102         }
103     } else {
104         say "Skipped, local change" if $verbose;
105         $skipped_local_change++;
106     }
107 }
108
109 if ( $verbose ) {
110     say "-----------------------------";
111     say "Sync succeeded:       $sync_success";
112     say "Sync failed   :       $sync_failed";
113     say "Skipped local change: $skipped_local_change";
114 }
115
116 =head1 OPTIONS
117
118 =over 4
119
120 =item B<-r, --run>
121
122 Actually carry out syncing operations. Without this option, the script will
123 only report what it would have done, but not change any data, locally or
124 remotely.
125
126 =item B<-v --verbose>
127
128 Report on the progress of the script.
129
130 =item B<-f --from>
131
132 Date and time to sync from, if this should be different from "1 second past
133 midnight of the day before". The date should be in this format:
134
135     2014-06-03T00:00:01
136
137 =item B<-d --debug>
138
139 Even more output.
140
141 =item B<-h, -?, --help>
142
143 Prints this help message and exits.
144
145 =back
146
147 =cut
148
149 sub get_options {
150
151   # Options
152   my $run     = '',
153   my $from    = '',
154   my $verbose = '';
155   my $debug   = '';
156   my $help    = '';
157
158   GetOptions (
159     'r|run'     => \$run,
160     'f|from=s'  => \$from,
161     'v|verbose' => \$verbose,
162     'd|debug'   => \$debug,
163     'h|?|help'  => \$help
164   );
165
166   pod2usage( -exitval => 0 ) if $help;
167
168   return ( $run, $from, $verbose, $debug );
169
170 }
171
172 =head1 AUTHOR
173
174 Magnus Enger <digitalutvikling@gmail.com>
175
176 =head1 COPYRIGHT
177
178 Copyright 2014 Oslo Public Library
179
180 =head1 LICENSE
181
182 This file is part of Koha.
183
184 Koha is free software; you can redistribute it and/or modify it under the terms
185 of the GNU General Public License as published by the Free Software Foundation;
186 either version 3 of the License, or (at your option) any later version.
187
188 You should have received a copy of the GNU General Public License along with
189 Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
190 Fifth Floor, Boston, MA 02110-1301 USA.
191
192 =cut