Bug 3738 Automate login to web-based self-check system
[koha.git] / opac / sco / sco-main.pl
1 #!/usr/bin/perl
2 #
3 # This code has been modified by Trendsetters (originally from opac-user.pl)
4 # This code has been modified by rch
5 # We're going to authenticate a self-check user.  we'll add a flag to borrowers 'selfcheck'
6 #
7 # We're in a controlled environment; we trust the user.
8 # So the selfcheck station will accept a patronid and issue items to that borrower.
9 # FIXME: NOT really a controlled environment...  We're on the internet!
10 #
11 # The checkout permission comes form the CGI cookie/session of a staff user.
12 # The patron is not really logging in here in the same way as they do on the
13 # rest of the OPAC.  So don't confuse loggedinuser with the patron user.
14 #
15 # FIXME: inputfocus not really used in TMPL
16
17 use strict;
18 use warnings;
19
20 use CGI;
21
22 use C4::Auth;
23 use C4::Koha;
24 use C4::Dates qw/format_date/;
25 use C4::Circulation;
26 use C4::Reserves;
27 use C4::Output;
28 use C4::Members;
29 use C4::Dates;
30 use C4::Biblio;
31 use C4::Items;
32
33 my $query = new CGI;
34 if (C4::Context->preference('AutoSelfCheckAllowed')) 
35 {
36         my $AutoSelfCheckID = C4::Context->preference('AutoSelfCheckID');
37         my $AutoSelfCheckPass = C4::Context->preference('AutoSelfCheckPass');
38         $query->param(-name=>'userid',-values=>[$AutoSelfCheckID]),
39         $query->param(-name=>'password',-values=>[$AutoSelfCheckPass]),
40 }
41 my ($template, $loggedinuser, $cookie) = get_template_and_user({
42     template_name   => "sco/sco-main.tmpl",
43     authnotrequired => 0,
44       flagsrequired => { circulate => "circulate_remaining_permissions" },
45     query => $query,
46     type  => "opac",
47     debug => 1,
48 });
49
50 my $issuerid = $loggedinuser;
51 my ($op, $patronid, $barcode, $confirmed, $timedout) = (
52     $query->param("op")         || '',
53     $query->param("patronid")   || '',
54     $query->param("barcode")    || '',
55     $query->param("confirmed")  || '',
56     $query->param("timedout")   || '', #not actually using this...
57 );
58
59 my %confirmation_strings = ( RENEW_ISSUE => "This item is already checked out to you.  Return it?", );
60 my $issuenoconfirm = 1; #don't need to confirm on issue.
61 #warn "issuerid: " . $issuerid;
62 my $issuer   = GetMemberDetails($issuerid);
63 my $item     = GetItem(undef,$barcode);
64 my $borrower = GetMemberDetails(undef,$patronid);
65
66 my $branch = $issuer->{branchcode};
67 my $confirm_required = 0;
68 my $return_only = 0;
69 #warn "issuer cardnumber: " .   $issuer->{cardnumber};
70 #warn "patron cardnumber: " . $borrower->{cardnumber};
71 if ($op eq "logout") {
72     $query->param( patronid => undef );
73 }
74 elsif ( $op eq "returnbook" ) {
75     my ($doreturn) = AddReturn( $barcode, $branch );
76     #warn "returnbook: " . $doreturn;
77     $borrower = GetMemberDetails( undef, $patronid );   # update borrower
78 }
79 elsif ( $op eq "checkout" ) {
80     my $impossible  = {};
81     my $needconfirm = {};
82     if ( !$confirmed ) {
83         ( $impossible, $needconfirm ) = CanBookBeIssued( $borrower, $barcode );
84     }
85     $confirm_required = scalar keys %$needconfirm;
86
87     #warn "confirm_required: " . $confirm_required ;
88     if (scalar keys %$impossible) {
89
90         #  warn "impossible: numkeys: " . scalar (keys(%$impossible));
91         #warn join " ", keys %$impossible;
92         my $issue_error = (keys %$impossible)[0];
93
94         # FIXME  we assume only one error.
95         $template->param(
96             impossible                => $issue_error,
97             "circ_error_$issue_error" => 1,
98             title                     => $item->{title},
99             hide_main                 => 1,
100         );
101         if ($issue_error eq 'DEBT') {
102             $template->param(amount => $impossible->{DEBT});
103         }
104         #warn "issue_error: " . $issue_error ;
105         if ( $issue_error eq "NO_MORE_RENEWALS" ) {
106             $return_only = 1;
107             $template->param(
108                 returnitem => 1,
109                 barcode    => $barcode,
110             );
111         }
112     } elsif ( $needconfirm->{RENEW_ISSUE} ) {
113         if ($confirmed) {
114             #warn "renewing";
115             AddRenewal( $borrower, $item->{itemnumber} );
116         } else {
117             #warn "renew confirmation";
118             $template->param(
119                 renew               => 1,
120                 barcode             => $barcode,
121                 confirm             => 1,
122                 confirm_renew_issue => 1,
123                 hide_main           => 1,
124             );
125         }
126     } elsif ( $confirm_required && !$confirmed ) {
127         #warn "failed confirmation";
128         my $issue_error = (keys %$needconfirm)[0];
129         $template->param(
130             impossible                => (keys %$needconfirm)[0],
131             "circ_error_$issue_error" => 1,
132             hide_main                 => 1,
133         );
134     } else {
135         if ( $confirmed || $issuenoconfirm ) {    # we'll want to call getpatroninfo again to get updated issues.
136             # warn "issuing book?";
137             AddIssue( $borrower, $barcode );
138             # ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
139             # $template->param(
140             #   patronid => $patronid,
141             #   validuser => 1,
142             # );
143         } else {
144             $confirm_required = 1;
145             #warn "issue confirmation";
146             $template->param(
147                 confirm    => "Issuing title: " . $item->{title},
148                 barcode    => $barcode,
149                 hide_main  => 1,
150                 inputfocus => 'confirm',
151             );
152         }
153     }
154 } # $op
155
156 if ($borrower->{cardnumber}) {
157 #   warn "issuer's  branchcode: " .   $issuer->{branchcode};
158 #   warn   "user's  branchcode: " . $borrower->{branchcode};
159     my $borrowername = sprintf "%s %s", ($borrower->{firstname} || ''), ($borrower->{surname} || '');
160     my @issues;
161     my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
162     foreach my $it (@$issueslist) {
163         $it->{date_due_display} = format_date($it->{date_due});
164         my ($renewokay, $renewerror) = CanBookBeIssued($borrower, $it->{'barcode'},'','');
165         $it->{'norenew'} = 1 if $renewokay->{'NO_MORE_RENEWALS'};
166         push @issues, $it;
167     }
168
169     $template->param(
170         validuser => 1,
171         borrowername => $borrowername,
172         issues_count => scalar(@issues),
173         ISSUES => \@issues,
174         patronid => $patronid,
175         noitemlinks => 1 ,
176     );
177     my $inputfocus = ($return_only      == 1) ? 'returnbook' :
178                      ($confirm_required == 1) ? 'confirm'    : 'barcode' ;
179     $template->param(
180         inputfocus => $inputfocus,
181                 nofines => 1,
182         "dateformat_" . C4::Context->preference('dateformat') => 1,
183     );
184     if (C4::Context->preference('ShowPatronImageInWebBasedSelfCheck')) {
185         my ($image, $dberror) = GetPatronImage($borrower->{cardnumber});
186         if ($image) {
187             $template->param(
188                 display_patron_image => 1,
189                 cardnumber           => $borrower->{cardnumber},
190             );
191         }
192     }
193 } else {
194     $template->param(
195         patronid   => $patronid,
196         nouser     => $patronid,
197     );
198 }
199
200 output_html_with_http_headers $query, $cookie, $template->output;