Bug 21035: Handle new lines when running reports
[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::Attributes qw( UpdateBorrowerAttribute );
16 use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLGetChanged );
17 use Koha::Patrons;
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::Patrons->find({ cardnumber => $patron->{cardnumber} });
74         my $borrowernumber = $stored_patron->borrowernumber;
75         if ( $run ) {
76             # FIXME Exceptions must be caught here
77             if ( $stored_patron->set(\%clean_patron)->store ) {
78                 # Get the sync object
79                 my $sync = Koha::Database->new->schema->resultset('BorrowerSync')->find({
80                     'synctype'       => 'norwegianpatrondb',
81                     'borrowernumber' => $borrowernumber,
82                 });
83                 # Update the syncstatus to 'synced'
84                 $sync->update( { 'syncstatus' => 'synced' } );
85                 # Update the 'synclast' attribute with the "server time" ("server_tid") returned by the method
86                 $sync->update( { 'lastsync' => $result->{'result'}->{'server_tid'} } );
87                 # Save social security number as attribute
88                 UpdateBorrowerAttribute(
89                     $borrowernumber,
90                     { code => 'fnr', attribute => $patron->{'_extra'}->{'socsec'} },
91                 );
92                 $sync_success++;
93             } else {
94                 $sync_failed++;
95             }
96         }
97     } else {
98         say "Skipped, local change" if $verbose;
99         $skipped_local_change++;
100     }
101 }
102
103 if ( $verbose ) {
104     say "-----------------------------";
105     say "Sync succeeded:       $sync_success";
106     say "Sync failed   :       $sync_failed";
107     say "Skipped local change: $skipped_local_change";
108 }
109
110 =head1 OPTIONS
111
112 =over 4
113
114 =item B<-r, --run>
115
116 Actually carry out syncing operations. Without this option, the script will
117 only report what it would have done, but not change any data, locally or
118 remotely.
119
120 =item B<-v --verbose>
121
122 Report on the progress of the script.
123
124 =item B<-f --from>
125
126 Date and time to sync from, if this should be different from "1 second past
127 midnight of the day before". The date should be in this format:
128
129     2014-06-03T00:00:01
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 $from    = '',
148   my $verbose = '';
149   my $debug   = '';
150   my $help    = '';
151
152   GetOptions (
153     'r|run'     => \$run,
154     'f|from=s'  => \$from,
155     'v|verbose' => \$verbose,
156     'd|debug'   => \$debug,
157     'h|?|help'  => \$help
158   );
159
160   pod2usage( -exitval => 0 ) if $help;
161
162   return ( $run, $from, $verbose, $debug );
163
164 }
165
166 =head1 AUTHOR
167
168 Magnus Enger <digitalutvikling@gmail.com>
169
170 =head1 COPYRIGHT
171
172 Copyright 2014 Oslo Public Library
173
174 =head1 LICENSE
175
176 This file is part of Koha.
177
178 Koha is free software; you can redistribute it and/or modify it under the terms
179 of the GNU General Public License as published by the Free Software Foundation;
180 either version 3 of the License, or (at your option) any later version.
181
182 You should have received a copy of the GNU General Public License along with
183 Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
184 Fifth Floor, Boston, MA 02110-1301 USA.
185
186 =cut