Bug 16601: Update of italian MARC21 files
[koha.git] / misc / cronjobs / nl-sync-to-koha.pl
blob84e23788b531ac4845005ea946ed40faca2e63b8
1 #!/usr/bin/perl
3 # Copyright 2014 Oslo Public Library
5 =head1 NAME
7 nl-sync-to-koha.pl - Sync patrons from the Norwegian national patron database (NL) to Koha.
9 =head1 SYNOPSIS
11 perl nl-sync-to-koha.pl -v --run
13 =cut
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;
23 # Get options
24 my ( $run, $from, $verbose, $debug ) = get_options();
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;
34 unless ( $run ) {
35 say "* You have not specified --run, no real syncing will be done.";
38 # Do the sync
39 my $sync_success = 0;
40 my $sync_failed = 0;
41 my $skipped_local_change = 0;
43 # Get the borrowers that have been changed
44 my $result = NLGetChanged( $from );
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 "-----------------------------";
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";
63 say 'Created by: ' . $patron->{'_extra'}->{'created_by'};
64 say 'Last change by: ' . $patron->{'_extra'}->{'last_change_by'};
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,
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'} },
98 $sync_success++;
99 } else {
100 $sync_failed++;
103 } else {
104 say "Skipped, local change" if $verbose;
105 $skipped_local_change++;
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";
116 =head1 OPTIONS
118 =over 4
120 =item B<-r, --run>
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.
126 =item B<-v --verbose>
128 Report on the progress of the script.
130 =item B<-f --from>
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:
135 2014-06-03T00:00:01
137 =item B<-d --debug>
139 Even more output.
141 =item B<-h, -?, --help>
143 Prints this help message and exits.
145 =back
147 =cut
149 sub get_options {
151 # Options
152 my $run = '',
153 my $from = '',
154 my $verbose = '';
155 my $debug = '';
156 my $help = '';
158 GetOptions (
159 'r|run' => \$run,
160 'f|from=s' => \$from,
161 'v|verbose' => \$verbose,
162 'd|debug' => \$debug,
163 'h|?|help' => \$help
166 pod2usage( -exitval => 0 ) if $help;
168 return ( $run, $from, $verbose, $debug );
172 =head1 AUTHOR
174 Magnus Enger <digitalutvikling@gmail.com>
176 =head1 COPYRIGHT
178 Copyright 2014 Oslo Public Library
180 =head1 LICENSE
182 This file is part of Koha.
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.
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.
192 =cut