Bug 15838 - Subscription duplicating: Reset fields from SubscriptionDuplicateDroppedI...
[koha.git] / members / nl-search.pl
blobdc6e585f219619148aa325154447452dddcd834f
1 #!/usr/bin/perl
3 # Copyright 2013 Oslo Public Library
5 # This file is part of Koha.
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.
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.
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>.
20 =head1 NAME
22 nl-search.pl - Script for searching the Norwegian national patron database
24 =head1 DESCRIPTION
26 This script will search the Norwegian national patron database, and let staff
27 import patrons from the natial database into the local database.
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.
33 See http://www.lanekortet.no/ for more information (in Norwegian).
35 =cut
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;
50 my $cgi = CGI->new;
51 my $dbh = C4::Context->dbh;
52 my $op = $cgi->param('op');
54 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
56 template_name => "members/nl-search.tt",
57 query => $cgi,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => { borrowers => 1 },
61 debug => 1,
65 my $userenv = C4::Context->userenv;
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;
75 if ( $op && $op eq 'search' ) {
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(
83 searchmember => $identifier,
84 dt_params => { iDisplayLength => -1 },
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,
101 } else {
102 $template->param( 'error' => join ', ', $result->faultcode, $result->faultstring, $result->faultdetail );
105 $template->param( 'q' => $identifier );
108 } elsif ( $op && $op eq 'save' ) {
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' => $cgi->param('kjonn'),
115 'dateofbirth' => $cgi->param('fdato'),
116 'cardnumber' => $cgi->param('lnr'),
117 'userid' => $cgi->param('lnr'),
118 'address' => $cgi->param('p_adresse1'),
119 'address2' => $cgi->param('p_adresse2'),
120 'zipcode' => $cgi->param('p_postnr'),
121 'city' => $cgi->param('p_sted'),
122 'country' => $cgi->param('p_land'),
123 'B_address' => $cgi->param('m_adresse1'),
124 'B_address2' => $cgi->param('m_adresse2'),
125 'B_zipcode' => $cgi->param('m_postnr'),
126 'B_city' => $cgi->param('m_sted'),
127 'B_country' => $cgi->param('m_land'),
128 'password' => NLDecodePin( $cgi->param('pin') ),
129 'dateexpiry' => $cgi->param('gyldig_til'),
130 'email' => $cgi->param('epost'),
131 'mobile' => $cgi->param('tlf_mobil'),
132 'phone' => $cgi->param('tlf_hjemme'),
133 'phonepro' => $cgi->param('tlf_jobb'),
134 'branchcode' => $userenv->{'branch'},
135 'categorycode' => $cgi->param('categorycode'),
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 => $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,
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' );
161 output_html_with_http_headers $cgi, $cookie, $template->output;
163 =head1 AUTHOR
165 Magnus Enger <digitalutvikling@gmail.com>
167 =cut