Bug 15128 - DBRev 15128
[koha.git] / members / nl-search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 Oslo Public Library
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 =head1 NAME
21
22 nl-search.pl - Script for searching the Norwegian national patron database
23
24 =head1 DESCRIPTION
25
26 This script will search the Norwegian national patron database, and let staff
27 import patrons from the natial database into the local database.
28
29 In order to use this, a username/password from the Norwegian national database
30 of libraries ("Base Bibliotek") is needed. A special key is also needed, in
31 order to decrypt and encrypt PIN-codes/passwords.
32
33 See http://www.lanekortet.no/ for more information (in Norwegian).
34
35 =cut
36
37 use Modern::Perl;
38 use CGI;
39 use C4::Auth;
40 use C4::Category;
41 use C4::Context;
42 use C4::Output;
43 use C4::Members;
44 use C4::Members::Attributes qw( SetBorrowerAttributes );
45 use C4::Utils::DataTables::Members;
46 use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLSearch NLDecodePin NLGetFirstname NLGetSurname NLSync );
47 use Koha::Database;
48 use Koha::DateUtils;
49
50 my $cgi = CGI->new;
51 my $dbh = C4::Context->dbh;
52 my $op  = $cgi->param('op');
53
54 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
55     {
56         template_name   => "members/nl-search.tt",
57         query           => $cgi,
58         type            => "intranet",
59         authnotrequired => 0,
60         flagsrequired   => { borrowers => 1 },
61         debug           => 1,
62     }
63 );
64
65 my $userenv = C4::Context->userenv;
66
67 # Check sysprefs
68 my $check_result = NLCheckSysprefs();
69 if ( $check_result->{'error'} == 1 ) {
70     $template->param( 'error' => $check_result );
71     output_html_with_http_headers $cgi, $cookie, $template->output;
72     exit 0;
73 }
74
75 if ( $op && $op eq 'search' ) {
76
77     # Get the string we are searching for
78     my $identifier = $cgi->param('q');
79     if ( $identifier ) {
80         # Local search
81         my $local_results = C4::Utils::DataTables::Members::search(
82             {
83                 searchmember => $identifier,
84                 dt_params => { iDisplayLength => -1 },
85             }
86         )->{patrons};
87         $template->param( 'local_result' => $local_results );
88         # Search NL, unless we got at least one hit and further searching is
89         # disabled
90         if ( scalar @{ $local_results } == 0 || C4::Context->preference("NorwegianPatronDBSearchNLAfterLocalHit") == 1 ) {
91             # TODO Check the format of the identifier before searching NL
92             my $result = NLSearch( $identifier );
93             unless ($result->fault) {
94                 my $r = $result->result();
95                 # Send the data to the template
96                 my @categories = C4::Category->all;
97                 $template->param(
98                     'result'     => $r,
99                     'categories' => \@categories,
100                 );
101             } else {
102                 $template->param( 'error' => join ', ', $result->faultcode, $result->faultstring, $result->faultdetail );
103             }
104         }
105         $template->param( 'q' => $identifier );
106     }
107
108 } elsif ( $op && $op eq 'save' ) {
109
110     # This is where we map from fields in NL to fields in Koha
111     my %borrower = (
112         'surname'      => NLGetSurname( $cgi->param('navn') ),
113         'firstname'    => NLGetFirstname( $cgi->param('navn') ),
114         'sex'          => scalar $cgi->param('kjonn'),
115         'dateofbirth'  => scalar $cgi->param('fdato'),
116         'cardnumber'   => scalar $cgi->param('lnr'),
117         'userid'       => scalar $cgi->param('lnr'),
118         'address'      => scalar $cgi->param('p_adresse1'),
119         'address2'     => scalar $cgi->param('p_adresse2'),
120         'zipcode'      => scalar $cgi->param('p_postnr'),
121         'city'         => scalar $cgi->param('p_sted'),
122         'country'      => scalar $cgi->param('p_land'),
123         'B_address'    => scalar $cgi->param('m_adresse1'),
124         'B_address2'   => scalar $cgi->param('m_adresse2'),
125         'B_zipcode'    => scalar $cgi->param('m_postnr'),
126         'B_city'       => scalar $cgi->param('m_sted'),
127         'B_country'    => scalar $cgi->param('m_land'),
128         'password'     => NLDecodePin( $cgi->param('pin') ),
129         'dateexpiry'   => scalar $cgi->param('gyldig_til'),
130         'email'        => scalar $cgi->param('epost'),
131         'mobile'       => scalar $cgi->param('tlf_mobil'),
132         'phone'        => scalar $cgi->param('tlf_hjemme'),
133         'phonepro'     => scalar $cgi->param('tlf_jobb'),
134         'branchcode'   => $userenv->{'branch'},
135         'categorycode' => scalar $cgi->param('categorycode'),
136     );
137     # Add the new patron
138     my $borrowernumber = &AddMember(%borrower);
139     if ( $borrowernumber ) {
140         # Add extended patron attributes
141         SetBorrowerAttributes($borrowernumber, [
142             { code => 'fnr', value => scalar $cgi->param('fnr_hash') },
143         ], 'no_branch_limit' );
144         # Override the default sync data created by AddMember
145         my $borrowersync = Koha::Database->new->schema->resultset('BorrowerSync')->find({
146             'synctype'       => 'norwegianpatrondb',
147             'borrowernumber' => $borrowernumber,
148         });
149         $borrowersync->update({ 'syncstatus', 'synced' });
150         $borrowersync->update({ 'lastsync',   $cgi->param('sist_endret') });
151         $borrowersync->update({ 'hashed_pin', $cgi->param('pin') });
152         # Try to sync in real time. If this fails it will be picked up by the cronjob
153         NLSync({ 'borrowernumber' => $borrowernumber });
154         # Redirect to the edit screen
155         print $cgi->redirect( "/cgi-bin/koha/members/memberentry.pl?op=modify&destination=circ&borrowernumber=$borrowernumber" );
156     } else {
157         $template->param( 'error' => 'COULD_NOT_ADD_PATRON' );
158     }
159 }
160
161 output_html_with_http_headers $cgi, $cookie, $template->output;
162
163 =head1 AUTHOR
164
165 Magnus Enger <digitalutvikling@gmail.com>
166
167 =cut